-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the merge and removeBatch mutators #27
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,16 @@ | ||
// @flow | ||
import type { MutableState, Mutator, Tools } from 'final-form' | ||
|
||
const merge: Mutator = ( | ||
[name, value]: any[], | ||
state: MutableState, | ||
{ changeValue }: Tools | ||
) => { | ||
changeValue( | ||
state, | ||
name, | ||
(array: ?(any[])): any[] => (array ? [...array, ...value] : value) | ||
) | ||
} | ||
|
||
export default merge |
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,35 @@ | ||
import merge from './merge' | ||
|
||
describe('merge', () => { | ||
const getOp = value => { | ||
const changeValue = jest.fn() | ||
merge(['foo', value], {}, { changeValue }) | ||
return changeValue.mock.calls[0][2] | ||
} | ||
|
||
it('should call changeValue once', () => { | ||
const changeValue = jest.fn() | ||
const state = {} | ||
const result = merge(['foo', ['bar', 'baz']], state, { changeValue }) | ||
expect(result).toBeUndefined() | ||
expect(changeValue).toHaveBeenCalled() | ||
expect(changeValue).toHaveBeenCalledTimes(1) | ||
expect(changeValue.mock.calls[0][0]).toBe(state) | ||
expect(changeValue.mock.calls[0][1]).toBe('foo') | ||
expect(typeof changeValue.mock.calls[0][2]).toBe('function') | ||
}) | ||
|
||
it('should turn undefined into an array with two values', () => { | ||
const op = getOp(['bar', 'baz']) | ||
const result = op(undefined) | ||
expect(Array.isArray(result)).toBe(true) | ||
expect(result).toEqual(['bar', 'baz']) | ||
}) | ||
|
||
it('should merge the array at the end of the original array', () => { | ||
const op = getOp(['d', 'e']) | ||
const result = op(['a', 'b', 'c']) | ||
expect(Array.isArray(result)).toBe(true) | ||
expect(result).toEqual(['a', 'b', 'c', 'd', 'e']) | ||
}) | ||
}) |
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,36 @@ | ||
// @flow | ||
import type { MutableState, Mutator, Tools } from 'final-form' | ||
|
||
const removeBatch: Mutator = ( | ||
[name, indexes]: any[], | ||
state: MutableState, | ||
{ changeValue }: Tools | ||
) => { | ||
changeValue( | ||
state, | ||
name, | ||
(array: ?(any[])): ?(any[]) => { | ||
if (!array || !indexes) { | ||
return array | ||
} | ||
|
||
let mask = new Array(indexes.length) | ||
for (let i = 0; i < indexes.length; i++) { | ||
mask[indexes[i]] = true | ||
} | ||
|
||
let offset = 0 | ||
for (let i = 0; i < array.length; i++) { | ||
if (mask[i] === undefined) { | ||
array[offset] = array[i] | ||
offset++ | ||
} | ||
} | ||
|
||
array.length = offset | ||
return array | ||
} | ||
) | ||
} | ||
|
||
export default removeBatch |
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,41 @@ | ||
import removeBatch from './removeBatch' | ||
|
||
describe('merge', () => { | ||
const getOp = value => { | ||
const changeValue = jest.fn() | ||
removeBatch(['foo', value], {}, { changeValue }) | ||
return changeValue.mock.calls[0][2] | ||
} | ||
|
||
it('should call changeValue once', () => { | ||
const changeValue = jest.fn() | ||
const state = {} | ||
const result = removeBatch(['foo', [1, 2]], state, { changeValue }) | ||
expect(result).toBeUndefined() | ||
expect(changeValue).toHaveBeenCalled() | ||
expect(changeValue).toHaveBeenCalledTimes(1) | ||
expect(changeValue.mock.calls[0][0]).toBe(state) | ||
expect(changeValue.mock.calls[0][1]).toBe('foo') | ||
expect(typeof changeValue.mock.calls[0][2]).toBe('function') | ||
}) | ||
|
||
it('should return undefined if array is undefined', () => { | ||
const op = getOp([0, 1]) | ||
const result = op(undefined) | ||
expect(result).toBeUndefined() | ||
}) | ||
|
||
it('should return the original array if no indexes are specified to be removed', () => { | ||
const op = getOp([]) | ||
const result = op(['a', 'b', 'c', 'd', 'e']) | ||
expect(Array.isArray(result)).toBe(true) | ||
expect(result).toEqual(['a', 'b', 'c', 'd', 'e']) | ||
}) | ||
|
||
it('should remove the values at the specified indexes', () => { | ||
const op = getOp([1, 3]) | ||
const result = op(['a', 'b', 'c', 'd', 'e']) | ||
expect(Array.isArray(result)).toBe(true) | ||
expect(result).toEqual(['a', 'c', 'e']) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could simply be:
array => array && indexes ? array.filter((item, i) => !indexes.includes(i)) : array
Although much simpler, the performance is significantly worse. Not sure what should be the goal here, since probably there won't be hundreds/thousands of item in the field array.
Any thoughts?