simple-update-in
Advanced tools
Comparing version 1.2.1-master.f2ba331 to 1.3.0
@@ -9,2 +9,8 @@ # Changelog | ||
## [1.3.0] - 2018-08-17 | ||
### Added | ||
- Support predicate function | ||
- `updateIn([1, 2, 3, 4, 5], [v => v % 2], v => v * 10)` will return `[10, 2, 30, 4, 50]` | ||
- Predicate function can be used as branching function to update multiple subtrees in a single call | ||
## [1.2.0] - 2018-04-14 | ||
@@ -11,0 +17,0 @@ ### Added |
@@ -29,3 +29,23 @@ 'use strict'; | ||
var accessor = path.shift(); | ||
if (typeof accessor === 'function') { | ||
if (Array.isArray(obj)) { | ||
obj.forEach(function (value, index) { | ||
if (accessor.call(obj, value, index)) { | ||
obj = setIn(obj, [index].concat(_toConsumableArray(path)), updater); | ||
} | ||
}); | ||
} else { | ||
Object.keys(obj).forEach(function (key) { | ||
if (accessor.call(obj, obj[key], key)) { | ||
obj = setIn(obj, [key].concat(_toConsumableArray(path)), updater); | ||
} | ||
}); | ||
} | ||
return obj; | ||
} | ||
var value = typeof obj !== 'undefined' && obj[accessor]; | ||
var nextObj = obj; | ||
@@ -32,0 +52,0 @@ |
{ | ||
"name": "simple-update-in", | ||
"version": "1.2.1-master.f2ba331", | ||
"version": "1.3.0", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"files": "lib/**/*", | ||
"files": [ | ||
"lib/**/*" | ||
], | ||
"scripts": { | ||
@@ -33,10 +35,7 @@ "build": "babel --out-dir lib src --ignore **/*.spec.js,**/*.test.js", | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-stage-3": "^6.24.1", | ||
"gulp": "^3.9.1", | ||
"gulp-exec": "^3.0.1", | ||
"jest": "^22.4.2", | ||
"last-commit-log": "^1.0.3" | ||
"jest": "^22.4.3" | ||
} | ||
} |
@@ -5,3 +5,3 @@ # simple-update-in | ||
[![Build Status](https://travis-ci.org/compulim/simple-update-in.svg?branch=master)](https://travis-ci.org/compulim/simple-update-in) | ||
[![npm version](https://badge.fury.io/js/simple-update-in.svg)](https://badge.fury.io/js/simple-update-in) [![Build Status](https://travis-ci.org/compulim/simple-update-in.svg?branch=master)](https://travis-ci.org/compulim/simple-update-in) | ||
@@ -161,2 +161,26 @@ We love [ImmutableJS](https://facebook.github.io/immutable-js). But sometimes, we want to start something from small. Thus, we created this package with zero dependencies. | ||
## Using predicate | ||
For path accessor, instead of `number` and `string`, you can also use `function`. | ||
Predicate for array has signature of `(value, index) => truthy/falsy`. And for map, `(value, key) => truthy/falsy`. | ||
```js | ||
const from = [1, 2, 3, 4, 5]; | ||
const actual = updateIn(from, [value => value % 2], value => value * 10); | ||
expect(actual).toEqual([10, 2, 30, 4, 50]); | ||
``` | ||
### Branching with predicate | ||
You can also use predicate to update multiple subsets at the same time. | ||
```js | ||
const from = [{ v: 1 }, { v: 2 }, { v: 3 }]; | ||
const actual = updateIn(from, [() => true, 'v'], v => v * 10); | ||
expect(actual).toEqual([{ v: 10 }, { v: 20 }, { v: 30 }]); | ||
``` | ||
# Contributions | ||
@@ -163,0 +187,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5
0
191
0
12384
5
81
1