Comparing version 2.0.1 to 2.0.2
{ | ||
"name": "dpath", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Sugar for myString.split('.'); Use: dp('a.b.c'); // => ['a','b','c']", | ||
@@ -5,0 +5,0 @@ "main": "source/index.js", |
# Dot Path | ||
Sugar for `myString.split('.');` Use: `dp('a.b.c'); // => ['a','b','c']` | ||
FP sugar for immutables, using dot path syntax for deep property access. | ||
## Why? | ||
Immutable.js is great, but the API is a bit awkward -- particularly the array-style path notation. These utilities provide functional programming sugar for working with the immutable.js API. | ||
So instead of `i.setIn(['a', 'b', 'c'], value);` you can do `set('a.b.c', i, value);`. | ||
Seems like a small thing, but with curry, you can do: `const setUserRole = set('user.role', i);` and then later `setUserRole('enterprise');` -- which is great for repeated set calls, such as in Redux actions, for instance. | ||
If you want to use FP pipes (lodash `flow`, etc...) it gets even better. Normally immutables force you out of point-free style, like this: | ||
```js | ||
const pipeline = pipe( | ||
getImmutable, | ||
immutable => immutable.getIn(['a', 'b', 'c']) | ||
map(doSomethingWithABC) | ||
); | ||
``` | ||
With dpath, you can do this, instead, and stay point-free: | ||
```js | ||
const pipeline = pipe( | ||
getImmutable, | ||
get('a.b.c') | ||
map(doSomethingWithABC) | ||
); | ||
``` | ||
## Install | ||
@@ -11,3 +40,3 @@ | ||
## Use | ||
## dp(path: String, delimiter: String) => Array | ||
@@ -22,3 +51,3 @@ ``` | ||
## Use with Immutable.js | ||
### Use with Immutable.js | ||
@@ -32,1 +61,28 @@ ``` | ||
``` | ||
## get(path: String, i: Immutable) => value: Any | ||
Deep property get for the Immutable.js API. | ||
```js | ||
const i = Immutable.fromJS({ | ||
a: { | ||
b: { c: 'c' } | ||
} | ||
}); | ||
get('a.b.c', i); // => 'c' | ||
``` | ||
## set(path: String, i: Immutable, val: Any) => Immutable | ||
Deep property set for the Immutable.js API. | ||
```js | ||
const i = Immutable.fromJS({ | ||
a: { | ||
b: { c: 'c' } | ||
} | ||
}); | ||
const newState = set('a.b.c', i, 23); | ||
get('a.b.c', newState); // => 23 | ||
``` |
@@ -5,3 +5,3 @@ /** | ||
// dp(path: String, delimiter: String) => Array | ||
const dp = (input, delimiter) => { | ||
var dp = function dp (input, delimiter) { | ||
var d = delimiter || '.'; | ||
@@ -11,12 +11,15 @@ return input.split(d); | ||
dp.dp = dp; | ||
// get(path: String, i: Immutable) => value: Any | ||
const get = (path, i) => i.getIn(dp(path)); | ||
dp.get = function get (path, i) { | ||
return i.getIn(dp(path)); | ||
}; | ||
// set(path: String, i: Immutable, val: Any) => Immutable | ||
const set = (path, i, val) => i.setIn(dp(path), val); | ||
dp.set = function set (path, i, val) { | ||
return i.setIn(dp(path), val); | ||
}; | ||
module.exports = dp; | ||
module.exports.dp = dp; | ||
module.exports.default = dp; | ||
module.exports.get = get; | ||
module.exports.set = set; |
10950
57
86