📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP

icepick

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

icepick - npm Package Compare versions

Comparing version

to
1.2.0

@@ -82,3 +82,7 @@ /**

}
return Object.freeze(coll);
if (typeof coll === "object") {
return Object.freeze(coll);
} else {
return coll;
}
}

@@ -140,2 +144,6 @@

exports.assoc = function assoc(coll, key, value) {
if (coll[key] === value) {
return _freeze(coll);
}
var newObj = clone(coll);

@@ -257,3 +265,3 @@

exports.assign = function assign(/*...objs*/) {
var newObj = _slice(arguments).reduce(singleAssign, {});
var newObj = rest(arguments).reduce(singleAssign, arguments[0]);

@@ -265,4 +273,3 @@ return _freeze(newObj);

return Object.keys(obj2).reduce(function (obj, key) {
obj[key] = freezeIfNeeded(obj2[key]);
return obj;
return i.assoc(obj, key, obj2[key]);
}, obj1);

@@ -272,3 +279,3 @@ }

exports.merge = merge;
function merge(target, source) {
function merge(target, source, resolver) {
if (target == null || source == null) {

@@ -281,20 +288,25 @@ return target;

var resolvedSourceVal =
resolver ? resolver(targetVal, sourceVal, key) : sourceVal;
if (weCareAbout(sourceVal) && weCareAbout(targetVal)) {
// if they are both frozen and reference equal, assume they are deep equal
if ((
(Object.isFrozen(sourceVal) && Object.isFrozen(targetVal)) ||
(Object.isFrozen(resolvedSourceVal) &&
Object.isFrozen(targetVal)) ||
process.env.NODE_ENV === "production"
) &&
sourceVal === targetVal) {
resolvedSourceVal === targetVal) {
return obj;
}
if (Array.isArray(sourceVal)) {
return i.assoc(obj, key, sourceVal);
return i.assoc(obj, key, resolvedSourceVal);
}
// recursively merge pairs of objects
return assocIfDifferent(obj, key, merge(targetVal, sourceVal));
return assocIfDifferent(obj, key,
merge(targetVal, resolvedSourceVal, resolver));
}
// primitive values, stuff with prototypes
return assocIfDifferent(obj, key, sourceVal);
return assocIfDifferent(obj, key, resolvedSourceVal);
}, target);

@@ -301,0 +313,0 @@ }

@@ -122,2 +122,8 @@ /* jshint elision:true */

it("should keep references the same if nothing changes", function () {
var o = i.freeze({a: 1});
var result = i.assoc(o, "a", 1);
expect(result).to.equal(o);
});
it("should be aliased as set", function () {

@@ -187,2 +193,8 @@ expect(i.set).to.equal(i.assoc);

});
it("should keep references the same if nothing changes", function () {
var o = i.freeze({a: {b: 1}});
var result = i.assocIn(o, ["a", "b"], 1);
expect(result).to.equal(o);
});
});

@@ -250,2 +262,8 @@

});
it("should keep references the same if nothing changes", function () {
var o = i.freeze({a: 1});
var result = i.updateIn(o, ["a", "b"], function (v) { return v; });
expect(result).to.equal(o);
});
});

@@ -381,2 +399,7 @@

it("should keep references the same if nothing changes", function () {
var o = i.freeze({a: 1});
var result = i.assign(o, {a: 1});
expect(result).to.equal(o);
});
});

@@ -439,2 +462,20 @@

describe("custom associator", function () {
it("should use the custom associator", function () {
var o1 = i.freeze({a: 1, b: {c: [1, 1]}, d: 1});
var o2 = i.freeze({a: 2, b: {c: [2]}});
function resolver(targetVal, sourceVal) {
if (Array.isArray(targetVal) && sourceVal) {
return targetVal.concat(sourceVal);
} else {
return sourceVal;
}
}
var result = i.merge(o1, o2, resolver);
expect(result).to.eql({a: 2, b: {c: [1, 1, 2]}, d: 1});
});
});
});

@@ -441,0 +482,0 @@

{
"name": "icepick",
"version": "1.1.0",
"version": "1.2.0",
"description": "Utilities for treating frozen JavaScript objects as persistent immutable collections.",

@@ -5,0 +5,0 @@ "main": "icepick.js",

# icepick [![Build Status via Travis CI](https://travis-ci.org/aearly/icepick.svg?branch=master)](https://travis-ci.org/aearly/icepick) [![NPM version](http://img.shields.io/npm/v/icepick.svg)](https://www.npmjs.org/package/icepick) [![Coverage Status](https://coveralls.io/repos/aearly/icepick/badge.svg?branch=)](https://coveralls.io/r/aearly/icepick?branch=)
Utilities for treating frozen JavaScript objects as persistent immutable collections.
Utilities for treating frozen JavaScript objects as persistent immutable collections.

@@ -203,9 +203,9 @@ ## Motivation

### merge(target, source)
### merge(target, source, [associator])
Deeply merge a `source` object into `target`, similar to Lodash.merge. Child collections that are both frozen and reference equal will be asusmed to be deeply equal. Arrays from the `source` object will completely replace those in the `target` object if the two differ. If nothing changed, the original reference will not change. Returns a frozen object, and works with both unfrozen and frozen objects.
Deeply merge a `source` object into `target`, similar to Lodash.merge. Child collections that are both frozen and reference equal will be assumed to be deeply equal. Arrays from the `source` object will completely replace those in the `target` object if the two differ. If nothing changed, the original reference will not change. Returns a frozen object, and works with both unfrozen and frozen objects.
```javascript
var defaults = {a: 1, c: {d: 1, e: [1, 2, 3], f: {g: 1}};
var obj = {c: {d: 2, e: [2], f: null};
var defaults = {a: 1, c: {d: 1, e: [1, 2, 3], f: {g: 1}}};
var obj = {c: {d: 2, e: [2], f: null}};

@@ -221,2 +221,22 @@ var result1 = i.merge(defaults, obj); // {a: 1, c: {d: 2, e: [2]}, f: null}

An optional `resolver` function can be given as the third argument to change the way values are merged. For example, if you'd prefer that Array values from source be concatenated to target (instead of the source Array just replacing the target Array):
```javascript
var o1 = i.freeze({a: 1, b: {c: [1, 1]}, d: 1});
var o2 = i.freeze({a: 2, b: {c: [2]}});
function resolver(targetVal, sourceVal, key) {
if (Array.isArray(targetVal) && sourceVal) {
return targetVal.concat(sourceVal);
} else {
return sourceVal;
}
}
var result3 = i.merge(o1, o2, resolver);
assert(result === {a: 2, b: {c: [1, 1, 2]}, d: 1});
```
The `resolver` function receives three arguments: the value from the target object, the value from the source object, and the key of the value being merged.
### Array.prototype methods

@@ -264,3 +284,3 @@

arr.find(function (item) { return item.b != null; }); // {b: 2}
arr.find(function (item) { return item.b != null; }); // {b: 2}
```

@@ -305,3 +325,3 @@

[`seamless-immutable`](https://github.com/rtfeldman/seamless-immutable) is the most similar to `icepick`. Its main difference is that it adds more methods to the prototypes of objects, and overrides array built-ins like `map` and `filter` to return frozen objects. It also adds a couple utility functions, like `asMutable` and `merge`. `icepick` does not modify the the methods or properties of collections in order to function, it merely provides a set of functions to operate on them, similar to Lodash, Underscore, or Ramda. This means that when passing frozen objects to third-party libraries, they will be able to `map` over them and obtain mutable arrays. `seamless-immutable` handles `Date`s, which `icepick` leaves as-is currently (as well as any other objects with custom constructors). `icepick` will detect circular references within an object and throw an Error, `seamless-immutable` will run into infinite recursion in such a case.
[`seamless-immutable`](https://github.com/rtfeldman/seamless-immutable) is the most similar to `icepick`. Its main difference is that it adds methods to the prototypes of objects, and overrides array built-ins like `map` and `filter` to return frozen objects. It also adds a couple utility functions, like `asMutable` and `merge`. `icepick` does not modify the the methods or properties of collections in order to function, it merely provides a set of functions to operate on them, similar to Lodash, Underscore, or Ramda. This means that when passing frozen objects to third-party libraries, they will be able to `map` over them and obtain mutable arrays. `seamless-immutable` handles `Date`s, which `icepick` leaves as-is currently (as well as any other objects with custom constructors). `icepick` will detect circular references within an object and throw an Error, `seamless-immutable` will run into infinite recursion in such a case.

@@ -308,0 +328,0 @@ I also would like to benchmark all of these libraries to see in what cases each is faster.