immutable-object-methods
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -16,2 +16,4 @@ 'use strict'; | ||
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } | ||
var changesInput = function changesInput(input, keys, value) { | ||
@@ -31,11 +33,12 @@ for (var i = 0; i < keys.length; ++i) { | ||
var input = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var keys = arguments[1]; | ||
var value = arguments[2]; | ||
var _ref = arguments[1]; | ||
var key = keys[0]; | ||
if (keys.length === 1) { | ||
return (0, _objectAssign2.default)({}, input, _defineProperty({}, key, value)); | ||
} | ||
var _ref2 = _toArray(_ref); | ||
return (0, _objectAssign2.default)({}, input, _defineProperty({}, key, _setIn(input[key], keys.slice(1), value))); | ||
var key = _ref2[0]; | ||
var rest = _ref2.slice(1); | ||
var value = arguments[2]; | ||
return (0, _objectAssign2.default)({}, input, _defineProperty({}, key, rest.length ? _setIn(input[key], rest, value) : value)); | ||
}; | ||
@@ -42,0 +45,0 @@ |
import {setIn} from 'immutable-object-methods'; | ||
const input = {a: {b: 'c'}}; | ||
const updated = setIn(input, ['a', 'd'], e); | ||
const updated = setIn(input, ['a', 'd'], 'e'); | ||
console.log(input); | ||
console.log(updated); |
@@ -15,13 +15,9 @@ import assign from 'object-assign'; | ||
const _setIn = (input = {}, keys, value) => { | ||
const key = keys[0]; | ||
if (keys.length === 1) { | ||
return assign({}, input, {[key]: value}); | ||
const _setIn = (input = {}, [key, ...rest], value) => assign( | ||
{}, input, { | ||
[key]: rest.length ? _setIn(input[key], rest, value) : value | ||
} | ||
); | ||
return assign({}, input, {[key]: _setIn(input[key], keys.slice(1), value)}); | ||
}; | ||
export const setIn = (input, keys, value) => { | ||
return changesInput(input, keys, value) ? _setIn(input, keys, value) : input; | ||
}; | ||
export const setIn = (input, keys, value) => | ||
changesInput(input, keys, value) ? _setIn(input, keys, value) : input; |
{ | ||
"name": "immutable-object-methods", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Update normal plain javascript object, immutable style. Simlar to how immutable.js, seamless-immutable etc does it but a lot smaller and simpler.", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"test": "ava", | ||
"test": "ava && semistandard | snazzy", | ||
"build": "rm -rf dist && mkdir -p dist && babel lib --out-dir dist", | ||
@@ -9,0 +9,0 @@ "watch": "rm -rf dist && mkdir -p dist && babel -w lib --out-dir dist", |
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
6151
114