check-types-mini
Version:
Validate options object
57 lines (48 loc) • 18.7 kB
JavaScript
/**
* @name check-types-mini
* @fileoverview Validate options object
* @version 8.0.23
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/check-types-mini/}
*/
;var checkTypesMini=(()=>{var oe=Object.create;var k=Object.defineProperty,ae=Object.defineProperties,ce=Object.getOwnPropertyDescriptor,le=Object.getOwnPropertyDescriptors,ue=Object.getOwnPropertyNames,U=Object.getOwnPropertySymbols,fe=Object.getPrototypeOf,_=Object.prototype.hasOwnProperty,pe=Object.prototype.propertyIsEnumerable;var Q=(e,t,c)=>t in e?k(e,t,{enumerable:!0,configurable:!0,writable:!0,value:c}):e[t]=c,E=(e,t)=>{for(var c in t||(t={}))_.call(t,c)&&Q(e,c,t[c]);if(U)for(var c of U(t))pe.call(t,c)&&Q(e,c,t[c]);return e},j=(e,t)=>ae(e,le(t));var L=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),ye=(e,t)=>{for(var c in t)k(e,c,{get:t[c],enumerable:!0})},q=(e,t,c,g)=>{if(t&&typeof t=="object"||typeof t=="function")for(let y of ue(t))!_.call(e,y)&&y!==c&&k(e,y,{get:()=>t[y],enumerable:!(g=ce(t,y))||g.enumerable});return e};var P=(e,t,c)=>(c=e!=null?oe(fe(e)):{},q(t||!e||!e.__esModule?k(c,"default",{value:e,enumerable:!0}):c,e)),ge=e=>q(k({},"__esModule",{value:!0}),e);var Y=L((J,M)=>{(function(e,t){typeof J=="object"&&typeof M<"u"?M.exports=t():typeof define=="function"&&define.amd?define(t):(e=typeof globalThis<"u"?globalThis:e||self,e.typeDetect=t())})(J,function(){"use strict";var e=typeof Promise=="function",t=function(d){if(typeof globalThis=="object")return globalThis;Object.defineProperty(d,"typeDetectGlobalObject",{get:function(){return this},configurable:!0});var T=typeDetectGlobalObject;return delete d.typeDetectGlobalObject,T}(Object.prototype),c=typeof Symbol<"u",g=typeof Map<"u",y=typeof Set<"u",h=typeof WeakMap<"u",p=typeof WeakSet<"u",r=typeof DataView<"u",a=c&&typeof Symbol.iterator<"u",i=c&&typeof Symbol.toStringTag<"u",o=y&&typeof Set.prototype.entries=="function",n=g&&typeof Map.prototype.entries=="function",l=o&&Object.getPrototypeOf(new Set().entries()),b=n&&Object.getPrototypeOf(new Map().entries()),$=a&&typeof Array.prototype[Symbol.iterator]=="function",S=$&&Object.getPrototypeOf([][Symbol.iterator]()),f=a&&typeof String.prototype[Symbol.iterator]=="function",s=f&&Object.getPrototypeOf(""[Symbol.iterator]()),u=8,m=-1;function O(d){var T=typeof d;if(T!=="object")return T;if(d===null)return"null";if(d===t)return"global";if(Array.isArray(d)&&(i===!1||!(Symbol.toStringTag in d)))return"Array";if(typeof window=="object"&&window!==null){if(typeof window.location=="object"&&d===window.location)return"Location";if(typeof window.document=="object"&&d===window.document)return"Document";if(typeof window.navigator=="object"){if(typeof window.navigator.mimeTypes=="object"&&d===window.navigator.mimeTypes)return"MimeTypeArray";if(typeof window.navigator.plugins=="object"&&d===window.navigator.plugins)return"PluginArray"}if((typeof window.HTMLElement=="function"||typeof window.HTMLElement=="object")&&d instanceof window.HTMLElement){if(d.tagName==="BLOCKQUOTE")return"HTMLQuoteElement";if(d.tagName==="TD")return"HTMLTableDataCellElement";if(d.tagName==="TH")return"HTMLTableHeaderCellElement"}}var C=i&&d[Symbol.toStringTag];if(typeof C=="string")return C;var A=Object.getPrototypeOf(d);return A===RegExp.prototype?"RegExp":A===Date.prototype?"Date":e&&A===Promise.prototype?"Promise":y&&A===Set.prototype?"Set":g&&A===Map.prototype?"Map":p&&A===WeakSet.prototype?"WeakSet":h&&A===WeakMap.prototype?"WeakMap":r&&A===DataView.prototype?"DataView":g&&A===b?"Map Iterator":y&&A===l?"Set Iterator":$&&A===S?"Array Iterator":f&&A===s?"String Iterator":A===null?"Object":Object.prototype.toString.call(d).slice(u,m)}return O})});var K=L((ve,z)=>{"use strict";z.exports=me;function N(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 he(e);let t=new Map;if(t.set(Date,p=>new Date(p)),t.set(Map,(p,r)=>new Map(g(Array.from(p),r))),t.set(Set,(p,r)=>new Set(g(Array.from(p),r))),e.constructorHandlers)for(let p of e.constructorHandlers)t.set(p[0],p[1]);let c=null;return e.proto?h:y;function g(p,r){let a=Object.keys(p),i=new Array(a.length);for(let o=0;o<a.length;o++){let n=a[o],l=p[n];typeof l!="object"||l===null?i[n]=l:l.constructor!==Object&&(c=t.get(l.constructor))?i[n]=c(l,r):ArrayBuffer.isView(l)?i[n]=N(l):i[n]=r(l)}return i}function y(p){if(typeof p!="object"||p===null)return p;if(Array.isArray(p))return g(p,y);if(p.constructor!==Object&&(c=t.get(p.constructor)))return c(p,y);let r={};for(let a in p){if(Object.hasOwnProperty.call(p,a)===!1)continue;let i=p[a];typeof i!="object"||i===null?r[a]=i:i.constructor!==Object&&(c=t.get(i.constructor))?r[a]=c(i,y):ArrayBuffer.isView(i)?r[a]=N(i):r[a]=y(i)}return r}function h(p){if(typeof p!="object"||p===null)return p;if(Array.isArray(p))return g(p,h);if(p.constructor!==Object&&(c=t.get(p.constructor)))return c(p,h);let r={};for(let a in p){let i=p[a];typeof i!="object"||i===null?r[a]=i:i.constructor!==Object&&(c=t.get(i.constructor))?r[a]=c(i,h):ArrayBuffer.isView(i)?r[a]=N(i):r[a]=h(i)}return r}}function he(e){let t=[],c=[],g=new Map;if(g.set(Date,a=>new Date(a)),g.set(Map,(a,i)=>new Map(h(Array.from(a),i))),g.set(Set,(a,i)=>new Set(h(Array.from(a),i))),e.constructorHandlers)for(let a of e.constructorHandlers)g.set(a[0],a[1]);let y=null;return e.proto?r:p;function h(a,i){let o=Object.keys(a),n=new Array(o.length);for(let l=0;l<o.length;l++){let b=o[l],$=a[b];if(typeof $!="object"||$===null)n[b]=$;else if($.constructor!==Object&&(y=g.get($.constructor)))n[b]=y($,i);else if(ArrayBuffer.isView($))n[b]=N($);else{let S=t.indexOf($);S!==-1?n[b]=c[S]:n[b]=i($)}}return n}function p(a){if(typeof a!="object"||a===null)return a;if(Array.isArray(a))return h(a,p);if(a.constructor!==Object&&(y=g.get(a.constructor)))return y(a,p);let i={};t.push(a),c.push(i);for(let o in a){if(Object.hasOwnProperty.call(a,o)===!1)continue;let n=a[o];if(typeof n!="object"||n===null)i[o]=n;else if(n.constructor!==Object&&(y=g.get(n.constructor)))i[o]=y(n,p);else if(ArrayBuffer.isView(n))i[o]=N(n);else{let l=t.indexOf(n);l!==-1?i[o]=c[l]:i[o]=p(n)}}return t.pop(),c.pop(),i}function r(a){if(typeof a!="object"||a===null)return a;if(Array.isArray(a))return h(a,r);if(a.constructor!==Object&&(y=g.get(a.constructor)))return y(a,r);let i={};t.push(a),c.push(i);for(let o in a){let n=a[o];if(typeof n!="object"||n===null)i[o]=n;else if(n.constructor!==Object&&(y=g.get(n.constructor)))i[o]=y(n,r);else if(ArrayBuffer.isView(n))i[o]=N(n);else{let l=t.indexOf(n);l!==-1?i[o]=c[l]:i[o]=r(n)}}return t.pop(),c.pop(),i}}});var re=L((te,x)=>{(function(e,t){"use strict";typeof x=="object"&&typeof x.exports=="object"?x.exports=t():typeof define=="function"&&define.amd?define([],t):e.objectPath=t()})(te,function(){"use strict";var e=Object.prototype.toString;function t(o,n){return o==null?!1:Object.prototype.hasOwnProperty.call(o,n)}function c(o){if(!o||h(o)&&o.length===0)return!0;if(typeof o!="string"){for(var n in o)if(t(o,n))return!1;return!0}return!1}function g(o){return e.call(o)}function y(o){return typeof o=="object"&&g(o)==="[object Object]"}var h=Array.isArray||function(o){return e.call(o)==="[object Array]"};function p(o){return typeof o=="boolean"||g(o)==="[object Boolean]"}function r(o){var n=parseInt(o);return n.toString()===o?n:o}function a(o){o=o||{};var n=function(f){return Object.keys(n).reduce(function(s,u){return u==="create"||typeof n[u]=="function"&&(s[u]=n[u].bind(n,f)),s},{})},l;o.includeInheritedProps?l=function(){return!0}:l=function(f,s){return typeof s=="number"&&Array.isArray(f)||t(f,s)};function b(f,s){if(l(f,s))return f[s]}var $;o.includeInheritedProps?$=function(f,s){typeof s!="string"&&typeof s!="number"&&(s=String(s));var u=b(f,s);if(s==="__proto__"||s==="prototype"||s==="constructor"&&typeof u=="function")throw new Error("For security reasons, object's magic properties cannot be set");return u}:$=function(f,s){return b(f,s)};function S(f,s,u,m){if(typeof s=="number"&&(s=[s]),!s||s.length===0)return f;if(typeof s=="string")return S(f,s.split(".").map(r),u,m);var O=s[0],d=$(f,O);return s.length===1?((d===void 0||!m)&&(f[O]=u),d):(d===void 0&&(typeof s[1]=="number"?f[O]=[]:f[O]={}),S(f[O],s.slice(1),u,m))}return n.has=function(f,s){if(typeof s=="number"?s=[s]:typeof s=="string"&&(s=s.split(".")),!s||s.length===0)return!!f;for(var u=0;u<s.length;u++){var m=r(s[u]);if(typeof m=="number"&&h(f)&&m<f.length||(o.includeInheritedProps?m in Object(f):t(f,m)))f=f[m];else return!1}return!0},n.ensureExists=function(f,s,u){return S(f,s,u,!0)},n.set=function(f,s,u,m){return S(f,s,u,m)},n.insert=function(f,s,u,m){var O=n.get(f,s);m=~~m,h(O)||(O=[],n.set(f,s,O)),O.splice(m,0,u)},n.empty=function(f,s){if(!c(s)&&f!=null){var u,m;if(u=n.get(f,s)){if(typeof u=="string")return n.set(f,s,"");if(p(u))return n.set(f,s,!1);if(typeof u=="number")return n.set(f,s,0);if(h(u))u.length=0;else if(y(u))for(m in u)l(u,m)&&delete u[m];else return n.set(f,s,null)}}},n.push=function(f,s){var u=n.get(f,s);h(u)||(u=[],n.set(f,s,u)),u.push.apply(u,Array.prototype.slice.call(arguments,2))},n.coalesce=function(f,s,u){for(var m,O=0,d=s.length;O<d;O++)if((m=n.get(f,s[O]))!==void 0)return m;return u},n.get=function(f,s,u){if(typeof s=="number"&&(s=[s]),!s||s.length===0)return f;if(f==null)return u;if(typeof s=="string")return n.get(f,s.split("."),u);var m=r(s[0]),O=$(f,m);return O===void 0?u:s.length===1?O:n.get(f[m],s.slice(1),u)},n.del=function(s,u){if(typeof u=="number"&&(u=[u]),s==null||c(u))return s;if(typeof u=="string")return n.del(s,u.split("."));var m=r(u[0]);if($(s,m),!l(s,m))return s;if(u.length===1)h(s)?s.splice(m,1):delete s[m];else return n.del(s[m],u.slice(1));return s},n}var i=a();return i.create=a,i.withInheritedProps=a({includeInheritedProps:!0}),i})});var Ae={};ye(Ae,{checkTypesMini:()=>Se,defaults:()=>ie,version:()=>Oe});var w=P(Y(),1);var ee=P(K(),1);var Z=P(K(),1);var De=(0,Z.default)();function v(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 D(e=[],t=[]){return!e||!Array.isArray(e)||!e.length?[]:!t||!Array.isArray(t)||!t.length?e:e.filter(c=>!t.includes(c))}function X(e=[],t=[]){return!e||!t?[]:Array.from(new Set(Array.from(e).filter(c=>new Set(t).has(c))))}function H(e){if(e.includes(".")){let t=e.lastIndexOf(".");if(!e.slice(0,t).includes("."))return e.slice(0,t);for(let c=t-1;c--;)if(e[c]===".")return e.slice(c+1,t)}return null}var B=(0,ee.default)();function R(e,t){let c={now:!1};function g(y,h,p,r){let a=B(y),i,o=E({depth:-1,path:""},p);if(o.depth+=1,Array.isArray(a))for(let n=0,l=a.length;n<l&&!r.now;n++){let b=o.path?`${o.path}.${n}`:`${n}`;a[n]!==void 0?(o.parent=B(a),o.parentType="array",o.parentKey=H(b),i=g(h(a[n],void 0,j(E({},o),{path:b}),r),h,j(E({},o),{path:b}),r),Number.isNaN(i)&&n<a.length?(a.splice(n,1),n-=1):a[n]=i):a.splice(n,1)}else if(v(a))for(let n in a){if(r.now&&n!=null)break;let l=o.path?`${o.path}.${n}`:n;o.depth===0&&n!=null&&(o.topmostKey=n),o.parent=B(a),o.parentType="object",o.parentKey=H(l),i=g(h(n,a[n],j(E({},o),{path:l}),r),h,j(E({},o),{path:l}),r),Number.isNaN(i)?delete a[n]:a[n]=i}return a}return g(e,t,{},c)}function F(e){return typeof e!="string"?e:e.length?[e]:[]}var I=P(re(),1);function W(e){if(typeof e!="string")throw new TypeError("Expected a string");return e.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&").replace(/-/g,"\\x2d")}var G=new Map,ne=(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(c=>{if(typeof c!="string"){if(typeof c>"u")return!1;throw new TypeError(`Expected '${t}' to be an array of strings, but found a type of '${typeof c}' in the array`)}return!0})},$e=(e,t)=>{t=E({caseSensitive:!1},t);let c=e+JSON.stringify(t);if(G.has(c))return G.get(c);let g=e[0]==="!";g&&(e=e.slice(1)),e=W(e).replace(/\\\*/g,"[\\s\\S]*");let y=new RegExp(`^${e}$`,t.caseSensitive?"":"i");return y.negated=g,G.set(c,y),y},de=(e,t,c,g)=>{if(e=ne(e,"inputs"),t=ne(t,"patterns"),t.length===0)return[];t=t.map(p=>$e(p,c));let{allPatterns:y}=c||{},h=[];for(let p of e){let r,a=[...t].fill(!1);for(let[i,o]of t.entries())if(o.test(p)&&(a[i]=!0,r=!o.negated,!r))break;if(!(r===!1||r===void 0&&t.some(i=>!i.negated)||y&&a.some((i,o)=>!i&&!t[o].negated))&&(h.push(p),g))break}return h};function V(e,t,c){return de(e,t,c,!0).length>0}var se="8.0.23";var Oe=se,ie={ignoreKeys:[],ignorePaths:[],acceptArrays:!1,acceptArraysIgnore:[],enforceStrictKeyset:!0,schema:{},msg:"check-types-mini",optsVarName:"opts"};function we(e,t,c){function g(i){return i!=null}function y(i,o){return typeof o=="string"&&(o=F(o)),Array.from(i).filter(n=>!o.some(l=>V(n,l,{caseSensitive:!0})))}let h=Object.prototype.hasOwnProperty,p=["any","anything","every","everything","all","whatever","whatevs"];if(!g(e))throw new Error("check-types-mini: [THROW_ID_01] First argument is missing!");let r=E(E({},ie),c);if(typeof r.ignoreKeys=="string"&&(r.ignoreKeys=[r.ignoreKeys]),typeof r.ignorePaths=="string"&&(r.ignorePaths=[r.ignorePaths]),typeof r.acceptArraysIgnore=="string"&&(r.acceptArraysIgnore=[r.acceptArraysIgnore]),r.msg=`${r.msg}`.trim(),r.msg[r.msg.length-1]===":"&&(r.msg=r.msg.slice(0,r.msg.length-1).trim()),v(r.schema))Object.keys(r.schema).forEach(i=>{if(v(r.schema[i])){let o={};R(r.schema[i],(n,l,b)=>{let $=l!==void 0?l:n;return!Array.isArray($)&&!v($)&&(o[`${i}.${b.path}`]=$),$}),delete r.schema[i],r.schema=E(E({},r.schema),o)}}),Object.keys(r.schema).forEach(i=>{Array.isArray(r.schema[i])||(r.schema[i]=[r.schema[i]]),r.schema[i]=r.schema[i].map(o=>`${o}`.toLowerCase().trim())});else if(r.schema!=null)throw new Error(`check-types-mini: opts.schema was customised to ${JSON.stringify(r.schema,null,0)} which is not object but ${typeof r.schema}`);if(g(t)||(t={}),r.enforceStrictKeyset)if(g(r.schema)&&Object.keys(r.schema).length){if(t&&y(D(Object.keys(e),Object.keys(t).concat(Object.keys(r.schema))),r.ignoreKeys).length){let i=D(Object.keys(e),Object.keys(t).concat(Object.keys(r.schema)));throw new TypeError(`${r.msg}: ${r.optsVarName}.enforceStrictKeyset is on and the following key${i.length>1?"s":""} ${i.length>1?"are":"is"} not covered by schema and/or reference objects: ${i.join(", ")}`)}}else if(v(t)&&Object.keys(t).length){if(y(D(Object.keys(e),Object.keys(t)),r.ignoreKeys).length!==0){let i=D(Object.keys(e),Object.keys(t));throw new TypeError(`${r.msg}: The input object has key${i.length>1?"s":""} which ${i.length>1?"are":"is"} not covered by the reference object: ${i.join(", ")}`)}else if(y(D(Object.keys(t),Object.keys(e)),r.ignoreKeys).length!==0){let i=D(Object.keys(t),Object.keys(e));throw new TypeError(`${r.msg}: The reference object has key${i.length>1?"s":""} which ${i.length>1?"are":"is"} not present in the input object: ${i.join(", ")}`)}}else throw new TypeError(`${r.msg}: Both ${r.optsVarName}.schema and reference objects are missing! We don't have anything to match the keys as you requested via resolvedOpts.enforceStrictKeyset!`);let a=[];R(e,(i,o,n)=>{let l=o,b=i;if(n.parentType==="array"&&(b=void 0,l=i),Array.isArray(a)&&a.length&&a.some(s=>n.path.startsWith(s))||b&&r.ignoreKeys.some(s=>b&&V(b,s))||r.ignorePaths.some(s=>V(n.path,s)))return l;let $=!(!v(l)&&!Array.isArray(l)&&Array.isArray(n.parent)),S=!1;v(r.schema)&&h.call(r.schema,n.path)&&(S=!0);let f=!1;if(v(t)&&I.default.has(t,n.path)&&(f=!0),r.enforceStrictKeyset&&$&&!S&&!f)throw new TypeError(`${r.msg}: ${r.optsVarName}.${n.path} is neither covered by reference object (second input argument), nor ${r.optsVarName}.schema! To stop this error, turn off ${r.optsVarName}.enforceStrictKeyset or provide some type reference (2nd argument or ${r.optsVarName}.schema).
Debug info:
obj = ${JSON.stringify(e,null,4)}
ref = ${JSON.stringify(t,null,4)}
innerObj = ${JSON.stringify(n,null,4)}
resolvedOpts = ${JSON.stringify(r,null,4)}
current = ${JSON.stringify(l,null,4)}
`);if(S){let s=F(r.schema[n.path]).map(u=>`${u}`.toLowerCase());if(I.default.set(r.schema,n.path,s),X(s,p).length)a.push(n.path);else if(l!==!0&&l!==!1&&!s.includes((0,w.default)(l).toLowerCase())||(l===!0||l===!1)&&!s.includes(String(l))&&!s.includes("boolean"))if(Array.isArray(l)&&r.acceptArrays){for(let u=0,m=l.length;u<m;u++)if(!s.includes((0,w.default)(l[u]).toLowerCase()))throw new TypeError(`${r.msg}: ${r.optsVarName}.${n.path}.${u}, the ${u}th element (equal to ${JSON.stringify(l[u],null,0)}) is of a type ${(0,w.default)(l[u]).toLowerCase()}, but only the following are allowed by the ${r.optsVarName}.schema: ${s.join(", ")}`)}else throw new TypeError(`${r.msg}: ${r.optsVarName}.${n.path} was customised to ${(0,w.default)(l)!=="string"?'"':""}${JSON.stringify(l,null,0)}${(0,w.default)(l)!=="string"?'"':""} (type: ${(0,w.default)(l).toLowerCase()}) which is not among the allowed types in schema (which is equal to ${JSON.stringify(s,null,0)})`)}else if(t&&v(t)&&f){let s=I.default.get(t,n.path);if(r.acceptArrays&&Array.isArray(l)&&!r.acceptArraysIgnore.includes(i)){if(!l.every(m=>(0,w.default)(m).toLowerCase()===(0,w.default)(t[i]).toLowerCase()))throw new TypeError(`${r.msg}: ${r.optsVarName}.${n.path} was customised to be array, but not all of its elements are ${(0,w.default)(t[i]).toLowerCase()}-type`)}else if((0,w.default)(l)!==(0,w.default)(s))throw new TypeError(`${r.msg}: ${r.optsVarName}.${n.path} was customised to ${(0,w.default)(l).toLowerCase()==="string"?"":'"'}${JSON.stringify(l,null,0)}${(0,w.default)(l).toLowerCase()==="string"?"":'"'} which is not ${(0,w.default)(s).toLowerCase()} but ${(0,w.default)(l).toLowerCase()}`)}return l})}function Se(e,t,c){we(e,t,c)}return ge(Ae);})();
/**
* @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 arrayiffy-if-string
* @fileoverview Put non-empty strings into arrays, turn empty-ones into empty arrays. Bypass everything else.
* @version 5.0.10
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/arrayiffy-if-string/}
*/