pure-assign
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -6,3 +6,4 @@ /** | ||
* @param baseObject An object which serves as the basis for the updated values. | ||
* @param updates An object whose values should replace the corresponding values in `baseObject`. | ||
* @param updates One or more objects whose values should replace the corresponding values | ||
* in `baseObject`. Values in rightward objects take precedence over those in earlier ones. | ||
* @returns An object whose keys and values match those of `baseObject` except where they have been | ||
@@ -12,2 +13,2 @@ * overridden by those of `updates`. The input objects are unchanged. If the returned | ||
*/ | ||
export default function pureAssign<T>(baseObject: T, updates: Partial<T>): T; | ||
export default function pureAssign<T>(baseObject: T, ...updates: Array<Partial<T>>): T; |
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
/** | ||
@@ -15,3 +7,4 @@ * Drop-in replacement for `Object.assign()` for "updating" immutable objects. Unlike | ||
* @param baseObject An object which serves as the basis for the updated values. | ||
* @param updates An object whose values should replace the corresponding values in `baseObject`. | ||
* @param updates One or more objects whose values should replace the corresponding values | ||
* in `baseObject`. Values in rightward objects take precedence over those in earlier ones. | ||
* @returns An object whose keys and values match those of `baseObject` except where they have been | ||
@@ -21,8 +14,25 @@ * overridden by those of `updates`. The input objects are unchanged. If the returned | ||
*/ | ||
function pureAssign(baseObject, updates) { | ||
for (var key in updates) { | ||
if (Object.prototype.hasOwnProperty.call(updates, key) | ||
function pureAssign(baseObject) { | ||
var updates = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
updates[_i - 1] = arguments[_i]; | ||
} | ||
if (updates.length === 0) { | ||
return baseObject; | ||
} | ||
else if (updates.length === 1) { | ||
return simplePureAssign(baseObject, updates[0]); | ||
} | ||
else { | ||
return simplePureAssign(baseObject, assign.apply(void 0, [{}].concat(updates))); | ||
} | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = pureAssign; | ||
function simplePureAssign(baseObject, update) { | ||
for (var key in update) { | ||
if (Object.prototype.hasOwnProperty.call(update, key) | ||
&& (!(key in baseObject) | ||
|| baseObject[key] !== updates[key])) { | ||
return __assign({}, baseObject, updates); | ||
|| baseObject[key] !== update[key])) { | ||
return assign({}, baseObject, update); | ||
} | ||
@@ -32,4 +42,13 @@ } | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = pureAssign; | ||
var assign = Object.assign || function (t) { | ||
for (var s = void 0, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) { | ||
if (Object.prototype.hasOwnProperty.call(s, p)) { | ||
t[p] = s[p]; | ||
} | ||
} | ||
} | ||
return t; | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "pure-assign", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Drop-in replacement for Object.assign() for \"updating\" immutable objects.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -45,5 +45,6 @@ # Pure Assign | ||
[PureComponent](https://facebook.github.io/react/docs/react-api.html#react.purecomponent), which | ||
only performs an update if its props have "changed" according to a shallow-equality check. This means | ||
that if your updates create new objects with the same values, they will re-render unnecessarily since | ||
the old props do not have object-equality with the new props, despite having the same values. | ||
only performs an update if its props have "changed" according to a shallow-equality check. This | ||
means that if your updates create new objects with the same values, they will trigger unnecessary | ||
re-renders since the old props do not have object-equality with the new props, despite having the | ||
same values. | ||
@@ -62,5 +63,27 @@ This is where `pureAssign()` comes in. `pureAssign(object, updates)` is equivalent to | ||
Like `Object.assign()`, multiple update arguments may be given, where values found in later objects | ||
take precedence over earlier ones. A new object is returned only if the final result of applying | ||
all updates has different values from the original object. For example: | ||
``` javascript | ||
import pureAssign from "pure-assign"; | ||
const userObject = { firstName: "Anastasia", lastName: "Steele" }; | ||
const updatedUserObject1 = pureAssign( | ||
userObject, | ||
{ firstName: "Christian", lastName: "Kavanagh" }, | ||
{ firstName: "Kate" } | ||
); | ||
console.log(updatedUserObject1); // { firstName: "Kate", lastName: "Kavanagh" } | ||
const updatedUserObject2 = pureAssign( | ||
userObject, | ||
{ firstName: "Ana", lastName: "Steele" }, | ||
{ firstName: "Anastasia" } | ||
); | ||
console.log(userObject === updatedObject2); // true | ||
``` | ||
For TypeScript users, `pureAssign` has an additional advantage in that it catches type errors | ||
of the following form, which would be uncaught if using `Object.assign()` or object spread: | ||
``` | ||
``` javascript | ||
import pureAssign from "pure-assign"; | ||
@@ -67,0 +90,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
10401
62
96