Skip to content

Commit 33a91ad

Browse files
committed
fix(proxy-state-tree): support second argument to indexOf
1 parent 8aea60d commit 33a91ad

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

packages/node_modules/proxy-state-tree/src/Proxyfier.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const arrayMutations = new Set([
1616
'copyWithin',
1717
])
1818

19+
const getValue = (proxyOrValue) =>
20+
proxyOrValue && proxyOrValue[IS_PROXY] ? proxyOrValue[VALUE] : proxyOrValue
21+
1922
export class Proxifier {
2023
CACHED_PROXY = Symbol('CACHED_PROXY')
2124
constructor(private tree: TTree) {}
@@ -105,7 +108,8 @@ export class Proxifier {
105108
if (prop === PATH) return path
106109
if (prop === VALUE) return value
107110
if (prop === 'indexOf') {
108-
return (arg) => value.indexOf(arg && arg[IS_PROXY] ? arg[VALUE] : arg)
111+
return (searchTerm, offset) =>
112+
value.indexOf(getValue(searchTerm), getValue(offset))
109113
}
110114
if (
111115
prop === 'length' ||

packages/node_modules/proxy-state-tree/src/index.test.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,17 @@ describe('ARRAYS', () => {
369369
})
370370
})
371371

372+
test('should create proxies of arrays', () => {
373+
const state = {
374+
foo: [],
375+
}
376+
const tree = new ProxyStateTree(state)
377+
const trackStateTree = tree.getTrackStateTree().track(() => {})
378+
const foo = trackStateTree.state.foo
379+
380+
expect(foo[IS_PROXY]).toBeTruthy()
381+
})
382+
372383
describe('MUTATIONS', () => {
373384
test('should throw when mutating without tracking mutations', () => {
374385
const state = {
@@ -493,24 +504,15 @@ describe('ARRAYS', () => {
493504
})
494505
})
495506

496-
test('should allow CONCAT of existing array', () => {
507+
test('should allow indexOf using proxy values', () => {
497508
const state = {
498-
foo: ['foo'],
509+
things: [{ some: 'thing' }],
510+
ids: [2, 1, 1, 1, 2],
499511
}
500512
const tree = new ProxyStateTree(state)
501-
const mutationTree = tree.getMutationTree()
502-
mutationTree.state.foo = mutationTree.state.foo.concat('bar')
503-
504-
expect(mutationTree.mutations).toEqual([
505-
{
506-
method: 'set',
507-
path: 'foo',
508-
args: [['foo', 'bar']],
509-
hasChangedValue: true,
510-
},
511-
])
512-
513-
expect(state.foo).toEqual(['foo', 'bar'])
513+
expect(tree.state.things.indexOf(tree.state.things[0])).toEqual(0)
514+
expect(tree.state.ids.indexOf(2)).toEqual(0)
515+
expect(tree.state.ids.indexOf(2, 1)).toEqual(4)
514516
})
515517
})
516518

0 commit comments

Comments
 (0)