Skip to content

Commit

Permalink
✨ Add flat and flatMap fix #321 (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Jan 24, 2019
1 parent c2d8ff7 commit 551e60d
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:

lint:
docker:
- image: circleci/node:10
- image: circleci/node:11
steps:
- checkout
- restore_cache:
Expand All @@ -18,7 +18,7 @@ jobs:

test:
docker:
- image: circleci/node:10
- image: circleci/node:11
steps:
- checkout
- restore_cache:
Expand All @@ -32,7 +32,7 @@ jobs:

build:
docker:
- image: circleci/node:10
- image: circleci/node:11
steps:
- checkout
- restore_cache:
Expand Down
16 changes: 16 additions & 0 deletions packages/immutadot/src/array/flat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { applyArrayMethod } from './applyArrayMethod'

/**
* Replaces an array by a new array with all sub-array elements concatenated into it recursively up to the specified depth.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {number} [depth] Depth level.
* @return {Object} Returns the updated object.
* @see {@link https://mdn.io/Array.prototype.flat|Array.prototype.flat} for more information.
* @since 2.0.0
*/
const flat = applyArrayMethod(Array.prototype.flat, { mutating: false })

export { flat }
30 changes: 30 additions & 0 deletions packages/immutadot/src/array/flat.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-env jest */
import { flat } from 'array'
import { immutaTest } from 'test.utils'
describe('array.map', () => {
it('should map elements and flatten', () => {
immutaTest({
nested: {
prop: [
[1, 2],
[3, 4],
],
},
other: {},
}, ['nested.prop'], (input, path) => {
const output = flat(input, path)
expect(output).toEqual({
nested: {
prop: [
1,
2,
3,
4,
],
},
other: {},
})
return output
})
})
})
16 changes: 16 additions & 0 deletions packages/immutadot/src/array/flatMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { applyArrayMethod } from './applyArrayMethod'

/**
* Replaces an array mapping each element using a mapping function, then flattening the result into a new array.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {function} callback The function invoked per iteration.
* @return {Object} Returns the updated object.
* @see {@link https://mdn.io/Array.prototype.flatMap|Array.prototype.flatMap} for more information.
* @since 2.0.0
*/
const flatMap = applyArrayMethod(Array.prototype.flatMap, { mutating: false })

export { flatMap }
30 changes: 30 additions & 0 deletions packages/immutadot/src/array/flatMap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-env jest */
import { flatMap } from 'array'
import { immutaTest } from 'test.utils'
describe('array.map', () => {
it('should map elements and flatten', () => {
immutaTest({
nested: {
prop: [
{ arr: [1, 2] },
{ arr: [3, 4] },
],
},
other: {},
}, ['nested.prop'], (input, path) => {
const output = flatMap(input, path, ({ arr }) => arr)
expect(output).toEqual({
nested: {
prop: [
1,
2,
3,
4,
],
},
other: {},
})
return output
})
})
})
2 changes: 2 additions & 0 deletions packages/immutadot/src/array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export { concat } from './concat'
export { copyWithin } from './copyWithin'
export { fill } from './fill'
export { filter } from './filter'
export { flat } from './flat'
export { flatMap } from './flatMap'
export { map } from './map'
export { pop } from './pop'
export { push } from './push'
Expand Down
2 changes: 2 additions & 0 deletions packages/immutadot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export {
copyWithin,
fill,
filter,
flat,
flatMap,
map,
pop,
push,
Expand Down

0 comments on commit 551e60d

Please sign in to comment.