immutable-deep-update
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "immutable-deep-update", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"main": "src/index.js", | ||
@@ -8,4 +8,4 @@ "author": "Gabriel Vergnaud", | ||
"scripts": { | ||
"start": "node examples/index.js", | ||
"test": "mocha" | ||
"start": "node examples/index.js", | ||
"test": "mocha" | ||
}, | ||
@@ -17,4 +17,4 @@ "dependencies": { | ||
"expect": "^1.20.2", | ||
"mocha": "^3.3.0" | ||
"mocha": "^3.4.1" | ||
} | ||
} |
@@ -91,3 +91,3 @@ # Immutable Deep Update | ||
- `'aze.rty'` => compose (prop 'aze') (prop 'rty') | ||
- `'aze['rty']'` => compose (prop 'aze') (prop 'rty') | ||
- `'aze["rty"]'` => compose (prop 'aze') (prop 'rty') | ||
- `'0'` => index 0 | ||
@@ -94,0 +94,0 @@ - `'[0]'` => index 0 |
@@ -47,5 +47,2 @@ const curry = require('lodash/fp/curry') | ||
const intValue = parseInt(strFragment) | ||
if (intValue == strFragment) return { type: Token.Num, value: intValue } | ||
const [__, propertyName] = (strFragment.match(squareBracketsPropertyRegexp) || []) | ||
@@ -52,0 +49,0 @@ if (propertyName) return { type: Token.Prop, value: propertyName.replace(quotesRegexp, '') } |
@@ -12,2 +12,4 @@ const compose = require('lodash/fp/compose') | ||
const setAtIndex = (index, value, arr) => [ ...arr.slice(0, index), value, ...arr.slice(index + 1) ] | ||
const composeLenses = (...lenses) => curry((point, extract, x) => | ||
@@ -33,4 +35,3 @@ lenses.map(l => l(point, extract)).reduceRight((acc, l) => l(acc), x)) | ||
const createLens = curry((getter, setter, key, point, extract, f, obj) => | ||
map(value => setter(key, value, obj), f(getter(key, obj))) | ||
) | ||
map(value => setter(key, value, obj), f(getter(key, obj)))) | ||
@@ -48,9 +49,10 @@ // identityLens :: (DataStructure s, Functor f) => _ -> _ -> f -> s k a -> f (s k b) | ||
(key, obj) => obj[key], | ||
(key, value, obj) => Object.assign({}, obj, { | ||
[key]: value | ||
}) | ||
(key, value, obj) => Array.isArray(obj) | ||
? setAtIndex(key, value, obj) | ||
: Object.assign({}, obj, { [key]: value }) | ||
) | ||
// lensProps :: (DataStructure (Object s), Functor f) => [String] -> _ -> _ -> f -> s String a -> f (s String b) | ||
const lensProps = (...keys) => composeLenses(...keys.map(key => lensProp(key))) | ||
const lensProps = (...keys) => | ||
composeLenses(...keys.map(key => lensProp(key))) | ||
@@ -67,10 +69,12 @@ | ||
(index, arr) => arr[index], | ||
(index, value, arr) => [ ...arr.slice(0, index), value, ...arr.slice(index + 1) ] | ||
setAtIndex | ||
) | ||
// mapped :: (DataStructure [s], Functor f) => (c -> f c) -> (f d -> d) -> (a -> f b) -> s k a -> f (s k b) | ||
const mapped = curry((point, extract, f, xs) => point(map(compose(extract, f), xs))) | ||
const mapped = curry((point, extract, f, xs) => | ||
point(map(compose(extract, f), xs))) | ||
// mapped :: (DataStructure (Object s), Functor f) => (c -> f c) -> (f d -> d) -> (a -> f b) -> s k a -> f (s k b) | ||
const mappedValues = curry((point, extract, f, obj) => point(mapValues(compose(extract, f), obj))) | ||
const mappedValues = curry((point, extract, f, obj) => | ||
point(mapValues(compose(extract, f), obj))) | ||
@@ -86,9 +90,12 @@ | ||
// view :: Lens s k -> s k a -> a | ||
const view = curry((lens, x) => compose(getConst, lens(Const.of, getConst, Const.of))(x)) | ||
const view = curry((lens, x) => | ||
compose(getConst, lens(Const.of, getConst, Const.of))(x)) | ||
// over :: Lens s k -> (a -> b) -> s k a -> s k b | ||
const over = curry((lens, f, x) => compose(runIdentity, lens(Identity.of, runIdentity, compose(Identity.of, f)))(x)) | ||
const over = curry((lens, f, x) => | ||
compose(runIdentity, lens(Identity.of, runIdentity, compose(Identity.of, f)))(x)) | ||
// set :: Lens s k -> b -> s k a -> s k b | ||
const set = curry((lens, v, x) => over(lens, () => v, x)) | ||
const set = curry((lens, v, x) => | ||
over(lens, () => v, x)) | ||
@@ -95,0 +102,0 @@ |
@@ -141,2 +141,16 @@ const expect = require('expect') | ||
it('should understand `xxx.0.yyy` syntax to access an index or a property in function of the type', () => { | ||
expect(over('friends.0.location.city', city => city + ' City', user)).toEqual({ | ||
firstname: 'Han', | ||
location: { city: 'Paris' }, | ||
friends: [ | ||
{ fisrtname: 'Luke', location: { city: 'New York City' } }, | ||
{ fisrtname: 'Darth Vador', location: { city: 'Dark star' } } | ||
] | ||
}) | ||
const obj = { prop: { '123': { name: 'lol' } } } | ||
expect(set('prop.123.name', 'Gab', obj)).toEqual({ prop: { '123': { name: 'Gab' } } }) | ||
}) | ||
it('should understand `xxx[0].yyy` syntax to access an index', () => { | ||
@@ -143,0 +157,0 @@ |
Sorry, the diff of this file is not supported yet
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
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
29666
12
481