Skip to content

Commit

Permalink
feat: added filter() operation
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-kovalev committed Nov 1, 2019
1 parent 819b9ca commit 215e322
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/operations/filter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { updateArray } = require('../common');

module.exports = filter;

/**
* Filters elements of array-item selected by path using provided predicate
* @param {Object | any[]} object object to update
* @param {string | string[]} path path to be updated
* @param {function} predicate filter predicate
* @returns {Object | any[]} updated object
* @description one of additional functions to work with array items
* @docs
* ```js
* import { filter } from 'immutable-object-update';
*
* const state = {
* a: {
* b: [ 1, 2, 3, 4, 5 ]
* }
* };
* const isOdd = item => item % 2;
*
* const updated = filter(state, [ 'a', 'b' ], isOdd);
*
* // or
*
* const updated = filter(state, 'a.b', isOdd);
* ```
*
* As a result we will receive new object with structure below:
*
* ```js
* {
* a: {
* b: [ 1, 3, 5 ]
* }
* }
* ```
*/
function filter(object, path, predicate) {
return updateArray(object, path, array => array.filter(predicate));
}
4 changes: 3 additions & 1 deletion src/operations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const unshift = require('./unshift');
const unshiftAll = require('./unshiftAll');
const insert = require('./insert');
const insertAll = require('./insertAll');
const filter = require('./filter');

module.exports = {
update,
Expand All @@ -27,5 +28,6 @@ module.exports = {
unshift,
unshiftAll,
insert,
insertAll
insertAll,
filter
};

0 comments on commit 215e322

Please sign in to comment.