Skip to content

Commit

Permalink
✨ Add array.splice and array.unshift fix #33 (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Jun 28, 2017
1 parent cef8531 commit 341e169
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/array/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import push from './push'
import splice from './splice'
import unshift from './unshift'
import xor from './xor'

/**
Expand All @@ -8,5 +10,7 @@ import xor from './xor'
*/
export {
push,
splice,
unshift,
xor,
}
10 changes: 5 additions & 5 deletions src/array/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import toArray from 'lodash/toArray'
import update from '../object/update'

/**
* Replaces by a new array containing the elements of the former array and one or more elements added to the end.
* Add one or more elements to the end of an array.
* @function push
* @memberof array
* @param {Object} object The object to modify.
Expand All @@ -18,8 +18,8 @@ import update from '../object/update'
* @see {@link object.update|update} for more information.
* @since 0.1.7
*/
export default update((value, ...values) => {
const newValue = toArray(value)
newValue.push(...values)
return newValue
export default update((array, ...values) => {
const newArray = toArray(array)
newArray.push(...values)
return newArray
})
24 changes: 24 additions & 0 deletions src/array/splice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import toArray from 'lodash/toArray'
import update from '../object/update'

/**
* Changes the contents of an array by removing existing elements and/or adding new elements.
* @function splice
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {number} start Index at which to start changing the array.
* @param {number} [deleteCount] The number of old array elements to remove.
* @param {...*} values The values to add.
* @return {Object} Returns the updated object.
* @example splice({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 1, 2, 5, 6) // => { nested: { prop: [1, 5, 6, 4] } }
* @see {@link https://lodash.com/docs#toArray|lodash.toArray} for more information.
* @see {@link https://mdn.io/Array.prototype.splice|Array.prototype.splice} for more information.
* @see {@link object.update|update} for more information.
* @since 0.1.14
*/
export default update((array, ...args) => {
const newArray = toArray(array)
newArray.splice(...args)
return newArray
})
14 changes: 14 additions & 0 deletions src/array/splice.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-env jest */
import splice from './splice'

describe('Splice', () => {

it('should replace two elements', () => {
const original = { nested: { prop: [1, 2, 3, 4] } }

const final = splice(original, 'nested.prop', 1, 2, 5, 6)

expect(final).toEqual({ nested: { prop: [1, 5, 6, 4] } })
expect(original).toEqual({ nested: { prop: [1, 2, 3, 4] } })
})
})
25 changes: 25 additions & 0 deletions src/array/unshift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import toArray from 'lodash/toArray'
import update from '../object/update'

/**
* Adds one or more elements at the start of an array.
* @function unshift
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {...*} values The values to add.
* @return {Object} Returns the updated object.
* @example <caption>Add one element.</caption>
* push({ nested: { prop: [1, 2] } }, 'nested.prop', 3) // => { nested: { prop: [3, 1, 2] } }
* @example <caption>Add several elements.</caption>
* push({ nested: { prop: [1, 2] } }, 'nested.prop', 3, 4) // => { nested: { prop: [3, 4, 1, 2] } }
* @see {@link https://lodash.com/docs#toArray|lodash.toArray} for more information.
* @see {@link https://mdn.io/Array.prototype.unshift|Array.prototype.unshift} for more information.
* @see {@link object.update|update} for more information.
* @since 0.1.7
*/
export default update((array, ...values) => {
const newArray = toArray(array)
newArray.unshift(...values)
return newArray
})
22 changes: 22 additions & 0 deletions src/array/unshift.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-env jest */
import unshift from './unshift'

describe('Unshift', () => {

it('should add one element', () => {
const original = { nested: { prop: [1, 2] } }

const final = unshift(original, 'nested.prop', 3)

expect(final).toEqual({ nested: { prop: [3, 1, 2] } })
expect(original).toEqual({ nested: { prop: [1, 2] } })
})

it('should add several elements', () => {
expect(unshift({ nested: { prop: [1, 2] } }, 'nested.prop', 3, 4)).toEqual({ nested: { prop: [3, 4, 1, 2] } })
})

it('should replace deep undefined with array', () => {
expect(unshift(undefined, 'nested.prop', 1)).toEqual({ nested: { prop: [1] } })
})
})

0 comments on commit 341e169

Please sign in to comment.