@@ -4,7 +4,7 @@ import { describe, expect, test } from 'vitest'
44
55import type { DataItemProps , Path } from '../src'
66import { applyValue , createDataType , deleteValue , isCycleReference } from '../src'
7- import { safeStringify , segmentArray } from '../src/utils'
7+ import { getPathValue , pathValueDefaultGetter , safeStringify , segmentArray } from '../src/utils'
88
99describe ( 'function applyValue' , ( ) => {
1010 const patches : any [ ] = [ { } , undefined , 1 , '2' , 3n , 0.4 ]
@@ -439,3 +439,94 @@ describe('function circularStringify', () => {
439439 expect ( safeStringify ( set ) ) . to . eq ( '[1,"[Circular]"]' )
440440 } )
441441} )
442+
443+ describe ( 'function pathValueDefaultGetter' , ( ) => {
444+ test ( 'should works with object' , ( ) => {
445+ const obj = {
446+ foo : 1
447+ }
448+ expect ( pathValueDefaultGetter ( obj , 'foo' ) ) . to . eq ( 1 )
449+ } )
450+
451+ test ( 'should works with array' , ( ) => {
452+ const array = [ 1 , 2 , 3 , 4 , 5 ]
453+ expect ( pathValueDefaultGetter ( array , 2 ) ) . to . eq ( 3 )
454+ } )
455+
456+ test ( 'should works with Map' , ( ) => {
457+ const map = new Map ( )
458+ map . set ( 'foo' , 1 )
459+ map . set ( 'bar' , 2 )
460+ expect ( pathValueDefaultGetter ( map , 'foo' ) ) . to . eq ( 1 )
461+ expect ( pathValueDefaultGetter ( map , 'not exist' ) ) . to . eq ( undefined )
462+ } )
463+
464+ test ( 'should works with WeakMap' , ( ) => {
465+ const map = new WeakMap ( )
466+ const key = { }
467+ map . set ( key , 1 )
468+ expect ( pathValueDefaultGetter ( map , key ) ) . to . eq ( 1 )
469+ } )
470+
471+ test ( 'should works with Set' , ( ) => {
472+ const set = new Set ( )
473+ set . add ( 1 )
474+ set . add ( 2 )
475+ expect ( pathValueDefaultGetter ( set , 1 ) ) . to . eq ( 2 )
476+ } )
477+
478+ test ( 'should not works with WeakSet' , ( ) => {
479+ const set = new WeakSet ( )
480+ set . add ( { } )
481+ expect ( ( ) => {
482+ pathValueDefaultGetter ( set , [ 0 ] )
483+ } ) . toThrow ( )
484+ } )
485+ } )
486+
487+ describe ( 'function getPathValue' , ( ) => {
488+ test ( 'should works with object' , ( ) => {
489+ const obj = {
490+ foo : {
491+ bar : {
492+ baz : 1
493+ }
494+ }
495+ }
496+ expect ( getPathValue ( obj , [ 'foo' , 'bar' , 'baz' ] ) ) . to . eq ( 1 )
497+ } )
498+
499+ test ( 'should works with array' , ( ) => {
500+ const array = [ 1 , [ 2 , [ 3 , 4 ] ] ]
501+ expect ( getPathValue ( array , [ 1 , 1 , 1 ] ) ) . to . eq ( 4 )
502+ } )
503+
504+ test ( 'should works with Map' , ( ) => {
505+ const map = new Map ( )
506+ map . set ( 'foo' , 1 )
507+ map . set ( 'bar' , 2 )
508+ expect ( getPathValue ( map , [ 'foo' ] ) ) . to . eq ( 1 )
509+ expect ( getPathValue ( map , [ 'not exist' ] ) ) . to . eq ( undefined )
510+ } )
511+
512+ test ( 'should works with WeakMap' , ( ) => {
513+ const map = new WeakMap ( )
514+ const key = { }
515+ map . set ( key , 1 )
516+ // @ts -ignore
517+ expect ( getPathValue ( map , [ key ] ) ) . to . eq ( 1 )
518+ } )
519+
520+ test ( 'should works with Set' , ( ) => {
521+ const set = new Set ( )
522+ set . add ( 1 )
523+ set . add ( 2 )
524+ expect ( getPathValue ( set , [ 1 ] ) ) . to . eq ( 2 )
525+ } )
526+
527+ test ( 'should not works with WeakSet' , ( ) => {
528+ const set = new WeakSet ( )
529+ set . add ( { } )
530+ expect ( getPathValue ( set , [ 0 ] ) ) . to . eq ( null )
531+ } )
532+ } )
0 commit comments