ast-delete-object
Version:
Delete all plain objects in AST if they contain a certain key/value pair
52 lines (50 loc) • 14.4 kB
JavaScript
/**
* @name ast-delete-object
* @fileoverview Delete all plain objects in AST if they contain a certain key/value pair
* @version 4.0.29
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ast-delete-object/}
*/
;var astDeleteObject=(()=>{var ne=Object.create;var S=Object.defineProperty,ie=Object.defineProperties,se=Object.getOwnPropertyDescriptor,oe=Object.getOwnPropertyDescriptors,ae=Object.getOwnPropertyNames,J=Object.getOwnPropertySymbols,ce=Object.getPrototypeOf,R=Object.prototype.hasOwnProperty,ue=Object.prototype.propertyIsEnumerable;var L=(e,t,s)=>t in e?S(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s,d=(e,t)=>{for(var s in t||(t={}))R.call(t,s)&&L(e,s,t[s]);if(J)for(var s of J(t))ue.call(t,s)&&L(e,s,t[s]);return e},j=(e,t)=>ie(e,oe(t));var H=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),le=(e,t)=>{for(var s in t)S(e,s,{get:t[s],enumerable:!0})},V=(e,t,s,l)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of ae(t))!R.call(e,u)&&u!==s&&S(e,u,{get:()=>t[u],enumerable:!(l=se(t,u))||l.enumerable});return e};var $=(e,t,s)=>(s=e!=null?ne(ce(e)):{},V(t||!e||!e.__esModule?S(s,"default",{value:e,enumerable:!0}):s,e)),fe=e=>V(S({},"__esModule",{value:!0}),e);var T=H((Ae,K)=>{"use strict";K.exports=pe;function O(e){return e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length)}function pe(e){if(e=e||{},e.circles)return ye(e);let t=new Map;if(t.set(Date,a=>new Date(a)),t.set(Map,(a,n)=>new Map(l(Array.from(a),n))),t.set(Set,(a,n)=>new Set(l(Array.from(a),n))),e.constructorHandlers)for(let a of e.constructorHandlers)t.set(a[0],a[1]);let s=null;return e.proto?f:u;function l(a,n){let r=Object.keys(a),i=new Array(r.length);for(let c=0;c<r.length;c++){let o=r[c],p=a[o];typeof p!="object"||p===null?i[o]=p:p.constructor!==Object&&(s=t.get(p.constructor))?i[o]=s(p,n):ArrayBuffer.isView(p)?i[o]=O(p):i[o]=n(p)}return i}function u(a){if(typeof a!="object"||a===null)return a;if(Array.isArray(a))return l(a,u);if(a.constructor!==Object&&(s=t.get(a.constructor)))return s(a,u);let n={};for(let r in a){if(Object.hasOwnProperty.call(a,r)===!1)continue;let i=a[r];typeof i!="object"||i===null?n[r]=i:i.constructor!==Object&&(s=t.get(i.constructor))?n[r]=s(i,u):ArrayBuffer.isView(i)?n[r]=O(i):n[r]=u(i)}return n}function f(a){if(typeof a!="object"||a===null)return a;if(Array.isArray(a))return l(a,f);if(a.constructor!==Object&&(s=t.get(a.constructor)))return s(a,f);let n={};for(let r in a){let i=a[r];typeof i!="object"||i===null?n[r]=i:i.constructor!==Object&&(s=t.get(i.constructor))?n[r]=s(i,f):ArrayBuffer.isView(i)?n[r]=O(i):n[r]=f(i)}return n}}function ye(e){let t=[],s=[],l=new Map;if(l.set(Date,r=>new Date(r)),l.set(Map,(r,i)=>new Map(f(Array.from(r),i))),l.set(Set,(r,i)=>new Set(f(Array.from(r),i))),e.constructorHandlers)for(let r of e.constructorHandlers)l.set(r[0],r[1]);let u=null;return e.proto?n:a;function f(r,i){let c=Object.keys(r),o=new Array(c.length);for(let p=0;p<c.length;p++){let b=c[p],g=r[b];if(typeof g!="object"||g===null)o[b]=g;else if(g.constructor!==Object&&(u=l.get(g.constructor)))o[b]=u(g,i);else if(ArrayBuffer.isView(g))o[b]=O(g);else{let A=t.indexOf(g);A!==-1?o[b]=s[A]:o[b]=i(g)}}return o}function a(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return f(r,a);if(r.constructor!==Object&&(u=l.get(r.constructor)))return u(r,a);let i={};t.push(r),s.push(i);for(let c in r){if(Object.hasOwnProperty.call(r,c)===!1)continue;let o=r[c];if(typeof o!="object"||o===null)i[c]=o;else if(o.constructor!==Object&&(u=l.get(o.constructor)))i[c]=u(o,a);else if(ArrayBuffer.isView(o))i[c]=O(o);else{let p=t.indexOf(o);p!==-1?i[c]=s[p]:i[c]=a(o)}}return t.pop(),s.pop(),i}function n(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return f(r,n);if(r.constructor!==Object&&(u=l.get(r.constructor)))return u(r,n);let i={};t.push(r),s.push(i);for(let c in r){let o=r[c];if(typeof o!="object"||o===null)i[c]=o;else if(o.constructor!==Object&&(u=l.get(o.constructor)))i[c]=u(o,n);else if(ArrayBuffer.isView(o))i[c]=O(o);else{let p=t.indexOf(o);p!==-1?i[c]=s[p]:i[c]=n(o)}}return t.pop(),s.pop(),i}}});var Q=H((W,D)=>{(function(e,t){typeof W=="object"&&typeof D<"u"?D.exports=t():typeof define=="function"&&define.amd?define(t):(e=typeof globalThis<"u"?globalThis:e||self,e.typeDetect=t())})(W,function(){"use strict";var e=typeof Promise=="function",t=function(y){if(typeof globalThis=="object")return globalThis;Object.defineProperty(y,"typeDetectGlobalObject",{get:function(){return this},configurable:!0});var k=typeDetectGlobalObject;return delete y.typeDetectGlobalObject,k}(Object.prototype),s=typeof Symbol<"u",l=typeof Map<"u",u=typeof Set<"u",f=typeof WeakMap<"u",a=typeof WeakSet<"u",n=typeof DataView<"u",r=s&&typeof Symbol.iterator<"u",i=s&&typeof Symbol.toStringTag<"u",c=u&&typeof Set.prototype.entries=="function",o=l&&typeof Map.prototype.entries=="function",p=c&&Object.getPrototypeOf(new Set().entries()),b=o&&Object.getPrototypeOf(new Map().entries()),g=r&&typeof Array.prototype[Symbol.iterator]=="function",A=g&&Object.getPrototypeOf([][Symbol.iterator]()),B=r&&typeof String.prototype[Symbol.iterator]=="function",Y=B&&Object.getPrototypeOf(""[Symbol.iterator]()),ee=8,te=-1;function re(y){var k=typeof y;if(k!=="object")return k;if(y===null)return"null";if(y===t)return"global";if(Array.isArray(y)&&(i===!1||!(Symbol.toStringTag in y)))return"Array";if(typeof window=="object"&&window!==null){if(typeof window.location=="object"&&y===window.location)return"Location";if(typeof window.document=="object"&&y===window.document)return"Document";if(typeof window.navigator=="object"){if(typeof window.navigator.mimeTypes=="object"&&y===window.navigator.mimeTypes)return"MimeTypeArray";if(typeof window.navigator.plugins=="object"&&y===window.navigator.plugins)return"PluginArray"}if((typeof window.HTMLElement=="function"||typeof window.HTMLElement=="object")&&y instanceof window.HTMLElement){if(y.tagName==="BLOCKQUOTE")return"HTMLQuoteElement";if(y.tagName==="TD")return"HTMLTableDataCellElement";if(y.tagName==="TH")return"HTMLTableHeaderCellElement"}}var M=i&&y[Symbol.toStringTag];if(typeof M=="string")return M;var m=Object.getPrototypeOf(y);return m===RegExp.prototype?"RegExp":m===Date.prototype?"Date":e&&m===Promise.prototype?"Promise":u&&m===Set.prototype?"Set":l&&m===Map.prototype?"Map":a&&m===WeakSet.prototype?"WeakSet":f&&m===WeakMap.prototype?"WeakMap":n&&m===DataView.prototype?"DataView":l&&m===b?"Map Iterator":u&&m===p?"Set Iterator":g&&m===A?"Array Iterator":B&&m===Y?"String Iterator":m===null?"Object":Object.prototype.toString.call(y).slice(ee,te)}return re})});var je={};le(je,{defaults:()=>X,deleteObj:()=>Se,version:()=>we});var Z=$(T(),1);var w=$(Q(),1);var z=$(T(),1);var _=$(T(),1);var ke=(0,_.default)();function h(e){if(e==null||typeof e!="object")return!1;let t=Object.getPrototypeOf(e);return t!==null&&t!==Object.prototype&&Object.getPrototypeOf(t)!==null?!1:!(Symbol.iterator in e)&&!(Symbol.toStringTag in e)}function he(e){return typeof e=="string"}function U(e,t){return h(e)&&he(t)&&t in e}function N(e){if(e.includes(".")){let t=e.lastIndexOf(".");if(!e.slice(0,t).includes("."))return e.slice(0,t);for(let s=t-1;s--;)if(e[s]===".")return e.slice(s+1,t)}return null}var P=(0,z.default)();function E(e,t){let s={now:!1};function l(u,f,a,n){let r=P(u),i,c=d({depth:-1,path:""},a);if(c.depth+=1,Array.isArray(r))for(let o=0,p=r.length;o<p&&!n.now;o++){let b=c.path?`${c.path}.${o}`:`${o}`;r[o]!==void 0?(c.parent=P(r),c.parentType="array",c.parentKey=N(b),i=l(f(r[o],void 0,j(d({},c),{path:b}),n),f,j(d({},c),{path:b}),n),Number.isNaN(i)&&o<r.length?(r.splice(o,1),o-=1):r[o]=i):r.splice(o,1)}else if(h(r))for(let o in r){if(n.now&&o!=null)break;let p=c.path?`${c.path}.${o}`:o;c.depth===0&&o!=null&&(c.topmostKey=o),c.parent=P(r),c.parentType="object",c.parentKey=N(p),i=l(f(o,r[o],j(d({},c),{path:p}),n),f,j(d({},c),{path:p}),n),Number.isNaN(i)?delete r[o]:r[o]=i}return r}return l(e,t,{},s)}function v(e){if(typeof e=="string")return!e.trim();if(!["object","string"].includes(typeof e)||!e)return!1;let t=!0;return e=E(e,(s,l,u,f)=>{let a=l!==void 0?l:s;return typeof a=="string"&&a.trim()&&(t=!1,f.now=!0),a}),t}function C(e){if(typeof e!="string")throw new TypeError("Expected a string");return e.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d")}var F=new Map,G=(e,t)=>{if(!Array.isArray(e))switch(typeof e){case"string":e=[e];break;case"undefined":e=[];break;default:throw new TypeError(`Expected '${t}' to be a string or an array, but got a type of '${typeof e}'`)}return e.filter(s=>{if(typeof s!="string"){if(typeof s>"u")return!1;throw new TypeError(`Expected '${t}' to be an array of strings, but found a type of '${typeof s}' in the array`)}return!0})},de=(e,t)=>{t=d({caseSensitive:!1},t);let s=e+JSON.stringify(t);if(F.has(s))return F.get(s);let l=e[0]==="!";l&&(e=e.slice(1)),e=C(e).replace(/\\\*/g,"[\\s\\S]*");let u=new RegExp(`^${e}$`,t.caseSensitive?"":"i");return u.negated=l,F.set(s,u),u},ge=(e,t,s,l)=>{if(e=G(e,"inputs"),t=G(t,"patterns"),t.length===0)return[];t=t.map(a=>de(a,s));let{allPatterns:u}=s||{},f=[];for(let a of e){let n,r=[...t].fill(!1);for(let[i,c]of t.entries())if(c.test(a)&&(r[i]=!0,n=!c.negated,!n))break;if(!(n===!1||n===void 0&&t.some(i=>!i.negated)||u&&r.some((i,c)=>!i&&!t[c].negated))&&(f.push(a),l))break}return f};function I(e,t,s){return ge(e,t,s,!0).length>0}function me(e){return h(e)?!Object.keys(e).length:Array.isArray(e)||typeof e=="string"?!e.length:!1}var be={hungryForWhitespace:!1,matchStrictly:!1,verboseWhenMismatches:!1,useWildcards:!1};function x(e,t,s){let l,u,f,a=0,n=d(d({},be),s);if(n.hungryForWhitespace&&n.matchStrictly&&h(e)&&v(e)&&h(t)&&!Object.keys(t).length)return!0;if((!n.hungryForWhitespace||n.hungryForWhitespace&&!v(e)&&v(t))&&h(e)&&Object.keys(e).length!==0&&h(t)&&Object.keys(t).length===0||(0,w.default)(e)!==(0,w.default)(t)&&(!n.hungryForWhitespace||n.hungryForWhitespace&&!v(e)))return!1;if(typeof e=="string"&&typeof t=="string")return n.hungryForWhitespace&&v(e)&&v(t)?!0:n.verboseWhenMismatches?e===t?!0:`Given string ${t} is not matched! We have ${e} on the other end.`:n.useWildcards?I(e,t,{caseSensitive:!0}):e===t;if(Array.isArray(e)&&Array.isArray(t)){if(n.hungryForWhitespace&&v(t)&&(!n.matchStrictly||n.matchStrictly&&e.length===t.length))return!0;if(!n.hungryForWhitespace&&t.length>e.length||n.matchStrictly&&t.length!==e.length)return n.verboseWhenMismatches?`The length of a given array, ${JSON.stringify(t,null,4)} is ${t.length} but the length of an array on the other end, ${JSON.stringify(e,null,4)} is ${e.length}`:!1;if(t.length===0)return e.length===0?!0:n.verboseWhenMismatches?`The given array has no elements, but the array on the other end, ${JSON.stringify(e,null,4)} does have some`:!1;for(let r=0,i=t.length;r<i;r++){f=!1;for(let c=a,o=e.length;c<o;c++)if(a+=1,x(e[c],t[r],n)===!0){f=!0;break}if(!f)return n.verboseWhenMismatches?`The given array ${JSON.stringify(t,null,4)} is not a subset of an array on the other end, ${JSON.stringify(e,null,4)}`:!1}}else if(h(e)&&h(t)){if(l=new Set(Object.keys(t)),u=new Set(Object.keys(e)),n.matchStrictly&&l.size!==u.size){if(!n.verboseWhenMismatches)return!1;let r=new Set([...l].filter(p=>!u.has(p))),i=r.size?` First object has unique keys: ${JSON.stringify(r,null,4)}.`:"",c=new Set([...u].filter(p=>!l.has(p))),o=c.size?` Second object has unique keys:
${JSON.stringify(c,null,4)}.`:"";return`When matching strictly, we found that both objects have different amount of keys.${i}${o}`}for(let r of l){if(!U(e,r))return!n.useWildcards||n.useWildcards&&!r.includes("*")?n.verboseWhenMismatches?`The given object has key "${r}" which the other-one does not have.`:!1:Object.keys(e).some(i=>I(i,r,{caseSensitive:!0}))?!0:n.verboseWhenMismatches?`The given object has key "${r}" which the other-one does not have.`:!1;if(e[r]!=null&&(0,w.default)(e[r])!==(0,w.default)(t[r])){if(!(v(e[r])&&v(t[r])&&n.hungryForWhitespace))return n.verboseWhenMismatches?`The given key ${r} is of a different type on both objects. On the first-one, it's ${(0,w.default)(t[r])}, on the second-one, it's ${(0,w.default)(e[r])}`:!1}else if(x(e[r],t[r],n)!==!0)return n.verboseWhenMismatches?`The given piece ${JSON.stringify(t[r],null,4)} and ${JSON.stringify(e[r],null,4)} don't match.`:!1}}else return n.hungryForWhitespace&&v(e)&&v(t)&&(!n.matchStrictly||n.matchStrictly&&me(t))?!0:e===t;return!0}var q="4.0.29";var we=q,Oe=(0,Z.default)(),X={matchKeysStrictly:!1,hungryForWhitespace:!1};function Se(e,t,s){if(!e)throw new Error("ast-delete-object/deleteObj(): [THROW_ID_01] Missing resolvedInput!");if(!t)throw new Error("ast-delete-object/deleteObj(): [THROW_ID_02] Missing second argument, object to search for and delete!");if(s&&!h(s))throw new Error("ast-delete-object/deleteObj(): [THROW_ID_03] Third argument, options object, must be an object!");let l=d(d({},X),s),u=Oe(e),f;return h(u)&&h(t)&&x(u,t,{hungryForWhitespace:l.hungryForWhitespace,matchStrictly:l.matchKeysStrictly})?{}:(u=E(u,(a,n)=>{if(f=n!==void 0?n:a,h(f)){if(h(t)&&h(f)&&!Object.keys(t).length&&!Object.keys(f).length)return NaN;if(x(f,t,{hungryForWhitespace:l.hungryForWhitespace,matchStrictly:l.matchKeysStrictly}))return NaN}return f}),u)}return fe(je);})();
/**
* @name codsen-utils
* @fileoverview Various utility functions
* @version 1.6.7
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/codsen-utils/}
*/
/**
* @name ast-monkey-util
* @fileoverview Utility library of AST helper functions
* @version 3.0.10
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ast-monkey-util/}
*/
/**
* @name ast-monkey-traverse
* @fileoverview Utility library to traverse AST
* @version 4.0.19
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ast-monkey-traverse/}
*/
/**
* @name ast-contains-only-empty-space
* @fileoverview Does AST contain only empty space?
* @version 4.0.19
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ast-contains-only-empty-space/}
*/
/**
* @name ast-compare
* @fileoverview Compare anything: AST, objects, arrays, strings and nested thereof
* @version 4.0.30
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ast-compare/}
*/