UNPKG

ast-delete-object

Version:

Delete all plain objects in AST if they contain a certain key/value pair

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