-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Slice walking implementation (#124)
* fix #94 * ✨ First naive implementation of slice walking * ✨ Support negative slice indexes * ⚡ Avoid multiple array reinstanciations * 💡 fix operation callback jsdoc * 🔨 Do not cap slice bounds at length * 🚚🔥 Move around some utils code, remove unused isArrayProp, inline apply callback for now * 🚨 fix lint after rebase * 🚚 Make apply's own test file * 🔨 Finer mutations detection in tests * 🚚 Move tests in apply's own test file * ✅ Test out of bound slice
- Loading branch information
Showing
7 changed files
with
289 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* eslint-env jest */ | ||
import { apply } from './apply' | ||
import { immutaTest } from 'test.utils' | ||
|
||
describe('Apply', () => { | ||
|
||
const _inc = (v, i = 1) => { | ||
let r = Number(v) | ||
if (Number.isNaN(r)) r = 0 | ||
return r + i | ||
} | ||
|
||
const inc = (obj, path, ...args) => apply(obj, path, (obj, prop) => { obj[prop] = _inc(obj[prop], ...args) }) | ||
|
||
it('should inc in an array slice', () => { | ||
immutaTest( | ||
input => { | ||
const output = inc(input, 'nested.prop[:].val', 2) | ||
expect(output).toEqual({ | ||
nested: { | ||
prop: [ | ||
{ | ||
val: 6, | ||
other: {}, | ||
}, | ||
{ val: -6 }, | ||
{ val: 2 }, | ||
{ val: 2 }, | ||
], | ||
}, | ||
}) | ||
return output | ||
}, | ||
{ | ||
nested: { | ||
prop: [ | ||
{ | ||
val: 4, | ||
other: {}, | ||
}, | ||
{ val: -8 }, | ||
{ val: 'a' }, | ||
{}, | ||
], | ||
}, | ||
}, | ||
'nested.prop.0.val', | ||
'nested.prop.1.val', | ||
'nested.prop.2.val', | ||
'nested.prop.3.val', | ||
) | ||
|
||
immutaTest( | ||
input => { | ||
const output = inc(input, 'nested.prop[1:3].val') | ||
expect(output).toEqual({ | ||
nested: { | ||
prop: [{ val: 0 }, { | ||
val: 2, | ||
other: {}, | ||
}, { val: 3 }, { val: 3 }], | ||
}, | ||
other: {}, | ||
}) | ||
return output | ||
}, | ||
{ | ||
nested: { | ||
prop: [{ val: 0 }, { | ||
val: 1, | ||
other: {}, | ||
}, { val: 2 }, { val: 3 }], | ||
}, | ||
other: {}, | ||
}, | ||
'nested.prop.1.val', | ||
'nested.prop.2.val', | ||
) | ||
|
||
immutaTest( | ||
input => { | ||
const output = inc(input, 'nested.prop[-3:-1].val') | ||
expect(output).toEqual({ | ||
nested: { | ||
prop: [{ val: 0 }, { | ||
val: 2, | ||
other: {}, | ||
}, { val: 3 }, { val: 3 }], | ||
}, | ||
other: {}, | ||
}) | ||
return output | ||
}, | ||
{ | ||
nested: { | ||
prop: [{ val: 0 }, { | ||
val: 1, | ||
other: {}, | ||
}, { val: 2 }, { val: 3 }], | ||
}, | ||
other: {}, | ||
}, | ||
'nested.prop.1.val', | ||
'nested.prop.2.val', | ||
) | ||
}) | ||
|
||
immutaTest( | ||
input => { | ||
const output = inc(input, 'nested.prop[3:5].val', 6) | ||
expect(output).toEqual({ | ||
nested: { prop: [{ val: 0 }, { val: 1 }, undefined, { val: 6 }, { val: 6 }] }, | ||
other: {}, | ||
}) | ||
return output | ||
}, | ||
{ | ||
nested: { prop: [{ val: 0 }, { val: 1 }] }, | ||
other: {}, | ||
}, | ||
'nested.prop.2', | ||
'nested.prop.3.val', | ||
'nested.prop.4.val', | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.