内容简介:immutability-util:一个开源 immutable data 的轮子
immutability-util
Mutate a copy of data without changing the original source. Inspired by kolodny/immutability-helper / ProtoTeam/immutability-helper-x and rewrite with ES6 syntax.
1. Features
Features or key points.
- Use chainble API to mutate a copy of object.
- Simplify your code by finding target to be updated with path string .
- Maybe can optimize your web app with a bit higher performance .
2. Install & Usage
Install with NPM.
npm install -S immutability-util
Then use it.
const iu = require('immutability-util'); // or import iu from 'immutability-util'; // obj need to be mutated. var obj = { a: 1, b: 2, c: { d: 3, e: { f: [4, 5, 6], g: { h: 'iu', }, }, i: { j: 'hello, world.', k: [7, 8, 9], l: [10, 11, 12], } }, }; // chainable usage. const state = iu(state) .$apply(['a'], v => v + 1) .$merge(['c', 'e', 'g'], { m: 'update'}) .$push(['c', 'e', 'f'], [7, 8, 9]) .$set(['c', 'i', 'j'], 'hello node 8.') .$splice(['c', 'i', 'k'], [1, 1, 10]) .$unset(['c'], ['d']) .$unshift(['c', 'i', 'l'], [13]) .value(); // then get the mutated copy. // or use path string. iu(obj).$set('c.i.j', 'hello node 8.').value();
And process array like this:
const obj = { a: { b: [{ c: [1, 2, 3], }, { d: 4, }, { e: [5, 6], }] } }; const state = iu(obj) .$apply('a.b[1].d', v => v + 1) .$push('a.b[0].c', [4]) .$set('a.b[2].e[0]', 'hello node 8.') .value();
3. Available API
After got the instance of ImmutabilityUtil
, you can use the chainable methods below.
-
$apply(path, function)
: passes in the current value to the function and updates it with the new returned value. -
$merge(path, object)
: merge the keys of object with the target. -
$push(path, array)
: push() all the items in array on the target. -
$set(path, any)
: replace the target entirely. -
$splice(path, array)
: call splice() with the array on the target with the parameters provided by the item. -
$unset(path, array_of_strings)
: remove the list of keys in array from the target object. -
$unshift(path, array)
: unshift() all the items in array on the target.
update()
, and put spec
into it, just like immutability-helper.
Then you can use API value()
to get the immutable data copy.
Your pull requests are needed for more API.
4. Build & Test
You can develop and test as below.
npm run build # run the testcases npm run test
You can run npm run benchmark
to get the comparison of performance.
5. License
MIT@ hustcc .
以上所述就是小编给大家介绍的《immutability-util:一个开源 immutable data 的轮子》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 开源一个顺手的golang web开发轮子fastgo
- 请继续重复发明轮子
- 造一个「轮子」musionUI
- 造轮子-golang日志系统
- 造轮子 | golang | 单元测试
- ButterKnife源码拆轮子学习
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
微信公众号深度解析
魏艳 / 化学工业出版社 / 2017-5 / 49.80元
本书是一本微信公众号营销的教科书,全方位揭秘了微信订阅号、微信服务号、微信企业号三大类型账号的运营管理策略和技巧,有助于企业构建一套全新的微信公众号营销体系,打造一个移动端的商业帝国,是企业和微商必读的微信公众号营销和运营宝典。 《微信公众号深度解析:订阅号+服务号+企业号三号运营全攻略》突出了“新”、“全”、“实战”三大特点,阐述了微信公众号在新形势下的现状、发展趋势和三大类型;微信公众号......一起来看看 《微信公众号深度解析》 这本书的介绍吧!