redux-create-state
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "redux-create-state", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Create new Redux state immutably", | ||
@@ -23,14 +23,11 @@ "keywords": [ | ||
"scripts": { | ||
"test": "ava -v" | ||
"test": "ava -v", | ||
"minify": "./node_modules/.bin/uglifyjs ./redux-create-state.js --output ./redux-create-state.min.js --comments /^!/", | ||
"format": "find ./redux-create-state.js | xargs -I{} ./node_modules/.bin/prettier --write {}" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.19.1", | ||
"gulp": "^3.9.1", | ||
"gulp-jscs": "^4.0.0", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-size": "^2.1.0", | ||
"gulp-uglify": "^2.1.2", | ||
"jsdoc-parse": "^3.0.0", | ||
"run-sequence": "^1.2.2" | ||
"ava": "^0.25.0", | ||
"prettier": "1.11.1", | ||
"uglify-js": "^3.3.18" | ||
} | ||
} |
/*! | ||
* redux-create-state v1.0.0 | ||
* https://github.com/niklasramo/mezr | ||
* redux-create-state v1.0.1 | ||
* https://github.com/niklasramo/redux-create-state | ||
* Copyright (c) 2017 Niklas Rämö <inramo@gmail.com> | ||
@@ -8,14 +8,11 @@ * Released under the MIT license | ||
(function (root, factory) { | ||
(function(root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
define([], factory); | ||
} | ||
else if (typeof module === 'object' && module.exports) { | ||
} else if (typeof module === 'object' && module.exports) { | ||
module.exports = factory(); | ||
} | ||
else { | ||
} else { | ||
root.reduxCreateState = factory(); | ||
} | ||
}(this, function () { | ||
})(this, function() { | ||
/** | ||
@@ -63,6 +60,19 @@ * Takes in the current state (array or object) and returns a new state which | ||
function createState(currentState) { | ||
// Clone (shallow) the current state object/array. | ||
var newState = cloneValue(currentState); | ||
// Store the current array indicator for the duration of this function. | ||
var arrayIndicator = createState._arrayIndexIndicator; | ||
// Init other variables. | ||
var argsLength; | ||
var insert; | ||
var pointer; | ||
var path; | ||
var pathValue; | ||
var key; | ||
var nextKey; | ||
var i; | ||
var ii; | ||
// If the new state is the object as the current state, let's throw an | ||
@@ -76,29 +86,27 @@ // error. Basically this situation should only occur if the current state is | ||
// Cache arguments length. | ||
var argsLength = arguments.length; | ||
argsLength = arguments.length; | ||
// Loop through the insert operations. | ||
for (var i = 1; i < argsLength; i++) { | ||
for (i = 1; i < argsLength; i++) { | ||
insert = arguments[i]; | ||
pointer = newState; | ||
path = insert[0].split('.'); | ||
pathValue = insert[1]; | ||
var insert = arguments[i]; | ||
var pointer = newState; | ||
var insertPath = insert[0].split('.'); | ||
var insertValue = insert[1]; | ||
// Go to the path or create it if it does not exist yet, and set the | ||
// value. Make sure that each existing array and object is cloned to | ||
// guarantee immutability for the inserted values and their paths. | ||
for (var ii = 0; ii < insertPath.length; ii++) { | ||
for (ii = 0; ii < path.length; ii++) { | ||
if (!path[ii]) continue; | ||
if (!insertPath[ii]) { | ||
continue; | ||
} | ||
key = path[ii]; | ||
nextKey = path[ii + 1]; | ||
var key = insertPath[ii]; | ||
var nextKey = insertPath[ii + 1]; | ||
// If key is an array index, format it into a valid index. | ||
if (key.charAt(0) === '#') { | ||
key = parseInt(key.substring(1)) || 0; | ||
if (key.indexOf(arrayIndicator) === 0) { | ||
key = +key.substring(arrayIndicator.length) || 0; | ||
// If a negative index is provided start reading from the end. | ||
if (key < 0) { | ||
key = Math.max(0, pointer.length + key); | ||
key = pointer.length + key; | ||
if (key < 0) key = 0; | ||
} | ||
@@ -109,18 +117,14 @@ } | ||
if (!nextKey) { | ||
pointer[key] = typeof insertValue === 'function' ? insertValue(cloneValue(pointer[key]), pointer) : insertValue; | ||
pointer[key] = | ||
typeof pathValue === 'function' | ||
? pathValue(cloneValue(pointer[key]), pointer) | ||
: pathValue; | ||
continue; | ||
} | ||
// Otherwise, let's create the path object/array. | ||
else { | ||
if (nextKey.charAt(0) === '#') { | ||
pointer[key] = Array.isArray(pointer[key]) ? pointer[key].concat() : []; | ||
} | ||
else { | ||
pointer[key] = isPlainObject(pointer[key]) ? cloneObject(pointer[key]) : {}; | ||
} | ||
pointer = pointer[key]; | ||
} | ||
// Let's create the path object/array. | ||
pointer = pointer[key] = !nextKey.indexOf(arrayIndicator) | ||
? Array.isArray(pointer[key]) ? pointer[key].concat() : [] | ||
: isPlainObject(pointer[key]) ? cloneObject(pointer[key]) : {}; | ||
} | ||
} | ||
@@ -130,5 +134,7 @@ | ||
return newState; | ||
} | ||
// The character that is used to detect array indices. | ||
createState._arrayIndexIndicator = '#'; | ||
/** | ||
@@ -142,5 +148,3 @@ * Check if a value is a plain object. | ||
function isPlainObject(val) { | ||
return typeof val === 'object' && Object.prototype.toString.call(val) === '[object Object]'; | ||
} | ||
@@ -156,14 +160,11 @@ | ||
function cloneObject(obj) { | ||
var ret = {}; | ||
var keys = Object.keys(obj); | ||
var i; | ||
var ret = {}; | ||
if (typeof Object.assign === 'function') { | ||
return Object.assign(ret, obj); | ||
for (i = 0; i < keys.length; i++) { | ||
ret[keys[i]] = obj[keys[i]]; | ||
} | ||
else { | ||
Object.keys().forEach(function (key) { | ||
ret[key] = obj[key]; | ||
}); | ||
return ret; | ||
} | ||
return ret; | ||
} | ||
@@ -179,5 +180,5 @@ | ||
function cloneValue(value) { | ||
return isPlainObject(value) ? cloneObject(value) : Array.isArray(value) ? value.concat() : value; | ||
return isPlainObject(value) | ||
? cloneObject(value) | ||
: Array.isArray(value) ? value.concat() : value; | ||
} | ||
@@ -210,3 +211,2 @@ | ||
return createState; | ||
})); | ||
}); |
/*! | ||
* redux-create-state v1.0.0 | ||
* https://github.com/niklasramo/mezr | ||
* redux-create-state v1.0.1 | ||
* https://github.com/niklasramo/redux-create-state | ||
* Copyright (c) 2017 Niklas Rämö <inramo@gmail.com> | ||
* Released under the MIT license | ||
*/ | ||
!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof module&&module.exports?module.exports=e():t.reduxCreateState=e()}(this,function(){function t(t){var o=n(t);if(o===t)throw new Error("[redux-create-state] State object must be a plain object or an array.");for(var a=arguments.length,c=1;c<a;c++)for(var i=arguments[c],f=o,u=i[0].split("."),s=i[1],b=0;b<u.length;b++)if(u[b]){var p=u[b],y=u[b+1];"#"===p.charAt(0)&&(p=parseInt(p.substring(1))||0)<0&&(p=Math.max(0,f.length+p)),y?("#"===y.charAt(0)?f[p]=Array.isArray(f[p])?f[p].concat():[]:f[p]=e(f[p])?r(f[p]):{},f=f[p]):f[p]="function"==typeof s?s(n(f[p]),f):s}return o}function e(t){return"object"==typeof t&&"[object Object]"===Object.prototype.toString.call(t)}function r(t){var e={};return"function"==typeof Object.assign?Object.assign(e,t):(Object.keys().forEach(function(r){e[r]=t[r]}),e)}function n(t){return e(t)?r(t):Array.isArray(t)?t.concat():t}return t}); | ||
(function(root,factory){if(typeof define==="function"&&define.amd){define([],factory)}else if(typeof module==="object"&&module.exports){module.exports=factory()}else{root.reduxCreateState=factory()}})(this,function(){function createState(currentState){var newState=cloneValue(currentState);var arrayIndicator=createState._arrayIndexIndicator;var argsLength;var insert;var pointer;var path;var pathValue;var key;var nextKey;var i;var ii;if(newState===currentState){throw new Error("[redux-create-state] State object must be a plain object or an array.")}argsLength=arguments.length;for(i=1;i<argsLength;i++){insert=arguments[i];pointer=newState;path=insert[0].split(".");pathValue=insert[1];for(ii=0;ii<path.length;ii++){if(!path[ii])continue;key=path[ii];nextKey=path[ii+1];if(key.indexOf(arrayIndicator)===0){key=+key.substring(arrayIndicator.length)||0;if(key<0){key=pointer.length+key;if(key<0)key=0}}if(!nextKey){pointer[key]=typeof pathValue==="function"?pathValue(cloneValue(pointer[key]),pointer):pathValue;continue}pointer=pointer[key]=!nextKey.indexOf(arrayIndicator)?Array.isArray(pointer[key])?pointer[key].concat():[]:isPlainObject(pointer[key])?cloneObject(pointer[key]):{}}}return newState}createState._arrayIndexIndicator="#";function isPlainObject(val){return typeof val==="object"&&Object.prototype.toString.call(val)==="[object Object]"}function cloneObject(obj){var ret={};var keys=Object.keys(obj);var i;for(i=0;i<keys.length;i++){ret[keys[i]]=obj[keys[i]]}return ret}function cloneValue(value){return isPlainObject(value)?cloneObject(value):Array.isArray(value)?value.concat():value}return createState}); |
3
18311
8
274