UNPKG

updeep

Version:

Easily update nested frozen objects and arrays in a declarative and immutable manner.

3 lines (2 loc) 4.42 kB
import n from"lodash/reject";import t from"lodash/isPlainObject";import r from"lodash/omitBy";import e from"lodash/forEach";import o from"lodash/map";import u from"lodash/mapValues";import c from"lodash/omit";function i(n){return function(n){return null!==n&&!(n instanceof RegExp)&&!(n instanceof ArrayBuffer)&&(Array.isArray(n)||"object"==typeof n)}(n)&&!Object.isFrozen(n)}function f(n){return Object.freeze(n),Object.keys(n).forEach(t=>{const r=n[t];i(r)&&f(r)}),n}function s(n){return"undefined"==typeof process||"production"===process.env.NODE_ENV||"dangerously_never_freeze"===process.env.UPDEEP_MODE||i(n)&&f(n),n}function a(n){const t=s(n);return()=>t}function l(t){return Array.isArray(t)?t:n(`${t}`.split("."),n=>!n)}const p="@@updeep/placeholder";function y(n,t){let r=n.length;for(r>t&&(r=t);n[r-1]===p;)r-=1;return r}function h(n){return function t(r,...e){const[o,u]=e;return y(arguments)>=1?n(r,o,u):t}}function d(n){return function t(r,e,...o){const[u,c]=o,i=y(arguments,2);if(e===p||u===p||c===p)throw new Error("Can only use placeholder on first argument of this function.");return i>=2?r===p?h((t,r,o)=>n(t,e,r,o)):n(r,e,u,c):1===i?h((t,e,o)=>n(r,t,e,o)):t}}function m(n){return function t(r,e,o,...u){const[c,i]=u,f=y(arguments,3);if(o===p||c===p||i===p)throw new Error("Can only use placeholder on first or second argument of this function.");return f>=3?r===p?e===p?d((t,r,e,u)=>n(t,r,o,e,u)):h((t,r,u)=>n(t,e,o,r,u)):e===p?h((t,e,u)=>n(r,t,o,e,u)):n(r,e,o,c,i):2===f?r===p?d((t,r,o,u)=>n(t,e,r,o,u)):h((t,o,u)=>n(r,e,t,o,u)):1===f?d((t,e,o,u)=>n(r,t,e,o,u)):t}}function g(n){return function t(r,e,o,u,...c){const[i,f]=c,s=y(arguments,4);if(u===p||i===p||f===p)throw new Error("Can only use placeholder on first, second or third argument of this function.");return s>=4?r===p?e===p?o===p?m((t,r,e,o,c)=>n(t,r,e,u,o,c)):d((t,r,e,c)=>n(t,r,o,u,e,c)):o===p?d((t,r,o,c)=>n(t,e,r,u,o,c)):h((t,r,c)=>n(t,e,o,u,r,c)):e===p?o===p?d((t,e,o,c)=>n(r,t,e,u,o,c)):h((t,e,c)=>n(r,t,o,u,e,c)):o===p?h((t,o,c)=>n(r,e,t,u,o,c)):n(r,e,o,u,i,f):3===s?r===p?e===p?m((t,r,e,u,c)=>n(t,r,o,e,u,c)):d((t,r,u,c)=>n(t,e,o,r,u,c)):e===p?d((t,e,u,c)=>n(r,t,o,e,u,c)):h((t,u,c)=>n(r,e,o,t,u,c)):2===s?r===p?m((t,r,o,u,c)=>n(t,e,r,o,u,c)):d((t,o,u,c)=>n(r,e,t,o,u,c)):1===s?m((t,e,o,u,c)=>n(r,t,e,o,u,c)):t}}function O(n,t=n.length){return[n,h,d,m,g][t](n)}var j=O(function(n,t,r){const e=l(n);let o=r;for(let n=0;n<e.length;n+=1){if(void 0===o)return!1;o=o[e[n]]}return"function"==typeof t?t(o):t===o});function b(){return b=Object.assign?Object.assign.bind():function(n){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(n[e]=r[e])}return n},b.apply(this,arguments)}function A(n,t=n.length){return O((...t)=>s(n(...t)),t)}const E={__omitted:!0},v=a(E);var w=A(function n(e,o,...u){if("function"==typeof e)return e(o,...u);if(!t(e))return e;const c=null==o?{}:o,i=function(t,r){return function(n,t,r){return Object.keys(n).reduce((r,e)=>t(r,n[e],e),{})}(t,(t,e,o)=>{let u=e;return Array.isArray(e)||null===e||"object"!=typeof e?"function"==typeof e&&(u=e(r[o])):u=n(e,r[o]),function(n,t,r){return n===E?r.hasOwnProperty(t):r[t]!==n}(u,o,r)&&(t[o]=u),t})}(e,c);return function(n){return!Object.keys(n).length}(i)?c:Array.isArray(c)?function(n,t){const r=[...t];return Object.keys(n).forEach(t=>{r[t]=n[t]}),r}(i,c).filter(n=>n!==E):r(b({},c,i),n=>n===E)},2),_=A(function(n,t,r,e){const o="function"==typeof n?n(e):n;return w(o?t:r,e)}),P=O((n,t,r)=>_(n,t,n=>n,r)),k=A(function(n,t){const r="function"==typeof n?n:w(n),c=(Array.isArray(t)?o:u)(t,r);return function(n,t){let r=!0;return e(t,(t,e)=>{if(t!==n[e])return r=!1,!1}),r}(t,c)?t:c});function z(n){return Array.isArray(n)?n.length:Object.keys(n).length}var D=A(function(n,t){const r=c(t,n);return null==t||z(r)!==z(t)?r:t}),B=A(function(n,t){const e=r(t,n);return null==t||z(e)!==z(t)?e:t}),C=A(function(t,r){const e=n(r,t);return r.length===e.length?r:e});const x="*";function N(n,t){return t===x?t=>Object.prototype.hasOwnProperty.call(t,x)?w({[x]:n},t):k(n,t):{[t]:n}}var R=O(function(n,t,r){const e=l(n).reduceRight(N,t);return w(e,r)}),V=O(function(n,t,r){return w(t,void 0===r?n:r)});const F=w;F._=p,F.constant=a,F.if=P,F.ifElse=_,F.is=j,F.freeze=s,F.map=k,F.omit=D,F.omitBy=B,F.reject=C,F.update=w,F.updateIn=R,F.omitted=v,F.withDefault=V;export{F as default}; //# sourceMappingURL=index.modern.mjs.map