@@ -32,6 +32,7 @@ import isEmpty from 'lodash.isempty';
3232
3333import mapPathsToObject from '@webkrafters/data-distillery' ;
3434import stringToDotPath from '@webkrafters/path-dotize' ;
35+ import getProperty from '@webkrafters/get-property' ;
3536import AutoImmutable from '@webkrafters/auto-immutable' ;
3637
3738import {
@@ -138,38 +139,30 @@ const useStore = <T extends State>(
138139 const resetState = useCallback < StoreInternal < T > [ "resetState" ] > (
139140 ( connection , propertyPaths = [ ] ) => {
140141 const original = _storage . clone ( _storage . getItem ( storageKey . current ) ) ;
141- let resetData ;
142- if ( ! propertyPaths . length ) {
143- resetData = { } ;
144- } else if ( propertyPaths . includes ( FULL_STATE_SELECTOR ) ) {
142+ let resetData = { } ;
143+ if ( propertyPaths . includes ( FULL_STATE_SELECTOR ) ) {
145144 resetData = isEmpty ( original ) ? CLEAR_TAG : { [ REPLACE_TAG ] : original } ;
146145 } else {
147- const visitedPathMap = { } ;
148- const transformer = ( { trail, value } : PropertyInfo ) => {
149- visitedPathMap [ trail . join ( '.' ) ] = null ;
150- return { [ REPLACE_TAG ] : value } ;
151- }
152- resetData = mapPathsToObject ( original , propertyPaths , transformer as Transform ) ;
153- if ( Object . keys ( visitedPathMap ) . length < propertyPaths . length ) {
154- for ( let path of propertyPaths ) {
155- path = stringToDotPath ( path ) ;
156- if ( path in visitedPathMap ) { continue }
157- let trail = path . split ( '.' ) ;
158- const keyTuple = trail . slice ( - 1 ) ;
159- trail = trail . slice ( 0 , - 1 ) ;
160- let node = resetData ;
161- for ( const t of trail ) {
162- if ( isEmpty ( node [ t ] ) ) {
163- node [ t ] = { } ;
164- }
165- node = node [ t ] ;
166- }
167- if ( DELETE_TAG in node ) {
168- node [ DELETE_TAG ] . push ( ...keyTuple ) ;
169- } else {
170- node [ DELETE_TAG ] = keyTuple ;
171- }
146+ for ( let path of propertyPaths ) {
147+ let node = resetData ;
148+ const tokens = stringToDotPath ( path ) . split ( '.' ) ;
149+ const { trail, ...pInfo } = getProperty ( original , tokens ) ;
150+ for ( let { length, ...keys } = trail , k = 0 ; k < length ; k ++ ) {
151+ if ( REPLACE_TAG in node ) { continue }
152+ const key = keys [ k ] ;
153+ if ( ! ( key in node ) ) { node [ key ] = { } }
154+ node = node [ key ] ;
172155 }
156+ if ( REPLACE_TAG in node ) { continue }
157+ if ( pInfo . exists ) {
158+ for ( const k in node ) { delete node [ k ] }
159+ node [ REPLACE_TAG ] = pInfo . _value ;
160+ continue ;
161+ }
162+ if ( ! ( DELETE_TAG in node ) ) { node [ DELETE_TAG ] = [ ] }
163+ const deletingKey = tokens [ trail . length ] ;
164+ ! node [ DELETE_TAG ] . includes ( deletingKey ) &&
165+ node [ DELETE_TAG ] . push ( deletingKey ) ;
173166 }
174167 }
175168 runPrehook ( prehooksRef . current , 'resetState' , [
0 commit comments