@thi.ng/paths
Advanced tools
Comparing version 2.1.1 to 2.1.2
@@ -6,2 +6,10 @@ # Change Log | ||
## [2.1.2](https://github.com/thi-ng/umbrella/compare/@thi.ng/paths@2.1.1...@thi.ng/paths@2.1.2) (2019-07-31) | ||
**Note:** Version bump only for package @thi.ng/paths | ||
## [2.1.1](https://github.com/thi-ng/umbrella/compare/@thi.ng/paths@2.1.0...@thi.ng/paths@2.1.1) (2019-07-12) | ||
@@ -8,0 +16,0 @@ |
@@ -1,222 +0,1 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@thi.ng/checks'), require('@thi.ng/errors')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@thi.ng/checks', '@thi.ng/errors'], factory) : | ||
(global = global || self, factory((global.thi = global.thi || {}, global.thi.ng = global.thi.ng || {}, global.thi.ng.paths = {}), global.thi.ng.checks, global.thi.ng.errors)); | ||
}(this, function (exports, checks, errors) { 'use strict'; | ||
const isa = Array.isArray; | ||
const iss = checks.isString; | ||
const _copy = (s) => (isa(s) ? s.slice() : Object.assign({}, s)); | ||
const compS = (k, f) => (s, v) => ((s = _copy(s)), (s[k] = f ? f(s[k], v) : v), s); | ||
const toPath = (path) => isa(path) | ||
? path | ||
: iss(path) | ||
? path.length > 0 | ||
? path.split(".") | ||
: [] | ||
: path != null | ||
? [path] | ||
: []; | ||
const exists = (obj, path) => { | ||
if (obj == null) { | ||
return false; | ||
} | ||
path = toPath(path); | ||
for (let n = path.length - 1, i = 0; i <= n; i++) { | ||
const k = path[i]; | ||
if (!obj.hasOwnProperty(k)) { | ||
return false; | ||
} | ||
obj = obj[k]; | ||
if (obj == null && i < n) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
const getter = (path) => { | ||
const ks = toPath(path); | ||
let [a, b, c, d] = ks; | ||
switch (ks.length) { | ||
case 0: | ||
return (s) => s; | ||
case 1: | ||
return (s) => (s != null ? s[a] : undefined); | ||
case 2: | ||
return (s) => s != null ? ((s = s[a]) != null ? s[b] : undefined) : undefined; | ||
case 3: | ||
return (s) => s != null | ||
? (s = s[a]) != null | ||
? (s = s[b]) != null | ||
? s[c] | ||
: undefined | ||
: undefined | ||
: undefined; | ||
case 4: | ||
return (s) => s != null | ||
? (s = s[a]) != null | ||
? (s = s[b]) != null | ||
? (s = s[c]) != null | ||
? s[d] | ||
: undefined | ||
: undefined | ||
: undefined | ||
: undefined; | ||
default: | ||
return (s) => { | ||
const n = ks.length - 1; | ||
let res = s; | ||
for (let i = 0; res != null && i <= n; i++) { | ||
res = res[ks[i]]; | ||
} | ||
return res; | ||
}; | ||
} | ||
}; | ||
const setter = (path) => { | ||
const ks = toPath(path); | ||
let [a, b, c, d] = ks; | ||
switch (ks.length) { | ||
case 0: | ||
return (_, v) => v; | ||
case 1: | ||
return (s, v) => ((s = _copy(s)), (s[a] = v), s); | ||
case 2: | ||
return (s, v) => { | ||
let x; | ||
s = _copy(s); | ||
s[a] = x = _copy(s[a]); | ||
x[b] = v; | ||
return s; | ||
}; | ||
case 3: | ||
return (s, v) => { | ||
let x, y; | ||
s = _copy(s); | ||
s[a] = x = _copy(s[a]); | ||
x[b] = y = _copy(x[b]); | ||
y[c] = v; | ||
return s; | ||
}; | ||
case 4: | ||
return (s, v) => { | ||
let x, y, z; | ||
s = _copy(s); | ||
s[a] = x = _copy(s[a]); | ||
x[b] = y = _copy(x[b]); | ||
y[c] = z = _copy(y[c]); | ||
z[d] = v; | ||
return s; | ||
}; | ||
default: | ||
let f; | ||
for (let i = ks.length; --i >= 0;) { | ||
f = compS(ks[i], f); | ||
} | ||
return f; | ||
} | ||
}; | ||
const getIn = (state, path) => getter(path)(state); | ||
const setIn = (state, path, val) => setter(path)(state, val); | ||
const setInMany = (state, ...pairs) => { | ||
const n = pairs.length; | ||
n & 1 && errors.illegalArgs(`require even number of args (got ${pairs.length})`); | ||
for (let i = 0; i < n; i += 2) { | ||
state = setIn(state, pairs[i], pairs[i + 1]); | ||
} | ||
return state; | ||
}; | ||
const updater = (path, fn) => { | ||
const g = getter(path); | ||
const s = setter(path); | ||
return (state, ...args) => s(state, fn.apply(null, (args.unshift(g(state)), args))); | ||
}; | ||
const updateIn = (state, path, fn, ...args) => setter(path)(state, fn.apply(null, (args.unshift(getter(path)(state)), args))); | ||
const deleteIn = (state, path) => { | ||
const ks = [...toPath(path)]; | ||
if (ks.length > 0) { | ||
const k = ks.pop(); | ||
return updateIn(state, ks, (x) => ((x = Object.assign({}, x)), delete x[k], x)); | ||
} | ||
}; | ||
const mutator = (path) => { | ||
const ks = toPath(path); | ||
let [a, b, c, d] = ks; | ||
switch (ks.length) { | ||
case 0: | ||
return (_, x) => x; | ||
case 1: | ||
return (s, x) => (s ? ((s[a] = x), s) : undefined); | ||
case 2: | ||
return (s, x) => { | ||
let t; | ||
return s | ||
? (t = s[a]) | ||
? ((t[b] = x), s) | ||
: undefined | ||
: undefined; | ||
}; | ||
case 3: | ||
return (s, x) => { | ||
let t; | ||
return s | ||
? (t = s[a]) | ||
? (t = t[b]) | ||
? ((t[c] = x), s) | ||
: undefined | ||
: undefined | ||
: undefined; | ||
}; | ||
case 4: | ||
return (s, x) => { | ||
let t; | ||
return s | ||
? (t = s[a]) | ||
? (t = t[b]) | ||
? (t = t[c]) | ||
? ((t[d] = x), s) | ||
: undefined | ||
: undefined | ||
: undefined | ||
: undefined; | ||
}; | ||
default: | ||
return (s, x) => { | ||
let t = s; | ||
const n = ks.length - 1; | ||
for (let k = 0; k < n; k++) { | ||
if (!(t = t[ks[k]])) | ||
return; | ||
} | ||
t[ks[n]] = x; | ||
return s; | ||
}; | ||
} | ||
}; | ||
const mutIn = (state, path, val) => mutator(path)(state, val); | ||
const mutInMany = (state, ...pairs) => { | ||
const n = pairs.length; | ||
n & 1 && errors.illegalArgs(`require even number of args (got ${pairs.length})`); | ||
for (let i = 0; i < n && state; i += 2) { | ||
state = mutIn(state, pairs[i], pairs[i + 1]); | ||
} | ||
return state; | ||
}; | ||
exports.deleteIn = deleteIn; | ||
exports.exists = exists; | ||
exports.getIn = getIn; | ||
exports.getter = getter; | ||
exports.mutIn = mutIn; | ||
exports.mutInMany = mutInMany; | ||
exports.mutator = mutator; | ||
exports.setIn = setIn; | ||
exports.setInMany = setInMany; | ||
exports.setter = setter; | ||
exports.toPath = toPath; | ||
exports.updateIn = updateIn; | ||
exports.updater = updater; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/checks"),require("@thi.ng/errors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/checks","@thi.ng/errors"],t):t(((e=e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.paths={}),e.thi.ng.checks,e.thi.ng.errors)}(this,function(e,t,n){"use strict";const r=Array.isArray,l=t.isString,u=e=>r(e)?e.slice():Object.assign({},e),s=(e,t)=>(n,r)=>(n=u(n),n[e]=t?t(n[e],r):r,n),i=e=>r(e)?e:l(e)?e.length>0?e.split("."):[]:null!=e?[e]:[],o=e=>{const t=i(e);let[n,r,l,u]=t;switch(t.length){case 0:return e=>e;case 1:return e=>null!=e?e[n]:void 0;case 2:return e=>null!=e&&null!=(e=e[n])?e[r]:void 0;case 3:return e=>null!=e&&null!=(e=e[n])&&null!=(e=e[r])?e[l]:void 0;case 4:return e=>null!=e&&null!=(e=e[n])&&null!=(e=e[r])&&null!=(e=e[l])?e[u]:void 0;default:return e=>{const n=t.length-1;let r=e;for(let e=0;null!=r&&e<=n;e++)r=r[t[e]];return r}}},c=e=>{const t=i(e);let[n,r,l,o]=t;switch(t.length){case 0:return(e,t)=>t;case 1:return(e,t)=>(e=u(e),e[n]=t,e);case 2:return(e,t)=>{let l;return(e=u(e))[n]=l=u(e[n]),l[r]=t,e};case 3:return(e,t)=>{let s,i;return(e=u(e))[n]=s=u(e[n]),s[r]=i=u(s[r]),i[l]=t,e};case 4:return(e,t)=>{let s,i,c;return(e=u(e))[n]=s=u(e[n]),s[r]=i=u(s[r]),i[l]=c=u(i[l]),c[o]=t,e};default:let e;for(let n=t.length;--n>=0;)e=s(t[n],e);return e}},a=(e,t,n)=>c(t)(e,n),h=(e,t,n,...r)=>c(t)(e,n.apply(null,(r.unshift(o(t)(e)),r))),g=e=>{const t=i(e);let[n,r,l,u]=t;switch(t.length){case 0:return(e,t)=>t;case 1:return(e,t)=>e?(e[n]=t,e):void 0;case 2:return(e,t)=>{let l;return e&&(l=e[n])?(l[r]=t,e):void 0};case 3:return(e,t)=>{let u;return e&&(u=e[n])&&(u=u[r])?(u[l]=t,e):void 0};case 4:return(e,t)=>{let s;return e&&(s=e[n])&&(s=s[r])&&(s=s[l])?(s[u]=t,e):void 0};default:return(e,n)=>{let r=e;const l=t.length-1;for(let e=0;e<l;e++)if(!(r=r[t[e]]))return;return r[t[l]]=n,e}}},f=(e,t,n)=>g(t)(e,n);e.deleteIn=(e,t)=>{const n=[...i(t)];if(n.length>0){const t=n.pop();return h(e,n,e=>(e=Object.assign({},e),delete e[t],e))}},e.exists=(e,t)=>{if(null==e)return!1;for(let n=(t=i(t)).length-1,r=0;r<=n;r++){const l=t[r];if(!e.hasOwnProperty(l))return!1;if(null==(e=e[l])&&r<n)return!1}return!0},e.getIn=(e,t)=>o(t)(e),e.getter=o,e.mutIn=f,e.mutInMany=(e,...t)=>{const r=t.length;1&r&&n.illegalArgs(`require even number of args (got ${t.length})`);for(let n=0;n<r&&e;n+=2)e=f(e,t[n],t[n+1]);return e},e.mutator=g,e.setIn=a,e.setInMany=(e,...t)=>{const r=t.length;1&r&&n.illegalArgs(`require even number of args (got ${t.length})`);for(let n=0;n<r;n+=2)e=a(e,t[n],t[n+1]);return e},e.setter=c,e.toPath=i,e.updateIn=h,e.updater=(e,t)=>{const n=o(e),r=c(e);return(e,...l)=>r(e,t.apply(null,(l.unshift(n(e)),l)))},Object.defineProperty(e,"__esModule",{value:!0})}); |
{ | ||
"name": "@thi.ng/paths", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "immutable, optimized path-based object property / array accessors", | ||
@@ -17,22 +17,23 @@ "module": "./index.js", | ||
"scripts": { | ||
"build": "yarn clean && yarn build:es6 && yarn build:bundle", | ||
"build": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module", | ||
"build:release": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module all", | ||
"build:es6": "tsc --declaration", | ||
"build:bundle": "../../scripts/bundle-module", | ||
"test": "rimraf build && tsc -p test/tsconfig.json && nyc mocha build/test/*.js", | ||
"build:test": "rimraf build && tsc -p test/tsconfig.json", | ||
"test": "yarn build:test && mocha build/test/*.js", | ||
"cover": "yarn build:test && nyc mocha build/test/*.js && nyc report --reporter=lcov", | ||
"clean": "rimraf *.js *.d.ts .nyc_output build coverage doc lib", | ||
"cover": "yarn test && nyc report --reporter=lcov", | ||
"doc": "node_modules/.bin/typedoc --mode modules --out doc --ignoreCompilerErrors src", | ||
"pub": "yarn build && yarn publish --access public" | ||
"pub": "yarn build:release && yarn publish --access public" | ||
}, | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^12.0.8", | ||
"@types/node": "^12.6.3", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.0.0", | ||
"typedoc": "^0.14.2", | ||
"typescript": "^3.5.2" | ||
"typescript": "^3.5.3" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/checks": "^2.2.1", | ||
"@thi.ng/errors": "^1.1.1" | ||
"@thi.ng/checks": "^2.2.2", | ||
"@thi.ng/errors": "^1.1.2" | ||
}, | ||
@@ -65,3 +66,3 @@ "keywords": [ | ||
"sideEffects": false, | ||
"gitHead": "47075afc37f3a16adee7c903a2935304c7cf5daf" | ||
"gitHead": "53eec7988c378fc37ae140e7174f36ef9b6208fe" | ||
} |
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
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
67746
892
1
Updated@thi.ng/checks@^2.2.2
Updated@thi.ng/errors@^1.1.2