-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
redux-create-state.min.js
7 lines (7 loc) · 1.76 KB
/
redux-create-state.min.js
1
2
3
4
5
6
7
/*!
* redux-create-state v1.0.1
* https://github.com/niklasramo/redux-create-state
* Copyright (c) 2017 Niklas Rämö <[email protected]>
* Released under the MIT license
*/
(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});