Skip to content

Commit

Permalink
✨ Add array functions fix #49 (#71)
Browse files Browse the repository at this point in the history
* differenceBy
* differenceWith
* dropRight
* dropWhile
* dropRightWhile
* fill
* intersection
* intersectionBy
* intersectionWith
* pullAll
* pullAllBy
* pullAllWith
* pullAt
* reverse
* slice
* union
* unionBy
* unionWith
* xorBy
* xorWith
* without
  • Loading branch information
nlepage committed Aug 17, 2017
1 parent 065a1cb commit a735d65
Show file tree
Hide file tree
Showing 44 changed files with 598 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/array/difference.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { convert } from '../util/convert'
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {...Array} arrays The arrays of values to exclude.
* @param {...Array} values The arrays of values to exclude.
* @return {Object} Returns the updated object.
* @example difference({ nested: { prop: [1, 2] } }, 'nested.prop', [2, 3]) // => { nested: { prop: [1] } }
* @see {@link https://lodash.com/docs#difference|lodash.difference} for more information.
Expand Down
18 changes: 18 additions & 0 deletions src/array/differenceBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import _differenceBy from 'lodash/differenceBy'
import { convert } from '../util/convert'

/**
* This method is like {@link array.difference} except that it uses <code>iteratee</code> to generate the value to be compared for each element.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {...Array} values The arrays of values to exclude.
* @param {Function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The iteratee invoked per element.
* @return {Object} Returns the updated object.
* @example differenceBy({ nested: { prop: [1.2, 3.4, 5.6] } }, 'nested.prop', [5.4, 2.1], Math.floor) // => { nested: { prop: [1.2, 3.4] } }
* @see {@link https://lodash.com/docs#differenceBy|lodash.differenceBy} for more information.
* @since 0.3.0
*/
const differenceBy = convert(_differenceBy)
export { differenceBy, differenceBy as default }
9 changes: 9 additions & 0 deletions src/array/differenceBy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { differenceBy } from './differenceBy'

describe('DifferenceBy', () => {

it('should remove intersecting elements according to iteratee', () => {
expect(differenceBy({ nested: { prop: [1.2, 3.4, 5.6] } }, 'nested.prop', [5.4, 2.1], Math.floor)).toEqual({ nested: { prop: [1.2, 3.4] } })
})
})
18 changes: 18 additions & 0 deletions src/array/differenceWith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import _differenceWith from 'lodash/differenceWith'
import { convert } from '../util/convert'

/**
* This method is like {@link array.difference} except that it uses <code>comparator</code> to compare elements of the former array to <code>values</code>.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {...Array} values The arrays of values to exclude.
* @param {Function} [comparator] The comparator invoked per element.
* @return {Object} Returns the updated object.
* @example differenceWith({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], (a, b) => a.x === b.x) // => { nested: { prop: [{ x: 1 }] } }
* @see {@link https://lodash.com/docs#differenceWith|lodash.differenceWith} for more information.
* @since 0.3.0
*/
const differenceWith = convert(_differenceWith)
export { differenceWith, differenceWith as default }
9 changes: 9 additions & 0 deletions src/array/differenceWith.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { differenceWith } from './differenceWith'

describe('DifferenceWith', () => {

it('should remove intersecting elements according to iteratee', () => {
expect(differenceWith({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], (a, b) => a.x === b.x)).toEqual({ nested: { prop: [{ x: 1 }] } })
})
})
17 changes: 17 additions & 0 deletions src/array/dropRight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _dropRight from 'lodash/dropRight'
import { convert } from '../util/convert'

/**
* Replaces an array dropping one or several elements at the end of the former array.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {number} [n=1] The number of elements to drop.
* @return {Object} Returns the updated object.
* @example dropRight({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 2) // => { nested: { prop: [1, 2] } }
* @see {@link https://lodash.com/docs#dropRight|lodash.dropRight} for more information.
* @since 0.3.0
*/
const dropRight = convert(_dropRight)
export { dropRight, dropRight as default }
9 changes: 9 additions & 0 deletions src/array/dropRight.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { dropRight } from './dropRight'

describe('DropRight', () => {

it('should drop several elements at the end of the array', () => {
expect(dropRight({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 2)).toEqual({ nested: { prop: [1, 2] } })
})
})
17 changes: 17 additions & 0 deletions src/array/dropRightWhile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _dropRightWhile from 'lodash/dropRightWhile'
import { convert } from '../util/convert'

/**
* Replaces an array excluding elements dropped from the end. Elements are dropped until <code>predicate</code> returns falsey.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {Function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
* @return {Object} Returns the updated object.
* @example dropRightWhile({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v > 2) // => { nested: { prop: [1, 2] } }
* @see {@link https://lodash.com/docs#dropRightWhile|lodash.dropRightWhile} for more information.
* @since 0.3.0
*/
const dropRightWhile = convert(_dropRightWhile)
export { dropRightWhile, dropRightWhile as default }
9 changes: 9 additions & 0 deletions src/array/dropRightWhile.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { dropRightWhile } from './dropRightWhile'

describe('DropRightWhile', () => {

it('should drop elements > 2 at the end of the array', () => {
expect(dropRightWhile({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v > 2)).toEqual({ nested: { prop: [1, 2] } })
})
})
17 changes: 17 additions & 0 deletions src/array/dropWhile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _dropWhile from 'lodash/dropWhile'
import { convert } from '../util/convert'

/**
* Replaces an array excluding elements dropped from the beginning. Elements are dropped until <code>predicate</code> returns falsey.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {Function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
* @return {Object} Returns the updated object.
* @example dropWhile({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v < 3) // => { nested: { prop: [3, 4] } }
* @see {@link https://lodash.com/docs#dropWhile|lodash.dropWhile} for more information.
* @since 0.3.0
*/
const dropWhile = convert(_dropWhile)
export { dropWhile, dropWhile as default }
9 changes: 9 additions & 0 deletions src/array/dropWhile.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import dropWhile from './dropWhile'

describe('DropWhile', () => {

it('should drop elements < 3 at the beginning of the array', () => {
expect(dropWhile({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v < 3)).toEqual({ nested: { prop: [3, 4] } })
})
})
19 changes: 19 additions & 0 deletions src/array/fill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import _fill from 'lodash/fill.js'
import { convert } from '../util/convert'

/**
* Replaces by an array filled with value from start up to, but not including, end.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {*} value The value to fill array with.
* @param {number} [start=0]
* @param {number} [end=array.length]
* @return {Object} Returns the updated object.
* @example fill({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 6, 1, 3) // => { nested: { prop: [1, 6, 6, 4] } }
* @see {@link https://lodash.com/docs#fill|lodash.fill} for more information.
* @since 0.3.0
*/
const fill = convert(_fill)
export { fill, fill as default }
9 changes: 9 additions & 0 deletions src/array/fill.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { fill } from './fill'

describe('Fill', () => {

it('should fill array with 6 from 1 to 3 excluded', () => {
expect(fill({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 6, 1, 3)).toEqual({ nested: { prop: [1, 6, 6, 4] } }) // 🍺
})
})
42 changes: 42 additions & 0 deletions src/array/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import { concat } from './concat'
import { difference } from './difference'
import { differenceBy } from './differenceBy'
import { differenceWith } from './differenceWith'
import { drop } from './drop'
import { dropRight } from './dropRight'
import { dropRightWhile } from './dropRightWhile'
import { dropWhile } from './dropWhile'
import { fill } from './fill'
import { intersection } from './intersection'
import { intersectionBy } from './intersectionBy'
import { intersectionWith } from './intersectionWith'
import { pull } from './pull'
import { pullAll } from './pullAll'
import { pullAllBy } from './pullAllBy'
import { pullAllWith } from './pullAllWith'
import { pullAt } from './pullAt'
import { push } from './push'
import { remove } from './remove'
import { reverse } from './reverse'
import { slice } from './slice'
import { splice } from './splice'
import { take } from './take'
import { takeRight } from './takeRight'
import { takeRightWhile } from './takeRightWhile'
import { takeWhile } from './takeWhile'
import { union } from './union'
import { unionBy } from './unionBy'
import { unionWith } from './unionWith'
import { unshift } from './unshift'
import { without } from './without'
import { xor } from './xor'
import { xorBy } from './xorBy'
import { xorWith } from './xorWith'

/**
* Array functions.
Expand All @@ -20,15 +41,36 @@ import { xor } from './xor'
export {
concat,
difference,
differenceBy,
differenceWith,
drop,
dropRight,
dropRightWhile,
dropWhile,
fill,
intersection,
intersectionBy,
intersectionWith,
pull,
pullAll,
pullAllBy,
pullAllWith,
pullAt,
push,
remove,
reverse,
slice,
splice,
take,
takeRight,
takeRightWhile,
takeWhile,
union,
unionBy,
unionWith,
unshift,
without,
xor,
xorBy,
xorWith,
}
17 changes: 17 additions & 0 deletions src/array/intersection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _intersection from 'lodash/intersection'
import { convert } from '../util/convert'

/**
* Replaces by an array of unique values that are included in th former array and all given arrays.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {...Array} [arrays] The arrays to inspect.
* @return {Object} Returns the updated object.
* @example intersection({ nested: { prop: [1, 2] } }, 'nested.prop', [2, 3]) // => { nested: { prop: [2] } }
* @see {@link https://lodash.com/docs#intersection|lodash.intersection} for more information.
* @since 0.3.0
*/
const intersection = convert(_intersection)
export { intersection, intersection as default }
9 changes: 9 additions & 0 deletions src/array/intersection.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { intersection } from './intersection'

describe('Intersection', () => {

it('should replace by intersection of arrays', () => {
expect(intersection({ nested: { prop: [1, 2] } }, 'nested.prop', [2, 3])).toEqual({ nested: { prop: [2] } })
})
})
18 changes: 18 additions & 0 deletions src/array/intersectionBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import _intersectionBy from 'lodash/intersectionBy'
import { convert } from '../util/convert'

/**
* This method is like {@link array.intersection} except that it uses <code>iteratee</code> to generate the value to be compared for each element.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {...Array} [arrays] The arrays to inspect.
* @param {Function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The iteratee invoked per element.
* @return {Object} Returns the updated object.
* @example intersectionBy({ nested: { prop: [1.2, 2.1] } }, 'nested.prop', [2.3, 3.2], Math.floor) // => { nested: { prop: [2.1] } }
* @see {@link https://lodash.com/docs#intersectionBy|lodash.intersectionBy} for more information.
* @since 0.3.0
*/
const intersectionBy = convert(_intersectionBy)
export { intersectionBy, intersectionBy as default }
9 changes: 9 additions & 0 deletions src/array/intersectionBy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { intersectionBy } from './intersectionBy'

describe('IntersectionBy', () => {

it('should replace by intersection of arrays', () => {
expect(intersectionBy({ nested: { prop: [1.2, 2.1] } }, 'nested.prop', [2.3, 3.2], Math.floor)).toEqual({ nested: { prop: [2.1] } })
})
})
18 changes: 18 additions & 0 deletions src/array/intersectionWith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import _intersectionWith from 'lodash/intersectionWith'
import { convert } from '../util/convert'

/**
* This method is like {@link array.intersection} except that it uses <code>comparator</code> to compare elements of the former array to <code>arrays</code>.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {...Array} [arrays] The arrays to inspect.
* @param {Function} [comparator] The comparator invoked per element.
* @return {Object} Returns the updated object.
* @example intersectionWith({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], (a, b) => a.x === b.x) // => { nested: { prop: [{ x: 2 }] } }
* @see {@link https://lodash.com/docs#intersectionWith|lodash.intersectionWith} for more information.
* @since 0.3.0
*/
const intersectionWith = convert(_intersectionWith)
export { intersectionWith, intersectionWith as default }
9 changes: 9 additions & 0 deletions src/array/intersectionWith.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { intersectionWith } from './intersectionWith'

describe('IntersectionWith', () => {

it('should replace by intersection of arrays', () => {
expect(intersectionWith({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], (a, b) => a.x === b.x)).toEqual({ nested: { prop: [{ x: 2 }] } })
})
})
17 changes: 17 additions & 0 deletions src/array/pullAll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import _pullAll from 'lodash/pullAll'
import { convert } from '../util/convert'

/**
* This method is like {@link array.pull} except that it accepts an array of values to remove.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {Array} values The values to remove.
* @return {Object} Returns the updated object.
* @example pullAll({ nested: { prop: [1, 2, 3, 1, 2, 3] } }, 'nested.prop', [1, 3]) // => { nested: { prop: [2, 2] } }
* @see {@link https://lodash.com/docs#pullAll|lodash.pullAll} for more information.
* @since 0.3.0
*/
const pullAll = convert(_pullAll)
export { pullAll, pullAll as default }
9 changes: 9 additions & 0 deletions src/array/pullAll.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { pullAll } from './pullAll'

describe('PullAll', () => {

it('should remove several matching elements', () => {
expect(pullAll({ nested: { prop: [1, 2, 3, 1, 2, 3] } }, 'nested.prop', [1, 3])).toEqual({ nested: { prop: [2, 2] } })
})
})
18 changes: 18 additions & 0 deletions src/array/pullAllBy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import _pullAllBy from 'lodash/pullAllBy'
import { convert } from '../util/convert'

/**
* This method is like {@link array.pullAll} except that it accepts <code>iteratee</code> to generate the criterion by which each element is compared.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {Array} values The values to remove.
* @param {Function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The iteratee invoked per element.
* @return {Object} Returns the updated object.
* @example pullAllBy({ nested: { prop: [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 1 }, { x: 2 }, { x: 3 }] } }, 'nested.prop', [{ x: 1 }, { x: 3 }], 'x') // => { nested: { prop: [{ x: 2 }, { x: 2 }] } }
* @see {@link https://lodash.com/docs#pullAllBy|lodash.pullAllBy} for more information.
* @since 0.3.0
*/
const pullAllBy = convert(_pullAllBy)
export { pullAllBy, pullAllBy as default }
9 changes: 9 additions & 0 deletions src/array/pullAllBy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-env jest */
import { pullAllBy } from './pullAllBy'

describe('PullAllBy', () => {

it('should remove several matching elements', () => {
expect(pullAllBy({ nested: { prop: [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 1 }, { x: 2 }, { x: 3 }] } }, 'nested.prop', [{ x: 1 }, { x: 3 }], 'x')).toEqual({ nested: { prop: [{ x: 2 }, { x: 2 }] } })
})
})

0 comments on commit a735d65

Please sign in to comment.