@@ -647,8 +647,8 @@ const omit = (obj, keys2) => {
647647 { ...obj }
648648 ) ;
649649} ;
650- const get = ( value , funcOrPath , defaultValue = null ) => {
651- const segments = funcOrPath . split ( / [ \. \[ \] ] / g) ;
650+ const get = ( value , path , defaultValue = null ) => {
651+ const segments = path . split ( / [ \. \[ \] ] / g) ;
652652 let current = value ;
653653 for ( const key of segments ) {
654654 if ( current === null )
@@ -663,20 +663,40 @@ const get = (value, funcOrPath, defaultValue = null) => {
663663 return defaultValue ;
664664 return current ;
665665} ;
666- const assign = ( a , b ) => {
667- if ( ! a && ! b )
666+ const set = ( initial , path , value ) => {
667+ if ( ! initial )
668668 return { } ;
669- if ( ! a )
670- return b ;
671- if ( ! b )
672- return a ;
673- return Object . entries ( a ) . reduce ( ( acc , [ key , value ] ) => {
669+ if ( ! path || ! value )
670+ return initial ;
671+ const segments = path . split ( / [ \. \[ \] ] / g) . filter ( ( x ) => ! ! x . trim ( ) ) ;
672+ const _set = ( node ) => {
673+ if ( segments . length > 1 ) {
674+ const key = segments . shift ( ) ;
675+ const nextIsNum = toInt ( segments [ 0 ] , null ) === null ? false : true ;
676+ node [ key ] = node [ key ] === void 0 ? nextIsNum ? [ ] : { } : node [ key ] ;
677+ _set ( node [ key ] ) ;
678+ } else {
679+ node [ segments [ 0 ] ] = value ;
680+ }
681+ } ;
682+ const cloned = clone ( initial ) ;
683+ _set ( cloned ) ;
684+ return cloned ;
685+ } ;
686+ const assign = ( initial , override ) => {
687+ if ( ! initial && ! override )
688+ return { } ;
689+ if ( ! initial )
690+ return override ;
691+ if ( ! override )
692+ return initial ;
693+ return Object . entries ( initial ) . reduce ( ( acc , [ key , value ] ) => {
674694 return {
675695 ...acc ,
676696 [ key ] : ( ( ) => {
677697 if ( isObject ( value ) )
678- return assign ( value , b [ key ] ) ;
679- return b [ key ] ;
698+ return assign ( value , override [ key ] ) ;
699+ return override [ key ] ;
680700 } ) ( )
681701 } ;
682702 } , { } ) ;
@@ -706,6 +726,13 @@ const crush = (value) => {
706726 ( k ) => get ( value , k )
707727 ) ;
708728} ;
729+ const construct = ( obj ) => {
730+ if ( ! obj )
731+ return { } ;
732+ return Object . keys ( obj ) . reduce ( ( acc , path ) => {
733+ return set ( acc , path , obj [ path ] ) ;
734+ } , { } ) ;
735+ } ;
709736
710737const random = ( min , max ) => {
711738 return Math . floor ( Math . random ( ) * ( max - min + 1 ) + min ) ;
@@ -847,4 +874,4 @@ const trim = (str, charsToTrim = " ") => {
847874 return str . replace ( regex , "" ) ;
848875} ;
849876
850- export { alphabetical , assign , boil , callable , camel , capitalize , chain , clone , cluster , compose , counting , crush , dash , debounce , defer , diff , draw , first , flat , fork , get , group , intersects , invert , isArray , isDate , isEmpty , isEqual , isFloat , isFunction , isInt , isNumber , isObject , isPrimitive , isString , isSymbol , iterate , keys , last , list , listify , lowerize , map , mapEntries , mapKeys , mapValues , max , memo , merge , min , objectify , omit , parallel , partial , partob , pascal , pick , proxied , random , range , reduce , replace , replaceOrAppend , retry , select , series , shake , shift , shuffle , sift , sleep , snake , sort , sum , template , throttle , title , toFloat , toInt , toggle , trim , tryit as try , tryit , uid , unique , upperize , zip , zipToObject } ;
877+ export { alphabetical , assign , boil , callable , camel , capitalize , chain , clone , cluster , compose , construct , counting , crush , dash , debounce , defer , diff , draw , first , flat , fork , get , group , intersects , invert , isArray , isDate , isEmpty , isEqual , isFloat , isFunction , isInt , isNumber , isObject , isPrimitive , isString , isSymbol , iterate , keys , last , list , listify , lowerize , map , mapEntries , mapKeys , mapValues , max , memo , merge , min , objectify , omit , parallel , partial , partob , pascal , pick , proxied , random , range , reduce , replace , replaceOrAppend , retry , select , series , set , shake , shift , shuffle , sift , sleep , snake , sort , sum , template , throttle , title , toFloat , toInt , toggle , trim , tryit as try , tryit , uid , unique , upperize , zip , zipToObject } ;
0 commit comments