Comparing version 1.2.5 to 1.3.0
# Changelog | ||
## 1.3.0 (October 3, 2017) | ||
* Add **update()** [#12, @dentrado] | ||
## 1.2.5 (April 2, 2017) | ||
@@ -4,0 +8,0 @@ |
@@ -20,2 +20,3 @@ 'use strict'; | ||
exports.setIn = setIn; | ||
exports.update = update; | ||
exports.updateIn = updateIn; | ||
@@ -234,3 +235,3 @@ exports.merge = merge; | ||
// -- Returns a new array obtained by replacing an item at | ||
// -- a specified index. If the provided item is the same | ||
// -- a specified index. If the provided item is the same as | ||
// -- (*referentially equal to*) the previous item at that position, | ||
@@ -254,3 +255,2 @@ // -- the original array is returned. | ||
if (array[idx] === newItem) return array; | ||
var len = array.length; | ||
@@ -302,3 +302,3 @@ var result = Array(len); | ||
// -- Returns a new object with a modified attribute. | ||
// -- If the provided value is the same (*referentially equal to*) | ||
// -- If the provided value is the same as (*referentially equal to*) | ||
// -- the previous value, the original object is returned. | ||
@@ -333,3 +333,3 @@ // -- | ||
// -- | ||
// -- * If the provided value is the same (*referentially equal to*) | ||
// -- * If the provided value is the same as (*referentially equal to*) | ||
// -- the previous value, the original object is returned. | ||
@@ -383,6 +383,32 @@ // -- * If the path does not exist, it will be created before setting | ||
// -- #### update() | ||
// -- Returns a new object with a modified attribute, | ||
// -- calculated via a user-provided callback based on the current value. | ||
// -- If the calculated value is the same as (*referentially equal to*) | ||
// -- the previous value, the original object is returned. | ||
// -- | ||
// -- Usage: `update<T: ArrayOrObject>(obj: T, key: Key, | ||
// -- fnUpdate: (prevValue: any) => any): T` | ||
// -- | ||
// -- ```js | ||
// -- obj = { a: 1, b: 2, c: 3 } | ||
// -- obj2 = update(obj, 'b', (val) => val + 1) | ||
// -- // { a: 1, b: 3, c: 3 } | ||
// -- obj2 === obj | ||
// -- // false | ||
// -- | ||
// -- // The same object is returned if there are no changes: | ||
// -- update(obj, 'b', (val) => val) === obj | ||
// -- // true | ||
// -- ``` | ||
function update(obj, key, fnUpdate) { | ||
var prevVal = obj == null ? undefined : obj[key]; | ||
var nextVal = fnUpdate(prevVal); | ||
return set(obj, key, nextVal); | ||
} | ||
// -- #### updateIn() | ||
// -- Returns a new object with a modified **nested** attribute, | ||
// -- calculated via a user-provided callback based on the current value. | ||
// -- If the calculated value is the same (*referentially equal to*) | ||
// -- If the calculated value is the same as (*referentially equal to*) | ||
// -- the previous value, the original object is returned. | ||
@@ -389,0 +415,0 @@ // -- |
@@ -1,2 +0,2 @@ | ||
"use strict";function throwStr(e){throw new Error(e)}function clone(e){if(Array.isArray(e))return e.slice();for(var r=Object.keys(e),t={},n=0;n<r.length;n++){var o=r[n];t[o]=e[o]}return t}function doMerge(e,r){var t=r;!(null!=t)&&throwStr(INVALID_ARGS);for(var n=!1,o=arguments.length,a=Array(o>2?o-2:0),s=2;o>s;s++)a[s-2]=arguments[s];for(var u=0;u<a.length;u++){var l=a[u];if(null!=l){var i=Object.keys(l);if(i.length)for(var c=0;c<=i.length;c++){var f=i[c];if(!e||void 0===t[f]){var d=l[f];void 0!==d&&d!==t[f]&&(n||(n=!0,t=clone(t)),t[f]=d)}}}}return t}function isObject(e){var r="undefined"==typeof e?"undefined":_typeof(e);return null!=e&&("object"===r||"function"===r)}function addLast(e,r){return e.concat(Array.isArray(r)?r:[r])}function addFirst(e,r){return Array.isArray(r)?r.concat(e):[r].concat(e)}function removeLast(e){return e.length?e.slice(0,e.length-1):e}function removeFirst(e){return e.length?e.slice(1):e}function insert(e,r,t){return e.slice(0,r).concat(Array.isArray(t)?t:[t]).concat(e.slice(r))}function removeAt(e,r){return r>=e.length||0>r?e:e.slice(0,r).concat(e.slice(r+1))}function replaceAt(e,r,t){if(e[r]===t)return e;for(var n=e.length,o=Array(n),a=0;n>a;a++)o[a]=e[a];return o[r]=t,o}function getIn(e,r){if(!Array.isArray(r)&&throwStr(INVALID_ARGS),null==e)return void 0;for(var t=e,n=0;n<r.length;n++){var o=r[n];if(t=null!=t?t[o]:void 0,void 0===t)return t}return t}function set(e,r,t){var n="number"==typeof r?[]:{},o=null==e?n:e;if(o[r]===t)return o;var a=clone(o);return a[r]=t,a}function doSetIn(e,r,t,n){var o=void 0,a=r[n];if(n===r.length-1)o=t;else{var s=isObject(e)?e[a]:"number"==typeof r[n+1]?[]:{};o=doSetIn(s,r,t,n+1)}return set(e,a,o)}function setIn(e,r,t){return r.length?doSetIn(e,r,t,0):t}function updateIn(e,r,t){var n=getIn(e,r),o=t(n);return setIn(e,r,o)}function merge(e,r,t,n,o,a){for(var s=arguments.length,u=Array(s>6?s-6:0),l=6;s>l;l++)u[l-6]=arguments[l];return u.length?doMerge.call.apply(doMerge,[null,!1,e,r,t,n,o,a].concat(u)):doMerge(!1,e,r,t,n,o,a)}function mergeIn(e,r,t,n,o,a,s){var u=getIn(e,r);null==u&&(u={});for(var l=void 0,i=arguments.length,c=Array(i>7?i-7:0),f=7;i>f;f++)c[f-7]=arguments[f];return l=c.length?doMerge.call.apply(doMerge,[null,!1,u,t,n,o,a,s].concat(c)):doMerge(!1,u,t,n,o,a,s),setIn(e,r,l)}function omit(e,r){for(var t=Array.isArray(r)?r:[r],n=!1,o=0;o<t.length;o++)if(hasOwnProperty.call(e,t[o])){n=!0;break}if(!n)return e;for(var a={},s=Object.keys(e),u=0;u<s.length;u++){var l=s[u];t.indexOf(l)>=0||(a[l]=e[l])}return a}function addDefaults(e,r,t,n,o,a){for(var s=arguments.length,u=Array(s>6?s-6:0),l=6;s>l;l++)u[l-6]=arguments[l];return u.length?doMerge.call.apply(doMerge,[null,!0,e,r,t,n,o,a].concat(u)):doMerge(!0,e,r,t,n,o,a)}Object.defineProperty(exports,"__esModule",{value:!0});var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};exports.clone=clone,exports.addLast=addLast,exports.addFirst=addFirst,exports.removeLast=removeLast,exports.removeFirst=removeFirst,exports.insert=insert,exports.removeAt=removeAt,exports.replaceAt=replaceAt,exports.getIn=getIn,exports.set=set,exports.setIn=setIn,exports.updateIn=updateIn,exports.merge=merge,exports.mergeIn=mergeIn,exports.omit=omit,exports.addDefaults=addDefaults;/*! | ||
"use strict";function throwStr(e){throw new Error(e)}function clone(e){if(Array.isArray(e))return e.slice();for(var r=Object.keys(e),t={},n=0;n<r.length;n++){var o=r[n];t[o]=e[o]}return t}function doMerge(e,r){var t=r;!(null!=t)&&throwStr(INVALID_ARGS);for(var n=!1,o=arguments.length,a=Array(o>2?o-2:0),s=2;o>s;s++)a[s-2]=arguments[s];for(var u=0;u<a.length;u++){var l=a[u];if(null!=l){var i=Object.keys(l);if(i.length)for(var c=0;c<=i.length;c++){var f=i[c];if(!e||void 0===t[f]){var d=l[f];void 0!==d&&d!==t[f]&&(n||(n=!0,t=clone(t)),t[f]=d)}}}}return t}function isObject(e){var r="undefined"==typeof e?"undefined":_typeof(e);return null!=e&&("object"===r||"function"===r)}function addLast(e,r){return e.concat(Array.isArray(r)?r:[r])}function addFirst(e,r){return Array.isArray(r)?r.concat(e):[r].concat(e)}function removeLast(e){return e.length?e.slice(0,e.length-1):e}function removeFirst(e){return e.length?e.slice(1):e}function insert(e,r,t){return e.slice(0,r).concat(Array.isArray(t)?t:[t]).concat(e.slice(r))}function removeAt(e,r){return r>=e.length||0>r?e:e.slice(0,r).concat(e.slice(r+1))}function replaceAt(e,r,t){if(e[r]===t)return e;for(var n=e.length,o=Array(n),a=0;n>a;a++)o[a]=e[a];return o[r]=t,o}function getIn(e,r){if(!Array.isArray(r)&&throwStr(INVALID_ARGS),null==e)return void 0;for(var t=e,n=0;n<r.length;n++){var o=r[n];if(t=null!=t?t[o]:void 0,void 0===t)return t}return t}function set(e,r,t){var n="number"==typeof r?[]:{},o=null==e?n:e;if(o[r]===t)return o;var a=clone(o);return a[r]=t,a}function doSetIn(e,r,t,n){var o=void 0,a=r[n];if(n===r.length-1)o=t;else{var s=isObject(e)?e[a]:"number"==typeof r[n+1]?[]:{};o=doSetIn(s,r,t,n+1)}return set(e,a,o)}function setIn(e,r,t){return r.length?doSetIn(e,r,t,0):t}function update(e,r,t){var n=null==e?void 0:e[r],o=t(n);return set(e,r,o)}function updateIn(e,r,t){var n=getIn(e,r),o=t(n);return setIn(e,r,o)}function merge(e,r,t,n,o,a){for(var s=arguments.length,u=Array(s>6?s-6:0),l=6;s>l;l++)u[l-6]=arguments[l];return u.length?doMerge.call.apply(doMerge,[null,!1,e,r,t,n,o,a].concat(u)):doMerge(!1,e,r,t,n,o,a)}function mergeIn(e,r,t,n,o,a,s){var u=getIn(e,r);null==u&&(u={});for(var l=void 0,i=arguments.length,c=Array(i>7?i-7:0),f=7;i>f;f++)c[f-7]=arguments[f];return l=c.length?doMerge.call.apply(doMerge,[null,!1,u,t,n,o,a,s].concat(c)):doMerge(!1,u,t,n,o,a,s),setIn(e,r,l)}function omit(e,r){for(var t=Array.isArray(r)?r:[r],n=!1,o=0;o<t.length;o++)if(hasOwnProperty.call(e,t[o])){n=!0;break}if(!n)return e;for(var a={},s=Object.keys(e),u=0;u<s.length;u++){var l=s[u];t.indexOf(l)>=0||(a[l]=e[l])}return a}function addDefaults(e,r,t,n,o,a){for(var s=arguments.length,u=Array(s>6?s-6:0),l=6;s>l;l++)u[l-6]=arguments[l];return u.length?doMerge.call.apply(doMerge,[null,!0,e,r,t,n,o,a].concat(u)):doMerge(!0,e,r,t,n,o,a)}Object.defineProperty(exports,"__esModule",{value:!0});var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};exports.clone=clone,exports.addLast=addLast,exports.addFirst=addFirst,exports.removeLast=removeLast,exports.removeFirst=removeFirst,exports.insert=insert,exports.removeAt=removeAt,exports.replaceAt=replaceAt,exports.getIn=getIn,exports.set=set,exports.setIn=setIn,exports.update=update,exports.updateIn=updateIn,exports.merge=merge,exports.mergeIn=mergeIn,exports.omit=omit,exports.addDefaults=addDefaults;/*! | ||
* Timm | ||
@@ -3,0 +3,0 @@ * |
{ | ||
"name": "timm", | ||
"version": "1.2.5", | ||
"version": "1.3.0", | ||
"description": "Immutability helpers with fast reads and acceptable writes", | ||
@@ -5,0 +5,0 @@ "main": "lib/timm.js", |
# timm [![Build Status](https://travis-ci.org/guigrpa/timm.svg)](https://travis-ci.org/guigrpa/timm) [![Coverage Status](https://coveralls.io/repos/github/guigrpa/timm/badge.svg?branch=master)](https://coveralls.io/github/guigrpa/timm?branch=master) [![npm version](https://img.shields.io/npm/v/timm.svg)](https://www.npmjs.com/package/timm) | ||
Immutability helpers with fast reads and acceptable writes | ||
Immutability helpers with fast reads and acceptable writes ([blog post](http://guigrpa.github.io/2016/06/16/painless-immutability/)) | ||
@@ -175,3 +175,3 @@ | ||
Returns a new array obtained by replacing an item at | ||
a specified index. If the provided item is the same | ||
a specified index. If the provided item is the same as | ||
(*referentially equal to*) the previous item at that position, | ||
@@ -219,3 +219,3 @@ the original array is returned. | ||
Returns a new object with a modified attribute. | ||
If the provided value is the same (*referentially equal to*) | ||
If the provided value is the same as (*referentially equal to*) | ||
the previous value, the original object is returned. | ||
@@ -242,3 +242,3 @@ | ||
* If the provided value is the same (*referentially equal to*) | ||
* If the provided value is the same as (*referentially equal to*) | ||
the previous value, the original object is returned. | ||
@@ -276,6 +276,27 @@ * If the path does not exist, it will be created before setting | ||
#### update() | ||
Returns a new object with a modified attribute, | ||
calculated via a user-provided callback based on the current value. | ||
If the calculated value is the same as (*referentially equal to*) | ||
the previous value, the original object is returned. | ||
Usage: `update<T: ArrayOrObject>(obj: T, key: Key, | ||
fnUpdate: (prevValue: any) => any): T` | ||
```js | ||
obj = { a: 1, b: 2, c: 3 } | ||
obj2 = update(obj, 'b', (val) => val + 1) | ||
// { a: 1, b: 3, c: 3 } | ||
obj2 === obj | ||
// false | ||
// The same object is returned if there are no changes: | ||
update(obj, 'b', (val) => val) === obj | ||
// true | ||
``` | ||
#### updateIn() | ||
Returns a new object with a modified **nested** attribute, | ||
calculated via a user-provided callback based on the current value. | ||
If the calculated value is the same (*referentially equal to*) | ||
If the calculated value is the same as (*referentially equal to*) | ||
the previous value, the original object is returned. | ||
@@ -282,0 +303,0 @@ |
@@ -211,3 +211,3 @@ // @flow | ||
// -- Returns a new array obtained by replacing an item at | ||
// -- a specified index. If the provided item is the same | ||
// -- a specified index. If the provided item is the same as | ||
// -- (*referentially equal to*) the previous item at that position, | ||
@@ -231,5 +231,4 @@ // -- the original array is returned. | ||
if (array[idx] === newItem) return array; | ||
const len: number = array.length; | ||
const result: Array<T> = Array(len); | ||
const len = array.length; | ||
const result = Array(len); | ||
for (let i = 0; i < len; i++) { | ||
@@ -283,3 +282,3 @@ result[i] = array[i]; | ||
// -- Returns a new object with a modified attribute. | ||
// -- If the provided value is the same (*referentially equal to*) | ||
// -- If the provided value is the same as (*referentially equal to*) | ||
// -- the previous value, the original object is returned. | ||
@@ -314,3 +313,3 @@ // -- | ||
// -- | ||
// -- * If the provided value is the same (*referentially equal to*) | ||
// -- * If the provided value is the same as (*referentially equal to*) | ||
// -- the previous value, the original object is returned. | ||
@@ -369,6 +368,36 @@ // -- * If the path does not exist, it will be created before setting | ||
// -- #### update() | ||
// -- Returns a new object with a modified attribute, | ||
// -- calculated via a user-provided callback based on the current value. | ||
// -- If the calculated value is the same as (*referentially equal to*) | ||
// -- the previous value, the original object is returned. | ||
// -- | ||
// -- Usage: `update<T: ArrayOrObject>(obj: T, key: Key, | ||
// -- fnUpdate: (prevValue: any) => any): T` | ||
// -- | ||
// -- ```js | ||
// -- obj = { a: 1, b: 2, c: 3 } | ||
// -- obj2 = update(obj, 'b', (val) => val + 1) | ||
// -- // { a: 1, b: 3, c: 3 } | ||
// -- obj2 === obj | ||
// -- // false | ||
// -- | ||
// -- // The same object is returned if there are no changes: | ||
// -- update(obj, 'b', (val) => val) === obj | ||
// -- // true | ||
// -- ``` | ||
export function update<T: ArrayOrObject>( | ||
obj: T, | ||
key: Key, | ||
fnUpdate: (prevValue: any) => any | ||
): T { | ||
const prevVal = obj == null ? undefined : (obj: any)[key]; | ||
const nextVal = fnUpdate(prevVal); | ||
return set(obj, key, nextVal); | ||
} | ||
// -- #### updateIn() | ||
// -- Returns a new object with a modified **nested** attribute, | ||
// -- calculated via a user-provided callback based on the current value. | ||
// -- If the calculated value is the same (*referentially equal to*) | ||
// -- If the calculated value is the same as (*referentially equal to*) | ||
// -- the previous value, the original object is returned. | ||
@@ -375,0 +404,0 @@ // -- |
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
210657
1148
422