Skip to content
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 array.copyWithin #326

Merged
merged 1 commit into from
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/immutadot/src/array/copyWithin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { applyArrayMethod } from './applyArrayMethod'

/**
* Shallow copies part of an array to another location in the same array, without modifying its size.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {number} target Zero based index at which to copy the sequence to.
* @param {number} [start] Zero based index at which to start copying elements from.
* @param {number} [end] Zero based index at which to end copying elements from.
* @return {Object} Returns the updated object.
* @example copyWithin({ nested: { prop: [1, 2, 3, 4, 5] } }, 'nested.prop', 0, 3, 4) // => { nested: { prop: [4, 2, 3, 4, 5] } }
* @see {@link https://mdn.io/Array.prototype.copyWithin|Array.prototype.copyWithin} for more information.
* @since 2.0.0
*/
const copyWithin = applyArrayMethod(Array.prototype.copyWithin, {
arity: 1,
mutating: true,
})

export { copyWithin }
23 changes: 23 additions & 0 deletions packages/immutadot/src/array/copyWithin.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-env jest */
import { copyWithin } from 'array'
import { immutaTest } from 'test.utils'

describe('array.copyWithin', () => {
it('should copy within an array', () => {
immutaTest({
nested: {
prop: [1, 2, 3, 4, 5],
},
other: {},
}, ['nested.prop'], (input, path) => {
const output = copyWithin(input, path, 0, 3, 4)
expect(output).toEqual({
nested: {
prop: [4, 2, 3, 4, 5],
},
other: {},
})
return output
})
})
})
3 changes: 2 additions & 1 deletion packages/immutadot/src/array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* @since 0.1.6
*/

export { filter } from './filter'
export { concat } from './concat'
export { copyWithin } from './copyWithin'
export { fill } from './fill'
export { filter } from './filter'
export { map } from './map'
export { pop } from './pop'
export { push } from './push'
Expand Down
1 change: 1 addition & 0 deletions packages/immutadot/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {
concat as arrayConcat,
copyWithin,
fill,
filter,
map,
Expand Down