UNPKG

object-hierarchy-access

Version:

Get/Set value from/to JS object hierarchy properties

2 lines (1 loc) 7.7 kB
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((r="undefined"!=typeof globalThis?globalThis:r||self).ObjectHierarchyAccess={})}(this,(function(r){"use strict";function e(r){return Array.isArray(r)||r instanceof Array}function n(r){return"object"==typeof r&&null!==r}function t(r){var e=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r).filter((function(e){var n=Object.getOwnPropertyDescriptor(r,e);return n&&n.enumerable}));n.length&&e.push.apply(e,n)}return e}function a(r){return e(r)?[]:n(r)?{}:r}function u(r,e){var n=e.name,t=e.getName;return n!==undefined?n:t?t.call(r,r):void 0}function o(r,n){var a=n.names,u=n.getNames;if(a!==undefined)return e(a)?a:[a];if(u){var o=u.call(r,r);if(o!==undefined)return e(o)?o:[o]}return t(r)}function f(r,e,n){var t=r;return e.forEach((function(r){var e=function(r){return"object"==typeof r&&null!==r?r:"function"==typeof r?{getName:r,value:{}}:{name:r,value:{}}}(r),a=e.value,o=e.type,f=e.create,c=e.override,i=e.created,l=e.skipped,p=e.got,v=function(r,e){var n=u(r,e);return n!==undefined?n:"undefined"}(t,e);if(n||c||!t[v]||"object"!=typeof t[v]||"__proto__"===v&&t[v]===Object.prototype){var h=a||(o?new o:f?f.call(t,t,v):{});t[v]=h,i&&i.call(t,t,v,h)}else l&&l.call(t,t,v,t[v]);var s=t;t=t[v],p&&p.call(s,s,v,t)})),t}function c(r,e){return f(r,e)}function i(r,e){var n=f(r,e.slice(0,-1));return{current:n,last:f(n,e.slice(-1),!0)}}function l(r){var e=r[r.length-1],n=Array.prototype.concat.apply([],r.slice(0,-1));return{hierarchies:n.slice(0,-1),prop:n[n.length-1],value:e}}function p(r){return Array.prototype.concat.apply([],r)}function v(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=p(e),a=r||{};return i(a,t),a}function h(r){return"object"==typeof r?r:"function"==typeof r?{getValue:r}:{name:r}}function s(r,e){var n,t=e.getValue,a=u(r,e);a!==undefined?n=r[a]:(a="undefined",t&&(n=t.call(r,r)));var o=e.got;return o&&o.call(r,r,a,n),{name:a,value:n}}function y(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=Array.prototype.concat.apply([],e),a=r;return a!==undefined&&null!==a&&t.every((function(r){var e=h(r),n=s(a,e).value;return a=n})),a}function d(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];if(r===undefined||null===r)return!1;for(var t=Array.prototype.concat.apply([],e),a=r,u=0;u<t.length;u++){var o=t[u];if(!a||!(o in a))return!1;a=a[o]}return!0}function g(r){var e=r[r.length-1];return{hierarchies:Array.prototype.concat.apply([],r.slice(0,-1)),callback:e}}function m(r){return e(r)?{names:r}:"object"==typeof r&&null!==r?r:"function"==typeof r?{getNames:r}:{names:r}}function b(r,e,n){var t=n.got,a=n.mapName,u=n.mapValue,o=n.mapped,f=r[e];t&&t.call(r,r,e,f);var c=a?a.call(r,r,e,f):e,i=u?u.call(r,r,e,f):f;return o&&o.call(r,r,c,i),{mappedName:c,mappedValue:i}}function j(r,e,n,t){var u=m(n[t]),f=o(r,u),c=n.length-1;f.forEach((function(o){if(o in r){var f=b(r,o,u),i=f.mappedName,l=f.mappedValue;e[i]=t<c?a(l):l,t<c&&"object"==typeof l&&null!==l&&j(l,e[i],n,t+1)}}))}function A(r,e,n,t){var a=m(n[t]),u=o(r,a),f=n.length-1;u.forEach((function(u){if(u in r){var o=b(r,u,a).mappedValue;t<f?A(o,e,n,t+1):e.push(o)}}))}function O(r){return"object"==typeof r&&null!==r?r:"function"==typeof r?{by:r}:{}}function k(r,e){var n=r.type,t=r.create;return n?new n:t?t.call(e,e):{}}function P(r,a){void 0===a&&(a=16);var u=[];if(n(r))for(var o=e(r)?Array:Object,f=[r],c=function(r){var a=Array,c=[];if(f.forEach((function(r){t(r).forEach((function(t){var u=r[t];n(u)&&(e(u)||(a=Object),c.push(u))}))})),u.push(o),!c.length)return"break";o=a,f=c},i=0;i<=a;i++){if("break"===c())break}return u}r.array2map=function(r,n,t){if(e(r)){for(var a={},u=0;u<r.length;u++){var o=r[u],f=y(o,n),c=y(o,t);a[f]=c}return a}},r.assign=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=l(e),a=t.hierarchies,u=t.prop,o=t.value,f=c(r,a);return f[u]=o,f},r.assignIfUndef=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=l(e),a=t.hierarchies,u=t.prop,o=t.value,f=c(r,a);return f[u]===undefined&&(f[u]=o),f},r.assignProp=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=p(e),a=i(r,t).current;return a},r.assignPropIfUndef=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=p(e),a=c(r,t.slice(0,-1));return c(a,t.slice(-1)),a},r.exist=d,r.get=y,r.group=function(r){for(var n=[],u=1;u<arguments.length;u++)n[u-1]=arguments[u];if(!n.length)return r;var o=Array.prototype.concat.apply([],n).map(O).filter((function(r){return r.by}));if(!o.length)return r;var f,c=o.length-1,i=t(r);return i.forEach((function(n){var t,u,i=r[n];o.forEach((function(o,l){var p=o.by;0===l?(f||(f=k(o,r)),t=f):(t[u]||(t[u]=k(o,r)),t=t[u]);var v=p.call(r,r,n,i);if(l!==c)u=v;else{t[v]||(t[v]=a(r));var h=t[v];e(h)?h.push(i):h[n]=i}}))})),f},r.map2array=function(r,e,n){if(r){var a=[];return t(r).forEach((function(t){var u,o=r[t],f="function"==typeof e?e.call(r,r,t,o):e,c="function"==typeof n?n.call(r,r,t,o):n;a.push(((u={})[f]=t,u[c]=o,u))})),a}},r.pick=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=[],a=r;return a!==undefined&&null!==a&&A(a,t,e,0),t},r.put=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=l(e),a=t.hierarchies,u=t.prop,o=t.value,f=c(r,a);return f[u]=o,o},r.putIfUndef=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=l(e),a=t.hierarchies,u=t.prop,o=t.value,f=c(r,a);return f[u]===undefined&&(f[u]=o),f[u]},r.putProp=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=p(e),a=i(r,t).last;return a},r.putPropIfUndef=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=p(e);return c(r,t)},r.redim=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];if(!r)return r;var a=Array.prototype.concat.apply([],e);if(!a.length)return r;var u=Math.min.apply(Math,a);if(!(u<0)){var o=Math.max.apply(Math,a),f=a.length-1,c=P(r,o);if(c.length&&!(o>=c.length)){var i=new c[a[0]],l=function e(r,n,u){if(u<=o)t(n).forEach((function(t){var a=u+1;d(n,t)&&e(r.concat(t),n[t],a)}));else{var l=a.map((function(e,t){return t<f?{name:r[e],type:c[a[t+1]]}:{name:r[e],value:n}}));v(i,l)}};return l([],r,0),i}}},r.select=function(r){for(var e,n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];var u=r;return u!==undefined&&null!==u&&j(u,e=a(u),n,0),e},r.set=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=l(e),a=t.hierarchies,u=t.prop,o=t.value,f=r||{},i=c(f,a);return i[u]=o,f},r.setIfUndef=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=l(e),a=t.hierarchies,u=t.prop,o=t.value,f=r||{},i=c(f,a);return i[u]===undefined&&(i[u]=o),f},r.setProp=v,r.setPropIfUndef=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=p(e),a=r||{};return c(a,t),a},r.traverse=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=g(e),a=t.hierarchies,u=t.callback,o=r;o!==undefined&&null!==o&&a.every((function(r){var e=h(r),n=s(o,e),t=n.name,a=n.value,f=o;return o=a,!1!==u.call(f,f,t,o)}))},r.traverseReverse=function(r){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var t=g(e),a=t.hierarchies,u=t.callback,o=r;if(o!==undefined&&null!==o){var f=[];a.every((function(r){var e=h(r),n=s(o,e),t=n.name,a=n.value,u=o;return o=a,f.push({parent:u,name:t,current:o}),o}));for(var c=f.length-1;c>=0;c--){var i=f[c],l=u.call(i.parent,i.parent,i.name,i.current);if(!1===l)break}}},Object.defineProperty(r,"__esModule",{value:!0})}));