-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringList.min.js
More file actions
1 lines (1 loc) · 7.17 KB
/
stringList.min.js
File metadata and controls
1 lines (1 loc) · 7.17 KB
1
var f=Object.defineProperty;var g=(o,s,t)=>s in o?f(o,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[s]=t;var l=(o,s)=>f(o,"name",{value:s,configurable:!0});var m=(o,s,t)=>g(o,typeof s!="symbol"?s+"":s,t);var p=l((o,s)=>{if(o&&s&&Object.isFrozen(o)){/* c8 ignore next 3 @preserve */return Object.freeze(s)}return s},"freezeIfImmutable"),c=l(o=>`Using ${o}() method on a string list will mutate the original list in place. The code relying on this list will behave unexpectedly and may lead to unsafe execution.`,"mutationWarning"),y=typeof console=="object"&&typeof console.warn=="function"&&typeof window>"u"&&typeof process=="object"&&typeof process.env=="object"&&typeof process.env.STRING_LITERAL_LIST_DEBUG<"u"&&!0&&"production"&&!1,n=class n extends Array{constructor(t){super();m(this,"enum");if(this.enum=Object.create(null),Array.isArray(t)&&t.length>0)for(let r=0;r<t.length;r+=2e3){let i=Array.prototype.slice.call(t,r,r+2e3).filter(a=>typeof a=="string"||typeof a=="number").map(a=>String(a));Array.prototype.push.apply(this,i),Object.assign(this.enum,Object.fromEntries(i.map(a=>[a,a])))}Object.defineProperty(this,"enum",{writable:!0,configurable:!1,enumerable:!1})}includes(t,e=0){return this.length===0||e>=this.length||!Object.prototype.hasOwnProperty.call(this.enum,t)?!1:e!==0&&typeof e=="number"&&(e>0||e>=this.length*-1)?Array.prototype.includes.call(this,t,e):!0}concat(...t){return p(this,new n(Array.prototype.concat.apply(this,t.flat())))}concatList(t){return p(this,new n(Array.prototype.concat.apply(this,t)))}toSorted(){return p(this,new n(Array.prototype.toSorted.apply(this,arguments)))}toReversed(){return p(this,new n(Array.prototype.toReversed.apply(this,arguments)))}toSpliced(){return p(this,new n(Array.prototype.toSpliced.apply(this,arguments)))}slice(){return p(this,new n(Array.prototype.slice.apply(Array.from(this),arguments)))}without(...t){return p(this,new n(Array.prototype.filter.call(this,e=>!t.find(r=>Array.isArray(r)?r.find(i=>e===(typeof i=="number"?String(i):i))!==void 0:e===(typeof r=="number"?String(r):r)))))}pick(...t){return t.length===0?this:p(this,new n(Array.prototype.filter.call(this,e=>t.find(r=>e===(typeof r=="number"?String(r):r)))))}withTrim(){return p(this,new n(Array.prototype.map.call(this,t=>t.trim())))}withPrefix(t=""){return p(this,new n(Array.prototype.map.call(this,e=>`${t}${e}`)))}withSuffix(t=""){return p(this,new n(Array.prototype.map.call(this,e=>`${e}${t}`)))}withReplace(t,e=void 0){return p(this,new n(Array.prototype.map.call(this,r=>r.replace(t,e))))}withReplaceAll(t,e=void 0){return p(this,new n(Array.prototype.map.call(this,r=>r.replaceAll(t,e))))}toLowerCase(){return p(this,new n(Array.prototype.map.call(this,t=>t.toLowerCase())))}toUpperCase(){return p(this,new n(Array.prototype.map.call(this,t=>t.toUpperCase())))}toCapitalize(){return this.withReplace(/\b\w/g,t=>t.toUpperCase())}toUnCapitalize(){return this.withReplace(/\b\w/g,t=>t.toLowerCase())}value(t){if(typeof t!="string"||!Object.prototype.hasOwnProperty.call(this.enum,t))throw new Error(`Invalid value ${t}`);return this.enum[t]}mutable(){return Array.prototype.slice.call(Array.from(this))}compat(){return this}happy(){return this}stringList(){return this}toRecordValue(t=void 0,...e){return Object.assign({},...e,Object.fromEntries(super.map.call(this,r=>{try{return[r,typeof t=="object"&&t!==null?Array.isArray(t)?t.slice():typeof structuredClone=="function"?structuredClone(t):Object.assign(Object.create(null),t):t]}catch(i){return[r,t]}})))}mapAsObject(t){return Object.fromEntries(Array.prototype.map.call(this,e=>[e,t(e)]))}toRecordType(t="any",e=void 0,...r){return Object.assign({},...r,Object.fromEntries(Array.prototype.map.call(this,i=>{try{return[i,typeof e=="object"&&e!==null?Array.isArray(e)?[...e]:typeof structuredClone=="function"?structuredClone(e):{...e}:e]}catch(a){return[i,e]}})))}asMap(){return new Map(Object.entries(this.enum))}asSet(){return new Set(Array.from(this))}asObject(){return Object.assign({},this.enum)}map(){return Array.prototype.map.apply(Array.from(this),arguments)}filter(){return Array.prototype.filter.apply(Array.from(this),arguments)}reduce(){return Array.prototype.reduce.apply(Array.from(this),arguments)}reduceRight(){return Array.prototype.reduceRight.apply(Array.from(this),arguments)}flat(){return Array.prototype.flat.apply(Array.from(this),arguments)}flatMap(){return Array.prototype.flatMap.apply(Array.from(this),arguments)}with(){return Array.prototype.with.apply(Array.from(this),arguments)}push(){if(y&&console.warn(c("push")),!Object.isFrozen(this))Array.prototype.push.apply(this,arguments),this.enum=Object.fromEntries(Array.prototype.map.call(this,t=>[t,t]));else throw new Error("Cannot set properties on a frozen object");return this.length}shift(){if(y&&console.warn(c("shift")),Object.isFrozen(this))throw new Error("Cannot set properties on a frozen object");{let t=Array.prototype.shift.apply(this,arguments);return this.enum=Object.fromEntries(Object.entries(this.enum).filter(([e])=>e!==t)),t}}unshift(){if(y&&console.warn(c("unshift")),!Object.isFrozen(this))Array.prototype.unshift.apply(this,arguments),this.enum=Object.assign(Object.create(null),this.enum,Object.fromEntries(Array.from(arguments).map(t=>[t,t])));else throw new Error("Cannot set properties on a frozen object");return this.length}copyWithin(){if(y&&console.warn(c("copyWithin")),!Object.isFrozen(this))Array.prototype.copyWithin.apply(this,Array.from(arguments)),this.enum=Object.fromEntries(Array.prototype.map.call(this,t=>[t,t]));else throw new Error("Cannot set properties on a frozen object");return this}pop(){if(y&&console.warn(c("pop")),Object.isFrozen(this))throw new Error("Cannot set properties on a frozen object");{let t=super.pop();return this.enum=Object.fromEntries(Array.prototype.map.call(this,e=>[e,e])),t}}fill(){if(y&&console.warn(c("fill")),!Object.isFrozen(this))Array.prototype.fill.apply(this,arguments),this.enum=Object.fromEntries(Array.prototype.map.call(this,t=>[t,t]));else throw new Error("Cannot set properties on a frozen object");return this}splice(){if(y&&console.warn(c("splice")),Object.isFrozen(this))throw new Error("Cannot set properties on a frozen object");{let t=this.mutable(),e=t.splice.apply(t,arguments);return super.splice.apply(this,arguments),this.enum=Object.fromEntries(Array.prototype.map.call(this,r=>[r,r])),e}}reverse(){if(y&&console.warn(c("reverse")),!Object.isFrozen(this))Array.prototype.reverse.apply(this,arguments);else throw new Error("Cannot set properties on a frozen object");return this}};l(n,"SL");var u=n,j=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice",reverse:"reverse"});var A=typeof console=="object"&&typeof console.warn=="function"&&typeof window>"u"&&typeof process=="object"&&typeof process.env=="object"&&typeof process.env.STRING_LITERAL_LIST_DEBUG<"u"&&!0&&"production"&&!1;function h(){let o=arguments.length>0?Array.isArray(arguments[0])?arguments[0]:arguments.length?Array.from(arguments):[]:[];if(A){let s=o.some(t=>typeof t!="string");o.length&&s&&console.warn(`Unexpected type in stringList(${typeof s}). Casting all arguments to string type.`)}return new u(o)}l(h,"stringListMutable");var w=h,b=h,L={stringList:w,sl:b};export{L as default,b as sl,w as stringList};