File tree 2 files changed +11
-0
lines changed
2 files changed +11
-0
lines changed Original file line number Diff line number Diff line change @@ -70,4 +70,11 @@ describe('deleteBy', () => {
70
70
expect ( deleteBy ( structure , 'kids.0.name' ) . kids [ 0 ] . name ) . not . toBeDefined ( )
71
71
expect ( deleteBy ( structure , 'kids.0.age' ) . kids [ 0 ] . age ) . not . toBeDefined ( )
72
72
} )
73
+
74
+ it ( 'should delete non-existent paths like a noop' , ( ) => {
75
+ expect ( deleteBy ( structure , 'nonexistent' ) ) . toEqual ( structure )
76
+ expect ( deleteBy ( structure , 'nonexistent.nonexistent' ) ) . toEqual ( structure )
77
+ expect ( deleteBy ( structure , 'kids.3.name' ) ) . toEqual ( structure )
78
+ expect ( deleteBy ( structure , 'nonexistent.3.nonexistent' ) ) . toEqual ( structure )
79
+ } )
73
80
} )
Original file line number Diff line number Diff line change @@ -76,6 +76,7 @@ export function deleteBy(obj: any, _path: any) {
76
76
const path = makePathArray ( _path )
77
77
78
78
function doDelete ( parent : any ) : any {
79
+ if ( ! parent ) return ;
79
80
if ( path . length === 1 ) {
80
81
const finalPath = path [ 0 ] !
81
82
const { [ finalPath ] : remove , ...rest } = parent
@@ -95,6 +96,9 @@ export function deleteBy(obj: any, _path: any) {
95
96
96
97
if ( typeof key === 'number' ) {
97
98
if ( Array . isArray ( parent ) ) {
99
+ if ( key >= parent . length ) {
100
+ return parent
101
+ }
98
102
const prefix = parent . slice ( 0 , key )
99
103
return [
100
104
...( prefix . length ? prefix : new Array ( key ) ) ,
You can’t perform that action at this time.
0 commit comments