ast-compare
Version:
Compare anything: AST, objects, arrays, strings and nested thereof
44 lines (42 loc) • 13.3 kB
JavaScript
/**
* @name ast-compare
* @fileoverview Compare anything: AST, objects, arrays, strings and nested thereof
* @version 4.0.31
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ast-compare/}
*/
;var astCompare=(()=>{var re=Object.create;var $=Object.defineProperty,ne=Object.defineProperties,oe=Object.getOwnPropertyDescriptor,se=Object.getOwnPropertyDescriptors,ie=Object.getOwnPropertyNames,B=Object.getOwnPropertySymbols,ae=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty,le=Object.prototype.propertyIsEnumerable;var J=(e,t,s)=>t in e?$(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s,b=(e,t)=>{for(var s in t||(t={}))F.call(t,s)&&J(e,s,t[s]);if(B)for(var s of B(t))le.call(t,s)&&J(e,s,t[s]);return e},A=(e,t)=>ne(e,se(t));var I=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),ue=(e,t)=>{for(var s in t)$(e,s,{get:t[s],enumerable:!0})},K=(e,t,s,c)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of ie(t))!F.call(e,u)&&u!==s&&$(e,u,{get:()=>t[u],enumerable:!(c=oe(t,u))||c.enumerable});return e};var D=(e,t,s)=>(s=e!=null?re(ae(e)):{},K(t||!e||!e.__esModule?$(s,"default",{value:e,enumerable:!0}):s,e)),ce=e=>K($({},"__esModule",{value:!0}),e);var R=I((x,V)=>{(function(e,t){typeof x=="object"&&typeof V<"u"?V.exports=t():typeof define=="function"&&define.amd?define(t):(e=typeof globalThis<"u"?globalThis:e||self,e.typeDetect=t())})(x,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 j=typeDetectGlobalObject;return delete y.typeDetectGlobalObject,j}(Object.prototype),s=typeof Symbol<"u",c=typeof Map<"u",u=typeof Set<"u",p=typeof WeakMap<"u",a=typeof WeakSet<"u",n=typeof DataView<"u",r=s&&typeof Symbol.iterator<"u",o=s&&typeof Symbol.toStringTag<"u",l=u&&typeof Set.prototype.entries=="function",i=c&&typeof Map.prototype.entries=="function",f=l&&Object.getPrototypeOf(new Set().entries()),d=i&&Object.getPrototypeOf(new Map().entries()),g=r&&typeof Array.prototype[Symbol.iterator]=="function",S=g&&Object.getPrototypeOf([][Symbol.iterator]()),L=r&&typeof String.prototype[Symbol.iterator]=="function",X=L&&Object.getPrototypeOf(""[Symbol.iterator]()),Y=8,ee=-1;function te(y){var j=typeof y;if(j!=="object")return j;if(y===null)return"null";if(y===t)return"global";if(Array.isArray(y)&&(o===!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 E=o&&y[Symbol.toStringTag];if(typeof E=="string")return E;var h=Object.getPrototypeOf(y);return h===RegExp.prototype?"RegExp":h===Date.prototype?"Date":e&&h===Promise.prototype?"Promise":u&&h===Set.prototype?"Set":c&&h===Map.prototype?"Map":a&&h===WeakSet.prototype?"WeakSet":p&&h===WeakMap.prototype?"WeakMap":n&&h===DataView.prototype?"DataView":c&&h===d?"Map Iterator":u&&h===f?"Set Iterator":g&&h===S?"Array Iterator":L&&h===X?"String Iterator":h===null?"Object":Object.prototype.toString.call(y).slice(Y,ee)}return te})});var k=I((we,H)=>{"use strict";H.exports=fe;function w(e){return e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length)}function fe(e){if(e=e||{},e.circles)return pe(e);let t=new Map;if(t.set(Date,a=>new Date(a)),t.set(Map,(a,n)=>new Map(c(Array.from(a),n))),t.set(Set,(a,n)=>new Set(c(Array.from(a),n))),e.constructorHandlers)for(let a of e.constructorHandlers)t.set(a[0],a[1]);let s=null;return e.proto?p:u;function c(a,n){let r=Object.keys(a),o=new Array(r.length);for(let l=0;l<r.length;l++){let i=r[l],f=a[i];typeof f!="object"||f===null?o[i]=f:f.constructor!==Object&&(s=t.get(f.constructor))?o[i]=s(f,n):ArrayBuffer.isView(f)?o[i]=w(f):o[i]=n(f)}return o}function u(a){if(typeof a!="object"||a===null)return a;if(Array.isArray(a))return c(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 o=a[r];typeof o!="object"||o===null?n[r]=o:o.constructor!==Object&&(s=t.get(o.constructor))?n[r]=s(o,u):ArrayBuffer.isView(o)?n[r]=w(o):n[r]=u(o)}return n}function p(a){if(typeof a!="object"||a===null)return a;if(Array.isArray(a))return c(a,p);if(a.constructor!==Object&&(s=t.get(a.constructor)))return s(a,p);let n={};for(let r in a){let o=a[r];typeof o!="object"||o===null?n[r]=o:o.constructor!==Object&&(s=t.get(o.constructor))?n[r]=s(o,p):ArrayBuffer.isView(o)?n[r]=w(o):n[r]=p(o)}return n}}function pe(e){let t=[],s=[],c=new Map;if(c.set(Date,r=>new Date(r)),c.set(Map,(r,o)=>new Map(p(Array.from(r),o))),c.set(Set,(r,o)=>new Set(p(Array.from(r),o))),e.constructorHandlers)for(let r of e.constructorHandlers)c.set(r[0],r[1]);let u=null;return e.proto?n:a;function p(r,o){let l=Object.keys(r),i=new Array(l.length);for(let f=0;f<l.length;f++){let d=l[f],g=r[d];if(typeof g!="object"||g===null)i[d]=g;else if(g.constructor!==Object&&(u=c.get(g.constructor)))i[d]=u(g,o);else if(ArrayBuffer.isView(g))i[d]=w(g);else{let S=t.indexOf(g);S!==-1?i[d]=s[S]:i[d]=o(g)}}return i}function a(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return p(r,a);if(r.constructor!==Object&&(u=c.get(r.constructor)))return u(r,a);let o={};t.push(r),s.push(o);for(let l in r){if(Object.hasOwnProperty.call(r,l)===!1)continue;let i=r[l];if(typeof i!="object"||i===null)o[l]=i;else if(i.constructor!==Object&&(u=c.get(i.constructor)))o[l]=u(i,a);else if(ArrayBuffer.isView(i))o[l]=w(i);else{let f=t.indexOf(i);f!==-1?o[l]=s[f]:o[l]=a(i)}}return t.pop(),s.pop(),o}function n(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return p(r,n);if(r.constructor!==Object&&(u=c.get(r.constructor)))return u(r,n);let o={};t.push(r),s.push(o);for(let l in r){let i=r[l];if(typeof i!="object"||i===null)o[l]=i;else if(i.constructor!==Object&&(u=c.get(i.constructor)))o[l]=u(i,n);else if(ArrayBuffer.isView(i))o[l]=w(i);else{let f=t.indexOf(i);f!==-1?o[l]=s[f]:o[l]=n(i)}}return t.pop(),s.pop(),o}}});var Oe={};ue(Oe,{compare:()=>P,defaults:()=>_,version:()=>me});var v=D(R(),1);var U=D(k(),1);var Q=D(k(),1);var $e=(0,Q.default)();function O(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 ye(e){return typeof e=="string"}function q(e,t){return O(e)&&ye(t)&&t in e}function M(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 T=(0,U.default)();function z(e,t){let s={now:!1};function c(u,p,a,n){let r=T(u),o,l=b({depth:-1,path:""},a);if(l.depth+=1,Array.isArray(r))for(let i=0,f=r.length;i<f&&!n.now;i++){let d=l.path?`${l.path}.${i}`:`${i}`;r[i]!==void 0?(l.parent=T(r),l.parentType="array",l.parentKey=M(d),o=c(p(r[i],void 0,A(b({},l),{path:d}),n),p,A(b({},l),{path:d}),n),Number.isNaN(o)&&i<r.length?(r.splice(i,1),i-=1):r[i]=o):r.splice(i,1)}else if(O(r))for(let i in r){if(n.now&&i!=null)break;let f=l.path?`${l.path}.${i}`:i;l.depth===0&&i!=null&&(l.topmostKey=i),l.parent=T(r),l.parentType="object",l.parentKey=M(f),o=c(p(i,r[i],A(b({},l),{path:f}),n),p,A(b({},l),{path:f}),n),Number.isNaN(o)?delete r[i]:r[i]=o}return r}return c(e,t,{},s)}function m(e){if(typeof e=="string")return!e.trim();if(!["object","string"].includes(typeof e)||!e)return!1;let t=!0;return e=z(e,(s,c,u,p)=>{let a=c!==void 0?c:s;return typeof a=="string"&&a.trim()&&(t=!1,p.now=!0),a}),t}function W(e){if(typeof e!="string")throw new TypeError("Expected a string");return e.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d")}var N=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})},ge=(e,t)=>{t=b({caseSensitive:!1},t);let s=e+JSON.stringify(t);if(N.has(s))return N.get(s);let c=e[0]==="!";c&&(e=e.slice(1)),e=W(e).replace(/\\\*/g,"[\\s\\S]*");let u=new RegExp(`^${e}$`,t.caseSensitive?"":"i");return u.negated=c,N.set(s,u),u},he=(e,t,s,c)=>{if(e=G(e,"inputs"),t=G(t,"patterns"),t.length===0)return[];t=t.map(a=>ge(a,s));let{allPatterns:u}=s||{},p=[];for(let a of e){let n,r=[...t].fill(!1);for(let[o,l]of t.entries())if(l.test(a)&&(r[o]=!0,n=!l.negated,!n))break;if(!(n===!1||n===void 0&&t.some(o=>!o.negated)||u&&r.some((o,l)=>!o&&!t[l].negated))&&(p.push(a),c))break}return p};function C(e,t,s){return he(e,t,s,!0).length>0}var Z="4.0.31";var me=Z;function be(e){return O(e)?!Object.keys(e).length:Array.isArray(e)||typeof e=="string"?!e.length:!1}var _={hungryForWhitespace:!1,matchStrictly:!1,verboseWhenMismatches:!1,useWildcards:!1};function P(e,t,s){let c,u,p,a=0,n=b(b({},_),s);if(n.hungryForWhitespace&&n.matchStrictly&&O(e)&&m(e)&&O(t)&&!Object.keys(t).length)return!0;if((!n.hungryForWhitespace||n.hungryForWhitespace&&!m(e)&&m(t))&&O(e)&&Object.keys(e).length!==0&&O(t)&&Object.keys(t).length===0||(0,v.default)(e)!==(0,v.default)(t)&&(!n.hungryForWhitespace||n.hungryForWhitespace&&!m(e)))return!1;if(typeof e=="string"&&typeof t=="string")return n.hungryForWhitespace&&m(e)&&m(t)?!0:n.verboseWhenMismatches?e===t?!0:`Given string ${t} is not matched! We have ${e} on the other end.`:n.useWildcards?C(e,t,{caseSensitive:!0}):e===t;if(Array.isArray(e)&&Array.isArray(t)){if(n.hungryForWhitespace&&m(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,o=t.length;r<o;r++){p=!1;for(let l=a,i=e.length;l<i;l++)if(a+=1,P(e[l],t[r],n)===!0){p=!0;break}if(!p)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(O(e)&&O(t)){if(c=new Set(Object.keys(t)),u=new Set(Object.keys(e)),n.matchStrictly&&c.size!==u.size){if(!n.verboseWhenMismatches)return!1;let r=new Set([...c].filter(f=>!u.has(f))),o=r.size?` First object has unique keys: ${JSON.stringify(r,null,4)}.`:"",l=new Set([...u].filter(f=>!c.has(f))),i=l.size?` Second object has unique keys:
${JSON.stringify(l,null,4)}.`:"";return`When matching strictly, we found that both objects have different amount of keys.${o}${i}`}for(let r of c){if(!q(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(o=>C(o,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,v.default)(e[r])!==(0,v.default)(t[r])){if(!(m(e[r])&&m(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,v.default)(t[r])}, on the second-one, it's ${(0,v.default)(e[r])}`:!1}else if(P(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&&m(e)&&m(t)&&(!n.matchStrictly||n.matchStrictly&&be(t))?!0:e===t;return!0}return ce(Oe);})();
/**
* @name codsen-utils
* @fileoverview Various utility functions
* @version 1.6.8
* @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.20
* @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.20
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ast-contains-only-empty-space/}
*/