Comparing version 1.5.0 to 1.5.1
@@ -5,2 +5,11 @@ # unchanged CHANGELOG | ||
## 1.5.1 | ||
- Return support for `Symbol` keys when using `assign`, `merge`, `remove`, and `set` | ||
- Add documentation for `has` method | ||
- Improve speed of `add` by eliminating unneeded call to curried wrapper | ||
- Improve speed of `merge` by eliminating unneeded extra cloning | ||
- Improve speed of `remove` by eliminating unneeded iterations | ||
- Allow cloning to work consistently even with data from across realms | ||
## 1.5.0 | ||
@@ -7,0 +16,0 @@ |
@@ -236,4 +236,8 @@ (function (global, factory) { | ||
var create$1 = O.create, | ||
getOwnPropertySymbols = O.getOwnPropertySymbols, | ||
getPrototypeOf = O.getPrototypeOf, | ||
keys = O.keys; | ||
keys = O.keys, | ||
propertyIsEnumerable = O.propertyIsEnumerable; | ||
var toStringObject = O.prototype.toString; | ||
var toStringFunction = Function.prototype.toString; | ||
/** | ||
@@ -244,3 +248,3 @@ * @constant {Symbol} REACT_ELEMENT | ||
var REACT_ELEMENT = typeof Symbol === 'function' && Symbol.for ? Symbol.for('react.element') : 0xeac7; | ||
var REACT_ELEMENT = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('react.element') : 0xeac7; | ||
/** | ||
@@ -287,24 +291,48 @@ * @constant {RegExp} FUNCTION_NAME | ||
/** | ||
* @function assign | ||
* @function getOwnProperties | ||
* | ||
* @description | ||
* a slimmer, faster version of Object.assign | ||
* get the own properties of an object, either keys or symbols | ||
* | ||
* @param {Object} object the object to get all keys and symbols of | ||
* @returns {Array<string|symbol>} the own properties of the object | ||
*/ | ||
var getOwnProperties = function getOwnProperties(object) { | ||
var ownSymbols = getOwnPropertySymbols(object); | ||
if (!ownSymbols.length) { | ||
return keys(object); | ||
} | ||
return keys(object).concat(reduce(ownSymbols, function (enumerableSymbols, symbol) { | ||
if (propertyIsEnumerable.call(object, symbol)) { | ||
enumerableSymbols.push(symbol); | ||
} | ||
return enumerableSymbols; | ||
}, [])); | ||
}; | ||
/** | ||
* @function assignFallback | ||
* | ||
* @description | ||
* a simple implementation of Object.assign | ||
* | ||
* @param {Object} target the target object | ||
* @param {Array<Object>} sources the objects to merge into target | ||
* @param {Object} source the object to merge into target | ||
* @returns {Object} the shallowly-merged object | ||
*/ | ||
var assign = function assign(target) { | ||
for (var _len = arguments.length, sources = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
sources[_key - 1] = arguments[_key]; | ||
var assignFallback = function assignFallback(target, source) { | ||
if (!source) { | ||
return target; | ||
} | ||
return reduce(sources, function (assigned, object) { | ||
return object ? reduce(keys(object), function (assignedTarget, key) { | ||
assignedTarget[key] = object[key]; | ||
return assignedTarget; | ||
}, assigned) : assigned; | ||
}, target); | ||
return reduce(getOwnProperties(source), function (clonedObject, property) { | ||
clonedObject[property] = source[property]; | ||
return clonedObject; | ||
}, Object(target)); | ||
}; | ||
var assign = typeof O.assign === 'function' ? O.assign : assignFallback; | ||
/** | ||
@@ -314,3 +342,7 @@ * @function isCloneable | ||
* @description | ||
* can the object be merged | ||
* can the object be cloned | ||
* | ||
* - the object exists and is an object | ||
* - the object is not a Date or RegExp | ||
* - the object is not a React element | ||
* | ||
@@ -322,3 +354,8 @@ * @param {*} object the object to test | ||
var isCloneable = function isCloneable(object) { | ||
return !!object && typeof object === 'object' && !(object instanceof Date || object instanceof RegExp) && object.$$typeof !== REACT_ELEMENT; | ||
if (!object || typeof object !== 'object') { | ||
return false; | ||
} | ||
var type = toStringObject.call(object); | ||
return type !== '[object Date]' && type !== '[object RegExp]' && object.$$typeof !== REACT_ELEMENT; | ||
}; | ||
@@ -336,3 +373,3 @@ /** | ||
var isGlobalConstructor = function isGlobalConstructor(fn) { | ||
return typeof fn === 'function' && global[fn.name || Function.prototype.toString.call(fn).split(FUNCTION_NAME)[1]] === fn; | ||
return typeof fn === 'function' && global[fn.name || toStringFunction.call(fn).split(FUNCTION_NAME)[1]] === fn; | ||
}; | ||
@@ -366,6 +403,11 @@ /** | ||
var getShallowClone = function getShallowClone(object) { | ||
return object.constructor === O ? assign({}, object) : isArray(object) ? cloneArray(object) : isGlobalConstructor(object.constructor) ? {} : reduce(keys(object), function (clone, key) { | ||
clone[key] = object[key]; | ||
return clone; | ||
}, create$1(getPrototypeOf(object))); | ||
if (object.constructor === O) { | ||
return assign({}, object); | ||
} | ||
if (isArray(object)) { | ||
return cloneArray(object); | ||
} | ||
return isGlobalConstructor(object.constructor) ? {} : assign(create$1(getPrototypeOf(object)), object); | ||
}; | ||
@@ -430,3 +472,3 @@ /** | ||
* get the value if it is not undefined, else get the fallback | ||
* | ||
*` | ||
* @param {any} value the main value to return | ||
@@ -489,9 +531,16 @@ * @param {any} fallbackValue the value to return if main is undefined | ||
var isObject1Array = isArray(object1); | ||
return isObject1Array !== isArray(object2) || !isCloneable(object1) ? cloneIfPossible(object2) : isObject1Array ? object1.concat(object2.map(cloneIfPossible)) : reduce(keys(object2), function (clone, key) { | ||
if (isObject1Array !== isArray(object2) || !isCloneable(object1)) { | ||
return cloneIfPossible(object2); | ||
} | ||
if (isObject1Array) { | ||
return object1.concat(object2); | ||
} | ||
var target = object1.constructor === O || isGlobalConstructor(object1.constructor) ? {} : create$1(getPrototypeOf(object1)); | ||
return reduce(getOwnProperties(object2), function (clone, key) { | ||
clone[key] = isDeep && isCloneable(object2[key]) ? getMergedObject(object1[key], object2[key], isDeep) : object2[key]; | ||
return clone; | ||
}, reduce(keys(object1), function (clone, key) { | ||
clone[key] = cloneIfPossible(object1[key]); | ||
return clone; | ||
}, object1.constructor === O ? {} : create$1(getPrototypeOf(object1)))); | ||
}, assign(target, object1)); | ||
}; | ||
@@ -526,3 +575,8 @@ /** | ||
var parsedPath = getParsedPath(path); | ||
return parsedPath.length === 1 ? object ? callIfFunction(object[parsedPath[0]], context, parameters) : void 0 : onMatchAtPath(parsedPath, object, function (ref, key) { | ||
if (parsedPath.length === 1) { | ||
return object ? callIfFunction(object[parsedPath[0]], context, parameters) : void 0; | ||
} | ||
return onMatchAtPath(parsedPath, object, function (ref, key) { | ||
return callIfFunction(ref[key], context, parameters); | ||
@@ -545,3 +599,8 @@ }); | ||
var parsedPath = getParsedPath(path); | ||
return parsedPath.length === 1 ? object ? getCoalescedValue(object[parsedPath[0]], noMatchValue) : noMatchValue : onMatchAtPath(parsedPath, object, function (ref, key) { | ||
if (parsedPath.length === 1) { | ||
return object ? getCoalescedValue(object[parsedPath[0]], noMatchValue) : noMatchValue; | ||
} | ||
return onMatchAtPath(parsedPath, object, function (ref, key) { | ||
return getCoalescedValue(ref[key], noMatchValue); | ||
@@ -619,8 +678,8 @@ }, false, noMatchValue); | ||
while (index < length) { | ||
while (index < length - 1) { | ||
array[index] = array[index + 1]; | ||
index++; | ||
++index; | ||
} | ||
array.length--; | ||
--array.length; | ||
} | ||
@@ -818,4 +877,5 @@ }; | ||
var add = curry(function (path, value, object) { | ||
var nestedValue = get(path, object); | ||
var fullPath = isArray(nestedValue) ? isArray(path) ? path.concat([nestedValue.length]) : (isEmptyPath(path) ? '' : path) + "[" + nestedValue.length + "]" : path; | ||
var isPathEmpty = isEmptyPath(path); | ||
var valueAtPath = isPathEmpty ? object : getNestedProperty(path, object); | ||
var fullPath = isArray(valueAtPath) ? isArray(path) ? path.concat([valueAtPath.length]) : (isPathEmpty ? '' : path) + "[" + valueAtPath.length + "]" : path; | ||
return set(fullPath, value, object); | ||
@@ -822,0 +882,0 @@ }); |
@@ -1,1 +0,1 @@ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n.unchanged={})}(this,function(n){"use strict";var o="function"==typeof Symbol?Symbol("curriable placeholder"):60881;function t(t){var r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:t.length;function e(){var n=arguments;return n.length>=r&&!function(n,t){for(var r=0;r<t;r++)if(n[r]===o)return!0;return!1}(n,r)?t.apply(this,n):function(){return e.apply(this,function(n,t){for(var r=new Array(n.length),e=0,u=0;u<n.length;u++)r[u]=n[u]===o&&e<t.length?t[e++]:n[u];if(e<t.length)for(;e<t.length;e++)r.push(t[e]);return r}(n,arguments))}}return e.arity=r,e.fn=t,e}t.__=o,t.uncurry=function(n){return n.fn};var r={clear:function(){r.results={},r.size=0},results:{},size:0},e=/"[^"]+"|`[^`]+`|'[^']+'|[^.[\]]+/g,u=/^\d+$/i,i=/^"[^"]+"|`[^`]+`|'[^']+'$/,f=function(n){var t,r,e=(t=n,i.test(t)?n.substring(1,n.length-1):n);return(r=e).length&&u.test(r)?+e:e},c=function(n){return"string"==typeof n?(t=n,r.results[t]||(500<r.size&&r.clear(),r.results[t]=t?t.match(e).map(f):[t],r.size++),r.results[t]):Array.isArray(n)?n.map(f):["number"==typeof n?n:""+n];var t},a=Object,l=a.create,s=a.getPrototypeOf,g=a.keys,p="function"==typeof Symbol&&Symbol.for?Symbol.for("react.element"):60103,y=/^\s*function\s*([^\(]*)/i,h=Array.isArray,v=function(n,t,r){for(var e=r,u=0;u<n.length;u++)e=t(e,n[u]);return e},d=function(n){return!!n&&"object"==typeof n&&!(n instanceof Date||n instanceof RegExp)&&n.$$typeof!==p},m=function(n,t,r){return"function"==typeof n?n.apply(t,r):void 0},b=function(r){return r.constructor===a?function(n){for(var t=arguments.length,r=new Array(1<t?t-1:0),e=1;e<t;e++)r[e-1]=arguments[e];return v(r,function(n,r){return r?v(g(r),function(n,t){return n[t]=r[t],n},n):n},n)}({},r):h(r)?function(n){for(var t=new n.constructor,r=0;r<n.length;r++)t[r]=n[r];return t}(r):"function"==typeof(n=r.constructor)&&global[n.name||Function.prototype.toString.call(n).split(y)[1]]===n?{}:v(g(r),function(n,t){return n[t]=r[t],n},l(s(r)));var n},A=function(n){return"number"==typeof n?[]:{}},S=function(n){return d(n)?b(n):n},_=function(n,t){return void 0===n?t:n},j=function n(t,r,e,u,o,i){void 0===i&&(i=0);var f,c,a=t[i],l=i+1;if(l!==t.length)return u?(r[a]=n(t,(f=r[a],c=t[l],d(f)?b(f):A(c)),e,u,o,l),r):r&&r[a]?n(t,r[a],e,u,o,l):o;var s=r||u?e(r,a):o;return u?r:s},w=function r(e,u,o){var n=h(e);return n===h(u)&&d(e)?n?e.concat(u.map(S)):v(g(u),function(n,t){return n[t]=o&&d(u[t])?r(e[t],u[t],o):u[t],n},v(g(e),function(n,t){return n[t]=S(e[t]),n},e.constructor===a?{}:l(s(e)))):S(u)},x=function(n){return h(n)?n:c(n)},z=function(n,t,r){var e=x(n);return 1===e.length?t?_(t[e[0]],r):r:j(e,t,function(n,t){return _(n[t],r)},!1,r)},O=function(n,t,r){var e=x(n),u=d(t)?b(t):A(e[0]);return 1===e.length?(r(u,e[0]),u):j(e,u,r,!0)},$=function(n,t){return void 0!==z(n,t)},P=function(n){return null==n||h(n)&&!n.length},k=t(function(n,r,t){return d(t)?P(n)?w(t,r,!1):O(n,t,function(n,t){n[t]=w(n[t],r,!1)}):r}),D=t(function(n,t,r,e){return void 0===e&&(e=r),P(n)?m(r,e,t):(u=e,o=t,i=r,1===(f=x(n)).length?i?m(i[f[0]],u,o):void 0:j(f,i,function(n,t){return m(n[t],u,o)}));var u,o,i,f},3),E=t(function(n,t){return P(n)?t:z(n,t)}),F=t(function(n,t,r){return P(t)?r:z(t,r,n)}),M=t(function(n,t){return P(n)?null!=t:$(n,t)}),R=t(function(n,r,t){return d(t)?P(n)?w(t,r,!0):O(n,t,function(n,t){n[t]=w(n[t],r,!0)}):r}),q=t(function(n,t){return P(n)?h(t)?[]:{}:$(n,t)?O(n,t,function(n,t){h(n)?function(n,t){if(n.length){for(var r=n.length,e=t;e<r;)n[e]=n[e+1],e++;n.length--}}(n,t):delete n[t]}):t}),B=t(function(n,r,t){return P(n)?r:O(n,t,function(n,t){n[t]=r})}),C=t(function(n,r,t){for(var e=arguments.length,u=new Array(3<e?e-3:0),o=3;o<e;o++)u[o-3]=arguments[o];return P(n)?r.apply(void 0,[t].concat(u)):O(n,t,function(n,t){return n[t]=r.apply(void 0,[n[t]].concat(u))})},3),G=t(function(n,t,r){var e=E(n,r),u=h(e)?h(n)?n.concat([e.length]):(P(n)?"":n)+"["+e.length+"]":n;return B(u,t,r)});n.__=o,n.assign=k,n.call=D,n.get=E,n.getOr=F,n.has=M,n.merge=R,n.remove=q,n.set=B,n.transform=C,n.add=G,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n.unchanged={})}(this,function(n){"use strict";var u="function"==typeof Symbol?Symbol("curriable placeholder"):60881;function t(t){var r=1<arguments.length&&void 0!==arguments[1]?arguments[1]:t.length;function e(){var n=arguments;return n.length>=r&&!function(n,t){for(var r=0;r<t;r++)if(n[r]===u)return!0;return!1}(n,r)?t.apply(this,n):function(){return e.apply(this,function(n,t){for(var r=new Array(n.length),e=0,o=0;o<n.length;o++)r[o]=n[o]===u&&e<t.length?t[e++]:n[o];if(e<t.length)for(;e<t.length;e++)r.push(t[e]);return r}(n,arguments))}}return e.arity=r,e.fn=t,e}t.__=u,t.uncurry=function(n){return n.fn};var r={clear:function(){r.results={},r.size=0},results:{},size:0},e=/"[^"]+"|`[^`]+`|'[^']+'|[^.[\]]+/g,o=/^\d+$/i,i=/^"[^"]+"|`[^`]+`|'[^']+'$/,c=function(n){var t,r,e=(t=n,i.test(t)?n.substring(1,n.length-1):n);return(r=e).length&&o.test(r)?+e:e},f=function(n){return"string"==typeof n?(t=n,r.results[t]||(500<r.size&&r.clear(),r.results[t]=t?t.match(e).map(c):[t],r.size++),r.results[t]):Array.isArray(n)?n.map(c):["number"==typeof n?n:""+n];var t},l=Object,a=l.create,s=l.getOwnPropertySymbols,p=l.getPrototypeOf,g=l.keys,y=l.propertyIsEnumerable,h=l.prototype.toString,v=Function.prototype.toString,d="function"==typeof Symbol&&"function"==typeof Symbol.for?Symbol.for("react.element"):60103,b=/^\s*function\s*([^\(]*)/i,m=Array.isArray,S=function(n,t,r){for(var e=r,o=0;o<n.length;o++)e=t(e,n[o]);return e},j=function(r){var n=s(r);return n.length?g(r).concat(S(n,function(n,t){return y.call(r,t)&&n.push(t),n},[])):g(r)},A="function"==typeof l.assign?l.assign:function(n,r){return r?S(j(r),function(n,t){return n[t]=r[t],n},Object(n)):n},O=function(n){if(!n||"object"!=typeof n)return!1;var t=h.call(n);return"[object Date]"!==t&&"[object RegExp]"!==t&&n.$$typeof!==d},_=function(n){return"function"==typeof n&&global[n.name||v.call(n).split(b)[1]]===n},w=function(n,t,r){return"function"==typeof n?n.apply(t,r):void 0},x=function(n){return n.constructor===l?A({},n):m(n)?function(n){for(var t=new n.constructor,r=0;r<n.length;r++)t[r]=n[r];return t}(n):_(n.constructor)?{}:A(a(p(n)),n)},z=function(n){return"number"==typeof n?[]:{}},$=function(n,t){return void 0===n?t:n},P=function n(t,r,e,o,u,i){void 0===i&&(i=0);var c,f,l=t[i],a=i+1;if(a!==t.length)return o?(r[l]=n(t,(c=r[l],f=t[a],O(c)?x(c):z(f)),e,o,u,a),r):r&&r[l]?n(t,r[l],e,o,u,a):u;var s=r||o?e(r,l):u;return o?r:s},E=function r(e,o,u){var n,t=m(e);if(t!==m(o)||!O(e))return O(n=o)?x(n):n;if(t)return e.concat(o);var i=e.constructor===l||_(e.constructor)?{}:a(p(e));return S(j(o),function(n,t){return n[t]=u&&O(o[t])?r(e[t],o[t],u):o[t],n},A(i,e))},k=function(n){return m(n)?n:f(n)},D=function(n,t,r){var e=k(n);return 1===e.length?t?$(t[e[0]],r):r:P(e,t,function(n,t){return $(n[t],r)},!1,r)},F=function(n,t,r){var e=k(n),o=O(t)?x(t):z(e[0]);return 1===e.length?(r(o,e[0]),o):P(e,o,r,!0)},I=function(n,t){return void 0!==D(n,t)},M=function(n){return null==n||m(n)&&!n.length},R=t(function(n,r,t){return O(t)?M(n)?E(t,r,!1):F(n,t,function(n,t){n[t]=E(n[t],r,!1)}):r}),q=t(function(n,t,r,e){return void 0===e&&(e=r),M(n)?w(r,e,t):(o=e,u=t,i=r,1===(c=k(n)).length?i?w(i[c[0]],o,u):void 0:P(c,i,function(n,t){return w(n[t],o,u)}));var o,u,i,c},3),B=t(function(n,t){return M(n)?t:D(n,t)}),C=t(function(n,t,r){return M(t)?r:D(t,r,n)}),G=t(function(n,t){return M(n)?null!=t:I(n,t)}),H=t(function(n,r,t){return O(t)?M(n)?E(t,r,!0):F(n,t,function(n,t){n[t]=E(n[t],r,!0)}):r}),J=t(function(n,t){return M(n)?m(t)?[]:{}:I(n,t)?F(n,t,function(n,t){m(n)?function(n,t){if(n.length){for(var r=n.length,e=t;e<r-1;)n[e]=n[e+1],++e;--n.length}}(n,t):delete n[t]}):t}),K=t(function(n,r,t){return M(n)?r:F(n,t,function(n,t){n[t]=r})}),L=t(function(n,r,t){for(var e=arguments.length,o=new Array(3<e?e-3:0),u=3;u<e;u++)o[u-3]=arguments[u];return M(n)?r.apply(void 0,[t].concat(o)):F(n,t,function(n,t){return n[t]=r.apply(void 0,[n[t]].concat(o))})},3),N=t(function(n,t,r){var e=M(n),o=e?r:D(n,r),u=m(o)?m(n)?n.concat([o.length]):(e?"":n)+"["+o.length+"]":n;return K(u,t,r)});n.__=u,n.assign=R,n.call=q,n.get=B,n.getOr=C,n.has=G,n.merge=H,n.remove=J,n.set=K,n.transform=L,n.add=N,Object.defineProperty(n,"__esModule",{value:!0})}); |
@@ -194,5 +194,6 @@ // external dependencies | ||
export var add = curry(function (path, value, object) { | ||
var nestedValue = get(path, object); | ||
var fullPath = isArray(nestedValue) ? isArray(path) ? path.concat([nestedValue.length]) : (isEmptyPath(path) ? '' : path) + "[" + nestedValue.length + "]" : path; | ||
var isPathEmpty = isEmptyPath(path); | ||
var valueAtPath = isPathEmpty ? object : getNestedProperty(path, object); | ||
var fullPath = isArray(valueAtPath) ? isArray(path) ? path.concat([valueAtPath.length]) : (isPathEmpty ? '' : path) + "[" + valueAtPath.length + "]" : path; | ||
return set(fullPath, value, object); | ||
}); |
123
es/utils.js
@@ -5,4 +5,8 @@ // external dependencies | ||
var create = O.create, | ||
getOwnPropertySymbols = O.getOwnPropertySymbols, | ||
getPrototypeOf = O.getPrototypeOf, | ||
keys = O.keys; | ||
keys = O.keys, | ||
propertyIsEnumerable = O.propertyIsEnumerable; | ||
var toStringObject = O.prototype.toString; | ||
var toStringFunction = Function.prototype.toString; | ||
/** | ||
@@ -13,3 +17,3 @@ * @constant {Symbol} REACT_ELEMENT | ||
var REACT_ELEMENT = typeof Symbol === 'function' && Symbol.for ? Symbol.for('react.element') : 0xeac7; | ||
var REACT_ELEMENT = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('react.element') : 0xeac7; | ||
/** | ||
@@ -67,24 +71,48 @@ * @constant {RegExp} FUNCTION_NAME | ||
/** | ||
* @function assign | ||
* @function getOwnProperties | ||
* | ||
* @description | ||
* a slimmer, faster version of Object.assign | ||
* get the own properties of an object, either keys or symbols | ||
* | ||
* @param {Object} object the object to get all keys and symbols of | ||
* @returns {Array<string|symbol>} the own properties of the object | ||
*/ | ||
export var getOwnProperties = function getOwnProperties(object) { | ||
var ownSymbols = getOwnPropertySymbols(object); | ||
if (!ownSymbols.length) { | ||
return keys(object); | ||
} | ||
return keys(object).concat(reduce(ownSymbols, function (enumerableSymbols, symbol) { | ||
if (propertyIsEnumerable.call(object, symbol)) { | ||
enumerableSymbols.push(symbol); | ||
} | ||
return enumerableSymbols; | ||
}, [])); | ||
}; | ||
/** | ||
* @function assignFallback | ||
* | ||
* @description | ||
* a simple implementation of Object.assign | ||
* | ||
* @param {Object} target the target object | ||
* @param {Array<Object>} sources the objects to merge into target | ||
* @param {Object} source the object to merge into target | ||
* @returns {Object} the shallowly-merged object | ||
*/ | ||
export var assign = function assign(target) { | ||
for (var _len = arguments.length, sources = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
sources[_key - 1] = arguments[_key]; | ||
export var assignFallback = function assignFallback(target, source) { | ||
if (!source) { | ||
return target; | ||
} | ||
return reduce(sources, function (assigned, object) { | ||
return object ? reduce(keys(object), function (assignedTarget, key) { | ||
assignedTarget[key] = object[key]; | ||
return assignedTarget; | ||
}, assigned) : assigned; | ||
}, target); | ||
return reduce(getOwnProperties(source), function (clonedObject, property) { | ||
clonedObject[property] = source[property]; | ||
return clonedObject; | ||
}, Object(target)); | ||
}; | ||
var assign = typeof O.assign === 'function' ? O.assign : assignFallback; | ||
/** | ||
@@ -94,3 +122,7 @@ * @function isCloneable | ||
* @description | ||
* can the object be merged | ||
* can the object be cloned | ||
* | ||
* - the object exists and is an object | ||
* - the object is not a Date or RegExp | ||
* - the object is not a React element | ||
* | ||
@@ -102,3 +134,8 @@ * @param {*} object the object to test | ||
export var isCloneable = function isCloneable(object) { | ||
return !!object && typeof object === 'object' && !(object instanceof Date || object instanceof RegExp) && object.$$typeof !== REACT_ELEMENT; | ||
if (!object || typeof object !== 'object') { | ||
return false; | ||
} | ||
var type = toStringObject.call(object); | ||
return type !== '[object Date]' && type !== '[object RegExp]' && object.$$typeof !== REACT_ELEMENT; | ||
}; | ||
@@ -116,3 +153,3 @@ /** | ||
export var isGlobalConstructor = function isGlobalConstructor(fn) { | ||
return typeof fn === 'function' && global[fn.name || Function.prototype.toString.call(fn).split(FUNCTION_NAME)[1]] === fn; | ||
return typeof fn === 'function' && global[fn.name || toStringFunction.call(fn).split(FUNCTION_NAME)[1]] === fn; | ||
}; | ||
@@ -146,6 +183,11 @@ /** | ||
export var getShallowClone = function getShallowClone(object) { | ||
return object.constructor === O ? assign({}, object) : isArray(object) ? cloneArray(object) : isGlobalConstructor(object.constructor) ? {} : reduce(keys(object), function (clone, key) { | ||
clone[key] = object[key]; | ||
return clone; | ||
}, create(getPrototypeOf(object))); | ||
if (object.constructor === O) { | ||
return assign({}, object); | ||
} | ||
if (isArray(object)) { | ||
return cloneArray(object); | ||
} | ||
return isGlobalConstructor(object.constructor) ? {} : assign(create(getPrototypeOf(object)), object); | ||
}; | ||
@@ -210,3 +252,3 @@ /** | ||
* get the value if it is not undefined, else get the fallback | ||
* | ||
*` | ||
* @param {any} value the main value to return | ||
@@ -269,9 +311,16 @@ * @param {any} fallbackValue the value to return if main is undefined | ||
var isObject1Array = isArray(object1); | ||
return isObject1Array !== isArray(object2) || !isCloneable(object1) ? cloneIfPossible(object2) : isObject1Array ? object1.concat(object2.map(cloneIfPossible)) : reduce(keys(object2), function (clone, key) { | ||
if (isObject1Array !== isArray(object2) || !isCloneable(object1)) { | ||
return cloneIfPossible(object2); | ||
} | ||
if (isObject1Array) { | ||
return object1.concat(object2); | ||
} | ||
var target = object1.constructor === O || isGlobalConstructor(object1.constructor) ? {} : create(getPrototypeOf(object1)); | ||
return reduce(getOwnProperties(object2), function (clone, key) { | ||
clone[key] = isDeep && isCloneable(object2[key]) ? getMergedObject(object1[key], object2[key], isDeep) : object2[key]; | ||
return clone; | ||
}, reduce(keys(object1), function (clone, key) { | ||
clone[key] = cloneIfPossible(object1[key]); | ||
return clone; | ||
}, object1.constructor === O ? {} : create(getPrototypeOf(object1)))); | ||
}, assign(target, object1)); | ||
}; | ||
@@ -306,3 +355,8 @@ /** | ||
var parsedPath = getParsedPath(path); | ||
return parsedPath.length === 1 ? object ? callIfFunction(object[parsedPath[0]], context, parameters) : void 0 : onMatchAtPath(parsedPath, object, function (ref, key) { | ||
if (parsedPath.length === 1) { | ||
return object ? callIfFunction(object[parsedPath[0]], context, parameters) : void 0; | ||
} | ||
return onMatchAtPath(parsedPath, object, function (ref, key) { | ||
return callIfFunction(ref[key], context, parameters); | ||
@@ -325,3 +379,8 @@ }); | ||
var parsedPath = getParsedPath(path); | ||
return parsedPath.length === 1 ? object ? getCoalescedValue(object[parsedPath[0]], noMatchValue) : noMatchValue : onMatchAtPath(parsedPath, object, function (ref, key) { | ||
if (parsedPath.length === 1) { | ||
return object ? getCoalescedValue(object[parsedPath[0]], noMatchValue) : noMatchValue; | ||
} | ||
return onMatchAtPath(parsedPath, object, function (ref, key) { | ||
return getCoalescedValue(ref[key], noMatchValue); | ||
@@ -399,9 +458,9 @@ }, false, noMatchValue); | ||
while (index < length) { | ||
while (index < length - 1) { | ||
array[index] = array[index + 1]; | ||
index++; | ||
++index; | ||
} | ||
array.length--; | ||
--array.length; | ||
} | ||
}; |
@@ -211,6 +211,7 @@ "use strict"; | ||
var add = (0, _curriable.curry)(function (path, value, object) { | ||
var nestedValue = get(path, object); | ||
var fullPath = (0, _utils.isArray)(nestedValue) ? (0, _utils.isArray)(path) ? path.concat([nestedValue.length]) : ((0, _utils.isEmptyPath)(path) ? '' : path) + "[" + nestedValue.length + "]" : path; | ||
var isPathEmpty = (0, _utils.isEmptyPath)(path); | ||
var valueAtPath = isPathEmpty ? object : (0, _utils.getNestedProperty)(path, object); | ||
var fullPath = (0, _utils.isArray)(valueAtPath) ? (0, _utils.isArray)(path) ? path.concat([valueAtPath.length]) : (isPathEmpty ? '' : path) + "[" + valueAtPath.length + "]" : path; | ||
return set(fullPath, value, object); | ||
}); | ||
exports.add = add; |
135
lib/utils.js
"use strict"; | ||
exports.__esModule = true; | ||
exports.splice = exports.isEmptyPath = exports.hasNestedProperty = exports.getDeepClone = exports.getNestedProperty = exports.callNestedProperty = exports.getParsedPath = exports.getMergedObject = exports.onMatchAtPath = exports.getCoalescedValue = exports.getNewChildClone = exports.cloneIfPossible = exports.getNewEmptyObject = exports.getNewEmptyChild = exports.getShallowClone = exports.callIfFunction = exports.isGlobalConstructor = exports.isCloneable = exports.assign = exports.reduce = exports.cloneArray = exports.isArray = void 0; | ||
exports.splice = exports.isEmptyPath = exports.hasNestedProperty = exports.getDeepClone = exports.getNestedProperty = exports.callNestedProperty = exports.getParsedPath = exports.getMergedObject = exports.onMatchAtPath = exports.getCoalescedValue = exports.getNewChildClone = exports.cloneIfPossible = exports.getNewEmptyObject = exports.getNewEmptyChild = exports.getShallowClone = exports.callIfFunction = exports.isGlobalConstructor = exports.isCloneable = exports.assignFallback = exports.getOwnProperties = exports.reduce = exports.cloneArray = exports.isArray = void 0; | ||
@@ -11,4 +11,8 @@ var _pathington = require("pathington"); | ||
var create = O.create, | ||
getOwnPropertySymbols = O.getOwnPropertySymbols, | ||
getPrototypeOf = O.getPrototypeOf, | ||
keys = O.keys; | ||
keys = O.keys, | ||
propertyIsEnumerable = O.propertyIsEnumerable; | ||
var toStringObject = O.prototype.toString; | ||
var toStringFunction = Function.prototype.toString; | ||
/** | ||
@@ -19,3 +23,3 @@ * @constant {Symbol} REACT_ELEMENT | ||
var REACT_ELEMENT = typeof Symbol === 'function' && Symbol.for ? Symbol.for('react.element') : 0xeac7; | ||
var REACT_ELEMENT = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('react.element') : 0xeac7; | ||
/** | ||
@@ -77,9 +81,37 @@ * @constant {RegExp} FUNCTION_NAME | ||
/** | ||
* @function assign | ||
* @function getOwnProperties | ||
* | ||
* @description | ||
* a slimmer, faster version of Object.assign | ||
* get the own properties of an object, either keys or symbols | ||
* | ||
* @param {Object} object the object to get all keys and symbols of | ||
* @returns {Array<string|symbol>} the own properties of the object | ||
*/ | ||
exports.reduce = reduce; | ||
var getOwnProperties = function getOwnProperties(object) { | ||
var ownSymbols = getOwnPropertySymbols(object); | ||
if (!ownSymbols.length) { | ||
return keys(object); | ||
} | ||
return keys(object).concat(reduce(ownSymbols, function (enumerableSymbols, symbol) { | ||
if (propertyIsEnumerable.call(object, symbol)) { | ||
enumerableSymbols.push(symbol); | ||
} | ||
return enumerableSymbols; | ||
}, [])); | ||
}; | ||
/** | ||
* @function assignFallback | ||
* | ||
* @description | ||
* a simple implementation of Object.assign | ||
* | ||
* @param {Object} target the target object | ||
* @param {Array<Object>} sources the objects to merge into target | ||
* @param {Object} source the object to merge into target | ||
* @returns {Object} the shallowly-merged object | ||
@@ -89,16 +121,17 @@ */ | ||
exports.reduce = reduce; | ||
exports.getOwnProperties = getOwnProperties; | ||
var assign = function assign(target) { | ||
for (var _len = arguments.length, sources = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
sources[_key - 1] = arguments[_key]; | ||
var assignFallback = function assignFallback(target, source) { | ||
if (!source) { | ||
return target; | ||
} | ||
return reduce(sources, function (assigned, object) { | ||
return object ? reduce(keys(object), function (assignedTarget, key) { | ||
assignedTarget[key] = object[key]; | ||
return assignedTarget; | ||
}, assigned) : assigned; | ||
}, target); | ||
return reduce(getOwnProperties(source), function (clonedObject, property) { | ||
clonedObject[property] = source[property]; | ||
return clonedObject; | ||
}, Object(target)); | ||
}; | ||
exports.assignFallback = assignFallback; | ||
var assign = typeof O.assign === 'function' ? O.assign : assignFallback; | ||
/** | ||
@@ -108,3 +141,7 @@ * @function isCloneable | ||
* @description | ||
* can the object be merged | ||
* can the object be cloned | ||
* | ||
* - the object exists and is an object | ||
* - the object is not a Date or RegExp | ||
* - the object is not a React element | ||
* | ||
@@ -115,7 +152,9 @@ * @param {*} object the object to test | ||
var isCloneable = function isCloneable(object) { | ||
if (!object || typeof object !== 'object') { | ||
return false; | ||
} | ||
exports.assign = assign; | ||
var isCloneable = function isCloneable(object) { | ||
return !!object && typeof object === 'object' && !(object instanceof Date || object instanceof RegExp) && object.$$typeof !== REACT_ELEMENT; | ||
var type = toStringObject.call(object); | ||
return type !== '[object Date]' && type !== '[object RegExp]' && object.$$typeof !== REACT_ELEMENT; | ||
}; | ||
@@ -136,3 +175,3 @@ /** | ||
var isGlobalConstructor = function isGlobalConstructor(fn) { | ||
return typeof fn === 'function' && global[fn.name || Function.prototype.toString.call(fn).split(FUNCTION_NAME)[1]] === fn; | ||
return typeof fn === 'function' && global[fn.name || toStringFunction.call(fn).split(FUNCTION_NAME)[1]] === fn; | ||
}; | ||
@@ -172,6 +211,11 @@ /** | ||
var getShallowClone = function getShallowClone(object) { | ||
return object.constructor === O ? assign({}, object) : isArray(object) ? cloneArray(object) : isGlobalConstructor(object.constructor) ? {} : reduce(keys(object), function (clone, key) { | ||
clone[key] = object[key]; | ||
return clone; | ||
}, create(getPrototypeOf(object))); | ||
if (object.constructor === O) { | ||
return assign({}, object); | ||
} | ||
if (isArray(object)) { | ||
return cloneArray(object); | ||
} | ||
return isGlobalConstructor(object.constructor) ? {} : assign(create(getPrototypeOf(object)), object); | ||
}; | ||
@@ -248,3 +292,3 @@ /** | ||
* get the value if it is not undefined, else get the fallback | ||
* | ||
*` | ||
* @param {any} value the main value to return | ||
@@ -316,9 +360,16 @@ * @param {any} fallbackValue the value to return if main is undefined | ||
var isObject1Array = isArray(object1); | ||
return isObject1Array !== isArray(object2) || !isCloneable(object1) ? cloneIfPossible(object2) : isObject1Array ? object1.concat(object2.map(cloneIfPossible)) : reduce(keys(object2), function (clone, key) { | ||
if (isObject1Array !== isArray(object2) || !isCloneable(object1)) { | ||
return cloneIfPossible(object2); | ||
} | ||
if (isObject1Array) { | ||
return object1.concat(object2); | ||
} | ||
var target = object1.constructor === O || isGlobalConstructor(object1.constructor) ? {} : create(getPrototypeOf(object1)); | ||
return reduce(getOwnProperties(object2), function (clone, key) { | ||
clone[key] = isDeep && isCloneable(object2[key]) ? getMergedObject(object1[key], object2[key], isDeep) : object2[key]; | ||
return clone; | ||
}, reduce(keys(object1), function (clone, key) { | ||
clone[key] = cloneIfPossible(object1[key]); | ||
return clone; | ||
}, object1.constructor === O ? {} : create(getPrototypeOf(object1)))); | ||
}, assign(target, object1)); | ||
}; | ||
@@ -359,3 +410,8 @@ /** | ||
var parsedPath = getParsedPath(path); | ||
return parsedPath.length === 1 ? object ? callIfFunction(object[parsedPath[0]], context, parameters) : void 0 : onMatchAtPath(parsedPath, object, function (ref, key) { | ||
if (parsedPath.length === 1) { | ||
return object ? callIfFunction(object[parsedPath[0]], context, parameters) : void 0; | ||
} | ||
return onMatchAtPath(parsedPath, object, function (ref, key) { | ||
return callIfFunction(ref[key], context, parameters); | ||
@@ -381,3 +437,8 @@ }); | ||
var parsedPath = getParsedPath(path); | ||
return parsedPath.length === 1 ? object ? getCoalescedValue(object[parsedPath[0]], noMatchValue) : noMatchValue : onMatchAtPath(parsedPath, object, function (ref, key) { | ||
if (parsedPath.length === 1) { | ||
return object ? getCoalescedValue(object[parsedPath[0]], noMatchValue) : noMatchValue; | ||
} | ||
return onMatchAtPath(parsedPath, object, function (ref, key) { | ||
return getCoalescedValue(ref[key], noMatchValue); | ||
@@ -467,8 +528,8 @@ }, false, noMatchValue); | ||
while (index < length) { | ||
while (index < length - 1) { | ||
array[index] = array[index + 1]; | ||
index++; | ||
++index; | ||
} | ||
array.length--; | ||
--array.length; | ||
} | ||
@@ -475,0 +536,0 @@ }; |
@@ -98,3 +98,3 @@ { | ||
"sideEffects": false, | ||
"version": "1.5.0" | ||
"version": "1.5.1" | ||
} |
# unchanged | ||
A tiny (~1.8kB minified+gzipped), [fast](https://github.com/planttheidea/unchanged/blob/master/benchmark_results.csv), unopinionated handler for updating JS objects and arrays immutably. | ||
A tiny (~1.9kB minified+gzipped), [fast](https://github.com/planttheidea/unchanged/blob/master/benchmark_results.csv), unopinionated handler for updating JS objects and arrays immutably. | ||
@@ -15,2 +15,3 @@ Supports nested key paths via path arrays or [dot-bracket syntax](https://github.com/planttheidea/pathington), and all methods are curriable (with placeholder support) for composability. Can be a drop-in replacement for the `lodash/fp` methods `get`, `set`, `merge`, and `omit` with a 90% smaller footprint. | ||
- [remove](#remove) | ||
- [has](#has) | ||
- [add](#add) | ||
@@ -151,2 +152,22 @@ - [merge](#merge) | ||
#### has | ||
`has(path: (Array<number|string>|number|string), object: (Array<any>|object)): boolean` | ||
Returns `true` if the object has the path provided, `false` otherwise. | ||
```javascript | ||
const object = { | ||
foo: [ | ||
{ | ||
bar: "baz" | ||
} | ||
] | ||
}; | ||
console.log(has("foo[0].bar", object)); // true | ||
console.log(has(["foo", 0, "bar"], object)); // true | ||
console.log(has("bar", object)); // false | ||
``` | ||
#### add | ||
@@ -153,0 +174,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
140526
1992
532