404
9 |
Sorry, but the page that you requested doesn't exist
10 | Back Home 11 | Search Archive 12 |iteratee
to generate the value to be compared for each element.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} values The arrays of values to exclude.
11 | * @param {Function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The iteratee invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @example differenceBy({ nested: { prop: [1.2, 3.4, 5.6] } }, 'nested.prop', [5.4, 2.1], Math.floor) // => { nested: { prop: [1.2, 3.4] } }
14 | * @see {@link https://lodash.com/docs#differenceBy|lodash.differenceBy} for more information.
15 | * @since 1.0.0
16 | */
17 | const differenceBy = apply(_differenceBy, { arity: 2 })
18 |
19 | export { differenceBy }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/differenceBy.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { differenceBy } from 'array'
3 | import { immutaTest } from 'test.utils'
4 | describe('DifferenceBy', () => {
5 | it('should remove intersecting elements according to iteratee', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1.2,
10 | 3.4,
11 | 5.6,
12 | ],
13 | },
14 | other: {},
15 | }, ['nested.prop'], (input, path) => {
16 | const output = differenceBy(input, path, [
17 | 5.4,
18 | 2.1,
19 | ], Math.floor)
20 | expect(output).toEqual({
21 | nested: {
22 | prop: [
23 | 1.2,
24 | 3.4,
25 | ],
26 | },
27 | other: {},
28 | })
29 | return output
30 | })
31 | })
32 | })
33 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/differenceWith.js:
--------------------------------------------------------------------------------
1 | import { differenceWith as _differenceWith } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * This method is like {@link array.difference} except that it uses comparator
to compare elements of the former array to values
.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} values The arrays of values to exclude.
11 | * @param {Function} [comparator] The comparator invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @example differenceWith({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], (a, b) => a.x === b.x) // => { nested: { prop: [{ x: 1 }] } }
14 | * @see {@link https://lodash.com/docs#differenceWith|lodash.differenceWith} for more information.
15 | * @since 1.0.0
16 | */
17 | const differenceWith = apply(_differenceWith, 2)
18 |
19 | export { differenceWith }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/differenceWith.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { differenceWith } from 'array'
3 | import { immutaTest } from 'test.utils'
4 | describe('DifferenceWith', () => {
5 | it('should remove intersecting elements according to iteratee', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{ x: 1 },
9 | { x: 2 },
10 | ],
11 | },
12 | other: {},
13 | }, ['nested.prop'], (input, path) => {
14 | const output = differenceWith(input, path, [{ x: 2 },
15 | { x: 3 },
16 | ], (a, b) => a.x === b.x)
17 | expect(output).toEqual({
18 | nested: { prop: [{ x: 1 }] },
19 | other: {},
20 | })
21 | return output
22 | })
23 | })
24 | })
25 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/drop.js:
--------------------------------------------------------------------------------
1 | import { drop as _drop } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces an array dropping one or several elements at the start of the former array.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {number} [n=1] The number of elements to drop.
11 | * @return {Object} Returns the updated object.
12 | * @example drop({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 2) // => { nested: { prop: [3, 4] } }
13 | * @see {@link https://lodash.com/docs#drop|lodash.drop} for more information.
14 | * @since 1.0.0
15 | */
16 | const drop = apply(_drop, { arity: 1 })
17 |
18 | export { drop }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/dropRight.js:
--------------------------------------------------------------------------------
1 | import { dropRight as _dropRight } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces an array dropping one or several elements at the end of the former array.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {number} [n=1] The number of elements to drop.
11 | * @return {Object} Returns the updated object.
12 | * @example dropRight({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 2) // => { nested: { prop: [1, 2] } }
13 | * @see {@link https://lodash.com/docs#dropRight|lodash.dropRight} for more information.
14 | * @since 1.0.0
15 | */
16 | const dropRight = apply(_dropRight, { arity: 1 })
17 | export { dropRight }
18 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/dropRight.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { dropRight } from 'array'
3 | import { immutaTest } from 'test.utils'
4 | describe('DropRight', () => {
5 | it('should drop several elements at the end of the array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = dropRight(input, path, 2)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 1,
22 | 2,
23 | ],
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/dropRightWhile.js:
--------------------------------------------------------------------------------
1 | import { dropRightWhile as _dropRightWhile } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces an array excluding elements dropped from the end. Elements are dropped until predicate
returns falsey.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {Function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
11 | * @return {Object} Returns the updated object.
12 | * @example dropRightWhile({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v > 2) // => { nested: { prop: [1, 2] } }
13 | * @see {@link https://lodash.com/docs#dropRightWhile|lodash.dropRightWhile} for more information.
14 | * @since 1.0.0
15 | */
16 | const dropRightWhile = apply(_dropRightWhile, { arity: 1 })
17 |
18 | export { dropRightWhile }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/dropRightWhile.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { dropRightWhile } from 'array'
3 | import { immutaTest } from 'test.utils'
4 | describe('DropRightWhile', () => {
5 | it('should drop elements > 2 at the end of the array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = dropRightWhile(input, path, v => v > 2)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 1,
22 | 2,
23 | ],
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/dropWhile.js:
--------------------------------------------------------------------------------
1 | import { dropWhile as _dropWhile } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces an array excluding elements dropped from the beginning. Elements are dropped until predicate
returns falsey.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {Function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
11 | * @return {Object} Returns the updated object.
12 | * @example dropWhile({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v < 3) // => { nested: { prop: [3, 4] } }
13 | * @see {@link https://lodash.com/docs#dropWhile|lodash.dropWhile} for more information.
14 | * @since 1.0.0
15 | */
16 | const dropWhile = apply(_dropWhile, { arity: 1 })
17 |
18 | export { dropWhile }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/dropWhile.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { dropWhile } from 'array'
3 | import { immutaTest } from 'test.utils'
4 | describe('DropWhile', () => {
5 | it('should drop elements < 3 at the beginning of the array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = dropWhile(input, path, v => v < 3)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 3,
22 | 4,
23 | ],
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/intersection.js:
--------------------------------------------------------------------------------
1 | import { intersection as _intersection } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an array of unique values that are included in th former array and all given arrays.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} [arrays] The arrays to inspect.
11 | * @return {Object} Returns the updated object.
12 | * @example intersection({ nested: { prop: [1, 2] } }, 'nested.prop', [2, 3]) // => { nested: { prop: [2] } }
13 | * @see {@link https://lodash.com/docs#intersection|lodash.intersection} for more information.
14 | * @since 1.0.0
15 | */
16 | const intersection = apply(_intersection, { arity: 2 })
17 |
18 | export { intersection }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/intersection.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { intersection } from 'array'
4 | describe('Intersection', () => {
5 | it('should replace by intersection of arrays', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | ],
12 | },
13 | other: {},
14 | }, ['nested.prop'], (input, path) => {
15 | const output = intersection(input, path, [
16 | 2,
17 | 3,
18 | ])
19 | expect(output).toEqual({
20 | nested: { prop: [2] },
21 | other: {},
22 | })
23 | return output
24 | })
25 | })
26 | })
27 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/intersectionBy.js:
--------------------------------------------------------------------------------
1 | import { intersectionBy as _intersectionBy } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * This method is like {@link array.intersection} except that it uses iteratee
to generate the value to be compared for each element.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} [arrays] The arrays to inspect.
11 | * @param {Function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The iteratee invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @example intersectionBy({ nested: { prop: [1.2, 2.1] } }, 'nested.prop', [2.3, 3.2], Math.floor) // => { nested: { prop: [2.1] } }
14 | * @see {@link https://lodash.com/docs#intersectionBy|lodash.intersectionBy} for more information.
15 | * @since 1.0.0
16 | */
17 | const intersectionBy = apply(_intersectionBy, { arity: 2 })
18 |
19 | export { intersectionBy }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/intersectionBy.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { intersectionBy } from 'array'
4 | describe('IntersectionBy', () => {
5 | it('should replace by intersection of arrays', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1.2,
10 | 2.1,
11 | ],
12 | },
13 | other: {},
14 | }, ['nested.prop'], (input, path) => {
15 | const output = intersectionBy(input, path, [
16 | 2.3,
17 | 3.2,
18 | ], Math.floor)
19 | expect(output).toEqual({
20 | nested: { prop: [2.1] },
21 | other: {},
22 | })
23 | return output
24 | })
25 | })
26 | })
27 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/intersectionWith.js:
--------------------------------------------------------------------------------
1 | import { intersectionWith as _intersectionWith } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * This method is like {@link array.intersection} except that it uses comparator
to compare elements of the former array to arrays
.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} [arrays] The arrays to inspect.
11 | * @param {Function} [comparator] The comparator invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @example intersectionWith({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], (a, b) => a.x === b.x) // => { nested: { prop: [{ x: 2 }] } }
14 | * @see {@link https://lodash.com/docs#intersectionWith|lodash.intersectionWith} for more information.
15 | * @since 1.0.0
16 | */
17 | const intersectionWith = apply(_intersectionWith, { arity: 2 })
18 |
19 | export { intersectionWith }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/intersectionWith.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { intersectionWith } from 'array'
4 | describe('IntersectionWith', () => {
5 | it('should replace by intersection of arrays', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{ x: 1 },
9 | { x: 2 },
10 | ],
11 | },
12 | other: {},
13 | }, ['nested.prop'], (input, path) => {
14 | const output = intersectionWith(input, path, [{ x: 2 },
15 | { x: 3 },
16 | ], (a, b) => a.x === b.x)
17 | expect(output).toEqual({
18 | nested: { prop: [{ x: 2 }] },
19 | other: {},
20 | })
21 | return output
22 | })
23 | })
24 | })
25 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pull.js:
--------------------------------------------------------------------------------
1 | import { pull as _pull } from 'lodash/fp'
2 | import { applyLodashFp } from 'util/applyLodashFp'
3 |
4 | /**
5 | * Replaces an array removing all given values from the former array.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...*} values The values to remove.
11 | * @return {Object} Returns the updated object.
12 | * @example pull({ nested: { prop: [1, 2, 3, 1, 2, 3] } }, 'nested.prop', 1, 3) // => { nested: { prop: [2, 2] } }
13 | * @see {@link https://lodash.com/docs#pull|lodash.pull} for more information.
14 | * @since 1.0.0
15 | */
16 | const pull = applyLodashFp(_pull, { arity: 2 })
17 |
18 | export { pull }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pull.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { pull } from 'array'
4 | describe('Pull', () => {
5 | it('should remove matching elements', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 1,
13 | 2,
14 | 3,
15 | ],
16 | },
17 | other: {},
18 | }, ['nested.prop'], (input, path) => {
19 | const output = pull(input, path, 2)
20 | expect(output).toEqual({
21 | nested: {
22 | prop: [
23 | 1,
24 | 3,
25 | 1,
26 | 3,
27 | ],
28 | },
29 | other: {},
30 | })
31 | return output
32 | })
33 | })
34 | it('should remove several matching elements', () => {
35 | immutaTest({
36 | nested: {
37 | prop: [
38 | 1,
39 | 2,
40 | 3,
41 | 1,
42 | 2,
43 | 3,
44 | ],
45 | },
46 | other: {},
47 | }, ['nested.prop'], (input, path) => {
48 | const output = pull(input, path, 1, 3)
49 | expect(output).toEqual({
50 | nested: {
51 | prop: [
52 | 2,
53 | 2,
54 | ],
55 | },
56 | other: {},
57 | })
58 | return output
59 | })
60 | })
61 | })
62 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pullAll.js:
--------------------------------------------------------------------------------
1 | import { pullAll as _pullAll } from 'lodash/fp'
2 | import { applyLodashFp } from 'util/applyLodashFp'
3 |
4 | /**
5 | * This method is like {@link array.pull} except that it accepts an array of values to remove.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {Array} values The values to remove.
11 | * @return {Object} Returns the updated object.
12 | * @example pullAll({ nested: { prop: [1, 2, 3, 1, 2, 3] } }, 'nested.prop', [1, 3]) // => { nested: { prop: [2, 2] } }
13 | * @see {@link https://lodash.com/docs#pullAll|lodash.pullAll} for more information.
14 | * @since 1.0.0
15 | */
16 | const pullAll = applyLodashFp(
17 | _pullAll,
18 | {
19 | arity: 2,
20 | fixedArity: true,
21 | },
22 | )
23 |
24 | export { pullAll }
25 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pullAll.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { pullAll } from 'array'
4 | describe('PullAll', () => {
5 | it('should remove several matching elements', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 1,
13 | 2,
14 | 3,
15 | ],
16 | },
17 | other: {},
18 | }, ['nested.prop'], (input, path) => {
19 | const output = pullAll(input, path, [
20 | 1,
21 | 3,
22 | ])
23 | expect(output).toEqual({
24 | nested: {
25 | prop: [
26 | 2,
27 | 2,
28 | ],
29 | },
30 | other: {},
31 | })
32 | return output
33 | })
34 | })
35 | })
36 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pullAllBy.js:
--------------------------------------------------------------------------------
1 | import { pullAllBy as _pullAllBy } from 'lodash/fp'
2 | import { applyLodashFp } from 'util/applyLodashFp'
3 |
4 | /**
5 | * This method is like {@link array.pullAll} except that it accepts iteratee
to generate the criterion by which each element is compared.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {Array} values The values to remove.
11 | * @param {Function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The iteratee invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @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 }] } }
14 | * @see {@link https://lodash.com/docs#pullAllBy|lodash.pullAllBy} for more information.
15 | * @since 1.0.0
16 | */
17 | const pullAllBy = applyLodashFp(_pullAllBy, { arity: 2 })
18 |
19 | export { pullAllBy }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pullAllBy.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { pullAllBy } from 'array'
4 | describe('PullAllBy', () => {
5 | it('should remove several matching elements', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{ x: 1 },
9 | { x: 2 },
10 | { x: 3 },
11 | { x: 1 },
12 | { x: 2 },
13 | { x: 3 },
14 | ],
15 | },
16 | other: {},
17 | }, ['nested.prop'], (input, path) => {
18 | const output = pullAllBy(input, path, [{ x: 1 },
19 | { x: 3 },
20 | ], 'x')
21 | expect(output).toEqual({
22 | nested: {
23 | prop: [{ x: 2 },
24 | { x: 2 },
25 | ],
26 | },
27 | other: {},
28 | })
29 | return output
30 | })
31 | })
32 | })
33 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pullAllWith.js:
--------------------------------------------------------------------------------
1 | import { pullAllWith as _pullAllWith } from 'lodash/fp'
2 | import { applyLodashFp } from 'util/applyLodashFp'
3 |
4 | /**
5 | * This method is like {@link array.pullAll} except that it accepts comparator
to compare elements.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {Array} values The values to remove.
11 | * @param {Function} [comparator] The comparator invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @example pullAllWith({ nested: { prop: [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 1 }, { x: 2 }, { x: 3 }] } }, 'nested.prop', [{ x: 1 }, { x: 3 }], (a, b) => a.x === b.x) // => { nested: { prop: [{ x: 2 }, { x: 2 }] } }
14 | * @see {@link https://lodash.com/docs#pullAllWith|lodash.pullAllWith} for more information.
15 | * @since 1.0.0
16 | */
17 | const pullAllWith = applyLodashFp(_pullAllWith, { arity: 2 })
18 |
19 | export { pullAllWith }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pullAllWith.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { pullAllWith } from 'array'
4 | describe('PullAllWith', () => {
5 | it('should remove several matching elements', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{ x: 1 },
9 | { x: 2 },
10 | { x: 3 },
11 | { x: 1 },
12 | { x: 2 },
13 | { x: 3 },
14 | ],
15 | },
16 | other: {},
17 | }, ['nested.prop'], (input, path) => {
18 | const output = pullAllWith(input, path, [{ x: 1 },
19 | { x: 3 },
20 | ], (a, b) => a.x === b.x)
21 | expect(output).toEqual({
22 | nested: {
23 | prop: [{ x: 2 },
24 | { x: 2 },
25 | ],
26 | },
27 | other: {},
28 | })
29 | return output
30 | })
31 | })
32 | })
33 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pullAt.js:
--------------------------------------------------------------------------------
1 | import { pullAt as _pullAt } from 'lodash/fp'
2 | import { applyLodashFp } from 'util/applyLodashFp'
3 |
4 | /**
5 | * Replaces an array removing the specified indexes from the former array.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...(number|number[])} [indexes] The indexes of elements to remove.
11 | * @return {Object} Returns the updated object.
12 | * @example pullAt({ nested: { prop: [4, 3, 2, 1] } }, 'nested.prop', 1, 3) // => { nested: { prop: [4, 2] } }
13 | * @see {@link https://lodash.com/docs#pullAt|lodash.pullAt} for more information.
14 | * @since 1.0.0
15 | */
16 | const pullAt = applyLodashFp(_pullAt, { arity: 2 })
17 |
18 | export { pullAt }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/pullAt.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { pullAt } from 'array'
4 | describe('PullAt', () => {
5 | it('should remove several elements', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 4,
10 | 3,
11 | 2,
12 | 1,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = pullAt(input, path, [
18 | 1,
19 | 3,
20 | ])
21 | expect(output).toEqual({
22 | nested: {
23 | prop: [
24 | 4,
25 | 2,
26 | ],
27 | },
28 | other: {},
29 | })
30 | return output
31 | })
32 | })
33 | })
34 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/remove.js:
--------------------------------------------------------------------------------
1 | import { remove as _remove } from 'lodash/fp'
2 | import { applyLodashFp } from 'util/applyLodashFp'
3 |
4 | /**
5 | * Replaces an array removing elements that predicate returns truthy for from the former array.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {Function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
11 | * @return {Object} Returns the updated object.
12 | * @example remove({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v > 2) // => { nested: { prop: [1, 2] } }
13 | * @see {@link https://lodash.com/docs#remove|lodash.remove} for more information.
14 | * @since 1.0.0
15 | */
16 | const remove = applyLodashFp(_remove, { arity: 1 })
17 |
18 | export { remove }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/remove.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { remove } from 'array'
4 | describe('Remove', () => {
5 | it('should remove an element', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | ],
13 | },
14 | other: {},
15 | }, ['nested.prop'], (input, path) => {
16 | const output = remove(input, path, v => v === 2)
17 | expect(output).toEqual({
18 | nested: {
19 | prop: [
20 | 1,
21 | 3,
22 | ],
23 | },
24 | other: {},
25 | })
26 | return output
27 | })
28 | })
29 | })
30 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/take.js:
--------------------------------------------------------------------------------
1 | import { take as _take } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Creates a slice of array with n
elements taken from the beginning.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {number} [n=1] Number of elements to take from the beginning of the array.
11 | * @return {Object} Returns the updated object.
12 | * @example take({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 2) // => { nested: { prop: [1, 2] } }
13 | * @see {@link https://lodash.com/docs#take|lodash.take} for more information.
14 | * @since 1.0.0
15 | */
16 | const take = apply(_take, { arity: 1 })
17 |
18 | export { take }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/take.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { take } from 'array'
4 | describe('Take', () => {
5 | it('should take one element', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | ],
13 | },
14 | other: {},
15 | }, ['nested.prop'], (input, path) => {
16 | const output = take(input, path, 1)
17 | expect(output).toEqual({
18 | nested: { prop: [1] },
19 | other: {},
20 | })
21 | return output
22 | })
23 | })
24 | it('should replace deep undefined with array', () => {
25 | immutaTest(undefined, ['nested.prop'], (input, path) => {
26 | const output = take(input, path)
27 | expect(output).toEqual({ nested: { prop: [] } })
28 | return output
29 | })
30 | })
31 | })
32 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/takeRight.js:
--------------------------------------------------------------------------------
1 | import { takeRight as _takeRight } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Creates a slice of array with n
elements taken from the end.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {number} [n=1] Number of elements to take from the end of the array.
11 | * @return {Object} Returns the updated object.
12 | * @example takeRight({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 2) // => { nested: { prop: [3, 4] } }
13 | * @see {@link https://lodash.com/docs#takeRight|lodash.takeRight} for more information.
14 | * @since 1.0.0
15 | */
16 | const takeRight = apply(_takeRight, { arity: 1 })
17 |
18 | export { takeRight }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/takeRight.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { takeRight } from 'array'
4 | describe('TakeRight', () => {
5 | it('should take one element at the end of the array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | ],
13 | },
14 | other: {},
15 | }, ['nested.prop'], (input, path) => {
16 | const output = takeRight(input, path, 1)
17 | expect(output).toEqual({
18 | nested: { prop: [3] },
19 | other: {},
20 | })
21 | return output
22 | })
23 | })
24 | it('should replace deep undefined with array', () => {
25 | immutaTest(undefined, ['nested.prop'], (input, path) => {
26 | const output = takeRight(input, path)
27 | expect(output).toEqual({ nested: { prop: [] } })
28 | return output
29 | })
30 | })
31 | })
32 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/takeRightWhile.js:
--------------------------------------------------------------------------------
1 | import { takeRightWhile as _takeRightWhile } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Creates a slice of array with elements taken from the end.
6 | * Elements are taken until predicate returns falsey.
7 | * The predicate is invoked with three arguments: (value, index, array).
8 | * @function
9 | * @memberof array
10 | * @param {Object} object The object to modify.
11 | * @param {Array|string} path The path of the property to set.
12 | * @param {Function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
13 | * @return {Object} Returns the updated object.
14 | * @example takeRightWhile({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v > 3) // => { nested: { prop: [4] } }
15 | * @see {@link https://lodash.com/docs#takeRightWhile|lodash.takeRightWhile} for more information.
16 | * @since 1.0.0
17 | */
18 | const takeRightWhile = apply(_takeRightWhile, { arity: 1 })
19 |
20 | export { takeRightWhile }
21 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/takeRightWhile.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { takeRightWhile } from 'array'
4 | describe('TakeRightWhile', () => {
5 | it('should take one element at the end of the array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | ],
13 | },
14 | other: {},
15 | }, ['nested.prop'], (input, path) => {
16 | const output = takeRightWhile(input, path, v => v > 2)
17 | expect(output).toEqual({
18 | nested: { prop: [3] },
19 | other: {},
20 | })
21 | return output
22 | })
23 | })
24 | it('should replace deep undefined with array', () => {
25 | immutaTest(undefined, ['nested.prop'], (input, path) => {
26 | const output = takeRightWhile(input, path, () => true)
27 | expect(output).toEqual({ nested: { prop: [] } })
28 | return output
29 | })
30 | })
31 | })
32 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/takeWhile.js:
--------------------------------------------------------------------------------
1 | import { takeWhile as _takeWhile } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Creates a slice of array with elements taken from the beginning.
6 | * Elements are taken until predicate returns falsey.
7 | * The predicate is invoked with three arguments: (value, index, array).
8 | * @function
9 | * @memberof array
10 | * @param {Object} object The object to modify.
11 | * @param {Array|string} path The path of the property to set.
12 | * @param {Function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
13 | * @return {Object} Returns the updated object.
14 | * @example takeWhile({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v < 2) // => { nested: { prop: [1] } }
15 | * @see {@link https://lodash.com/docs#takeWhile|lodash.takeWhile} for more information.
16 | * @since 1.0.0
17 | */
18 | const takeWhile = apply(_takeWhile, { arity: 1 })
19 |
20 | export { takeWhile }
21 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/takeWhile.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { takeWhile } from 'array'
4 | describe('TakeWhile', () => {
5 | it('should take one element at the beginning of the array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 1,
13 | 2,
14 | 3,
15 | ],
16 | },
17 | other: {},
18 | }, ['nested.prop'], (input, path) => {
19 | const output = takeWhile(input, path, v => v < 2)
20 | expect(output).toEqual({
21 | nested: { prop: [1] },
22 | other: {},
23 | })
24 | return output
25 | })
26 | })
27 | it('should replace deep undefined with array', () => {
28 | immutaTest(undefined, ['nested.prop'], (input, path) => {
29 | const output = takeWhile(input, path, () => true)
30 | expect(output).toEqual({ nested: { prop: [] } })
31 | return output
32 | })
33 | })
34 | })
35 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/union.js:
--------------------------------------------------------------------------------
1 | import { union as _union } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces an array by an array of unique values, in order, from the former array and the given arrays.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} [arrays] The arrays to inspect.
11 | * @return {Object} Returns the updated object.
12 | * @example union({ nested: { prop: [1, 2] } }, 'nested.prop', [2, 3]) // => { nested: { prop: [1, 2, 3] } }
13 | * @see {@link https://lodash.com/docs#union|lodash.union} for more information.
14 | * @since 1.0.0
15 | */
16 | const union = apply(_union, { arity: 2 })
17 |
18 | export { union }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/union.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { union } from 'array'
4 | describe('Union', () => {
5 | it('should create a set of unique values', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | ],
12 | },
13 | other: {},
14 | }, ['nested.prop'], (input, path) => {
15 | const output = union(input, path, [
16 | 2,
17 | 3,
18 | ])
19 | expect(output).toEqual({
20 | nested: {
21 | prop: [
22 | 1,
23 | 2,
24 | 3,
25 | ],
26 | },
27 | other: {},
28 | })
29 | return output
30 | })
31 | })
32 | })
33 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/unionBy.js:
--------------------------------------------------------------------------------
1 | import { unionBy as _unionBy } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * This method is like {@link array.union} except that it accepts iteratee
to generate the criterion by which elements are compared.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} [arrays] The arrays to inspect.
11 | * @param {Function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The iteratee invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @example unionBy({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], 'x') // => { nested: { prop: [{ x: 1 }, { x: 2 }, { x: 3 }] } }
14 | * @see {@link https://lodash.com/docs#unionBy|lodash.unionBy} for more information.
15 | * @since 1.0.0
16 | */
17 | const unionBy = apply(_unionBy, { arity: 2 })
18 |
19 | export { unionBy }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/unionBy.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { unionBy } from 'array'
4 | describe('UnionBy', () => {
5 | it('should create a set of unique values', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{ x: 1 },
9 | { x: 2 },
10 | ],
11 | },
12 | other: {},
13 | }, ['nested.prop'], (input, path) => {
14 | const output = unionBy(input, path, [{ x: 2 },
15 | { x: 3 },
16 | ], 'x')
17 | expect(output).toEqual({
18 | nested: {
19 | prop: [{ x: 1 },
20 | { x: 2 },
21 | { x: 3 },
22 | ],
23 | },
24 | other: {},
25 | })
26 | return output
27 | })
28 | })
29 | })
30 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/unionWith.js:
--------------------------------------------------------------------------------
1 | import { unionWith as _unionWith } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * This method is like {@link array.union} except that it accepts comparator
to compare elements.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} [arrays] The arrays to inspect.
11 | * @param {Function} [comparator] The comparator invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @example unionWith({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], (a, b) => a.x === b.x) // => { nested: { prop: [{ x: 1 }, { x: 2 }, { x: 3 }] } }
14 | * @see {@link https://lodash.com/docs#unionWith|lodash.unionWith} for more information.
15 | * @since 1.0.0
16 | */
17 | const unionWith = apply(_unionWith, { arity: 2 })
18 |
19 | export { unionWith }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/unionWith.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { unionWith } from 'array'
4 | describe('UnionWith', () => {
5 | it('should create a set of unique values', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{ x: 1 },
9 | { x: 2 },
10 | ],
11 | },
12 | other: {},
13 | }, ['nested.prop'], (input, path) => {
14 | const output = unionWith(input, path, [{ x: 2 },
15 | { x: 3 },
16 | ], (a, b) => a.x === b.x)
17 | expect(output).toEqual({
18 | nested: {
19 | prop: [{ x: 1 },
20 | { x: 2 },
21 | { x: 3 },
22 | ],
23 | },
24 | other: {},
25 | })
26 | return output
27 | })
28 | })
29 | })
30 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/without.js:
--------------------------------------------------------------------------------
1 | import { pull } from './pull'
2 |
3 | /**
4 | * This method is an alias of {@link array.pull}.
5 | * @function
6 | * @memberof array
7 | * @since 1.0.0
8 | */
9 | const without = pull
10 |
11 | export { without }
12 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/without.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { without } from 'array'
4 | describe('Without', () => {
5 | it('should remove several matching elements', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 1,
13 | 2,
14 | 3,
15 | ],
16 | },
17 | other: {},
18 | }, ['nested.prop'], (input, path) => {
19 | const output = without(input, path, 1, 3)
20 | expect(output).toEqual({
21 | nested: {
22 | prop: [
23 | 2,
24 | 2,
25 | ],
26 | },
27 | other: {},
28 | })
29 | return output
30 | })
31 | })
32 | })
33 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/xor.js:
--------------------------------------------------------------------------------
1 | import { xor as _xor } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces an array by the symmetric difference of the former array and the given arrays.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} arrays The arrays to inspect.
11 | * @return {Object} Returns the updated object.
12 | * @example xor({ nested: { prop: [1, 2] } }, 'nested.prop', [2, 3]) // => { nested: { prop: [1, 3] } }
13 | * @see {@link https://lodash.com/docs#xor|lodash.xor} for more information.
14 | * @since 1.0.0
15 | */
16 | const xor = apply(_xor, { arity: 2 })
17 |
18 | export { xor }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/xor.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { xor } from 'array'
4 | describe('Xor', () => {
5 | const withOneAndTwo = {
6 | nested: {
7 | prop: [
8 | 1,
9 | 2,
10 | ],
11 | },
12 | other: {},
13 | }
14 | const oneAndThree = [
15 | 1,
16 | 3,
17 | ]
18 | const withOneAndThree = {
19 | nested: { prop: oneAndThree },
20 | other: {},
21 | }
22 | const twoAndThree = [
23 | 2,
24 | 3,
25 | ]
26 | const withTwoAndThree = {
27 | nested: { prop: twoAndThree },
28 | other: {},
29 | }
30 | it('should xor arrays', () => {
31 | immutaTest(withOneAndTwo, ['nested.prop'], (input, path) => {
32 | const output = xor(input, path, twoAndThree)
33 | expect(output).toEqual(withOneAndThree)
34 | return output
35 | })
36 | immutaTest(withOneAndTwo, ['nested.prop'], (input, path) => {
37 | const output = xor(input, path, oneAndThree)
38 | expect(output).toEqual(withTwoAndThree)
39 | return output
40 | })
41 | })
42 | it('should xor deep undefined to array', () => {
43 | immutaTest(undefined, ['nested.prop'], (input, path) => {
44 | const output = xor(input, path, oneAndThree)
45 | expect(output).toEqual({ nested: { prop: oneAndThree } })
46 | return output
47 | })
48 | })
49 | })
50 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/xorBy.js:
--------------------------------------------------------------------------------
1 | import { xorBy as _xorBy } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * This method is like {@link array.xor} except that it accepts iteratee
to generate the criterion by which elements are compared.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} arrays The arrays to inspect.
11 | * @param {Function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The iteratee invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @example xorBy({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], 'x') // => { nested: { prop: [{ x: 1 }, { x: 3 }] } }
14 | * @see {@link https://lodash.com/docs#xorBy|lodash.xorBy} for more information.
15 | * @since 1.0.0
16 | */
17 | const xorBy = apply(_xorBy, { arity: 2 })
18 |
19 | export { xorBy }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/xorBy.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { xorBy } from 'array'
4 | describe('XorBy', () => {
5 | it('should xor arrays', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{ x: 1 },
9 | { x: 2 },
10 | ],
11 | },
12 | other: {},
13 | }, ['nested.prop'], (input, path) => {
14 | const output = xorBy(input, path, [{ x: 2 },
15 | { x: 3 },
16 | ], 'x')
17 | expect(output).toEqual({
18 | nested: {
19 | prop: [{ x: 1 },
20 | { x: 3 },
21 | ],
22 | },
23 | other: {},
24 | })
25 | return output
26 | })
27 | })
28 | })
29 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/xorWith.js:
--------------------------------------------------------------------------------
1 | import { xorWith as _xorWith } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * This method is like {@link array.xor} except that it accepts comparator
to compare elements.
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} arrays The arrays to inspect.
11 | * @param {Function} [comparator] The comparator invoked per element.
12 | * @return {Object} Returns the updated object.
13 | * @example xorWith({ nested: { prop: [{ x: 1 }, { x: 2 }] } }, 'nested.prop', [{ x: 2 }, { x: 3 }], (a, b) => a.x === a.b) // => { nested: { prop: [{ x: 1 }, { x: 3 }] } }
14 | * @see {@link https://lodash.com/docs#xorWith|lodash.xorWith} for more information.
15 | * @since 1.0.0
16 | */
17 | const xorWith = apply(_xorWith, { arity: 2 })
18 |
19 | export { xorWith }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/array/xorWith.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { xorWith } from 'array'
4 | describe('XorWith', () => {
5 | it('should xor arrays', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{ x: 1 },
9 | { x: 2 },
10 | ],
11 | },
12 | other: {},
13 | }, ['nested.prop'], (input, path) => {
14 | const output = xorWith(input, path, [{ x: 2 },
15 | { x: 3 },
16 | ], (a, b) => a.x === b.x)
17 | expect(output).toEqual({
18 | nested: {
19 | prop: [{ x: 1 },
20 | { x: 3 },
21 | ],
22 | },
23 | other: {},
24 | })
25 | return output
26 | })
27 | })
28 | })
29 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/filter.js:
--------------------------------------------------------------------------------
1 | import { filter as _filter } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an array of elements predicate
returns truthy for.
6 | * @function
7 | * @memberof collection
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
11 | * @return {Object} Returns the updated object.
12 | * @see {@link https://lodash.com/docs#filter|lodash.filter} for more information.
13 | * @example filter({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v % 2) // => { nested: { prop: [1, 3] } }
14 | * @since 1.0.0
15 | */
16 | const filter = apply(_filter, { arity: 1 })
17 |
18 | export { filter }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/filter.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { filter } from 'collection'
3 | import { immutaTest } from 'test.utils'
4 | describe('Filter', () => {
5 | it('should filter an array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = filter(input, path, v => v % 2)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 1,
22 | 3,
23 | ],
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/index.js:
--------------------------------------------------------------------------------
1 | import { filter } from './filter'
2 | import { map } from './map'
3 | import { orderBy } from './orderBy'
4 | import { reject } from './reject'
5 | import { shuffle } from './shuffle'
6 | import { sortBy } from './sortBy'
7 |
8 | /**
9 | * Collection functions.
10 | * @namespace collection
11 | * @since 1.0.0
12 | */
13 | export {
14 | filter,
15 | map,
16 | orderBy,
17 | reject,
18 | shuffle,
19 | sortBy,
20 | }
21 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/map.js:
--------------------------------------------------------------------------------
1 | import { map as _map } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an array of values by running each element in the former collection thru iteratee.
6 | * The iteratee is invoked with three arguments: (value, index|key, collection).
7 | * @function
8 | * @memberof collection
9 | * @param {Object} object The object to modify.
10 | * @param {Array|string} path The path of the property to set.
11 | * @param {function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
12 | * @return {Object} Returns the updated object.
13 | * @see {@link https://lodash.com/docs#map|lodash.map} for more information.
14 | * @example map({ nested: { prop: [1, 2, 3] } }, 'nested.prop', v => v * 2) // => { nested: { prop: [2, 4, 6] } }
15 | * @since 1.0.0
16 | */
17 | const map = apply(_map, { arity: 1 })
18 |
19 | export { map }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/map.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { map } from 'collection'
4 | describe('Map', () => {
5 | it('should map an array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | ],
12 | },
13 | other: {},
14 | }, ['nested.prop'], (input, path) => {
15 | const output = map(input, path, v => v * 2)
16 | expect(output).toEqual({
17 | nested: {
18 | prop: [
19 | 2,
20 | 4,
21 | ],
22 | },
23 | other: {},
24 | })
25 | return output
26 | })
27 | })
28 | it('should replace deep undefined with empty array', () => {
29 | immutaTest(undefined, ['nested.prop'], (input, path) => {
30 | const output = map(input, path)
31 | expect(output).toEqual({ nested: { prop: [] } })
32 | return output
33 | })
34 | })
35 | })
36 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/orderBy.js:
--------------------------------------------------------------------------------
1 | import { orderBy as _orderBy } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an array of sorted by iteratees
in specified orders
.
6 | * @function
7 | * @memberof collection
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {Array[]|Function[]|Object[]|string[]} [iteratees=[{@link https://lodash.com/docs#identity|lodash.identity}]] The iteratees to sort by.
11 | * @param {string[]} [orders] The sort orders of iteratees
.
12 | * @return {Object} Returns the updated object.
13 | * @see {@link https://lodash.com/docs#orderBy|lodash.orderBy} for more information.
14 | * @example
15 | * orderBy({ nested: { prop: [{ name: 'Yvo', age: 2 }, { name: 'Nico', age: 666 }, { name: 'Nico', age: 30 }] } }, 'nested.prop', ['name', 'age'], ['asc', 'desc'])
16 | * // => { nested: { prop: [{ name: 'Nico', age: 666 }, { name: 'Nico', age: 30 }, { name: 'Yvo', age: 2 }] } }
17 | * @since 1.0.0
18 | */
19 | const orderBy = apply(_orderBy, { arity: 2 })
20 |
21 | export { orderBy }
22 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/orderBy.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { orderBy } from 'collection'
4 | describe('OrderBy', () => {
5 | it('should sort array by name asc and age desc', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{
9 | name: 'Yvo',
10 | age: 2,
11 | },
12 | {
13 | name: 'Nico',
14 | age: 666,
15 | },
16 | {
17 | name: 'Nico',
18 | age: 30,
19 | },
20 | ],
21 | },
22 | other: {},
23 | }, ['nested.prop'], (input, path) => {
24 | const output = orderBy(input, path, [
25 | 'name',
26 | 'age',
27 | ], [
28 | 'asc',
29 | 'desc',
30 | ])
31 | expect(output).toEqual({
32 | nested: {
33 | prop: [{
34 | name: 'Nico',
35 | age: 666,
36 | },
37 | {
38 | name: 'Nico',
39 | age: 30,
40 | },
41 | {
42 | name: 'Yvo',
43 | age: 2,
44 | },
45 | ],
46 | },
47 | other: {},
48 | })
49 | return output
50 | })
51 | })
52 | })
53 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/reject.js:
--------------------------------------------------------------------------------
1 | import { reject as _reject } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an array of elements predicate
returns falsy for.
6 | * @function
7 | * @memberof collection
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
11 | * @return {Object} Returns the updated object.
12 | * @see {@link https://lodash.com/docs#reject|lodash.reject} for more information.
13 | * @example reject({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v % 2) // => { nested: { prop: [2, 4] } }
14 | * @since 1.0.0
15 | */
16 | const reject = apply(_reject, { arity: 1 })
17 |
18 | export { reject }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/reject.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { reject } from 'collection'
4 | describe('Reject', () => {
5 | it('should reject elements of an array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = reject(input, path, v => v % 2)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 2,
22 | 4,
23 | ],
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/shuffle.js:
--------------------------------------------------------------------------------
1 | import { shuffle as _shuffle } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an array of shuffled elements.
6 | * @function
7 | * @memberof collection
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @return {Object} Returns the updated object.
11 | * @see {@link https://lodash.com/docs#shuffle|lodash.shuffle} for more information.
12 | * @example shuffle({ nested: { prop: [1, 2, 3, 4, 5, 6, 7, 8, 9] } }, 'nested.prop') // => { nested: { prop: [7, 3, 9, 1, 4, 5, 6, 8, 2] } }
13 | * @since 1.0.0
14 | */
15 | const shuffle = apply(
16 | _shuffle,
17 | {
18 | arity: 1,
19 | fixedArity: true,
20 | },
21 | )
22 |
23 | export { shuffle }
24 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/shuffle.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { shuffle } from 'collection'
4 | describe('Shuffle', () => {
5 | it('should shuffle an array', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | 5,
14 | 6,
15 | 7,
16 | 8,
17 | 9,
18 | ],
19 | },
20 | other: {},
21 | }, ['nested.prop'], (input, path) => {
22 | const output = shuffle(input, path)
23 | expect(output.nested.prop).toEqual(expect.arrayContaining([
24 | 1,
25 | 2,
26 | 3,
27 | 4,
28 | 5,
29 | 6,
30 | 7,
31 | 8,
32 | 9,
33 | ]))
34 | expect(output.nested.prop).not.toEqual([
35 | 1,
36 | 2,
37 | 3,
38 | 4,
39 | 5,
40 | 6,
41 | 7,
42 | 8,
43 | 9,
44 | ])
45 | return output
46 | })
47 | })
48 | })
49 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/sortBy.js:
--------------------------------------------------------------------------------
1 | import { sortBy as _sortBy } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an array of sorted by iteratees
.
6 | * @function
7 | * @memberof collection
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {Array[]|Function[]|Object[]|string[]} [iteratees=[{@link https://lodash.com/docs#identity|lodash.identity}]] The iteratees to sort by.
11 | * @return {Object} Returns the updated object.
12 | * @see {@link https://lodash.com/docs#sortBy|lodash.sortBy} for more information.
13 | * @example
14 | * sortBy({ nested: { prop: [{ name: 'Yvo', age: 2 }, { name: 'Nico', age: 666 }, { name: 'Nico', age: 30 }] } }, 'nested.prop', ['name', 'age'])
15 | * // => { nested: { prop: [{ name: 'Nico', age: 30 }, { name: 'Nico', age: 666 }, { name: 'Yvo', age: 2 }] } }
16 | * @since 1.0.0
17 | */
18 | const sortBy = apply(_sortBy, { arity: 1 })
19 |
20 | export { sortBy }
21 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/collection/sortBy.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { sortBy } from 'collection'
4 | describe('SortBy', () => {
5 | it('should sort array by name and age', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [{
9 | name: 'Yvo',
10 | age: 2,
11 | },
12 | {
13 | name: 'Nico',
14 | age: 666,
15 | },
16 | {
17 | name: 'Nico',
18 | age: 30,
19 | },
20 | ],
21 | },
22 | other: {},
23 | }, ['nested.prop'], (input, path) => {
24 | const output = sortBy(input, path, [
25 | 'name',
26 | 'age',
27 | ])
28 | expect(output).toEqual({
29 | nested: {
30 | prop: [{
31 | name: 'Nico',
32 | age: 30,
33 | },
34 | {
35 | name: 'Nico',
36 | age: 666,
37 | },
38 | {
39 | name: 'Yvo',
40 | age: 2,
41 | },
42 | ],
43 | },
44 | other: {},
45 | })
46 | return output
47 | })
48 | })
49 | })
50 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/index.js:
--------------------------------------------------------------------------------
1 | export * from './array'
2 | export * from './collection'
3 | export * from './object'
4 | export * from './string'
5 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/defaults.js:
--------------------------------------------------------------------------------
1 | import { defaults as _defaults } from 'lodash/fp'
2 | import { applyLodashFp } from 'util/applyLodashFp'
3 |
4 | /**
5 | * Replaces by an object assigning own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to undefined
.6 | * Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored. 7 | * @function 8 | * @memberof object 9 | * @param {Object} object The object to modify. 10 | * @param {Array|string} path The path of the property to set. 11 | * @param {...Object} [sources] The source objects. 12 | * @return {Object} Returns the updated object. 13 | * @example defaults({ nested: { a: 1, b: 2 } }, 'nested', { b: 3, c: 4 }) // => { nested: { a:1, b: 2, c: 4 } } 14 | * @see {@link https://lodash.com/docs#defaults|lodash.defaults} for more information. 15 | * @since 1.0.0 16 | */ 17 | const defaults = applyLodashFp(_defaults, { arity: 2 }) 18 | 19 | export { defaults } 20 | -------------------------------------------------------------------------------- /packages/immutadot-lodash/src/object/defaults.spec.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | import { defaults } from 'object' 3 | import { immutaTest } from 'test.utils' 4 | describe('Defaults', () => { 5 | it('should assign default properties objects', () => { 6 | immutaTest({ 7 | nested: { 8 | prop: { 9 | a: 1, 10 | b: 2, 11 | }, 12 | }, 13 | other: {}, 14 | }, ['nested.prop'], (input, path) => { 15 | const output = defaults(input, path, { 16 | b: 3, 17 | c: 4, 18 | }) 19 | expect(output).toEqual({ 20 | nested: { 21 | prop: { 22 | a: 1, 23 | b: 2, 24 | c: 4, 25 | }, 26 | }, 27 | other: {}, 28 | }) 29 | return output 30 | }) 31 | }) 32 | }) 33 | -------------------------------------------------------------------------------- /packages/immutadot-lodash/src/object/index.js: -------------------------------------------------------------------------------- 1 | import { defaults } from './defaults' 2 | import { mapKeys } from './mapKeys' 3 | import { mapValues } from './mapValues' 4 | import { merge } from './merge' 5 | import { omit } from './omit' 6 | import { omitBy } from './omitBy' 7 | import { pick } from './pick' 8 | import { pickBy } from './pickBy' 9 | 10 | /** 11 | * Object functions. 12 | * @namespace object 13 | * @since 1.0.0 14 | */ 15 | export { 16 | defaults, 17 | mapKeys, 18 | mapValues, 19 | merge, 20 | omit, 21 | omitBy, 22 | pick, 23 | pickBy, 24 | } 25 | -------------------------------------------------------------------------------- /packages/immutadot-lodash/src/object/mapKeys.js: -------------------------------------------------------------------------------- 1 | import { mapKeys as _mapKeys } from 'lodash' 2 | import { apply } from 'immutadot/core' 3 | 4 | /** 5 | * Replaces by an object with the same values as the former object and values generated by running each own enumerable string keyed property of the former object thru
iteratee
.
6 | * The iteratee is invoked with three arguments: (value, key, object).
7 | * @function
8 | * @memberof object
9 | * @param {Object} object The object to modify.
10 | * @param {Array|string} path The path of the property to set.
11 | * @param {function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
12 | * @return {Object} Returns the updated object.
13 | * @example mapKeys({ nested: { a: 1, b: 2, c: 3 } }, 'nested', (v, k) => '_' + k) // => { nested: { _a: 1, _b: 2, _c: 3 } }
14 | * @see {@link https://lodash.com/docs#mapKeys|lodash.mapKeys} for more information.
15 | * @since 1.0.0
16 | */
17 | const mapKeys = apply(_mapKeys, { arity: 1 })
18 |
19 | export { mapKeys }
20 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/mapKeys.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { mapKeys } from 'object'
4 | describe('MapKeys', () => {
5 | it('should replace the keys of object', () => {
6 | immutaTest({
7 | nested: {
8 | prop: {
9 | a: 1,
10 | b: 2,
11 | c: 3,
12 | },
13 | },
14 | other: {},
15 | }, ['nested.prop'], (input, path) => {
16 | const output = mapKeys(input, path, (_, k) => `_${k}`)
17 | expect(output).toEqual({
18 | nested: {
19 | prop: {
20 | _a: 1,
21 | _b: 2,
22 | _c: 3,
23 | },
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/mapValues.js:
--------------------------------------------------------------------------------
1 | import { mapValues as _mapValues } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an object with the same keys as the former object and values generated by running each own enumerable string keyed property of object thru iteratee
.
6 | * The iteratee is invoked with three arguments: (value, key, object).
7 | * @function
8 | * @memberof object
9 | * @param {Object} object The object to modify.
10 | * @param {Array|string} path The path of the property to set.
11 | * @param {function} [iteratee={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
12 | * @return {Object} Returns the updated object.
13 | * @example mapValues({ nested: { a: 1, b: 2, c: 3 } }, 'nested', v => v * v) // => { nested: { a: 1, b: 4, c: 9 } }
14 | * @example mapValues({ nested: { a: { age: 40, name: 'John' }, b: { age: 30, name: 'Alice' } } }, 'nested', 'age') // => { nested: { a: 40, b: 30 } }
15 | * @see {@link https://lodash.com/docs#mapValues|lodash.mapValues} for more information.
16 | * @since 1.0.0
17 | */
18 | const mapValues = apply(_mapValues, { arity: 1 })
19 |
20 | export { mapValues }
21 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/mapValues.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { mapValues } from 'object'
4 | describe('MapValues', () => {
5 | it('should map over each values of an object', () => {
6 | immutaTest({
7 | nested: {
8 | prop: {
9 | a: 1,
10 | b: 2,
11 | c: 3,
12 | },
13 | },
14 | other: {},
15 | }, ['nested.prop'], (input, path) => {
16 | const output = mapValues(input, path, v => v * v)
17 | expect(output).toEqual({
18 | nested: {
19 | prop: {
20 | a: 1,
21 | b: 4,
22 | c: 9,
23 | },
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/merge.js:
--------------------------------------------------------------------------------
1 | import { merge as _merge } from 'lodash/fp'
2 | import { applyLodashFp } from 'util/applyLodashFp'
3 |
4 | /**
5 | * Replaces by an object deeply merging own enumerable string keyed properties of source objects to the former object.6 | * Source objects are applied from left to right. Subsequent sources overwrite properties of previous sources. 7 | * @function 8 | * @memberof object 9 | * @param {Object} object The object to modify. 10 | * @param {Array|string} path The path of the property to set. 11 | * @param {...Object} [sources] The source objects. 12 | * @return {Object} Returns the updated object. 13 | * @example merge({ nested: { prop: { a: 1 } } }, 'nested', { prop: { a: 2, b: 3 } }) // => { nested: { prop: { a: 2, b: 3 } } } 14 | * @see {@link https://lodash.com/docs#merge|lodash.merge} for more information. 15 | * @since 1.0.0 16 | */ 17 | const merge = applyLodashFp(_merge, { arity: 2 }) 18 | 19 | export { merge } 20 | -------------------------------------------------------------------------------- /packages/immutadot-lodash/src/object/merge.spec.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | import { immutaTest } from 'test.utils' 3 | import { merge } from 'object' 4 | describe('Merge', () => { 5 | it('should merge objects', () => { 6 | immutaTest({ 7 | nested: { prop: { a: 1 } }, 8 | other: {}, 9 | }, ['nested'], (input, path) => { 10 | const output = merge(input, path, { 11 | prop: { 12 | a: 2, 13 | b: 3, 14 | }, 15 | }) 16 | expect(output).toEqual({ 17 | nested: { 18 | prop: { 19 | a: 2, 20 | b: 3, 21 | }, 22 | }, 23 | other: {}, 24 | }) 25 | return output 26 | }) 27 | }) 28 | }) 29 | -------------------------------------------------------------------------------- /packages/immutadot-lodash/src/object/omit.js: -------------------------------------------------------------------------------- 1 | import { omit as _omit } from 'lodash' 2 | import { apply } from 'immutadot/core' 3 | 4 | /** 5 | * Replaces by an object omitting specified properties. 6 | * @function 7 | * @memberof object 8 | * @param {Object} object The object to modify. 9 | * @param {Array|string} path The path of the property to set. 10 | * @param {...(string|string[])} [paths] The property paths to omit. 11 | * @return {Object} Returns the updated object. 12 | * @example omit({ nested: { a: 1, b: 2, c: 3 } }, 'nested', 'b') // => { nested: { a:1, c: 3 } } 13 | * @see {@link https://lodash.com/docs#omit|lodash.omit} for more information. 14 | * @since 1.0.0 15 | */ 16 | const omit = apply(_omit, { arity: 1 }) 17 | 18 | export { omit } 19 | -------------------------------------------------------------------------------- /packages/immutadot-lodash/src/object/omit.spec.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | import { immutaTest } from 'test.utils' 3 | import { omit } from 'object' 4 | describe('Omit', () => { 5 | it('should omit properties of object', () => { 6 | immutaTest({ 7 | nested: { 8 | prop: { 9 | a: 1, 10 | b: 2, 11 | c: 3, 12 | }, 13 | }, 14 | other: {}, 15 | }, ['nested.prop'], (input, path) => { 16 | const output = omit(input, path, 'b') 17 | expect(output).toEqual({ 18 | nested: { 19 | prop: { 20 | a: 1, 21 | c: 3, 22 | }, 23 | }, 24 | other: {}, 25 | }) 26 | return output 27 | }) 28 | }) 29 | }) 30 | -------------------------------------------------------------------------------- /packages/immutadot-lodash/src/object/omitBy.js: -------------------------------------------------------------------------------- 1 | import { omitBy as _omitBy } from 'lodash' 2 | import { apply } from 'immutadot/core' 3 | 4 | /** 5 | * Replaces by an object omitting properties that
predicate
doesn't return truthy for.
6 | * @function
7 | * @memberof object
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per property.
11 | * @return {Object} Returns the updated object.
12 | * @example omitBy({ nested: { a: 1, b: 2, c: 3 } }, 'nested', v => v === 2) // => { nested: { a:1, c: 3 } }
13 | * @see {@link https://lodash.com/docs#omitBy|lodash.omitBy} for more information.
14 | * @since 1.0.0
15 | */
16 | const omitBy = apply(_omitBy, { arity: 1 })
17 |
18 | export { omitBy }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/omitBy.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { omitBy } from 'object'
4 | describe('OmitBy', () => {
5 | it('should omit properties matching predicate', () => {
6 | immutaTest({
7 | nested: {
8 | prop: {
9 | a: 1,
10 | b: 2,
11 | c: 3,
12 | },
13 | },
14 | other: {},
15 | }, ['nested.prop'], (input, path) => {
16 | const output = omitBy(input, path, v => v === 2)
17 | expect(output).toEqual({
18 | nested: {
19 | prop: {
20 | a: 1,
21 | c: 3,
22 | },
23 | },
24 | other: {},
25 | })
26 | return output
27 | })
28 | })
29 | })
30 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/pick.js:
--------------------------------------------------------------------------------
1 | import { pick as _pick } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an object picking specified properties.
6 | * @function
7 | * @memberof object
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...(string|string[])} [paths] The property paths to pick.
11 | * @return {Object} Returns the updated object.
12 | * @example pick({ nested: { a: 1, b: 2, c: 3 } }, 'nested', 'b') // => { nested: { b: 2 } }
13 | * @see {@link https://lodash.com/docs#pick|lodash.pick} for more information.
14 | * @since 1.0.0
15 | */
16 | const pick = apply(_pick, { arity: 1 })
17 |
18 | export { pick }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/pick.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { pick } from 'object'
4 | describe('Pick', () => {
5 | it('should pick properties of object', () => {
6 | immutaTest({
7 | nested: {
8 | prop: {
9 | a: 1,
10 | b: 2,
11 | c: 3,
12 | },
13 | },
14 | other: {},
15 | }, ['nested.prop'], (input, path) => {
16 | const output = pick(input, path, 'b')
17 | expect(output).toEqual({
18 | nested: { prop: { b: 2 } },
19 | other: {},
20 | })
21 | return output
22 | })
23 | })
24 | })
25 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/pickBy.js:
--------------------------------------------------------------------------------
1 | import { pickBy as _pickBy } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Replaces by an object picking properties that predicate
returns truthy for.
6 | * @function
7 | * @memberof object
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {function} [predicate={@link https://lodash.com/docs#identity|lodash.identity}] The function invoked per iteration.
11 | * @return {Object} Returns the updated object.
12 | * @example pickBy({ nested: { a: 1, b: 2, c: 3, d: 4 } }, 'nested', v => v < 3) // => { nested: { a: 1, b: 2 } }
13 | * @see {@link https://lodash.com/docs#pickBy|lodash.pickBy} for more information.
14 | * @since 1.0.0
15 | */
16 | const pickBy = apply(_pickBy, { arity: 1 })
17 |
18 | export { pickBy }
19 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/object/pickBy.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { pickBy } from 'object'
4 | describe('PickBy', () => {
5 | it('should pick all matching props by value', () => {
6 | immutaTest({
7 | nested: {
8 | prop: {
9 | a: 1,
10 | b: 2,
11 | c: 3,
12 | d: 4,
13 | },
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = pickBy(input, path, v => v < 3)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: {
21 | a: 1,
22 | b: 2,
23 | },
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/string/capitalize.js:
--------------------------------------------------------------------------------
1 | import { capitalize as _capitalize } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Converts the first character of string to upper case and the remaining to lower case.
6 | * @function
7 | * @memberof string
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @return {Object} Returns the updated object.
11 | * @example capitalize({ nested: { a: "a string" } }, 'nested.a') // => { nested: { a: "A string" } }
12 | * @see {@link https://lodash.com/docs#capitalize|lodash.capitalize} for more information.
13 | * @since 1.0.0
14 | */
15 | const capitalize = apply(
16 | _capitalize,
17 | {
18 | arity: 1,
19 | fixedArity: true,
20 | },
21 | )
22 |
23 | export { capitalize }
24 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/string/capitalize.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { capitalize } from 'string'
3 | import { immutaTest } from 'test.utils'
4 | describe('Capitalize', () => {
5 | it('should convert the first character of string to upper case and the remaining to lower case', () => {
6 | immutaTest({ nested: { prop: 'a string' } }, ['nested.prop'], (input, path) => {
7 | const output = capitalize(input, path)
8 | expect(output).toEqual({ nested: { prop: 'A string' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/string/index.js:
--------------------------------------------------------------------------------
1 | import { capitalize } from './capitalize'
2 | import { toLower } from './toLower'
3 | import { toUpper } from './toUpper'
4 |
5 | /**
6 | * String functions.
7 | * @namespace string
8 | * @since 1.0.0
9 | */
10 | export {
11 | capitalize,
12 | toLower,
13 | toUpper,
14 | }
15 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/string/toLower.js:
--------------------------------------------------------------------------------
1 | import { toLower as _toLower } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Converts string, as a whole, to lower case just like String#toLowerCase.
6 | * @function
7 | * @memberof string
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @return {Object} Returns the updated object.
11 | * @example toLower({ nested: { a: "A STRING" } }, 'nested.a') // => { nested: { a: "a string" } }
12 | * @see {@link https://lodash.com/docs#toLower|lodash.toLower} for more information.
13 | * @since 1.0.0
14 | */
15 | const toLower = apply(
16 | _toLower,
17 | {
18 | arity: 1,
19 | fixedArity: true,
20 | },
21 | )
22 |
23 | export { toLower }
24 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/string/toLower.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { toLower } from 'string'
4 | describe('toLower', () => {
5 | it('should convert string, as a whole, to lower case just like String#toLowerCase', () => {
6 | immutaTest({ nested: { prop: 'A STRING' } }, ['nested.prop'], (input, path) => {
7 | const output = toLower(input, path)
8 | expect(output).toEqual({ nested: { prop: 'a string' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/string/toUpper.js:
--------------------------------------------------------------------------------
1 | import { toUpper as _toUpper } from 'lodash'
2 | import { apply } from 'immutadot/core'
3 |
4 | /**
5 | * Converts string, as a whole, to upper case just like String#toUpperCase.
6 | * @function
7 | * @memberof string
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @return {Object} Returns the updated object.
11 | * @example toUpper({ nested: { a: "a string" } }, 'nested.a') // => { nested: { a: "A STRING" } }
12 | * @see {@link https://lodash.com/docs#toUpper|lodash.toUpper} for more information.
13 | * @since 1.0.0
14 | */
15 | const toUpper = apply(
16 | _toUpper,
17 | {
18 | arity: 1,
19 | fixedArity: true,
20 | },
21 | )
22 |
23 | export { toUpper }
24 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/string/toUpper.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { toUpper } from 'string'
4 | describe('toUpper', () => {
5 | it('should convert string, as a whole, to upper case just like String#toUpperCase', () => {
6 | immutaTest({ nested: { prop: 'a string' } }, ['nested.prop'], (input, path) => {
7 | const output = toUpper(input, path)
8 | expect(output).toEqual({ nested: { prop: 'A STRING' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/util/applyLodashFp.js:
--------------------------------------------------------------------------------
1 | import { apply } from 'immutadot/core'
2 | import { lodashFpConvert } from './lodashFpConvert'
3 |
4 | /**
5 | * Converts and wraps a lodash/fp function.
6 | * @memberof util
7 | * @param {function} fn The lodash/fp function.
8 | * @return {function} Returns the wrapped function.
9 | * @see {@link util.convert|convert} for more information.
10 | * @since 0.2.0
11 | * @private
12 | */
13 | function applyLodashFp(fn, { arity = fn.length, fixedArity = false } = {}) {
14 | return apply(
15 | lodashFpConvert(fn),
16 | {
17 | arity,
18 | fixedArity,
19 | },
20 | )
21 | }
22 |
23 | export { applyLodashFp }
24 |
--------------------------------------------------------------------------------
/packages/immutadot-lodash/src/util/lodashFpConvert.js:
--------------------------------------------------------------------------------
1 | const lodashFpConvertOptions = {
2 | curry: false,
3 | fixed: false,
4 | rearg: false,
5 | }
6 |
7 | /**
8 | * Converts a lodash/fp function with options { curry: false, fixed: false, rearg: false }
.
9 | * @memberof util
10 | * @param {function} fn The lodash/fp function.
11 | * @return {function} Returns the converted lodash/fp function.
12 | * @since 0.2.0
13 | * @private
14 | */
15 | const lodashFpConvert = fn => fn.convert(lodashFpConvertOptions)
16 |
17 | export { lodashFpConvert }
18 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/.gitignore:
--------------------------------------------------------------------------------
1 | /dist/**/*.*
2 | !/index.js
3 | !/config/**/*
--------------------------------------------------------------------------------
/packages/immutadot-parser/.npmignore:
--------------------------------------------------------------------------------
1 | /config/
2 | /coverage/
3 | /src/
4 | /tsconfig.json
5 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/config/jest.js:
--------------------------------------------------------------------------------
1 | const rootDir = process.cwd()
2 |
3 | module.exports = {
4 | globals: { 'ts-jest': { tsConfig: 'tsconfig.json' } },
5 | preset: 'ts-jest',
6 | rootDir,
7 | testEnvironment: 'node',
8 | }
9 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/index.js:
--------------------------------------------------------------------------------
1 | require = require('esm')(module)
2 | module.exports = require('./dist/index.js')
3 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "immutadot-parser",
3 | "version": "2.0.0-alpha.1",
4 | "description": "@immutadot/parser is the path parser of immutadot.",
5 | "keywords": [
6 | "immutable",
7 | "dot-notation",
8 | "nested-structures",
9 | "redux"
10 | ],
11 | "main": "index.js",
12 | "module": "dist/index.js",
13 | "license": "MIT",
14 | "homepage": "https://immutadot.zenika.com",
15 | "bugs": "https://github.com/zenika-open-source/immutadot/issues",
16 | "repository": "github:zenika-open-source/immutadot",
17 | "author": "Nicolas Lepage (https://github.com/nlepage)",
18 | "contributors": [
19 | "Yvonnick FRIN (https://github.com/frinyvonnick)",
20 | "Hugo WOOD (https://github.com/hgwood)"
21 | ],
22 | "dependencies": {
23 | "esm": "~3.1.2"
24 | },
25 | "devDependencies": {
26 | "@types/jest": "~23.3.12",
27 | "jest": "~23.6.0",
28 | "jsdoc": "~3.6.2",
29 | "lerna": "~3.14.1",
30 | "ts-jest": "~23.10.5",
31 | "tslint": "~5.17.0",
32 | "typescript": "~3.5.1"
33 | },
34 | "scripts": {
35 | "prebuild": "yarn clean",
36 | "build": "tsc -b tsconfig.json",
37 | "clean": "rm -rf dist/",
38 | "lint": "tslint src/**/*",
39 | "test": "jest -c config/jest.js",
40 | "test:coverage": "jest -c config/jest.js --maxWorkers=2 --coverage",
41 | "docs": "jsdoc -c ../../config/jsdoc.json",
42 | "docs:private": "jsdoc -c ../../config/jsdoc.json -p"
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/src/index.ts:
--------------------------------------------------------------------------------
1 | export * from "./toPath"
2 | export { NavType } from "./path"
3 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/src/maybe.ts:
--------------------------------------------------------------------------------
1 | export type Maybearg
is a valid slice index once converted to a number.
18 | *
19 | * @param arg The value to test
20 | * @returns True if arg
is a valid slice index once converted to a number, false otherwise.
21 | *
22 | * @remarks
23 | * Since 1.0.0
24 | */
25 | export const isValidString = (arg: any) => isValid(arg ? Number(arg) : undefined)
26 |
27 | /**
28 | * Converts str
to a slice index.
29 | *
30 | * @param str The string to convert
31 | * @param defaultValue The default value if str
is empty
32 | * @returns undefined
if str
is empty, otherwise an int (may be NaN)
33 | *
34 | * @remarks
35 | * Since 1.0.0
36 | */
37 | export const fromString = (str: string): SliceBound =>
38 | str === undefined || str === "" ? undefined : Number(str)
39 |
40 | export const SliceBound = {
41 | fromString,
42 | isValid,
43 | isValidString,
44 | }
45 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/src/sliceStep.ts:
--------------------------------------------------------------------------------
1 | import { SliceBound } from "./sliceBound"
2 |
3 | export type SliceStep = SliceBound
4 |
5 | export const isValid = (arg: any): arg is SliceStep => SliceBound.isValid(arg) && arg !== 0
6 |
7 | export const isValidString = (arg: any) => isValid(arg ? Number(arg) : undefined)
8 |
9 | export const fromString = (str: string): SliceStep => SliceBound.fromString(str)
10 |
11 | export const SliceStep = {
12 | fromString,
13 | isValid,
14 | isValidString,
15 | }
16 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/src/toPath.ts:
--------------------------------------------------------------------------------
1 | import { Path } from "./path"
2 | import { PathParser } from "./pathParser"
3 | import { isNil, toString } from "./utils"
4 |
5 | const MAX_CACHE_SIZE = 1000
6 | const cache = new Map10 | * The cache has a maximum size of 1000, when overflowing the cache is cleared. 11 | * 12 | * @param {string} str The string to convert 13 | * @returns {Array
31 | * If
arg
is not a string, its string representation will be parsed.
32 | *
33 | * @param {*} arg The value to convert
34 | * @returns {Path} The path represented as an array of keys
35 | *
36 | * @remarks
37 | * Since 1.0.0
38 | */
39 | const toPath = (arg: unknown): Path => {
40 | if (isNil(arg)) { return [] }
41 |
42 | return memoizedStringToPath(toString(arg))
43 | }
44 |
45 | export { toPath }
46 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/src/utils.spec.ts:
--------------------------------------------------------------------------------
1 | import {
2 | isNil,
3 | toString,
4 | } from "./utils"
5 |
6 | describe("Utils", () => {
7 | describe("util.isNil", () => {
8 | it("should return true for undefined and null", () => {
9 | expect(isNil(undefined)).toBe(true)
10 | expect(isNil(null)).toBe(true)
11 | })
12 |
13 | it("should return false for any other value than undefined and null", () => {
14 | expect(isNil(true)).toBe(false)
15 | expect(isNil({})).toBe(false)
16 | expect(isNil([])).toBe(false)
17 | expect(isNil("")).toBe(false)
18 | expect(isNil(.6)).toBe(false)
19 | expect(isNil("null")).toBe(false)
20 | expect(isNil("undefined")).toBe(false)
21 | })
22 | })
23 |
24 | describe("util.toString", () => {
25 | it("should return string representation", () => {
26 | expect(toString(undefined)).toBe("undefined")
27 | expect(toString(null)).toBe("null")
28 | expect(toString("🍺")).toBe("🍺")
29 | expect(toString(666)).toBe("666")
30 | })
31 | })
32 | })
33 |
--------------------------------------------------------------------------------
/packages/immutadot-parser/src/utils.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Tests whether `arg` is a `undefined` or `null`.
3 | *
4 | * @remarks
5 | * Since 1.0.0
6 | *
7 | * @param arg The value to test
8 | * @returns `true` if `arg` is `undefined` or `null`, `false` otherwise
9 | */
10 | export const isNil = (arg: unknown): arg is null | undefined => arg === undefined || arg === null
11 |
12 | /**
13 | * Converts `arg` to a string using string interpolation.
14 | *
15 | * @remarks
16 | * Since 1.0.0
17 | *
18 | * @param arg The value to convert
19 | * @returns The string representation of `arg`
20 | */
21 | export const toString = (arg: unknown) => typeof arg === "string" ? arg : `${arg}`
22 |
23 | /**
24 | * This is an alias for {@link https://mdn.io/Number.isInteger | `Number.isInteger`}.
25 | *
26 | * @remarks
27 | * Since 1.0.0
28 | */
29 | export const isIndex = (arg: unknown): arg is number =>
30 | // Number.isInteger actually accept anything, TypeScript lib is wrong here
31 | // so it is safe to assert that arg is a number
32 | Number.isInteger(arg as number)
33 |
34 | /**
35 | * Strip slashes preceding occurences of quote
from str
36 | * Possible quotes are
"
and '
.
37 | *
38 | * @param str The string
39 | * @param quote The quote to unescape
40 | * @returns The unescaped string
41 | *
42 | * @remarks
43 | * Since 1.0.0
44 | */
45 | export const unescapeQuotes = (str: string, quote: string) => str.replace(new RegExp(`\\\\${quote}`, "g"), quote)
46 |
--------------------------------------------------------------------------------
/packages/immutadot/.npmignore:
--------------------------------------------------------------------------------
1 | /coverage/
2 | /src/
3 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/applyArrayMethod.js:
--------------------------------------------------------------------------------
1 | import { apply } from 'core/apply'
2 | import { isNil } from 'util/lang'
3 |
4 | const toArray = array => {
5 | if (isNil(array)) return []
6 | if (Array.isArray(array)) return array
7 | return [array]
8 | }
9 |
10 | const toArrayCopy = array => {
11 | if (isNil(array)) return []
12 | if (Array.isArray(array)) return [...array]
13 | return [array]
14 | }
15 |
16 | const applyMethodReturnResult = (method, thisArg, args) => method.apply(thisArg, args)
17 |
18 | const applyMethodReturnThis = (method, thisArg, args) => {
19 | method.apply(thisArg, args)
20 | return thisArg
21 | }
22 |
23 | const applyArrayMethod = (name, { method = Array.prototype[name], arity = method && method.length, fixedArity = false, mutating = true } = {}) => {
24 | if (!method) {
25 | const error = `immutadot: Array.prototype.${name} is not available`
26 | // eslint-disable-next-line no-console
27 | console.warn(error)
28 | return () => { throw TypeError(error) }
29 | }
30 |
31 | const getArray = mutating ? toArrayCopy : toArray
32 | const applyMethod = mutating ? applyMethodReturnThis : applyMethodReturnResult
33 | return apply(
34 | (value, ...args) => applyMethod(method, getArray(value), args),
35 | {
36 | arity: arity + 1,
37 | fixedArity,
38 | },
39 | )
40 | }
41 |
42 | export { applyArrayMethod }
43 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/applyArrayMethod.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | /* eslint-disable no-console */
3 | import { applyArrayMethod } from './applyArrayMethod'
4 |
5 | describe('applyArrayMethod', () => {
6 |
7 | it('should console.warn if method does not exist', () => {
8 | const warn = jest.spyOn(console, 'warn')
9 |
10 | applyArrayMethod('doesNotExist')
11 |
12 | expect(warn).toBeCalledWith('immutadot: Array.prototype.doesNotExist is not available')
13 |
14 | warn.mockRestore()
15 | })
16 |
17 | it('should throw a TypeError if called and method does not exist', () => {
18 | const doesNotExist = applyArrayMethod('doesNotExist')
19 |
20 | expect(doesNotExist).toThrowError(TypeError('immutadot: Array.prototype.doesNotExist is not available'))
21 | })
22 | })
23 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/concat.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces an array concatenating the former array with additional arrays and/or values.5 | * ⚠ Due to name conflicts, this function is named
arrayConcat
when imported from top level (import { arrayConcat } from 'immutadot'
).
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...Array} arrays The arrays to concatenate.
11 | * @return {Object} Returns the updated object.
12 | * @example concat({ nested: { prop: [1, 2] } }, 'nested.prop', [3, 4]) // => { nested: { prop: [1, 2, 3, 4] } }
13 | * @see {@link https://mdn.io/Array.prototype.concat|Array.prototype.concat} for more information.
14 | * @since 0.2.0
15 | */
16 | const concat = applyArrayMethod('concat', { mutating: false })
17 |
18 | export { concat }
19 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/copyWithin.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Shallow copies part of an array to another location in the same array, without modifying its size.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {number} target Zero based index at which to copy the sequence to.
10 | * @param {number} [start] Zero based index at which to start copying elements from.
11 | * @param {number} [end] Zero based index at which to end copying elements from.
12 | * @return {Object} Returns the updated object.
13 | * @example copyWithin({ nested: { prop: [1, 2, 3, 4, 5] } }, 'nested.prop', 0, 3, 4) // => { nested: { prop: [4, 2, 3, 4, 5] } }
14 | * @see {@link https://mdn.io/Array.prototype.copyWithin|Array.prototype.copyWithin} for more information.
15 | * @since 2.0.0
16 | */
17 | const copyWithin = applyArrayMethod('copyWithin', {
18 | arity: 1,
19 | mutating: true,
20 | })
21 |
22 | export { copyWithin }
23 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/copyWithin.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { copyWithin } from 'array'
3 | import { immutaTest } from 'test.utils'
4 |
5 | describe('array.copyWithin', () => {
6 | it('should copy within an array', () => {
7 | immutaTest({
8 | nested: {
9 | prop: [1, 2, 3, 4, 5],
10 | },
11 | other: {},
12 | }, ['nested.prop'], (input, path) => {
13 | const output = copyWithin(input, path, 0, 3, 4)
14 | expect(output).toEqual({
15 | nested: {
16 | prop: [4, 2, 3, 4, 5],
17 | },
18 | other: {},
19 | })
20 | return output
21 | })
22 | })
23 | })
24 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/fill.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces by an array filled with value from start up to, but not including, end.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {*} value The value to fill array with.
10 | * @param {number} [start=0]
11 | * @param {number} [end=array.length]
12 | * @return {Object} Returns the updated object.
13 | * @example fill({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 6, 1, 3) // => { nested: { prop: [1, 6, 6, 4] } }
14 | * @see {@link https://mdn.io/Array.prototype.fill|Array.prototype.fill} for more information.
15 | * @since 0.3.0
16 | */
17 | const fill = applyArrayMethod('fill')
18 |
19 | export { fill }
20 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/fill.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { fill } from 'array'
3 | import { immutaTest } from 'test.utils'
4 | describe('array.fill', () => {
5 | it('should fill array with 6 from 1 to 3 excluded', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = fill(input, path, 6, 1, 3)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 1,
22 | 6,
23 | 6,
24 | 4,
25 | ],
26 | },
27 | other: {},
28 | })
29 | return output
30 | })
31 | })
32 | })
33 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/filter.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces by an array of elements predicate
returns truthy for.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {function} predicate The function invoked per iteration.
10 | * @return {Object} Returns the updated object.
11 | * @example filter({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v % 2) // => { nested: { prop: [1, 3] } }
12 | * @see {@link https://mdn.io/Array.prototype.filter|Array.prototype.filter} for more information.
13 | * @since 1.0.0
14 | */
15 | const filter = applyArrayMethod('filter', { mutating: false })
16 |
17 | export { filter }
18 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/filter.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { filter } from 'array'
3 | import { immutaTest } from 'test.utils'
4 | describe('array.filter', () => {
5 | it('should filter elements', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = filter(input, path, v => v % 2 === 0)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 2,
22 | 4,
23 | ],
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | it('should replace deep undefined with array', () => {
31 | immutaTest(undefined, ['nested.prop'], (input, path) => {
32 | const output = filter(input, path, () => true)
33 | expect(output).toEqual({ nested: { prop: [] } })
34 | return output
35 | })
36 | })
37 | it('should wrap value in an array', () => {
38 | immutaTest({ nested: { prop: 2 } }, ['nested.prop'], (input, path) => {
39 | const output = filter(input, path, () => true)
40 | expect(output).toEqual({ nested: { prop: [2] } })
41 | return output
42 | })
43 | })
44 | })
45 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/flat.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces an array by a new array with all sub-array elements concatenated into it recursively up to the specified depth.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {number} [depth] Depth level.
10 | * @return {Object} Returns the updated object.
11 | * @see {@link https://mdn.io/Array.prototype.flat|Array.prototype.flat} for more information.
12 | * @since 2.0.0
13 | */
14 | const flat = applyArrayMethod('flat', { mutating: false })
15 |
16 | export { flat }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/flat.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { flat } from 'array'
3 | import { immutaTest } from 'test.utils'
4 | describe('array.map', () => {
5 | it('should map elements and flatten', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | [1, 2],
10 | [3, 4],
11 | ],
12 | },
13 | other: {},
14 | }, ['nested.prop'], (input, path) => {
15 | const output = flat(input, path)
16 | expect(output).toEqual({
17 | nested: {
18 | prop: [
19 | 1,
20 | 2,
21 | 3,
22 | 4,
23 | ],
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/flatMap.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces an array mapping each element using a mapping function, then flattening the result into a new array.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {function} callback The function invoked per iteration.
10 | * @return {Object} Returns the updated object.
11 | * @see {@link https://mdn.io/Array.prototype.flatMap|Array.prototype.flatMap} for more information.
12 | * @since 2.0.0
13 | */
14 | const flatMap = applyArrayMethod('flatMap', { mutating: false })
15 |
16 | export { flatMap }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/flatMap.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { flatMap } from 'array'
3 | import { immutaTest } from 'test.utils'
4 | describe('array.map', () => {
5 | it('should map elements and flatten', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | { arr: [1, 2] },
10 | { arr: [3, 4] },
11 | ],
12 | },
13 | other: {},
14 | }, ['nested.prop'], (input, path) => {
15 | const output = flatMap(input, path, ({ arr }) => arr)
16 | expect(output).toEqual({
17 | nested: {
18 | prop: [
19 | 1,
20 | 2,
21 | 3,
22 | 4,
23 | ],
24 | },
25 | other: {},
26 | })
27 | return output
28 | })
29 | })
30 | })
31 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Array functions.
3 | * @namespace array
4 | * @since 0.1.6
5 | */
6 |
7 | export { concat } from './concat'
8 | export { copyWithin } from './copyWithin'
9 | export { fill } from './fill'
10 | export { filter } from './filter'
11 | export { flat } from './flat'
12 | export { flatMap } from './flatMap'
13 | export { map } from './map'
14 | export { pop } from './pop'
15 | export { push } from './push'
16 | export { reverse } from './reverse'
17 | export { shift } from './shift'
18 | export { slice } from './slice'
19 | export { splice } from './splice'
20 | export { sort } from './sort'
21 | export { unshift } from './unshift'
22 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/map.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces by an array of values by running each element in the former collection thru callback.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {function} callback The function invoked per iteration.
10 | * @return {Object} Returns the updated object.
11 | * @example map({ nested: { prop: [1, 2, 3] } }, 'nested.prop', v => v * 2) // => { nested: { prop: [2, 4, 6] } }
12 | * @see {@link https://mdn.io/Array.prototype.map|Array.prototype.map} for more information.
13 | * @since 1.0.0
14 | */
15 | const map = applyArrayMethod('map', { mutating: false })
16 |
17 | export { map }
18 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/map.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { map } from 'array'
4 | describe('array.map', () => {
5 | it('should map elements', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = map(input, path, v => v * v)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 1,
22 | 4,
23 | 9,
24 | 16,
25 | ],
26 | },
27 | other: {},
28 | })
29 | return output
30 | })
31 | })
32 | it('should replace deep undefined with array', () => {
33 | immutaTest(undefined, ['nested.prop'], (input, path) => {
34 | const output = map(input, path, v => v)
35 | expect(output).toEqual({ nested: { prop: [] } })
36 | return output
37 | })
38 | })
39 | it('should wrap value in an array', () => {
40 | immutaTest({ nested: { prop: 2 } }, ['nested.prop'], (input, path) => {
41 | const output = map(input, path, v => v * v)
42 | expect(output).toEqual({ nested: { prop: [4] } })
43 | return output
44 | })
45 | })
46 | })
47 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/pop.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces by an array of elements with last element removed.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @return {Object} Returns the updated object.
10 | * @example pop({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop') // => { nested: { prop: [1, 2, 3] } }
11 | * @see {@link https://mdn.io/Array.prototype.pop|Array.prototype.pop} for more information.
12 | * @since 1.0.0
13 | */
14 | const pop = applyArrayMethod('pop', { fixedArity: true })
15 |
16 | export { pop }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/pop.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { pop } from 'array'
4 | describe('array.pop', () => {
5 | it('should remove last element', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = pop(input, path)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 1,
22 | 2,
23 | 3,
24 | ],
25 | },
26 | other: {},
27 | })
28 | return output
29 | })
30 | })
31 | it('should replace deep undefined with array', () => {
32 | immutaTest(undefined, ['nested.prop'], (input, path) => {
33 | const output = pop(input, path, () => true)
34 | expect(output).toEqual({ nested: { prop: [] } })
35 | return output
36 | })
37 | })
38 | it('should wrap value in array and remove it', () => {
39 | immutaTest({
40 | nested: { prop: 123 },
41 | other: {},
42 | }, ['nested.prop'], (input, path) => {
43 | const output = pop(input, path)
44 | expect(output).toEqual({
45 | nested: { prop: [] },
46 | other: {},
47 | })
48 | return output
49 | })
50 | })
51 | })
52 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/push.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces an array adding one or more elements to the end of the former array.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {...*} values The values to add.
10 | * @return {Object} Returns the updated object.
11 | * @example start
up to, but not including, end
.5 | * ⚠ Due to name conflicts, this function is named
arraySlice
when imported from top level (import { arraySlice } from 'immutadot'
).
6 | * @function
7 | * @memberof array
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {number} [start=0] The start position.
11 | * @param {number} [end=array.length] The end position.
12 | * @return {Object} Returns the updated object.
13 | * @example slice({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 1, 3) // => { nested: { prop: [2, 3] } }
14 | * @see {@link https://mdn.io/Array.prototype.slice|Array.prototype.slice} for more information.
15 | * @since 0.3.0
16 | */
17 | const slice = applyArrayMethod('slice', {
18 | arity: 0,
19 | mutating: false,
20 | })
21 |
22 | export { slice }
23 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/slice.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { slice } from 'array'
4 |
5 | describe('array.slice', () => {
6 | it('should replace by a slice', () => {
7 | immutaTest({
8 | nested: {
9 | prop: [
10 | 1,
11 | 2,
12 | 3,
13 | 4,
14 | ],
15 | },
16 | other: {},
17 | }, ['nested.prop'], (input, path) => {
18 | const output = slice(input, path, 1, 3)
19 | expect(output).toEqual({
20 | nested: {
21 | prop: [
22 | 2,
23 | 3,
24 | ],
25 | },
26 | other: {},
27 | })
28 | return output
29 | })
30 | })
31 |
32 | it('should create a copy of all the array', () => {
33 | immutaTest({
34 | nested: { prop: [1, 2] },
35 | other: {},
36 | }, ['nested.prop'], (input, path) => {
37 | const output = slice(path)(input)
38 | expect(output).toEqual({
39 | nested: { prop: [1, 2] },
40 | other: {},
41 | })
42 | return output
43 | })
44 | })
45 | })
46 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/sort.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces by a sorted array, in natural order or according to the optional comparator
.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {function?} comparator The comparator function.
10 | * @return {Object} Returns the updated object.
11 | * @example sort({ nested: { prop: [2, 4, 3, 1] } }, 'nested.prop') // => { nested: { prop: [1, 2, 3, 4] } }
12 | * @example sort({ nested: { prop: [2, 4, 3, 1] } }, 'nested.prop', (a, b) => b - a) // => { nested: { prop: [4, 3, 2, 1] } }
13 | * @see {@link https://mdn.io/Array.prototype.sort|Array.prototype.sort} for more information.
14 | * @since 1.0.0
15 | */
16 | const sort = applyArrayMethod('sort', { arity: 0 })
17 |
18 | export { sort }
19 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/splice.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces an array removing and/or adding elements at index
in the former array.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {number} start Index at which to start changing the array.
10 | * @param {number} [deleteCount] The number of old array elements to remove.
11 | * @param {...*} values The values to add.
12 | * @return {Object} Returns the updated object.
13 | * @example splice({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 1, 2, 5, 6) // => { nested: { prop: [1, 5, 6, 4] } }
14 | * @see {@link https://mdn.io/Array.prototype.splice|Array.prototype.splice} for more information.
15 | * @since 0.2.0
16 | */
17 | const splice = applyArrayMethod('splice')
18 |
19 | export { splice }
20 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/splice.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { splice } from 'array'
4 | describe('array.splice', () => {
5 | it('should replace two elements', () => {
6 | immutaTest({
7 | nested: {
8 | prop: [
9 | 1,
10 | 2,
11 | 3,
12 | 4,
13 | ],
14 | },
15 | other: {},
16 | }, ['nested.prop'], (input, path) => {
17 | const output = splice(input, path, 1, 2, 6, 6)
18 | expect(output).toEqual({
19 | nested: {
20 | prop: [
21 | 1,
22 | 6,
23 | 6,
24 | 4,
25 | ],
26 | },
27 | other: {},
28 | })
29 | return output
30 | })
31 | })
32 | })
33 |
--------------------------------------------------------------------------------
/packages/immutadot/src/array/unshift.js:
--------------------------------------------------------------------------------
1 | import { applyArrayMethod } from './applyArrayMethod'
2 |
3 | /**
4 | * Replaces an array adding elements at the start of the former array.
5 | * @function
6 | * @memberof array
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {...*} values The values to add.
10 | * @return {Object} Returns the updated object.
11 | * @example args
.15 | * Each function is called with the result of the previous one.
16 | * Non functions
args
are tolerated and will be ignored.
17 | * @memberof core
18 | * @param {...(function|Arrayargs
20 | * @since 1.0.0
21 | */
22 | function flow(...args) {
23 | const fns = flatten(args)
24 | .filter(fn => isFunction(fn))
25 | .map(fn => fn.applier === undefined ? (
26 | ([obj, appliedPaths]) => [fn(obj), appliedPaths]
27 | ) : (
28 | ([obj, appliedPaths]) => [
29 | fn.applier(obj, appliedPaths),
30 | [...appliedPaths, fn.applier.path],
31 | ]
32 | ))
33 | return obj => {
34 | const [result] = fns.reduce(
35 | (acc, fn) => fn(acc),
36 | [obj, []],
37 | )
38 | return result
39 | }
40 | }
41 |
42 | export { flow }
43 |
--------------------------------------------------------------------------------
/packages/immutadot/src/core/get.js:
--------------------------------------------------------------------------------
1 | import { nav } from 'nav/nav'
2 | import { toPath } from 'immutadot-parser'
3 |
4 | const isGetter = Symbol('isGetter')
5 |
6 | /**
7 | * Gets the value at path
of obj
.
8 | * @memberof core
9 | * @param {*} [obj] The object.
10 | * @param {string|Array} path The path of the property to get.
11 | * @return {*} Returns the value
12 | * @example get({ nested: { prop: 'val' } }, 'nested.prop') // => 'val'
13 | * @since 1.0.0
14 | */
15 | function get(...args) {
16 | if (args.length >= 2)
17 | return _get(...args)
18 |
19 | const getter = obj => _get(obj, ...args)
20 | getter[isGetter] = true
21 |
22 | return getter
23 | }
24 |
25 | function _get(obj, path) {
26 | return nav(toPath(path))(obj).get()
27 | }
28 |
29 | function resolveGetter(value, obj) {
30 | if (value && value[isGetter]) return value(obj)
31 | return value
32 | }
33 |
34 | // FIXME stop exporting isGetter
35 | export { get, isGetter, resolveGetter }
36 |
--------------------------------------------------------------------------------
/packages/immutadot/src/core/get.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { get } from 'core'
3 | describe('core.get', () => {
4 | const obj = {
5 | nested1: { prop: 'val' },
6 | nested2: { arr: [{ val: 'arrVal1' }, { val: 'arrVal2' }] },
7 | nested3: [[{ val: 1 }, { val: 2 }], [{ val: 3 }, { val: 4 }]],
8 | }
9 |
10 | it('should get a prop', () => {
11 | expect(get(obj, 'nested1.prop')).toBe('val')
12 | expect(get(obj, 'nested1.prop.length')).toBe(3)
13 | expect(get(obj, 'nested2.arr.length')).toBe(2)
14 | expect(get(obj, 'nested2.arr[0].val')).toBe('arrVal1')
15 | })
16 |
17 | it('should get multiple props', () => {
18 | expect(get(obj, 'nested2.arr[:].val')).toEqual(['arrVal1', 'arrVal2'])
19 | expect(get(obj, 'nested3[:][:].val')).toEqual([[1, 2], [3, 4]])
20 | })
21 |
22 | it('should return undefined for unexisting path', () => {
23 | expect(get(obj, 'nested1.foo')).toBe(undefined)
24 | expect(get(obj, 'nested3.val')).toBe(undefined)
25 | expect(get(obj, 'nested2.arr[2].val')).toBe(undefined)
26 | })
27 |
28 | it('should support currying the object', () => {
29 | expect(get('nested2.arr[0].val')(obj)).toBe('arrVal1')
30 | })
31 | })
32 |
--------------------------------------------------------------------------------
/packages/immutadot/src/core/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Core functions.
3 | * @namespace core
4 | * @since 1.0.0
5 | */
6 |
7 | export { apply } from './apply'
8 | export { flow } from './flow'
9 | export { get } from './get'
10 | export { set } from './set'
11 | export { unset } from './unset'
12 | export { update } from './update'
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/core/set.js:
--------------------------------------------------------------------------------
1 | import { apply } from './apply'
2 |
3 | /**
4 | * Sets the value at path
of obj
.
5 | * @memberof core
6 | * @param {*} obj The object to modify.
7 | * @param {string|Array} path The path of the property to set.
8 | * @param {*} value The value to set.
9 | * @return {*} Returns the updated object.
10 | * @example set({ nested: { prop: 'old' } }, 'nested.prop', 'new') // => { nested: { prop: 'new' } }
11 | * @since 1.0.0
12 | */
13 | const set = apply((_, value) => value, { fixedArity: true })
14 |
15 | export { set }
16 |
--------------------------------------------------------------------------------
/packages/immutadot/src/core/unset.js:
--------------------------------------------------------------------------------
1 | import { curry } from './curry'
2 | import { nav } from 'nav/nav'
3 | import { toPath } from 'immutadot-parser'
4 |
5 | /**
6 | * Removes the property at path
of object
.
7 | * @function
8 | * @memberof core
9 | * @param {Object} obj The object to modify.
10 | * @param {Array|string} path The path of the property to unset.
11 | * @return {Object} Returns the updated object.
12 | * @example unset({ nested: { prop: 'value' } }, 'nested.prop') // => { nested: {} }
13 | * @since 1.0.0
14 | */
15 | const unset = curry((obj, path) => nav(toPath(path))(obj).unset(), { fixedArity: true })
16 |
17 | export { unset }
18 |
--------------------------------------------------------------------------------
/packages/immutadot/src/core/update.js:
--------------------------------------------------------------------------------
1 | import { apply } from './apply'
2 |
3 | /**
4 | * Updates the value at path
of object
using the updater
function.5 | * The updater is invoked with
value
and …args
.6 | * Be carefull, the
updater
function must not mutate its value
argument.
7 | * @function
8 | * @memberof core
9 | * @param {Object} obj The object to modify.
10 | * @param {Array|string} path The path of the property to set.
11 | * @param {function} updater The function to produce the updated value.
12 | * @param {...*} args The remaining args.
13 | * @return {Object} Returns the updated object.
14 | * @example &&
between the former value and args
5 | * @function
6 | * @memberof lang
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {...*} [args] Other operands.
10 | * @return {Object} Returns the updated object.
11 | * @example and({ nested: { prop: true } }, 'nested.prop', true) // { nested: { prop: true } }
12 | * @example and({ nested: { prop: true } }, 'nested.prop', true, false) // { nested: { prop: false } }
13 | * @since 1.0.0
14 | */
15 | const and = apply((v, ...args) => args.reduce((acc, arg) => acc && arg, v))
16 |
17 | export { and }
18 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/divide.js:
--------------------------------------------------------------------------------
1 | import { apply } from 'core/apply'
2 |
3 | /**
4 | * Replaces by the division of the former number and the given number.
5 | * @function
6 | * @memberof lang
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {number} divisor The second number in the division.
10 | * @return {Object} Returns the updated object.
11 | * @example divide({ nested: { prop: 1332 } }, 'nested.prop', 2) // => { nested: { prop: 666 } }
12 | * @since 1.0.0
13 | */
14 | const divide = apply((value, divider) => Number(value) / Number(divider), { fixedArity: true })
15 |
16 | export { divide }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/divide.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { divide } from 'lang'
3 | import { immutaTest } from 'test.utils'
4 | describe('lang.divide', () => {
5 | it('should divide two numbers', () => {
6 | immutaTest({
7 | nested: { prop: 1332 },
8 | other: {},
9 | }, ['nested.prop'], (input, path) => {
10 | const output = divide(input, path, 2)
11 | expect(output).toEqual({
12 | nested: { prop: 666 },
13 | other: {},
14 | })
15 | return output
16 | })
17 | })
18 | })
19 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Lang functions.
3 | * @namespace lang
4 | * @since 0.1.5
5 | */
6 |
7 | export { add } from './add'
8 | export { and } from './and'
9 | export { divide } from './divide'
10 | export { multiply } from './multiply'
11 | export { or } from './or'
12 | export { subtract } from './subtract'
13 | export { toggle } from './toggle'
14 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/multiply.js:
--------------------------------------------------------------------------------
1 | import { apply } from 'core/apply'
2 |
3 | /**
4 | * Replaces by the multiplication of the former number and the given number.
5 | * @function
6 | * @memberof lang
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {number} multiplicand The second number in the multiplication.
10 | * @return {Object} Returns the updated object.
11 | * @example multiply({ nested: { prop: 333 } }, 'nested.prop', 2) // => { nested: { prop: 666 } }
12 | * @since 1.0.0
13 | */
14 | const multiply = apply((value, multiplier) => Number(value) * Number(multiplier), { fixedArity: true })
15 |
16 | export { multiply }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/multiply.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { multiply } from 'lang'
4 | describe('lang.multiply', () => {
5 | it('should multiply two numbers', () => {
6 | immutaTest({
7 | nested: { prop: 333 },
8 | other: {},
9 | }, ['nested.prop'], (input, path) => {
10 | const output = multiply(input, path, 2)
11 | expect(output).toEqual({
12 | nested: { prop: 666 },
13 | other: {},
14 | })
15 | return output
16 | })
17 | })
18 | })
19 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/or.js:
--------------------------------------------------------------------------------
1 | import { apply } from 'core/apply'
2 |
3 | /**
4 | * Applies ||
between the former value and args
5 | * @function
6 | * @memberof lang
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {...*} [args] Other operands.
10 | * @return {Object} Returns the updated object.
11 | * @example or({ nested: { prop: false } }, 'nested.prop', true) // { nested: { prop: true } }
12 | * @example or({ nested: { prop: true } }, 'nested.prop', false, false) // { nested: { prop: true } }
13 | * @since 1.0.0
14 | */
15 | const or = apply((v, ...args) => args.reduce((acc, arg) => acc || arg, v))
16 |
17 | export { or }
18 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/subtract.js:
--------------------------------------------------------------------------------
1 | import { apply } from 'core/apply'
2 |
3 | /**
4 | * Replaces by the subtraction of the former number by the given number.
5 | * @function
6 | * @memberof lang
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {number} subtrahend The number to subtract.
10 | * @return {Object} Returns the updated object.
11 | * @example subtract({ nested: { prop: 2000 } }, 'nested.prop', 336) // => { nested: { prop: 1664 } }
12 | * @since 1.0.0
13 | */
14 | const subtract = apply((value, subtraction) => Number(value) - Number(subtraction), { fixedArity: true })
15 |
16 | export { subtract }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/subtract.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { subtract } from 'lang'
4 | describe('lang.subtract', () => {
5 | it('should subtract two numbers', () => {
6 | immutaTest({
7 | nested: { prop: 2000 },
8 | other: {},
9 | }, ['nested.prop'], (input, path) => {
10 | const output = subtract(input, path, 336)
11 | expect(output).toEqual({
12 | nested: { prop: 1664 },
13 | other: {},
14 | })
15 | return output
16 | })
17 | })
18 | })
19 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/toggle.js:
--------------------------------------------------------------------------------
1 | import { apply } from 'core/apply'
2 |
3 | /**
4 | * Applies !
to the property.
5 | * @function
6 | * @memberof lang
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @return {Object} Returns the updated object.
10 | * @example toggle({ nested: { prop: true } }, 'nested.prop') // { nested: { prop: false } }
11 | * @since 0.1.5
12 | */
13 | const toggle = apply(v => !v, { fixedArity: true })
14 |
15 | export { toggle }
16 |
--------------------------------------------------------------------------------
/packages/immutadot/src/lang/toggle.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { toggle } from 'lang'
4 | describe('lang.toggle', () => {
5 | const withTrue = {
6 | nested: { prop: true },
7 | other: {},
8 | }
9 | const withFalse = {
10 | nested: { prop: false },
11 | other: {},
12 | }
13 | it('should toggle false to true', () => {
14 | immutaTest(withFalse, ['nested.prop'], (input, path) => {
15 | const output = toggle(input, path)
16 | expect(output).toEqual(withTrue)
17 | return output
18 | })
19 | })
20 | it('should toggle deep undefined to true', () => {
21 | immutaTest(undefined, ['nested.prop'], (input, path) => {
22 | const output = toggle(input, path)
23 | expect(output).toEqual({ nested: { prop: true } })
24 | return output
25 | })
26 | })
27 | it('should toggle true to false', () => {
28 | immutaTest(withTrue, ['nested.prop'], (input, path) => {
29 | const output = toggle(input, path)
30 | expect(output).toEqual(withFalse)
31 | return output
32 | })
33 | })
34 | })
35 |
--------------------------------------------------------------------------------
/packages/immutadot/src/nav/arrayNav.js:
--------------------------------------------------------------------------------
1 | import { isNil, length } from 'util/lang'
2 | import { BaseNav } from './baseNav'
3 |
4 | export class ArrayNav extends BaseNav {
5 | get length() {
6 | return length(this.value)
7 | }
8 |
9 | copy() {
10 | const { value } = this
11 | if (isNil(value)) return []
12 | return Array.isArray(value) ? [...value] : { ...value }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/packages/immutadot/src/nav/baseNav.js:
--------------------------------------------------------------------------------
1 | export class BaseNav {
2 | constructor(value, next) {
3 | this.value = value
4 | this._next = next
5 | }
6 |
7 | get final() {
8 | return false
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/packages/immutadot/src/nav/finalNav.js:
--------------------------------------------------------------------------------
1 | class FinalNav {
2 | constructor(value) {
3 | this.value = value
4 | }
5 |
6 | get() {
7 | return this.value
8 | }
9 |
10 | update(updater) {
11 | return updater(this.value)
12 | }
13 |
14 | get final() {
15 | return true
16 | }
17 | }
18 |
19 | export function finalNav(value) {
20 | return new FinalNav(value)
21 | }
22 |
--------------------------------------------------------------------------------
/packages/immutadot/src/nav/indexNav.js:
--------------------------------------------------------------------------------
1 | import { ArrayNav } from './arrayNav'
2 | import { isNil } from 'util/lang'
3 |
4 | class IndexNav extends ArrayNav {
5 | constructor(value, index, next) {
6 | super(value, next)
7 | this._index = index
8 | }
9 |
10 | get index() {
11 | const { _index, length } = this
12 | if (_index >= 0) return _index
13 | if (-_index > length) return undefined
14 | return Math.max(length + _index, 0)
15 | }
16 |
17 | get next() {
18 | const { _next, index, value } = this
19 | return (isNil(value) || index === undefined) ? _next(undefined) : _next(value[index])
20 | }
21 |
22 | get() {
23 | return this.next.get()
24 | }
25 |
26 | update(updater) {
27 | const copy = this.copy()
28 | copy[this.index] = this.next.update(updater)
29 | return copy
30 | }
31 |
32 | unset() {
33 | const copy = this.copy()
34 | if (this.next.final)
35 | delete copy[this.index]
36 | else
37 | copy[this.index] = this.next.unset()
38 | return copy
39 | }
40 | }
41 |
42 | export function indexNav(index, next) {
43 | return value => new IndexNav(value, index, next)
44 | }
45 |
--------------------------------------------------------------------------------
/packages/immutadot/src/nav/nav.js:
--------------------------------------------------------------------------------
1 | import { NavType } from 'immutadot-parser'
2 | import { finalNav } from './finalNav'
3 | import { indexNav } from './indexNav'
4 | import { propNav } from './propNav'
5 | import { propsNav } from './propsNav'
6 | import { sliceNav } from './sliceNav'
7 |
8 | export function nav(path) {
9 | if (path.length === 0) throw new TypeError('path should not be empty')
10 |
11 | return path.reduceRight((next, [type, value]) => toNav(type)(value, next), finalNav)
12 | }
13 |
14 | function toNav(type) {
15 | switch (type) {
16 | case NavType.allProps:
17 | case NavType.list:
18 | return propsNav
19 | case NavType.index: return indexNav
20 | case NavType.prop: return propNav
21 | case NavType.slice: return sliceNav
22 | default: throw TypeError(type)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/packages/immutadot/src/nav/objectNav.js:
--------------------------------------------------------------------------------
1 | import { BaseNav } from './baseNav'
2 | import { isNil } from 'util/lang'
3 |
4 | export class ObjectNav extends BaseNav {
5 | copy() {
6 | const { value } = this
7 | return isNil(value) ? {} : { ...value }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/packages/immutadot/src/nav/propNav.js:
--------------------------------------------------------------------------------
1 | import { ObjectNav } from './objectNav'
2 | import { isNil } from 'util/lang'
3 |
4 | class PropNav extends ObjectNav {
5 | constructor(value, key, next) {
6 | super(value, next)
7 | this.key = key
8 | }
9 |
10 | get next() {
11 | const { _next, key, value } = this
12 | return isNil(value) ? _next(undefined) : _next(value[key])
13 | }
14 |
15 | get() {
16 | return this.next.get()
17 | }
18 |
19 | update(updater) {
20 | const copy = this.copy()
21 | copy[this.key] = this.next.update(updater)
22 | return copy
23 | }
24 |
25 | unset() {
26 | const copy = this.copy()
27 | if (this.next.final)
28 | delete copy[this.key]
29 | else
30 | copy[this.key] = this.next.unset()
31 | return copy
32 | }
33 | }
34 |
35 | export function propNav(key, next) {
36 | return value => new PropNav(value, key, next)
37 | }
38 |
--------------------------------------------------------------------------------
/packages/immutadot/src/nav/propsNav.js:
--------------------------------------------------------------------------------
1 | import { ObjectNav } from './objectNav'
2 | import { isNil } from 'util/lang'
3 |
4 | class PropsNav extends ObjectNav {
5 | constructor(value, keys, next) {
6 | super(value, next)
7 | this._keys = keys
8 | }
9 |
10 | get keys() {
11 | const { _keys, value } = this
12 |
13 | if (_keys !== undefined) return _keys
14 |
15 | return isNil(value) ? [] : Object.keys(value)
16 | }
17 |
18 | get() {
19 | const { _next, keys, value } = this
20 |
21 | return keys.map(key => _next(value[key]).get())
22 | }
23 |
24 | update(updater) {
25 | const { _next, keys, value } = this
26 |
27 | const copy = this.copy()
28 | for (const key of keys) copy[key] = _next(value[key]).update(updater)
29 | return copy
30 | }
31 |
32 | unset() {
33 | const { _next, keys, value } = this
34 |
35 | const copy = this.copy()
36 | for (const key of keys) {
37 | const next = _next(value[key])
38 | if (next.final)
39 | delete copy[key]
40 | else
41 | copy[key] = next.unset()
42 | }
43 | return copy
44 | }
45 | }
46 |
47 | export function propsNav(keys, next) {
48 | return value => new PropsNav(value, keys, next)
49 | }
50 |
--------------------------------------------------------------------------------
/packages/immutadot/src/nav/sliceNav.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { sliceNav } from './sliceNav'
4 |
5 | describe('SliceNav', () => {
6 |
7 | it('should update a slice', () => {
8 | const input = [0, 1]
9 | const next = value => ({
10 | update: updater => updater(value),
11 | })
12 | const updater = value => value + 1
13 |
14 | const test = (params, expected) => {
15 | const output = sliceNav(params, next)(input).update(updater)
16 |
17 | expect(input).toEqual([0, 1])
18 | expect(output).not.toBe(input)
19 | expect(output).toEqual(expected)
20 | }
21 |
22 | test([0, undefined], [1, 2])
23 | })
24 |
25 | it('should avoid unnecessary copies', () => {
26 | const input = [
27 | { val: 0 },
28 | { val: 1 },
29 | ]
30 | const next = value => ({
31 | update: updater => updater(value),
32 | })
33 | const updater = value => ({
34 | ...value,
35 | val: value.val + 1,
36 | })
37 |
38 | const test = params => immutaTest(input, [], () => sliceNav(params, next)(input).update(updater))
39 |
40 | test([0, 0])
41 | test([1, 1])
42 | test([1, 0])
43 | test([2, undefined])
44 | test([undefined, 0])
45 | test([undefined, -2])
46 | })
47 | })
48 |
--------------------------------------------------------------------------------
/packages/immutadot/src/object/assign.js:
--------------------------------------------------------------------------------
1 | import { apply } from 'core/apply'
2 |
3 | /**
4 | * Replaces by an object assigning own enumerable string keyed properties of source objects to the destination object.5 | * Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources. 6 | * @function 7 | * @memberof object 8 | * @param {Object} object The object to modify. 9 | * @param {Array|string} path The path of the property to set. 10 | * @param {...Object} [sources] The source objects. 11 | * @return {Object} Returns the updated object. 12 | * @example assign({ nested: { a: 1, b: 2 } }, 'nested', { b: 3, c: 4 }) // => { nested: { a:1, b: 3, c: 4 } } 13 | * @see {@link https://mdn.io/Object.prototype.assign|Object.prototype.assign} for more information. 14 | * @since 0.1.12 15 | */ 16 | const assign = apply((obj, ...args) => Object.assign({ ...obj }, ...args)) 17 | 18 | export { assign } 19 | -------------------------------------------------------------------------------- /packages/immutadot/src/object/assign.spec.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | import { assign } from 'object' 3 | import { immutaTest } from 'test.utils' 4 | describe('object.assign', () => { 5 | it('should assign objects', () => { 6 | immutaTest({ 7 | nested: { 8 | prop: { 9 | a: 1, 10 | b: 2, 11 | }, 12 | }, 13 | other: {}, 14 | }, ['nested.prop'], (input, path) => { 15 | const output = assign(input, path, { 16 | b: 3, 17 | c: 4, 18 | }) 19 | expect(output).toEqual({ 20 | nested: { 21 | prop: { 22 | a: 1, 23 | b: 3, 24 | c: 4, 25 | }, 26 | }, 27 | other: {}, 28 | }) 29 | return output 30 | }) 31 | }) 32 | }) 33 | -------------------------------------------------------------------------------- /packages/immutadot/src/object/create.js: -------------------------------------------------------------------------------- 1 | import { set } from 'core/set' 2 | 3 | /** 4 | * create a new Object with the value set at
path
5 | * @function
6 | * @memberof object
7 | * @param {Array|string} path The path of the property to set.
8 | * @param {*} value The value to set
9 | * @return {Object} Returns the new object createialized.
10 | * @example create({}, 'nested.prop', 1) // => { nested: { prop: 1 } }
11 | * @since 2.0.0
12 | */
13 | const create = (path, value) => set({}, path, value)
14 |
15 | export { create }
16 |
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/object/create.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { create } from 'object'
3 | import { immutaTest } from 'test.utils'
4 | describe('object.create', () => {
5 | it('should create objects', () => {
6 | immutaTest(undefined, ['nested.prop'], (input, path) => {
7 | const output = create(path, 1)
8 | expect(output).toEqual({ nested: { prop: 1 } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/object/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Object functions.
3 | * @namespace object
4 | * @since 0.1.12
5 | */
6 |
7 | export { assign } from './assign'
8 | export { create } from './create'
9 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/applyStringMethod.js:
--------------------------------------------------------------------------------
1 | import { apply } from 'core/apply'
2 | import { toString } from 'util/lang'
3 |
4 | export function applyStringMethod(method, { arity = method.length, fixedArity = false } = {}) {
5 | return apply(
6 | (value, ...args) => method.apply(toString(value), args),
7 | {
8 | arity: arity + 1,
9 | fixedArity,
10 | },
11 | )
12 | }
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/concat.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string concatenated with strings
.5 | * ⚠ Due to name conflicts, this function is named
stringConcat
when imported from top level (import { stringConcat } from 'immutadot'
).
6 | * @function
7 | * @memberof string
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {...string} strings Strings to concatenate.
11 | * @return {Object} Returns the updated object.
12 | * @example concat({ nested: { a: 'Hello' } }, 'nested.a', ' world', ' !') // => { nested: { a: 'Hello world !' } }
13 | * @see {@link https://mdn.io/String.prototype.concat|String.prototype.concat} for more information.
14 | * @since 1.0.0
15 | */
16 | const concat = applyStringMethod(String.prototype.concat)
17 |
18 | export { concat }
19 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/concat.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { concat } from 'string'
3 | import { immutaTest } from 'test.utils'
4 | describe('string.concat', () => {
5 | it('should concat strings', () => {
6 | immutaTest({ nested: { prop: 'Hello' } }, ['nested.prop'], (input, path) => {
7 | const output = concat(input, path, ' world', ' !')
8 | expect(output).toEqual({ nested: { prop: 'Hello world !' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * String functions.
3 | * @namespace string
4 | * @since 0.3.0
5 | */
6 |
7 | export { concat } from './concat'
8 | export { padEnd } from './padEnd'
9 | export { padStart } from './padStart'
10 | export { replace } from './replace'
11 | export { slice } from './slice'
12 | export { substring } from './substring'
13 | export { toLocaleLowerCase } from './toLocaleLowerCase'
14 | export { toLocaleUpperCase } from './toLocaleUpperCase'
15 | export { toLowerCase } from './toLowerCase'
16 | export { toUpperCase } from './toUpperCase'
17 | export { trim } from './trim'
18 | export { trimStart, trimLeft } from './trimStart'
19 | export { trimEnd, trimRight } from './trimEnd'
20 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/padEnd.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string padded at the end with padString
to the given length
.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {number} targetLength The length to pad to.
10 | * @param {string} [padString=' '] String to add.
11 | * @return {Object} Returns the updated object.
12 | * @example padEnd({ nested: { a: 'Hellow' } }, 10) // => { nested: { a: 'Hellow ' } }
13 | * @example padEnd({ nested: { a: 'Hellow' } }, 10, '?!') // => { nested: { a: 'Hellow?!?!' } }
14 | * @see {@link https://mdn.io/String.prototype.padEnd|String.prototype.padEnd} for more information.
15 | * @since 1.0.0
16 | */
17 | const padEnd = applyStringMethod(String.prototype.padEnd)
18 |
19 | export { padEnd }
20 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/padEnd.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { padEnd } from 'string'
4 | describe('string.padEnd', () => {
5 | it('should add trailing spaces', () => {
6 | immutaTest({ nested: { prop: 'Hellow' } }, ['nested.prop'], (input, path) => {
7 | const output = padEnd(input, path, 10)
8 | expect(output).toEqual({ nested: { prop: 'Hellow ' } })
9 | return output
10 | })
11 | })
12 | it('should add trailing question/exclamation marks', () => {
13 | immutaTest({ nested: { prop: 'Hellow' } }, ['nested.prop'], (input, path) => {
14 | const output = padEnd(input, path, 10, '?!')
15 | expect(output).toEqual({ nested: { prop: 'Hellow?!?!' } })
16 | return output
17 | })
18 | })
19 | })
20 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/padStart.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string padded at the start with padString
to the given length
.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {number} targetLength The length to pad to.
10 | * @param {string} [padString=' '] String to add.
11 | * @return {Object} Returns the updated object.
12 | * @example padStart({ nested: { a: 'Hellow' } }, 10) // => { nested: { a: ' Hellow' } }
13 | * @example padStart({ nested: { a: 'Hellow' } }, 10, '?!') // => { nested: { a: '?!?!Hellow' } }
14 | * @see {@link https://mdn.io/String.prototype.padStart|String.prototype.padStart} for more information.
15 | * @since 1.0.0
16 | */
17 | const padStart = applyStringMethod(String.prototype.padStart)
18 |
19 | export { padStart }
20 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/padStart.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { padStart } from 'string'
4 | describe('string.padStart', () => {
5 | it('should add leading spaces', () => {
6 | immutaTest({ nested: { prop: 'Hellow' } }, ['nested.prop'], (input, path) => {
7 | const output = padStart(input, path, 10)
8 | expect(output).toEqual({ nested: { prop: ' Hellow' } })
9 | return output
10 | })
11 | })
12 | it('should add leading question/exclamation marks', () => {
13 | immutaTest({ nested: { prop: 'Hellow' } }, ['nested.prop'], (input, path) => {
14 | const output = padStart(input, path, 10, '?!')
15 | expect(output).toEqual({ nested: { prop: '?!?!Hellow' } })
16 | return output
17 | })
18 | })
19 | })
20 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/replace.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces matches for pattern in string with replacement.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {RegExp|string} pattern The pattern to replace.
10 | * @param {Function|string} replacement The match replacement.
11 | * @return {Object} Returns the updated object.
12 | * @example replace({ nested: { a: 'Hi Nico' } }, 'nested.a', 'Nico', 'Yvo') // => { nested: { a: 'Hi Yvo' } }
13 | * @see {@link https://mdn.io/String.prototype.replace|String.prototype.replace} for more information.
14 | * @since 0.3.0
15 | */
16 | const replace = applyStringMethod(String.prototype.replace, { fixedArity: true })
17 |
18 | export { replace }
19 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/replace.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { replace } from 'string'
4 | describe('string.replace', () => {
5 | it('should replace matches for pattern in string with replacement', () => {
6 | immutaTest({ nested: { prop: 'Hi Nico' } }, ['nested.prop'], (input, path) => {
7 | const output = replace(input, path, 'Nico', 'Yvo')
8 | expect(output).toEqual({ nested: { prop: 'Hi Yvo' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/slice.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by a slice of former string starting at beginIndex
and ending at endIndex
or end of the string.5 | * ⚠ Due to name conflicts, this function is named
stringSlice
when imported from top level (import { stringSlice } from 'immutadot'
).
6 | * @function
7 | * @memberof string
8 | * @param {Object} object The object to modify.
9 | * @param {Array|string} path The path of the property to set.
10 | * @param {number} beginIndex Beginning index of slice.
11 | * @param {number?} endIndex Ending index of slice.
12 | * @return {Object} Returns the updated object.
13 | * @example slice({ nested: { a: 'Hello World !' } }, 6) // => { nested: { a: 'World !' } }
14 | * @example slice({ nested: { a: 'Hello World !' } }, 6, 11) // => { nested: { a: 'World' } }
15 | * @see {@link https://mdn.io/String.prototype.slice|String.prototype.slice} for more information.
16 | * @since 1.0.0
17 | */
18 | const slice = applyStringMethod(String.prototype.slice, { arity: 1 })
19 |
20 | export { slice }
21 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/slice.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { slice } from 'string'
4 |
5 | describe('string.slice', () => {
6 | it('should return a slice to the end', () => {
7 | immutaTest({ nested: { prop: 'Hello World !' } }, ['nested.prop'], (input, path) => {
8 | const output = slice(input, path, 6)
9 | expect(output).toEqual({ nested: { prop: 'World !' } })
10 | return output
11 | })
12 |
13 | immutaTest({ nested: { prop: 'Hello World !' } }, ['nested.prop'], (input, path) => {
14 | const output = slice(path)(6)(input)
15 | expect(output).toEqual({ nested: { prop: 'World !' } })
16 | return output
17 | })
18 | })
19 |
20 | it('should return a slice', () => {
21 | immutaTest({ nested: { prop: 'Hello World !' } }, ['nested.prop'], (input, path) => {
22 | const output = slice(input, path, 6, 11)
23 | expect(output).toEqual({ nested: { prop: 'World' } })
24 | return output
25 | })
26 |
27 | immutaTest({ nested: { prop: 'Hello World !' } }, ['nested.prop'], (input, path) => {
28 | const output = slice(path)(6, 11)(input)
29 | expect(output).toEqual({ nested: { prop: 'World' } })
30 | return output
31 | })
32 | })
33 | })
34 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/substring.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by a slice of former string starting at beginIndex
and ending at endIndex
or end of the string.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {number} beginIndex Beginning index of slice.
10 | * @param {number?} endIndex Ending index of slice.
11 | * @return {Object} Returns the updated object.
12 | * @example substring({ nested: { a: 'Hello World !' } }, 6) // => { nested: { a: 'World !' } }
13 | * @example substring({ nested: { a: 'Hello World !' } }, 6, 11) // => { nested: { a: 'World' } }
14 | * @see {@link https://mdn.io/String.prototype.substring|String.prototype.substring} for more information.
15 | * @since 1.0.0
16 | */
17 | const substring = applyStringMethod(String.prototype.substring, { arity: 1 })
18 |
19 | export { substring }
20 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/substring.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { substring } from 'string'
4 |
5 | describe('string.substring', () => {
6 | it('should return a slice to the end', () => {
7 | immutaTest({ nested: { prop: 'Hello World !' } }, ['nested.prop'], (input, path) => {
8 | const output = substring(input, path, 6)
9 | expect(output).toEqual({ nested: { prop: 'World !' } })
10 | return output
11 | })
12 |
13 | immutaTest({ nested: { prop: 'Hello World !' } }, ['nested.prop'], (input, path) => {
14 | const output = substring(path)(6)(input)
15 | expect(output).toEqual({ nested: { prop: 'World !' } })
16 | return output
17 | })
18 | })
19 |
20 | it('should return a slice', () => {
21 | immutaTest({ nested: { prop: 'Hello World !' } }, ['nested.prop'], (input, path) => {
22 | const output = substring(input, path, 6, 11)
23 | expect(output).toEqual({ nested: { prop: 'World' } })
24 | return output
25 | })
26 |
27 | immutaTest({ nested: { prop: 'Hello World !' } }, ['nested.prop'], (input, path) => {
28 | const output = substring(path)(6, 11)(input)
29 | expect(output).toEqual({ nested: { prop: 'World' } })
30 | return output
31 | })
32 | })
33 | })
34 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/toLocaleLowerCase.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string in lower case.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {string?} locale Locale.
10 | * @return {Object} Returns the updated object.
11 | * @example toLocaleLowerCase({ nested: { a: 'ÇA vA Bien ?' } }, 'fr_fr') // => { nested: { a: 'ça va bien ?' } }
12 | * @see {@link https://mdn.io/String.prototype.toLocaleLowerCase|String.prototype.toLocaleLowerCase} for more information.
13 | * @since 1.0.0
14 | */
15 | const toLocaleLowerCase = applyStringMethod(String.prototype.toLocaleLowerCase)
16 |
17 | export { toLocaleLowerCase }
18 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/toLocaleLowerCase.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { toLocaleLowerCase } from 'string'
4 | describe('string.toLocaleLowerCase', () => {
5 | it('should replace capitals by lower case letters', () => {
6 | immutaTest({ nested: { prop: 'ÇA vA Bien ?' } }, ['nested.prop'], (input, path) => {
7 | const output = toLocaleLowerCase(input, path, 'fr-FR')
8 | expect(output).toEqual({ nested: { prop: 'ça va bien ?' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/toLocaleUpperCase.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string in upper case.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @param {string?} locale Locale.
10 | * @return {Object} Returns the updated object.
11 | * @example toLocaleUpperCase({ nested: { a: 'çA vA Bien ?' } }, 'fr_fr') // => { nested: { a: 'ÇA VA BIEN ?' } }
12 | * @see {@link https://mdn.io/String.prototype.toLocaleUpperCase|String.prototype.toLocaleUpperCase} for more information.
13 | * @since 1.0.0
14 | */
15 | const toLocaleUpperCase = applyStringMethod(String.prototype.toLocaleUpperCase)
16 |
17 | export { toLocaleUpperCase }
18 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/toLocaleUpperCase.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { toLocaleUpperCase } from 'string'
4 | describe('string.toLocaleUpperCase', () => {
5 | it('should replace lower case letters by capitals', () => {
6 | immutaTest({ nested: { prop: 'çA vA Bien ?' } }, ['nested.prop'], (input, path) => {
7 | const output = toLocaleUpperCase(input, path, 'fr-FR')
8 | expect(output).toEqual({ nested: { prop: 'ÇA VA BIEN ?' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/toLowerCase.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string in lower case.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @return {Object} Returns the updated object.
10 | * @example toLowerCase({ nested: { a: 'Hello WORLD !' } }) // => { nested: { a: 'hello world !' } }
11 | * @see {@link https://mdn.io/String.prototype.toLowerCase|String.prototype.toLowerCase} for more information.
12 | * @since 1.0.0
13 | */
14 | const toLowerCase = applyStringMethod(String.prototype.toLowerCase, { fixedArity: true })
15 |
16 | export { toLowerCase }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/toLowerCase.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { toLocaleLowerCase } from 'string'
4 | describe('string.toLowerCase', () => {
5 | it('should replace capitals by lower case letters', () => {
6 | immutaTest({ nested: { prop: 'Hello WORLD !' } }, ['nested.prop'], (input, path) => {
7 | const output = toLocaleLowerCase(input, path)
8 | expect(output).toEqual({ nested: { prop: 'hello world !' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/toUpperCase.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string in upper case.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @return {Object} Returns the updated object.
10 | * @example toUpperCase({ nested: { a: 'Hello world !' } }) // => { nested: { a: 'HELLO WORLD !' } }
11 | * @see {@link https://mdn.io/String.prototype.toUpperCase|String.prototype.toUpperCase} for more information.
12 | * @since 1.0.0
13 | */
14 | const toUpperCase = applyStringMethod(String.prototype.toUpperCase, { fixedArity: true })
15 |
16 | export { toUpperCase }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/toUpperCase.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { toUpperCase } from 'string'
4 | describe('string.toUpperCase', () => {
5 | it('should replace lower case letters by capitals', () => {
6 | immutaTest({ nested: { prop: 'Hello world !' } }, ['nested.prop'], (input, path) => {
7 | const output = toUpperCase(input, path)
8 | expect(output).toEqual({ nested: { prop: 'HELLO WORLD !' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/trim.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string stripped of whitespaces at start and end.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @return {Object} Returns the updated object.
10 | * @example trim({ nested: { a: ' Hello world ! ' } }) // => { nested: { a: 'Hello world !' } }
11 | * @see {@link https://mdn.io/String.prototype.trim|String.prototype.trim} for more information.
12 | * @since 1.0.0
13 | */
14 | const trim = applyStringMethod(String.prototype.trim, { fixedArity: true })
15 |
16 | export { trim }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/trim.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { trim } from 'string'
4 | describe('string.trim', () => {
5 | it('should strip whitespaces at start and end', () => {
6 | immutaTest({ nested: { prop: ' Hello World ! ' } }, ['nested.prop'], (input, path) => {
7 | const output = trim(input, path)
8 | expect(output).toEqual({ nested: { prop: 'Hello World !' } })
9 | return output
10 | })
11 | })
12 | })
13 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/trimEnd.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string stripped of whitespaces at end.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @return {Object} Returns the updated object.
10 | * @example trimEnd({ nested: { a: ' Hello world ! ' } }) // => { nested: { a: ' Hello world !' } }
11 | * @see {@link https://mdn.io/String.prototype.trimEnd|String.prototype.trimEnd} for more information.
12 | * @since 1.0.0
13 | */
14 | const trimEnd = applyStringMethod(String.prototype.trimEnd, { fixedArity: true })
15 |
16 | export { trimEnd, trimEnd as trimRight }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/trimEnd.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { trimEnd } from 'string'
4 |
5 | describe('string.trimEnd', () => {
6 | it('should strip whitespaces at end', () => {
7 | immutaTest({ nested: { prop: ' Hello World ! ' } }, ['nested.prop'], (input, path) => {
8 | const output = trimEnd(input, path)
9 | expect(output).toEqual({ nested: { prop: ' Hello World !' } })
10 | return output
11 | })
12 | })
13 | })
14 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/trimStart.js:
--------------------------------------------------------------------------------
1 | import { applyStringMethod } from './applyStringMethod'
2 |
3 | /**
4 | * Replaces by former string stripped of whitespaces at start.
5 | * @function
6 | * @memberof string
7 | * @param {Object} object The object to modify.
8 | * @param {Array|string} path The path of the property to set.
9 | * @return {Object} Returns the updated object.
10 | * @example trimStart({ nested: { a: ' Hello world ! ' } }) // => { nested: { a: 'Hello world ! ' } }
11 | * @see {@link https://mdn.io/String.prototype.trimStart|String.prototype.trimStart} for more information.
12 | * @since 1.0.0
13 | */
14 | const trimStart = applyStringMethod(String.prototype.trimStart, { fixedArity: true })
15 |
16 | export { trimStart, trimStart as trimLeft }
17 |
--------------------------------------------------------------------------------
/packages/immutadot/src/string/trimStart.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { immutaTest } from 'test.utils'
3 | import { trimStart } from 'string'
4 |
5 | describe('string.trimStart', () => {
6 | it('should strip whitespaces at start', () => {
7 | immutaTest({ nested: { prop: ' Hello World ! ' } }, ['nested.prop'], (input, path) => {
8 | const output = trimStart(input, path)
9 | expect(output).toEqual({ nested: { prop: 'Hello World ! ' } })
10 | return output
11 | })
12 | })
13 | })
14 |
--------------------------------------------------------------------------------
/packages/immutadot/src/util/array.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Flattens an array.
3 | * @param {Array} arr The array to flatten.
4 | * @returns {Array} The flattened array.
5 | * @memberof util
6 | * @private
7 | * @since 1.0.0
8 | */
9 | export function flatten(arr) {
10 | return arr.reduce(
11 | (flat, val) => Array.isArray(val) ? flat.concat(val) : [...flat, val],
12 | [],
13 | )
14 | }
15 |
--------------------------------------------------------------------------------
/packages/immutadot/src/util/array.spec.js:
--------------------------------------------------------------------------------
1 | /* eslint-env jest */
2 | import { flatten } from './array'
3 |
4 | describe('Array utils', () => {
5 | describe('util.flatten', () => {
6 | it('should flatten array', () => {
7 | expect(flatten([
8 | [
9 | 'a',
10 | 'b',
11 | ],
12 | 'c', [
13 | 'd',
14 | 'e',
15 | ],
16 | ])).toEqual([
17 | 'a',
18 | 'b',
19 | 'c',
20 | 'd',
21 | 'e',
22 | ])
23 | })
24 | })
25 | })
26 |
--------------------------------------------------------------------------------
/packages/immutadot/src/util/doc.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Util functions.
3 | * @namespace util
4 | * @private
5 | * @since 0.1.13
6 | */
7 |
--------------------------------------------------------------------------------
/tslint.json:
--------------------------------------------------------------------------------
1 | {
2 | "defaultSeverity": "error",
3 | "extends": [
4 | "tslint:recommended"
5 | ],
6 | "jsRules": {},
7 | "rules": {
8 | "semicolon": [true, "never"]
9 | },
10 | "rulesDirectory": []
11 | }
--------------------------------------------------------------------------------