UNPKG

deepdash

Version:

➔ 𝐃eep standalone lib / 𝐋odash extension: ✓ eachDeep ✓ filterDeep ✓ mapDeep ✓ reduceDeep ✓ pickDeep ✓ omitDeep ✓ keysDeep ✓ index ✓ condenseDeep ⋮ Parents stack ⋮ Circular check ⋮ Leaves only mode ⋮ Children mode ⋮ cherry-pick ⋮ esm

3 lines (2 loc) 16.1 kB
var deepdash=function(){"use strict";function e(e){return function(r,n,t){if(!e[r])if(e.mixin){var i={};i[r]=n,e.mixin(i,{chain:t})}else e[r]=n;return e}}function r(e){return function(e){for(var r=[],n=0;n<e.length;n++)n in e||r.push(n);for(var t=r.length;t--;)e.splice(r[t],1);return e}}var n=/\D/,t=/^[a-zA-Z_$]+([\w_$]*)$/,i=/"/g;function a(){for(var e=[],r=arguments.length;r--;)e[r]=arguments[r];return e.reduce((function(e,r){return e?!r||r.startsWith("[")?""+e+r:e+"."+r:r}),"")}function o(e){return function(r){for(var o=[],l=arguments.length-1;l-- >0;)o[l]=arguments[l+1];return o=o.filter((function(e){return void 0!==e})),e.isString(r)?a.apply(void 0,o.concat([r])):Array.isArray(r)?(o=a.apply(void 0,o),r.reduce((function(e,r){var a=typeof r;return"number"===a?r<0||r%1!=0?e+'["'+r+'"]':e+"["+r+"]":"string"!==a?e+'["'+r+'"]':r?n.test(r)?t.test(r)?e?e+"."+r:""+e+r:e+'["'+r.replace(i,'\\"')+'"]':e+"["+r+"]":e+'[""]'}),o)):void 0}}function l(e){var r=typeof e;return null!=e&&("object"==r||"function"==r)}o.notChainable=!0;var c=/^[a-zA-Z_$]+([\w_$]*)$/,h=/"/g,u=Object.prototype.hasOwnProperty;var f=function(e,r,n){this.obj=e,this._options=r,this.break=n},p={path:{configurable:!0},parent:{configurable:!0},parents:{configurable:!0},depth:{configurable:!0},isLeaf:{configurable:!0},isCircular:{configurable:!0},circularParentIndex:{configurable:!0},circularParent:{configurable:!0},childrenPath:{configurable:!0},info:{configurable:!0}};function s(e,r){var n={isObject:l(e)};return n.isArray=n.isObject&&Array.isArray(e),n.isEmpty=n.isArray?!e.length:!n.isObject||function(e,r){for(var n in e)if(!r||u.call(e,n))return!1;return!0}(e,r),n}function d(e){var r=function(e){var r=o(e);return function(i){var a=i.options,o=i.obj,l=i.callback;a.pathFormatArray="array"==a.pathFormat,i.depth=0;for(var c=!1,h=function(){return c=!0,!1};i&&!c;){if(!i.inited){if(i.inited=!0,i.info=s(i.value,a.ownPropertiesOnly),a.checkCircular&&(i.circularParentIndex=-1,i.circularParent=null,i.isCircular=!1,i.info.isObject&&!i.info.isEmpty))for(var u=i.parent;u;){if(u.value===i.value){i.isCircular=!0,i.circularParent=u,i.circularParentIndex=i.depth-u.depth-1;break}u=u.parent}if(i.children=[],a.childrenPath&&a.childrenPath.forEach((function(r,n){var t=e.get(i.value,r),o=s(t,a.ownPropertiesOnly);o.isEmpty||i.children.push([r,a.strChildrenPath[n],t,o])})),i.isLeaf=i.isCircular||void 0!==a.childrenPath&&!i.children.length||!i.info.isObject||i.info.isEmpty,i.needCallback=(i.depth||a.includeRoot)&&(!a.leavesOnly||i.isLeaf),i.needCallback){var p=new f(o,a,h);p.setItem(i,!1);try{i.res=l(i.value,i.key,i.parent&&i.parent.value,p)}catch(e){throw e.message&&(e.message+="\ncallback failed before deep iterate at:\n"+r(i.path)),e}}if(c)break;!1!==i.res&&(c||i.isCircular||!i.info.isObject||(void 0===a.childrenPath||!i.depth&&a.rootIsChildren?i.childrenItems=i.info.isArray?n(i,i.value,a,[],""):t(i,i.value,a,[],""):(i.childrenItems=[],i.children.length&&i.children.forEach((function(e){var r=e[0],o=e[1],l=e[2],c=e[3];i.childrenItems=i.childrenItems.concat(c.isArray?n(i,l,a,r,o):t(i,l,a,r,o))}))))),i.currentChildIndex=-1}if(i.childrenItems&&i.currentChildIndex<i.childrenItems.length-1)i.currentChildIndex++,i.childrenItems[i.currentChildIndex].parentItem=i,i=i.childrenItems[i.currentChildIndex];else{if(i.needCallback&&a.callbackAfterIterate){var d=new f(o,a,h);d.setItem(i,!0);try{l(i.value,i.key,i.parent&&i.parent.value,d)}catch(e){throw e.message&&(e.message+="\ncallback failed after deep iterate at:\n"+r(i.path)),e}}i=i.parentItem}}};function n(e,r,n,t,i){var a;n.pathFormatArray||(a=e.strPath||"",i&&a&&!i.startsWith("[")&&(a+="."),a+=i||"");for(var o=[],l=0;l<r.length;l++){var c=r[l];if(void 0!==c||l in r){var h=void 0,u=!n.pathFormatArray;u&&(h=a+"["+l+"]"),o.push({value:c,key:l+"",path:(e.path||[]).concat(t,[l+""]),strPath:h,depth:e.depth+1,parent:{value:e.value,key:e.key,path:u?e.strPath:e.path,parent:e.parent,depth:e.depth,info:e.info},childrenPath:t.length&&t||void 0,strChildrenPath:i||void 0})}}return o}function t(e,r,n,t,i){var a;n.pathFormatArray||(a=e.strPath||"",i&&a&&!i.startsWith("[")&&(a+="."),a+=i||"");var o=[],l=!n.pathFormatArray;for(var f in r)if(!n.ownPropertiesOnly||u.call(r,f)){var p=void 0;l&&(p=c.test(f)?a?a+"."+f:""+f:a+'["'+f.replace(h,'\\"')+'"]'),o.push({value:r[f],key:f,path:(e.path||[]).concat(t,[f]),strPath:p,depth:e.depth+1,parent:{value:e.value,key:e.key,path:l?e.strPath:e.path,parent:e.parent,depth:e.depth,info:e.info},childrenPath:t.length&&t||void 0,strChildrenPath:i||void 0})}return o}}(e);return function(n,t,i){if(void 0===t&&(t=e.identity),void 0!==(i=e.merge({includeRoot:!Array.isArray(n),pathFormat:"string",checkCircular:!1,leavesOnly:!1,ownPropertiesOnly:!0},i||{})).childrenPath){if(i.includeRoot||void 0!==i.rootIsChildren||(i.rootIsChildren=Array.isArray(n)),!e.isString(i.childrenPath)&&!Array.isArray(i.childrenPath))throw Error("childrenPath can be string or array");e.isString(i.childrenPath)&&(i.childrenPath=[i.childrenPath]),i.strChildrenPath=i.childrenPath,i.childrenPath=[];for(var a=i.strChildrenPath.length-1;a>=0;a--)i.childrenPath[a]=e.toPath(i.strChildrenPath[a])}return r({value:n,callback:t,options:i,obj:n}),n}}function v(e){var r=d(e),n=function(e){for(var r=[],n=0;n<e.length;n++)n in e||r.push(n);for(var t=r.length;t--;)e.splice(r[t],1);return e},t=e.each||e.forArray;return function(i,a){var o={checkCircular:(a=e.merge({checkCircular:!1},a||{})).checkCircular,ownPropertiesOnly:a.ownPropertiesOnly},l=[];return r(i,(function(e,r,n,t){!t.isCircular&&Array.isArray(e)&&l.push(e)}),o),Array.isArray(i)&&l.push(i),t(l,n),i}}function y(e){return function(r,n){var t=(n=Array.isArray(n)?e.clone(n):e.toPath(n)).pop(),i=n.length?e.get(r,n):r;return void 0!==i&&t in i}}function C(e){var r=d(e),n=function(e){for(var r=[],n=0;n<e.length;n++)n in e||r.push(n);for(var t=r.length;t--;)e.splice(r[t],1);return e};return function(i,a,o){a=e.iteratee(a),o?void 0!==(o=e.cloneDeep(o)).leafsOnly&&(o.leavesOnly=o.leafsOnly):o={},o.onTrue||(o.onTrue={}),o.onFalse||(o.onFalse={}),o.onUndefined||(o.onUndefined={}),void 0!==o.childrenPath&&(void 0===o.onTrue.skipChildren&&(o.onTrue.skipChildren=!1),void 0===o.onUndefined.skipChildren&&(o.onUndefined.skipChildren=!1),void 0===o.onFalse.skipChildren&&(o.onFalse.skipChildren=!1),void 0===o.onTrue.cloneDeep&&(o.onTrue.cloneDeep=!0),void 0===o.onUndefined.cloneDeep&&(o.onUndefined.cloneDeep=!0),void 0===o.onFalse.cloneDeep&&(o.onFalse.cloneDeep=!0));var c={pathFormat:(o=e.merge({checkCircular:!1,keepCircular:!0,leavesOnly:void 0===o.childrenPath,condense:!0,cloneDeep:e.cloneDeep,pathFormat:"string",onTrue:{skipChildren:!0,cloneDeep:!0,keepIfEmpty:!0},onUndefined:{skipChildren:!1,cloneDeep:!1,keepIfEmpty:!1},onFalse:{skipChildren:!0,cloneDeep:!1,keepIfEmpty:!1}},o)).pathFormat,checkCircular:o.checkCircular,childrenPath:o.childrenPath,includeRoot:o.includeRoot,rootIsChildren:o.rootIsChildren,ownPropertiesOnly:o.ownPropertiesOnly,callbackAfterIterate:!0,leavesOnly:!1},h=Array.isArray(i),u=h?[]:l(i)?{}:null,f=!!o.condense&&[];if(r(i,(function(e,r,n,i){if(!i.afterIterate){if(i.info._filterDeep={},i.isCircular){var c=t(i,u);return o.keepCircular?i.info._filterDeep.res=c[r]="replaceCircularBy"in o?o.replaceCircularBy:void 0!==i.circularParent.path?i.circularParent.info._filterDeep.res:u:(delete c[r],f&&(c===i.parent.info._filterDeep.res&&i.parent.info.isArray||Array.isArray(c))&&!i.parent.info._filterDeep.isSparse&&(i.parent.info._filterDeep.isSparse=!0,f.push(i.parent.info)),i.info._filterDeep.excluded=!0),!1}var h=!o.leavesOnly||i.isLeaf?a(e,r,n,i):void 0;if(l(h)||(h=void 0===h?o.onUndefined:h?o.onTrue:o.onFalse),i.info._filterDeep.reply=h,i.info._filterDeep.empty=void 0===h.empty||h.empty,h.keepIfEmpty||!h.skipChildren)if(o.cloneDeep&&h.cloneDeep)if(void 0!==i.path){var p=t(i,u);i.info._filterDeep.res=p[r]=o.cloneDeep(e)}else i.info._filterDeep.res=u=o.cloneDeep(e);else if(void 0!==i.path){var s=t(i,u);i.info._filterDeep.res=s[r]=i.info.isArray?[]:i.info.isObject?{}:e}else i.info._filterDeep.res=u=i.info.isArray?[]:i.info.isObject?{}:e;return!h.skipChildren}if(!i.afterIterate||i.isCircular);else{var d=i.info._filterDeep.reply;if(i.info._filterDeep.empty&&!d.keepIfEmpty)if(void 0===i.path)u=null;else{var v=t(i,u);delete v[r],f&&(v===i.parent.info._filterDeep.res&&i.parent.info.isArray||Array.isArray(v))&&!i.parent.info._filterDeep.isSparse&&(i.parent.info._filterDeep.isSparse=!0,f.push(i.parent.info)),i.info._filterDeep.excluded=!0}else for(var y=i.parent;y&&(y.info._filterDeep.reply||(y.info._filterDeep.reply=o.onUndefined),y.info._filterDeep.empty);)y.info._filterDeep.empty=!1,y=y.parent}}),c),f){for(var p=0;p<f.length;p++){var s=f[p];s._filterDeep.isSparse&&!s._filterDeep.excluded&&n(s.children)}h&&n(u)}return!h||u.length||c.includeRoot?u:null};function t(e,r){if(e.parent.info.children)return e.parent.info.children;e.parent.info._filterDeep||(e.parent.info._filterDeep={});var n=e.parent.info._filterDeep.res;if(void 0===n&&(n=e.parent.info._filterDeep.res=r),e._item.childrenPath)for(var t=e.parent.value,i=0;i<e._item.childrenPath.length;i++){var a=e._item.childrenPath[i];t=t[a],n[a]||(n[a]=Array.isArray(t)?[]:{}),n=n[a]}return e.parent.info.children=n,n}}function P(e){var r=d(e);return function(n,t,i){t=e.iteratee(t),i?void 0!==(i=e.cloneDeep(i)).leafsOnly&&(i.leavesOnly=i.leafsOnly):i={};var a,o={pathFormat:(i=e.merge({checkCircular:!1,leavesOnly:void 0===i.childrenPath,pathFormat:"string"},i)).pathFormat,checkCircular:i.checkCircular,ownPropertiesOnly:i.ownPropertiesOnly,childrenPath:i.childrenPath,includeRoot:i.includeRoot,rootIsChildren:i.rootIsChildren,callbackAfterIterate:!1,leavesOnly:i.leavesOnly};return r(n,(function(e,r,n,i){if(t(e,r,n,i))return a={value:e,key:r,parent:n,context:i},i.break()}),o),a}}function m(e){var r=P(e);return function(e,n,t){var i=r(e,n,t);return i&&i.context.path}}function g(e){var r=P(e);return function(e,n,t){var i=r(e,n,t);return i&&i.value}}function b(e){return d(e)}function D(e){var r=d(e);return function(n,t){(t=e.merge({checkCircular:!1,includeCircularPath:!0,leavesOnly:!t||void 0===t.childrenPath},t||{}))&&void 0!==t.leafsOnly&&(t.leavesOnly=t.leafsOnly);var i={pathFormat:"string",checkCircular:t.checkCircular,ownPropertiesOnly:t.ownPropertiesOnly,includeRoot:t.includeRoot,childrenPath:t.childrenPath,rootIsChildren:t.rootIsChildren,leavesOnly:t.leavesOnly},a={};return r(n,(function(e,r,n,i){i.isCircular&&!t.includeCircularPath||void 0!==i.path&&(a[i.path]=e)}),i),a}}function k(e){var r=d(e);return function(n,t){t&&void 0!==t.leafsOnly&&(t.leavesOnly=t.leafsOnly);var i={pathFormat:(t=e.merge({checkCircular:!1,includeCircularPath:!0,leavesOnly:!t||void 0===t.childrenPath,pathFormat:"string"},t||{})).pathFormat,checkCircular:t.checkCircular,ownPropertiesOnly:t.ownPropertiesOnly,includeRoot:t.includeRoot,childrenPath:t.childrenPath,rootIsChildren:t.rootIsChildren,leavesOnly:t.leavesOnly},a=[];return r(n,(function(e,r,n,i){i.isCircular&&!t.includeCircularPath||void 0!==i.path&&a.push(i.path)}),i),a}}function _(e){return k(e)}function O(e){var r=d(e);return function(e,n,t,i){var a=void 0!==t;return r(e,(function(e,r,i,o){a?t=n(t,e,r,i,o):(t=e,a=!0)}),i),t}}function A(e){var r=O(e);return function(n,t,i){return t=e.iteratee(t),r(n,(function(e,r,n,i,a){return e.push(t(r,n,i,a)),e}),[],i)}}function I(e){var r=d(e),n=o(e);return function(t,i,a){void 0===a&&(a={}),i=e.iteratee(i),a.cloneDeep=a.cloneDeep||e.cloneDeep,a.callbackAfterIterate=!1;var o=[];r(t,(function(e,r,t,l){if(void 0!==r){var c=i(e,r,t,l)+"";if(c!==r){var h=l.path,u="array"===a.pathFormat?JSON.stringify(h):h,f="array"===a.pathFormat?(l.parent.path||[]).concat(l.childrenPath||[],[c]):n([c],l.parent.path,l.childrenPath),p="array"===a.pathFormat?JSON.stringify(f):f;o[l.depth-1]||(o[l.depth-1]=[]),o[l.depth-1].push({oldPath:h,oldPathStr:u,newPath:f,newPathStr:p})}}}),a);for(var l=a.cloneDeep(t),c=o.length,h=function(){if(o[c]){var r={};o[c].forEach((function(n){var t,i=n.oldPath,a=n.oldPathStr,o=n.newPath,c=n.newPathStr;if(Object.prototype.hasOwnProperty.call(r,a))t=r[a],delete r[a];else{if(void 0===(t=e.get(l,i))&&!e.has(l,i))return;e.unset(l,i)}e.has(l,o)&&!Object.prototype.hasOwnProperty.call(r,c)&&(r[c]=e.get(l,o)),e.set(l,o,t)}))}};c--;)h();return l}}function F(e){var r=d(e);return function(n,t,i){t=e.iteratee(t);var a,o=Array.isArray(n)?[]:e.isObject(n)?{}:e.clone(n);return r(n,(function(r,n,i,l){l.skipChildren=function(e){a=e},a=void 0;var c=t(r,n,i,l);if(l.isLeaf||void 0!==a||(a=r!==c&&Array.isArray(r)!=Array.isArray(c)),void 0!==l.path?e.set(o,l.path,c):o=c,a)return!1}),i),o}}function w(e){var r=o(e);return function(n,t){var i,a;e.isString(n)?i=n:a=n,t=Array.isArray(t)?e.cloneDeep(t):[t];for(var o=0;o<t.length;o++)if(e.isString(t[o])&&(t[o]=e.toPath(t[o])),Array.isArray(t[o])){if(void 0===a&&(a=e.toPath(i)),a.length>=t[o].length&&e.isEqual(e.takeRight(a,t[o].length),t[o]))return t[o]}else{if(!(t[o]instanceof RegExp))throw new Error("To match path use only string/regex or array of them.");if(void 0===i&&(i=r(n)),t[o].test(i))return t[o]}return!1}}function x(e){var r=w(e),n=C(e);return function(t,i,a){var o=!(a=e.merge({invert:!1},a||{})).invert;return(a=e.merge({onMatch:{cloneDeep:!1,skipChildren:!1,keepIfEmpty:!o},onNotMatch:{cloneDeep:!1,skipChildren:!1,keepIfEmpty:o}},a)).leavesOnly=!1,a.childrenPath=void 0,a.includeRoot=void 0,a.pathFormat="array",a.onTrue=a.invert?a.onMatch:a.onNotMatch,a.onFalse=a.invert?a.onNotMatch:a.onMatch,n(t,(function(e,n,t,o){return!1!==r(o.path,i)?a.invert:!a.invert}),a)}}function E(e){var r=x(e);return function(n,t,i){return(i=e.merge({invert:!1},i||{})).invert=!0,r(n,t,i)}}function j(e){var r=P(e);return function(e,n,t){return!!r(e,n,t)}}return f.prototype.setItem=function(e,r){this._item=e,this.afterIterate=r},p.path.get=function(){return this._options.pathFormatArray?this._item.path:this._item.strPath},p.parent.get=function(){return this._item.parent},p.parents.get=function(){if(!this._item._parents){this._item._parents=[];for(var e=this._item.parent;e;)this._item._parents[e.depth]=e,e=e.parent}return this._item._parents},p.depth.get=function(){return this._item.depth},p.isLeaf.get=function(){return this._item.isLeaf},p.isCircular.get=function(){return this._item.isCircular},p.circularParentIndex.get=function(){return this._item.circularParentIndex},p.circularParent.get=function(){return this._item.circularParent},p.childrenPath.get=function(){return void 0!==this._options.childrenPath&&(this._options.pathFormatArray?this._item.childrenPath:this._item.strChildrenPath)||void 0},p.info.get=function(){return this._item.info},Object.defineProperties(f.prototype,p),y.notChainable=!0,w.notChainable=!0,function(n){return function(n){e(n)("condense",(function(e){for(var r=[],n=0;n<e.length;n++)n in e||r.push(n);for(var t=r.length;t--;)e.splice(r[t],1);return e}),!r.notChainable)}(n),function(r){e(r)("condenseDeep",v(r),!v.notChainable)}(n),function(r){e(r)("eachDeep",d(r),!d.notChainable)}(n),function(r){e(r)("exists",y(r),!y.notChainable)}(n),function(r){e(r)("filterDeep",C(r),!C.notChainable)}(n),function(r){e(r)("findDeep",P(r),!P.notChainable)}(n),function(r){e(r)("findPathDeep",m(r),!m.notChainable)}(n),function(r){e(r)("findValueDeep",g(r),!g.notChainable)}(n),function(r){e(r)("forEachDeep",b(r),!b.notChainable)}(n),function(r){e(r)("index",D(r),!D.notChainable)}(n),function(r){e(r)("keysDeep",_(r),!_.notChainable)}(n),function(r){e(r)("mapDeep",A(r),!A.notChainable)}(n),function(r){e(r)("mapKeysDeep",I(r),!I.notChainable)}(n),function(r){e(r)("mapValuesDeep",F(r),!F.notChainable)}(n),function(r){e(r)("omitDeep",x(r),!x.notChainable)}(n),function(r){e(r)("pathMatches",w(r),!w.notChainable)}(n),function(r){e(r)("pathToString",o(r),!o.notChainable)}(n),function(r){e(r)("paths",k(r),!k.notChainable)}(n),function(r){e(r)("pickDeep",E(r),!E.notChainable)}(n),function(r){e(r)("reduceDeep",O(r),!O.notChainable)}(n),function(r){e(r)("someDeep",j(r),!j.notChainable)}(n),n}}(); //# sourceMappingURL=deepdash.min.js.map