From d17ba20794016fdd3f810b34b574fe630010c0d2 Mon Sep 17 00:00:00 2001 From: rawpixel-vincent Date: Tue, 13 Aug 2024 11:27:15 +0700 Subject: [PATCH] chore: Add methods to convert StringLiteralList to Map, Set, and Object --- README.md | 3 +++ StringLiteralList.js | 12 +++++++++++ package.json | 8 ++++---- strict.cjs | 2 +- strict.min.cjs | 2 +- strict.min.js | 2 +- stringList.cjs | 2 +- stringList.min.cjs | 2 +- stringList.min.js | 2 +- stringList.test.js | 48 +++++++++++++++++++++++++++++++++++++++++++ stringListFunction.js | 6 ++++-- types/list.d.ts | 43 +++++++++++++++++++++++++++++++------- 12 files changed, 113 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index ddf4e47..2c5d8b9 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,9 @@ The StringList class extends the Array with new methods and supercharges the arr - `withReplaceAll(search, replacement)`: call the String.prototype.replaceAll on all. - `to"Case"()` methods for case transform, uppercase, lowercase capitalize, etc... - `toRecordValue/toRecordType` methods to create a type of `Record` with initial value and builtin or infered types. + - `asMap` method to create a map of the literals. + - `asSet` method to create a set of the literals. + - `asObject` method to create an object of the literals. The array order is preserved in a Tuple to infer the types when possible, some methods like toSorted will cause the new instance to be flagged as unsorted and methods like join() will return a generic string type. diff --git a/StringLiteralList.js b/StringLiteralList.js index b76ee7c..81c80c3 100644 --- a/StringLiteralList.js +++ b/StringLiteralList.js @@ -212,6 +212,18 @@ export class SL extends Array { ); } + asMap() { + return new Map(super.map((e) => [e, e])); + } + + asSet() { + return new Set(this); + } + + asObject() { + return Object.assign({}, ...super.map((e) => ({ [e]: e }))); + } + // Methods returning the native array map() { const mut = this.mutable(); diff --git a/package.json b/package.json index 0091792..9affd24 100644 --- a/package.json +++ b/package.json @@ -47,11 +47,11 @@ "lint": "eslint --fix \"./*.js\"", "lint:ci": "eslint . --ext .js", "build": "rm -f *.min.* && npm run build:main:cjs && npm run build:main:min && npm run build:main:min:cjs && npm run build:strict:cjs && npm run build:strict:min && npm run build:strict:min:cjs", - "build:strict:min": "esbuild ./strict.js --target=chrome86,edge89,firefox88,safari13,opera76 --bundle --format=esm --minify --outfile=strict.min.js --legal-comments=inline", - "build:strict:min:cjs": "esbuild ./strict.js --target=chrome86,edge89,firefox88,safari13,opera76 --bundle --format=cjs --minify --outfile=strict.min.cjs --legal-comments=inline", + "build:strict:min": "esbuild ./strict.js --target=chrome64,edge89,firefox88,safari13,opera76 --bundle --format=esm --minify --outfile=strict.min.js --legal-comments=inline", + "build:strict:min:cjs": "esbuild ./strict.js --target=chrome64,edge89,firefox88,safari13,opera76 --bundle --format=cjs --minify --outfile=strict.min.cjs --legal-comments=inline", "build:strict:cjs": "esbuild ./strict.js --bundle --format=cjs --outfile=strict.cjs --target=node18 --minify --legal-comments=inline", - "build:main:min": "esbuild ./stringList.js --target=chrome86,edge89,firefox88,safari13,opera76 --bundle --format=esm --minify --outfile=stringList.min.js --legal-comments=inline", - "build:main:min:cjs": "esbuild ./stringList.js --target=chrome86,edge89,firefox88,safari13,opera76 --bundle --format=cjs --minify --outfile=stringList.min.cjs --legal-comments=inline", + "build:main:min": "esbuild ./stringList.js --target=chrome64,edge89,firefox88,safari13,opera76 --bundle --format=esm --minify --outfile=stringList.min.js --legal-comments=inline", + "build:main:min:cjs": "esbuild ./stringList.js --target=chrome64,edge89,firefox88,safari13,opera76 --bundle --format=cjs --minify --outfile=stringList.min.cjs --legal-comments=inline", "build:main:cjs": "esbuild ./stringList.js --bundle --format=cjs --outfile=stringList.cjs --target=node18 --minify --legal-comments=inline", "prepare": "husky" }, diff --git a/strict.cjs b/strict.cjs index 33a8275..ee0ab2f 100644 --- a/strict.cjs +++ b/strict.cjs @@ -1 +1 @@ -var a=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var d=(e,t)=>{for(var r in t)a(e,r,{get:t[r],enumerable:!0})},y=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of m(t))!c.call(e,i)&&i!==r&&a(e,i,{get:()=>t[i],enumerable:!(n=h(t,i))||n.enumerable});return e};var w=e=>y(a({},"__esModule",{value:!0}),e);var R={};d(R,{default:()=>b,sl:()=>f,stringList:()=>l});module.exports=w(R);var s=(e,t)=>{if(e&&t&&Object.isFrozen(e)){/* c8 ignore next 3 @preserve */return Object.freeze(t)}return t},u=class e extends Array{infered={Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0};enum;hasEmpty=!1;constructor(...t){let r=[],n=[],i=!1;for(let p of t.flat())typeof p=="string"&&(p===""&&(i=!0),r.push([p,p]),n.push(p));super(...n),this.hasEmpty=i,this.enum=Object.fromEntries(r),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...t){return s(this,new e(...super.concat.apply(this,t.flat())))}concatList(t){return this.concat(...t)}toSorted(){return s(this,new e(...super.toSorted.apply(this,arguments)))}toReversed(){return s(this,new e(...super.toReversed.apply(this,arguments)))}reverse(){return s(this,new e(...super.reverse.apply(this,arguments)))}toSpliced(){return s(this,new e(...super.toSpliced.apply(this,arguments)))}slice(){return s(this,new e(...super.slice.apply(this,arguments)))}without(){let t=Array.from(arguments).flatMap(r=>Array.isArray(r)?r.filter(n=>typeof n=="string"):typeof r=="string"?[r]:[]);return s(this,new e(...this.filter(r=>!t.includes(r))))}withTrim(){return s(this,new e(...super.map(t=>t.trim())))}withPrefix(t=""){return s(this,new e(...super.map(r=>`${t}${r}`)))}withSuffix(t=""){return s(this,new e(...super.map(r=>`${r}${t}`)))}withReplace(t,r=void 0){return s(this,new e(...super.map(n=>n.replace(t,r))))}withReplaceAll(t,r=void 0){return s(this,new e(...super.map(n=>n.replaceAll(t,r))))}toLowerCase(){return s(this,new e(...super.map(t=>t.toLowerCase())))}toUpperCase(){return s(this,new e(...super.map(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"&&(this.enum[t]||this.hasEmpty&&t===""))return t;throw new Error(`Invalid value ${t}`)}mutable(){return Array.from(this)}toRecordValue(t=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(n=>[n,t])))}toRecordType(t="any",r=void 0,...n){return Object.assign({},...n,Object.fromEntries(super.map(i=>[i,r])))}map(){let t=this.mutable();return t.map.apply(t,arguments)}filter(){let t=this.mutable();return t.filter.apply(t,arguments)}reduce(){let t=this.mutable();return t.reduce.apply(t,arguments)}reduceRight(){let t=this.mutable();return t.reduceRight.apply(t,arguments)}flat(){let t=this.mutable();return t.flat.apply(t,arguments)}flatMap(){let t=this.mutable();return t.flatMap.apply(t,arguments)}with(){let t=this.mutable();return t.with.apply(t,arguments)}},E=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function g(...e){let t=e,r=e.some(n=>typeof n!="string");return e.length&&r&&(typeof window>"u",t=e.flatMap(n=>Array.isArray(n)?n.filter(i=>typeof i=="string"):typeof n=="string"?[n]:typeof n=="number"?[String(n)]:[])),new u(...t)}function o(...e){return Object.freeze(g(...e))}var l=o,f=o,b={stringList:l,sl:f}; +var a=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var d=(t,e)=>{for(var r in e)a(t,r,{get:e[r],enumerable:!0})},y=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of c(e))!m.call(t,i)&&i!==r&&a(t,i,{get:()=>e[i],enumerable:!(s=h(e,i))||s.enumerable});return t};var w=t=>y(a({},"__esModule",{value:!0}),t);var O={};d(O,{default:()=>b,sl:()=>f,stringList:()=>l});module.exports=w(O);var n=(t,e)=>{if(t&&e&&Object.isFrozen(t)){/* c8 ignore next 3 @preserve */return Object.freeze(e)}return e},u=class t extends Array{infered={Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0};enum;hasEmpty=!1;constructor(...e){let r=[],s=[],i=!1;for(let p of e.flat())typeof p=="string"&&(p===""&&(i=!0),r.push([p,p]),s.push(p));super(...s),this.hasEmpty=i,this.enum=Object.fromEntries(r),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...e){return n(this,new t(...super.concat.apply(this,e.flat())))}concatList(e){return this.concat(...e)}toSorted(){return n(this,new t(...super.toSorted.apply(this,arguments)))}toReversed(){return n(this,new t(...super.toReversed.apply(this,arguments)))}reverse(){return n(this,new t(...super.reverse.apply(this,arguments)))}toSpliced(){return n(this,new t(...super.toSpliced.apply(this,arguments)))}slice(){return n(this,new t(...super.slice.apply(this,arguments)))}without(){let e=Array.from(arguments).flatMap(r=>Array.isArray(r)?r.filter(s=>typeof s=="string"):typeof r=="string"?[r]:[]);return n(this,new t(...this.filter(r=>!e.includes(r))))}withTrim(){return n(this,new t(...super.map(e=>e.trim())))}withPrefix(e=""){return n(this,new t(...super.map(r=>`${e}${r}`)))}withSuffix(e=""){return n(this,new t(...super.map(r=>`${r}${e}`)))}withReplace(e,r=void 0){return n(this,new t(...super.map(s=>s.replace(e,r))))}withReplaceAll(e,r=void 0){return n(this,new t(...super.map(s=>s.replaceAll(e,r))))}toLowerCase(){return n(this,new t(...super.map(e=>e.toLowerCase())))}toUpperCase(){return n(this,new t(...super.map(e=>e.toUpperCase())))}toCapitalize(){return this.withReplace(/\b\w/g,e=>e.toUpperCase())}toUnCapitalize(){return this.withReplace(/\b\w/g,e=>e.toLowerCase())}value(e){if(typeof e=="string"&&(this.enum[e]||this.hasEmpty&&e===""))return e;throw new Error(`Invalid value ${e}`)}mutable(){return Array.from(this)}toRecordValue(e=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(s=>[s,e])))}toRecordType(e="any",r=void 0,...s){return Object.assign({},...s,Object.fromEntries(super.map(i=>[i,r])))}asMap(){return new Map(super.map(e=>[e,e]))}asSet(){return new Set(this)}asObject(){return Object.assign({},...super.map(e=>({[e]:e})))}map(){let e=this.mutable();return e.map.apply(e,arguments)}filter(){let e=this.mutable();return e.filter.apply(e,arguments)}reduce(){let e=this.mutable();return e.reduce.apply(e,arguments)}reduceRight(){let e=this.mutable();return e.reduceRight.apply(e,arguments)}flat(){let e=this.mutable();return e.flat.apply(e,arguments)}flatMap(){let e=this.mutable();return e.flatMap.apply(e,arguments)}with(){let e=this.mutable();return e.with.apply(e,arguments)}},A=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function g(...t){let e=t,r=t.some(s=>typeof s!="string");return t.length&&r&&(typeof window>"u"&&process&&process.env,e=t.flatMap(s=>Array.isArray(s)?s.filter(i=>typeof i=="string"):typeof s=="string"?[s]:typeof s=="number"?[String(s)]:[])),new u(...e)}function o(...t){return Object.freeze(g(...t))}var l=o,f=o,b={stringList:l,sl:f}; diff --git a/strict.min.cjs b/strict.min.cjs index c6eaca9..64df414 100644 --- a/strict.min.cjs +++ b/strict.min.cjs @@ -1 +1 @@ -var a=Object.defineProperty;var c=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var w=(e,n,t)=>n in e?a(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t;var g=(e,n)=>{for(var t in n)a(e,t,{get:n[t],enumerable:!0})},b=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let s of d(n))!y.call(e,s)&&s!==t&&a(e,s,{get:()=>n[s],enumerable:!(r=c(n,s))||r.enumerable});return e};var R=e=>b(a({},"__esModule",{value:!0}),e);var o=(e,n,t)=>(w(e,typeof n!="symbol"?n+"":n,t),t);var O={};g(O,{default:()=>E,sl:()=>m,stringList:()=>h});module.exports=R(O);var i=(e,n)=>{if(e&&n&&Object.isFrozen(e)){/* c8 ignore next 3 @preserve */return Object.freeze(n)}return n},l=class e extends Array{constructor(...t){let r=[],s=[],u=!1;for(let p of t.flat())typeof p=="string"&&(p===""&&(u=!0),r.push([p,p]),s.push(p));super(...s);o(this,"infered",{Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0});o(this,"enum");o(this,"hasEmpty",!1);this.hasEmpty=u,this.enum=Object.fromEntries(r),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...t){return i(this,new e(...super.concat.apply(this,t.flat())))}concatList(t){return this.concat(...t)}toSorted(){return i(this,new e(...super.toSorted.apply(this,arguments)))}toReversed(){return i(this,new e(...super.toReversed.apply(this,arguments)))}reverse(){return i(this,new e(...super.reverse.apply(this,arguments)))}toSpliced(){return i(this,new e(...super.toSpliced.apply(this,arguments)))}slice(){return i(this,new e(...super.slice.apply(this,arguments)))}without(){let t=Array.from(arguments).flatMap(r=>Array.isArray(r)?r.filter(s=>typeof s=="string"):typeof r=="string"?[r]:[]);return i(this,new e(...this.filter(r=>!t.includes(r))))}withTrim(){return i(this,new e(...super.map(t=>t.trim())))}withPrefix(t=""){return i(this,new e(...super.map(r=>`${t}${r}`)))}withSuffix(t=""){return i(this,new e(...super.map(r=>`${r}${t}`)))}withReplace(t,r=void 0){return i(this,new e(...super.map(s=>s.replace(t,r))))}withReplaceAll(t,r=void 0){return i(this,new e(...super.map(s=>s.replaceAll(t,r))))}toLowerCase(){return i(this,new e(...super.map(t=>t.toLowerCase())))}toUpperCase(){return i(this,new e(...super.map(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"&&(this.enum[t]||this.hasEmpty&&t===""))return t;throw new Error(`Invalid value ${t}`)}mutable(){return Array.from(this)}toRecordValue(t=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(s=>[s,t])))}toRecordType(t="any",r=void 0,...s){return Object.assign({},...s,Object.fromEntries(super.map(u=>[u,r])))}map(){let t=this.mutable();return t.map.apply(t,arguments)}filter(){let t=this.mutable();return t.filter.apply(t,arguments)}reduce(){let t=this.mutable();return t.reduce.apply(t,arguments)}reduceRight(){let t=this.mutable();return t.reduceRight.apply(t,arguments)}flat(){let t=this.mutable();return t.flat.apply(t,arguments)}flatMap(){let t=this.mutable();return t.flatMap.apply(t,arguments)}with(){let t=this.mutable();return t.with.apply(t,arguments)}},j=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function A(...e){let n=e,t=e.some(r=>typeof r!="string");return e.length&&t&&(typeof window>"u",n=e.flatMap(r=>Array.isArray(r)?r.filter(s=>typeof s=="string"):typeof r=="string"?[r]:typeof r=="number"?[String(r)]:[])),new l(...n)}function f(...e){return Object.freeze(A(...e))}var h=f,m=f,E={stringList:h,sl:m}; +var a=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var w=(t,s,e)=>s in t?a(t,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[s]=e;var g=(t,s)=>{for(var e in s)a(t,e,{get:s[e],enumerable:!0})},b=(t,s,e,r)=>{if(s&&typeof s=="object"||typeof s=="function")for(let n of d(s))!y.call(t,n)&&n!==e&&a(t,n,{get:()=>s[n],enumerable:!(r=m(s,n))||r.enumerable});return t};var O=t=>b(a({},"__esModule",{value:!0}),t);var o=(t,s,e)=>(w(t,typeof s!="symbol"?s+"":s,e),e);var E={};g(E,{default:()=>A,sl:()=>c,stringList:()=>h});module.exports=O(E);var i=(t,s)=>{if(t&&s&&Object.isFrozen(t)){/* c8 ignore next 3 @preserve */return Object.freeze(s)}return s},l=class t extends Array{constructor(...e){let r=[],n=[],u=!1;for(let p of e.flat())typeof p=="string"&&(p===""&&(u=!0),r.push([p,p]),n.push(p));super(...n);o(this,"infered",{Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0});o(this,"enum");o(this,"hasEmpty",!1);this.hasEmpty=u,this.enum=Object.fromEntries(r),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...e){return i(this,new t(...super.concat.apply(this,e.flat())))}concatList(e){return this.concat(...e)}toSorted(){return i(this,new t(...super.toSorted.apply(this,arguments)))}toReversed(){return i(this,new t(...super.toReversed.apply(this,arguments)))}reverse(){return i(this,new t(...super.reverse.apply(this,arguments)))}toSpliced(){return i(this,new t(...super.toSpliced.apply(this,arguments)))}slice(){return i(this,new t(...super.slice.apply(this,arguments)))}without(){let e=Array.from(arguments).flatMap(r=>Array.isArray(r)?r.filter(n=>typeof n=="string"):typeof r=="string"?[r]:[]);return i(this,new t(...this.filter(r=>!e.includes(r))))}withTrim(){return i(this,new t(...super.map(e=>e.trim())))}withPrefix(e=""){return i(this,new t(...super.map(r=>`${e}${r}`)))}withSuffix(e=""){return i(this,new t(...super.map(r=>`${r}${e}`)))}withReplace(e,r=void 0){return i(this,new t(...super.map(n=>n.replace(e,r))))}withReplaceAll(e,r=void 0){return i(this,new t(...super.map(n=>n.replaceAll(e,r))))}toLowerCase(){return i(this,new t(...super.map(e=>e.toLowerCase())))}toUpperCase(){return i(this,new t(...super.map(e=>e.toUpperCase())))}toCapitalize(){return this.withReplace(/\b\w/g,e=>e.toUpperCase())}toUnCapitalize(){return this.withReplace(/\b\w/g,e=>e.toLowerCase())}value(e){if(typeof e=="string"&&(this.enum[e]||this.hasEmpty&&e===""))return e;throw new Error(`Invalid value ${e}`)}mutable(){return Array.from(this)}toRecordValue(e=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(n=>[n,e])))}toRecordType(e="any",r=void 0,...n){return Object.assign({},...n,Object.fromEntries(super.map(u=>[u,r])))}asMap(){return new Map(super.map(e=>[e,e]))}asSet(){return new Set(this)}asObject(){return Object.assign({},...super.map(e=>({[e]:e})))}map(){let e=this.mutable();return e.map.apply(e,arguments)}filter(){let e=this.mutable();return e.filter.apply(e,arguments)}reduce(){let e=this.mutable();return e.reduce.apply(e,arguments)}reduceRight(){let e=this.mutable();return e.reduceRight.apply(e,arguments)}flat(){let e=this.mutable();return e.flat.apply(e,arguments)}flatMap(){let e=this.mutable();return e.flatMap.apply(e,arguments)}with(){let e=this.mutable();return e.with.apply(e,arguments)}},x=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function R(...t){let s=t,e=t.some(r=>typeof r!="string");return t.length&&e&&(typeof window>"u"&&process&&process.env,s=t.flatMap(r=>Array.isArray(r)?r.filter(n=>typeof n=="string"):typeof r=="string"?[r]:typeof r=="number"?[String(r)]:[])),new l(...s)}function f(...t){return Object.freeze(R(...t))}var h=f,c=f,A={stringList:h,sl:c}; diff --git a/strict.min.js b/strict.min.js index ef5936d..75c75b4 100644 --- a/strict.min.js +++ b/strict.min.js @@ -1 +1 @@ -var f=Object.defineProperty;var h=(e,n,t)=>n in e?f(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t;var a=(e,n,t)=>(h(e,typeof n!="symbol"?n+"":n,t),t);var i=(e,n)=>{if(e&&n&&Object.isFrozen(e)){/* c8 ignore next 3 @preserve */return Object.freeze(n)}return n},o=class e extends Array{constructor(...t){let r=[],s=[],u=!1;for(let p of t.flat())typeof p=="string"&&(p===""&&(u=!0),r.push([p,p]),s.push(p));super(...s);a(this,"infered",{Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0});a(this,"enum");a(this,"hasEmpty",!1);this.hasEmpty=u,this.enum=Object.fromEntries(r),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...t){return i(this,new e(...super.concat.apply(this,t.flat())))}concatList(t){return this.concat(...t)}toSorted(){return i(this,new e(...super.toSorted.apply(this,arguments)))}toReversed(){return i(this,new e(...super.toReversed.apply(this,arguments)))}reverse(){return i(this,new e(...super.reverse.apply(this,arguments)))}toSpliced(){return i(this,new e(...super.toSpliced.apply(this,arguments)))}slice(){return i(this,new e(...super.slice.apply(this,arguments)))}without(){let t=Array.from(arguments).flatMap(r=>Array.isArray(r)?r.filter(s=>typeof s=="string"):typeof r=="string"?[r]:[]);return i(this,new e(...this.filter(r=>!t.includes(r))))}withTrim(){return i(this,new e(...super.map(t=>t.trim())))}withPrefix(t=""){return i(this,new e(...super.map(r=>`${t}${r}`)))}withSuffix(t=""){return i(this,new e(...super.map(r=>`${r}${t}`)))}withReplace(t,r=void 0){return i(this,new e(...super.map(s=>s.replace(t,r))))}withReplaceAll(t,r=void 0){return i(this,new e(...super.map(s=>s.replaceAll(t,r))))}toLowerCase(){return i(this,new e(...super.map(t=>t.toLowerCase())))}toUpperCase(){return i(this,new e(...super.map(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"&&(this.enum[t]||this.hasEmpty&&t===""))return t;throw new Error(`Invalid value ${t}`)}mutable(){return Array.from(this)}toRecordValue(t=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(s=>[s,t])))}toRecordType(t="any",r=void 0,...s){return Object.assign({},...s,Object.fromEntries(super.map(u=>[u,r])))}map(){let t=this.mutable();return t.map.apply(t,arguments)}filter(){let t=this.mutable();return t.filter.apply(t,arguments)}reduce(){let t=this.mutable();return t.reduce.apply(t,arguments)}reduceRight(){let t=this.mutable();return t.reduceRight.apply(t,arguments)}flat(){let t=this.mutable();return t.flat.apply(t,arguments)}flatMap(){let t=this.mutable();return t.flatMap.apply(t,arguments)}with(){let t=this.mutable();return t.with.apply(t,arguments)}},w=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function m(...e){let n=e,t=e.some(r=>typeof r!="string");return e.length&&t&&(typeof window>"u",n=e.flatMap(r=>Array.isArray(r)?r.filter(s=>typeof s=="string"):typeof r=="string"?[r]:typeof r=="number"?[String(r)]:[])),new o(...n)}function l(...e){return Object.freeze(m(...e))}var c=l,d=l,O={stringList:c,sl:d};export{O as default,d as sl,c as stringList}; +var f=Object.defineProperty;var h=(t,s,e)=>s in t?f(t,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):t[s]=e;var a=(t,s,e)=>(h(t,typeof s!="symbol"?s+"":s,e),e);var i=(t,s)=>{if(t&&s&&Object.isFrozen(t)){/* c8 ignore next 3 @preserve */return Object.freeze(s)}return s},o=class t extends Array{constructor(...e){let r=[],n=[],u=!1;for(let p of e.flat())typeof p=="string"&&(p===""&&(u=!0),r.push([p,p]),n.push(p));super(...n);a(this,"infered",{Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0});a(this,"enum");a(this,"hasEmpty",!1);this.hasEmpty=u,this.enum=Object.fromEntries(r),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...e){return i(this,new t(...super.concat.apply(this,e.flat())))}concatList(e){return this.concat(...e)}toSorted(){return i(this,new t(...super.toSorted.apply(this,arguments)))}toReversed(){return i(this,new t(...super.toReversed.apply(this,arguments)))}reverse(){return i(this,new t(...super.reverse.apply(this,arguments)))}toSpliced(){return i(this,new t(...super.toSpliced.apply(this,arguments)))}slice(){return i(this,new t(...super.slice.apply(this,arguments)))}without(){let e=Array.from(arguments).flatMap(r=>Array.isArray(r)?r.filter(n=>typeof n=="string"):typeof r=="string"?[r]:[]);return i(this,new t(...this.filter(r=>!e.includes(r))))}withTrim(){return i(this,new t(...super.map(e=>e.trim())))}withPrefix(e=""){return i(this,new t(...super.map(r=>`${e}${r}`)))}withSuffix(e=""){return i(this,new t(...super.map(r=>`${r}${e}`)))}withReplace(e,r=void 0){return i(this,new t(...super.map(n=>n.replace(e,r))))}withReplaceAll(e,r=void 0){return i(this,new t(...super.map(n=>n.replaceAll(e,r))))}toLowerCase(){return i(this,new t(...super.map(e=>e.toLowerCase())))}toUpperCase(){return i(this,new t(...super.map(e=>e.toUpperCase())))}toCapitalize(){return this.withReplace(/\b\w/g,e=>e.toUpperCase())}toUnCapitalize(){return this.withReplace(/\b\w/g,e=>e.toLowerCase())}value(e){if(typeof e=="string"&&(this.enum[e]||this.hasEmpty&&e===""))return e;throw new Error(`Invalid value ${e}`)}mutable(){return Array.from(this)}toRecordValue(e=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(n=>[n,e])))}toRecordType(e="any",r=void 0,...n){return Object.assign({},...n,Object.fromEntries(super.map(u=>[u,r])))}asMap(){return new Map(super.map(e=>[e,e]))}asSet(){return new Set(this)}asObject(){return Object.assign({},...super.map(e=>({[e]:e})))}map(){let e=this.mutable();return e.map.apply(e,arguments)}filter(){let e=this.mutable();return e.filter.apply(e,arguments)}reduce(){let e=this.mutable();return e.reduce.apply(e,arguments)}reduceRight(){let e=this.mutable();return e.reduceRight.apply(e,arguments)}flat(){let e=this.mutable();return e.flat.apply(e,arguments)}flatMap(){let e=this.mutable();return e.flatMap.apply(e,arguments)}with(){let e=this.mutable();return e.with.apply(e,arguments)}},w=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function c(...t){let s=t,e=t.some(r=>typeof r!="string");return t.length&&e&&(typeof window>"u"&&process&&process.env,s=t.flatMap(r=>Array.isArray(r)?r.filter(n=>typeof n=="string"):typeof r=="string"?[r]:typeof r=="number"?[String(r)]:[])),new o(...s)}function l(...t){return Object.freeze(c(...t))}var m=l,d=l,E={stringList:m,sl:d};export{E as default,d as sl,m as stringList}; diff --git a/stringList.cjs b/stringList.cjs index 2f42c8d..c0cefbf 100644 --- a/stringList.cjs +++ b/stringList.cjs @@ -1 +1 @@ -var a=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var d=(e,t)=>{for(var r in t)a(e,r,{get:t[r],enumerable:!0})},y=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of m(t))!c.call(e,i)&&i!==r&&a(e,i,{get:()=>t[i],enumerable:!(n=h(t,i))||n.enumerable});return e};var w=e=>y(a({},"__esModule",{value:!0}),e);var b={};d(b,{default:()=>g,sl:()=>f,stringList:()=>l});module.exports=w(b);var s=(e,t)=>{if(e&&t&&Object.isFrozen(e)){/* c8 ignore next 3 @preserve */return Object.freeze(t)}return t},u=class e extends Array{infered={Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0};enum;hasEmpty=!1;constructor(...t){let r=[],n=[],i=!1;for(let p of t.flat())typeof p=="string"&&(p===""&&(i=!0),r.push([p,p]),n.push(p));super(...n),this.hasEmpty=i,this.enum=Object.fromEntries(r),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...t){return s(this,new e(...super.concat.apply(this,t.flat())))}concatList(t){return this.concat(...t)}toSorted(){return s(this,new e(...super.toSorted.apply(this,arguments)))}toReversed(){return s(this,new e(...super.toReversed.apply(this,arguments)))}reverse(){return s(this,new e(...super.reverse.apply(this,arguments)))}toSpliced(){return s(this,new e(...super.toSpliced.apply(this,arguments)))}slice(){return s(this,new e(...super.slice.apply(this,arguments)))}without(){let t=Array.from(arguments).flatMap(r=>Array.isArray(r)?r.filter(n=>typeof n=="string"):typeof r=="string"?[r]:[]);return s(this,new e(...this.filter(r=>!t.includes(r))))}withTrim(){return s(this,new e(...super.map(t=>t.trim())))}withPrefix(t=""){return s(this,new e(...super.map(r=>`${t}${r}`)))}withSuffix(t=""){return s(this,new e(...super.map(r=>`${r}${t}`)))}withReplace(t,r=void 0){return s(this,new e(...super.map(n=>n.replace(t,r))))}withReplaceAll(t,r=void 0){return s(this,new e(...super.map(n=>n.replaceAll(t,r))))}toLowerCase(){return s(this,new e(...super.map(t=>t.toLowerCase())))}toUpperCase(){return s(this,new e(...super.map(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"&&(this.enum[t]||this.hasEmpty&&t===""))return t;throw new Error(`Invalid value ${t}`)}mutable(){return Array.from(this)}toRecordValue(t=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(n=>[n,t])))}toRecordType(t="any",r=void 0,...n){return Object.assign({},...n,Object.fromEntries(super.map(i=>[i,r])))}map(){let t=this.mutable();return t.map.apply(t,arguments)}filter(){let t=this.mutable();return t.filter.apply(t,arguments)}reduce(){let t=this.mutable();return t.reduce.apply(t,arguments)}reduceRight(){let t=this.mutable();return t.reduceRight.apply(t,arguments)}flat(){let t=this.mutable();return t.flat.apply(t,arguments)}flatMap(){let t=this.mutable();return t.flatMap.apply(t,arguments)}with(){let t=this.mutable();return t.with.apply(t,arguments)}},E=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function o(...e){let t=e,r=e.some(n=>typeof n!="string");return e.length&&r&&(typeof window>"u",t=e.flatMap(n=>Array.isArray(n)?n.filter(i=>typeof i=="string"):typeof n=="string"?[n]:typeof n=="number"?[String(n)]:[])),new u(...t)}var l=o,f=o,g={stringList:l,sl:f}; +var a=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var d=(t,e)=>{for(var r in e)a(t,r,{get:e[r],enumerable:!0})},y=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of c(e))!m.call(t,i)&&i!==r&&a(t,i,{get:()=>e[i],enumerable:!(s=h(e,i))||s.enumerable});return t};var w=t=>y(a({},"__esModule",{value:!0}),t);var b={};d(b,{default:()=>g,sl:()=>f,stringList:()=>l});module.exports=w(b);var n=(t,e)=>{if(t&&e&&Object.isFrozen(t)){/* c8 ignore next 3 @preserve */return Object.freeze(e)}return e},u=class t extends Array{infered={Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0};enum;hasEmpty=!1;constructor(...e){let r=[],s=[],i=!1;for(let p of e.flat())typeof p=="string"&&(p===""&&(i=!0),r.push([p,p]),s.push(p));super(...s),this.hasEmpty=i,this.enum=Object.fromEntries(r),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...e){return n(this,new t(...super.concat.apply(this,e.flat())))}concatList(e){return this.concat(...e)}toSorted(){return n(this,new t(...super.toSorted.apply(this,arguments)))}toReversed(){return n(this,new t(...super.toReversed.apply(this,arguments)))}reverse(){return n(this,new t(...super.reverse.apply(this,arguments)))}toSpliced(){return n(this,new t(...super.toSpliced.apply(this,arguments)))}slice(){return n(this,new t(...super.slice.apply(this,arguments)))}without(){let e=Array.from(arguments).flatMap(r=>Array.isArray(r)?r.filter(s=>typeof s=="string"):typeof r=="string"?[r]:[]);return n(this,new t(...this.filter(r=>!e.includes(r))))}withTrim(){return n(this,new t(...super.map(e=>e.trim())))}withPrefix(e=""){return n(this,new t(...super.map(r=>`${e}${r}`)))}withSuffix(e=""){return n(this,new t(...super.map(r=>`${r}${e}`)))}withReplace(e,r=void 0){return n(this,new t(...super.map(s=>s.replace(e,r))))}withReplaceAll(e,r=void 0){return n(this,new t(...super.map(s=>s.replaceAll(e,r))))}toLowerCase(){return n(this,new t(...super.map(e=>e.toLowerCase())))}toUpperCase(){return n(this,new t(...super.map(e=>e.toUpperCase())))}toCapitalize(){return this.withReplace(/\b\w/g,e=>e.toUpperCase())}toUnCapitalize(){return this.withReplace(/\b\w/g,e=>e.toLowerCase())}value(e){if(typeof e=="string"&&(this.enum[e]||this.hasEmpty&&e===""))return e;throw new Error(`Invalid value ${e}`)}mutable(){return Array.from(this)}toRecordValue(e=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(s=>[s,e])))}toRecordType(e="any",r=void 0,...s){return Object.assign({},...s,Object.fromEntries(super.map(i=>[i,r])))}asMap(){return new Map(super.map(e=>[e,e]))}asSet(){return new Set(this)}asObject(){return Object.assign({},...super.map(e=>({[e]:e})))}map(){let e=this.mutable();return e.map.apply(e,arguments)}filter(){let e=this.mutable();return e.filter.apply(e,arguments)}reduce(){let e=this.mutable();return e.reduce.apply(e,arguments)}reduceRight(){let e=this.mutable();return e.reduceRight.apply(e,arguments)}flat(){let e=this.mutable();return e.flat.apply(e,arguments)}flatMap(){let e=this.mutable();return e.flatMap.apply(e,arguments)}with(){let e=this.mutable();return e.with.apply(e,arguments)}},A=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function o(...t){let e=t,r=t.some(s=>typeof s!="string");return t.length&&r&&(typeof window>"u"&&process&&process.env,e=t.flatMap(s=>Array.isArray(s)?s.filter(i=>typeof i=="string"):typeof s=="string"?[s]:typeof s=="number"?[String(s)]:[])),new u(...e)}var l=o,f=o,g={stringList:l,sl:f}; diff --git a/stringList.min.cjs b/stringList.min.cjs index fef5cb2..d0f411e 100644 --- a/stringList.min.cjs +++ b/stringList.min.cjs @@ -1 +1 @@ -var u=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var w=(s,t)=>{for(var e in t)u(s,e,{get:t[e],enumerable:!0})},g=(s,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let p of c(t))!y.call(s,p)&&p!==e&&u(s,p,{get:()=>t[p],enumerable:!(r=f(t,p))||r.enumerable});return s};var d=s=>g(u({},"__esModule",{value:!0}),s);var R={};w(R,{default:()=>j,sl:()=>m,stringList:()=>h});module.exports=d(R);var b=Object.defineProperty,v=(s,t,e)=>t in s?b(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,l=(s,t,e)=>(v(s,typeof t!="symbol"?t+"":t,e),e),a=(s,t)=>{if(s&&t&&Object.isFrozen(s)){/* c8 ignore next 3 @preserve */return Object.freeze(t)}return t},O=class i extends Array{constructor(...t){let e=[],r=[],p=!1;for(let n of t.flat())typeof n=="string"&&(n===""&&(p=!0),e.push([n,n]),r.push(n));super(...r),l(this,"infered",{Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0}),l(this,"enum"),l(this,"hasEmpty",!1),this.hasEmpty=p,this.enum=Object.fromEntries(e),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...t){return a(this,new i(...super.concat.apply(this,t.flat())))}concatList(t){return this.concat(...t)}toSorted(){return a(this,new i(...super.toSorted.apply(this,arguments)))}toReversed(){return a(this,new i(...super.toReversed.apply(this,arguments)))}reverse(){return a(this,new i(...super.reverse.apply(this,arguments)))}toSpliced(){return a(this,new i(...super.toSpliced.apply(this,arguments)))}slice(){return a(this,new i(...super.slice.apply(this,arguments)))}without(){let t=Array.from(arguments).flatMap(e=>Array.isArray(e)?e.filter(r=>typeof r=="string"):typeof e=="string"?[e]:[]);return a(this,new i(...this.filter(e=>!t.includes(e))))}withTrim(){return a(this,new i(...super.map(t=>t.trim())))}withPrefix(t=""){return a(this,new i(...super.map(e=>`${t}${e}`)))}withSuffix(t=""){return a(this,new i(...super.map(e=>`${e}${t}`)))}withReplace(t,e=void 0){return a(this,new i(...super.map(r=>r.replace(t,e))))}withReplaceAll(t,e=void 0){return a(this,new i(...super.map(r=>r.replaceAll(t,e))))}toLowerCase(){return a(this,new i(...super.map(t=>t.toLowerCase())))}toUpperCase(){return a(this,new i(...super.map(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"&&(this.enum[t]||this.hasEmpty&&t===""))return t;throw new Error(`Invalid value ${t}`)}mutable(){return Array.from(this)}toRecordValue(t=void 0,...e){return Object.assign({},...e,Object.fromEntries(super.map(r=>[r,t])))}toRecordType(t="any",e=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(p=>[p,e])))}map(){let t=this.mutable();return t.map.apply(t,arguments)}filter(){let t=this.mutable();return t.filter.apply(t,arguments)}reduce(){let t=this.mutable();return t.reduce.apply(t,arguments)}reduceRight(){let t=this.mutable();return t.reduceRight.apply(t,arguments)}flat(){let t=this.mutable();return t.flat.apply(t,arguments)}flatMap(){let t=this.mutable();return t.flatMap.apply(t,arguments)}with(){let t=this.mutable();return t.with.apply(t,arguments)}},C=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function o(...s){let t=s,e=s.some(r=>typeof r!="string");return s.length&&e&&(typeof window>"u",t=s.flatMap(r=>Array.isArray(r)?r.filter(p=>typeof p=="string"):typeof r=="string"?[r]:typeof r=="number"?[String(r)]:[])),new O(...t)}var h=o,m=o,j={stringList:h,sl:m}; +var u=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var w=(s,e)=>{for(var t in e)u(s,t,{get:e[t],enumerable:!0})},g=(s,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of c(e))!y.call(s,n)&&n!==t&&u(s,n,{get:()=>e[n],enumerable:!(r=f(e,n))||r.enumerable});return s};var b=s=>g(u({},"__esModule",{value:!0}),s);var R={};w(R,{default:()=>O,sl:()=>m,stringList:()=>h});module.exports=b(R);var d=Object.defineProperty,v=(s,e,t)=>e in s?d(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,l=(s,e,t)=>(v(s,typeof e!="symbol"?e+"":e,t),t),a=(s,e)=>{if(s&&e&&Object.isFrozen(s)){/* c8 ignore next 3 @preserve */return Object.freeze(e)}return e},j=class i extends Array{constructor(...e){let t=[],r=[],n=!1;for(let p of e.flat())typeof p=="string"&&(p===""&&(n=!0),t.push([p,p]),r.push(p));super(...r),l(this,"infered",{Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0}),l(this,"enum"),l(this,"hasEmpty",!1),this.hasEmpty=n,this.enum=Object.fromEntries(t),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...e){return a(this,new i(...super.concat.apply(this,e.flat())))}concatList(e){return this.concat(...e)}toSorted(){return a(this,new i(...super.toSorted.apply(this,arguments)))}toReversed(){return a(this,new i(...super.toReversed.apply(this,arguments)))}reverse(){return a(this,new i(...super.reverse.apply(this,arguments)))}toSpliced(){return a(this,new i(...super.toSpliced.apply(this,arguments)))}slice(){return a(this,new i(...super.slice.apply(this,arguments)))}without(){let e=Array.from(arguments).flatMap(t=>Array.isArray(t)?t.filter(r=>typeof r=="string"):typeof t=="string"?[t]:[]);return a(this,new i(...this.filter(t=>!e.includes(t))))}withTrim(){return a(this,new i(...super.map(e=>e.trim())))}withPrefix(e=""){return a(this,new i(...super.map(t=>`${e}${t}`)))}withSuffix(e=""){return a(this,new i(...super.map(t=>`${t}${e}`)))}withReplace(e,t=void 0){return a(this,new i(...super.map(r=>r.replace(e,t))))}withReplaceAll(e,t=void 0){return a(this,new i(...super.map(r=>r.replaceAll(e,t))))}toLowerCase(){return a(this,new i(...super.map(e=>e.toLowerCase())))}toUpperCase(){return a(this,new i(...super.map(e=>e.toUpperCase())))}toCapitalize(){return this.withReplace(/\b\w/g,e=>e.toUpperCase())}toUnCapitalize(){return this.withReplace(/\b\w/g,e=>e.toLowerCase())}value(e){if(typeof e=="string"&&(this.enum[e]||this.hasEmpty&&e===""))return e;throw new Error(`Invalid value ${e}`)}mutable(){return Array.from(this)}toRecordValue(e=void 0,...t){return Object.assign({},...t,Object.fromEntries(super.map(r=>[r,e])))}toRecordType(e="any",t=void 0,...r){return Object.assign({},...r,Object.fromEntries(super.map(n=>[n,t])))}asMap(){return new Map(super.map(e=>[e,e]))}asSet(){return new Set(this)}asObject(){return Object.assign({},...super.map(e=>({[e]:e})))}map(){let e=this.mutable();return e.map.apply(e,arguments)}filter(){let e=this.mutable();return e.filter.apply(e,arguments)}reduce(){let e=this.mutable();return e.reduce.apply(e,arguments)}reduceRight(){let e=this.mutable();return e.reduceRight.apply(e,arguments)}flat(){let e=this.mutable();return e.flat.apply(e,arguments)}flatMap(){let e=this.mutable();return e.flatMap.apply(e,arguments)}with(){let e=this.mutable();return e.with.apply(e,arguments)}},E=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function o(...s){let e=s,t=s.some(r=>typeof r!="string");return s.length&&t&&(typeof window>"u"&&process&&process.env,e=s.flatMap(r=>Array.isArray(r)?r.filter(n=>typeof n=="string"):typeof r=="string"?[r]:typeof r=="number"?[String(r)]:[])),new j(...e)}var h=o,m=o,O={stringList:h,sl:m}; diff --git a/stringList.min.js b/stringList.min.js index 0658956..7eb136b 100644 --- a/stringList.min.js +++ b/stringList.min.js @@ -1 +1 @@ -var f=Object.defineProperty;var h=(r,n,t)=>n in r?f(r,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[n]=t;var a=(r,n,t)=>(h(r,typeof n!="symbol"?n+"":n,t),t);var i=(r,n)=>{if(r&&n&&Object.isFrozen(r)){/* c8 ignore next 3 @preserve */return Object.freeze(n)}return n},o=class r extends Array{constructor(...t){let e=[],s=[],u=!1;for(let p of t.flat())typeof p=="string"&&(p===""&&(u=!0),e.push([p,p]),s.push(p));super(...s);a(this,"infered",{Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0});a(this,"enum");a(this,"hasEmpty",!1);this.hasEmpty=u,this.enum=Object.fromEntries(e),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...t){return i(this,new r(...super.concat.apply(this,t.flat())))}concatList(t){return this.concat(...t)}toSorted(){return i(this,new r(...super.toSorted.apply(this,arguments)))}toReversed(){return i(this,new r(...super.toReversed.apply(this,arguments)))}reverse(){return i(this,new r(...super.reverse.apply(this,arguments)))}toSpliced(){return i(this,new r(...super.toSpliced.apply(this,arguments)))}slice(){return i(this,new r(...super.slice.apply(this,arguments)))}without(){let t=Array.from(arguments).flatMap(e=>Array.isArray(e)?e.filter(s=>typeof s=="string"):typeof e=="string"?[e]:[]);return i(this,new r(...this.filter(e=>!t.includes(e))))}withTrim(){return i(this,new r(...super.map(t=>t.trim())))}withPrefix(t=""){return i(this,new r(...super.map(e=>`${t}${e}`)))}withSuffix(t=""){return i(this,new r(...super.map(e=>`${e}${t}`)))}withReplace(t,e=void 0){return i(this,new r(...super.map(s=>s.replace(t,e))))}withReplaceAll(t,e=void 0){return i(this,new r(...super.map(s=>s.replaceAll(t,e))))}toLowerCase(){return i(this,new r(...super.map(t=>t.toLowerCase())))}toUpperCase(){return i(this,new r(...super.map(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"&&(this.enum[t]||this.hasEmpty&&t===""))return t;throw new Error(`Invalid value ${t}`)}mutable(){return Array.from(this)}toRecordValue(t=void 0,...e){return Object.assign({},...e,Object.fromEntries(super.map(s=>[s,t])))}toRecordType(t="any",e=void 0,...s){return Object.assign({},...s,Object.fromEntries(super.map(u=>[u,e])))}map(){let t=this.mutable();return t.map.apply(t,arguments)}filter(){let t=this.mutable();return t.filter.apply(t,arguments)}reduce(){let t=this.mutable();return t.reduce.apply(t,arguments)}reduceRight(){let t=this.mutable();return t.reduceRight.apply(t,arguments)}flat(){let t=this.mutable();return t.flat.apply(t,arguments)}flatMap(){let t=this.mutable();return t.flatMap.apply(t,arguments)}with(){let t=this.mutable();return t.with.apply(t,arguments)}},y=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function l(...r){let n=r,t=r.some(e=>typeof e!="string");return r.length&&t&&(typeof window>"u",n=r.flatMap(e=>Array.isArray(e)?e.filter(s=>typeof s=="string"):typeof e=="string"?[e]:typeof e=="number"?[String(e)]:[])),new o(...n)}var m=l,c=l,O={stringList:m,sl:c};export{O as default,c as sl,m as stringList}; +var f=Object.defineProperty;var h=(r,s,e)=>s in r?f(r,s,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[s]=e;var a=(r,s,e)=>(h(r,typeof s!="symbol"?s+"":s,e),e);var i=(r,s)=>{if(r&&s&&Object.isFrozen(r)){/* c8 ignore next 3 @preserve */return Object.freeze(s)}return s},o=class r extends Array{constructor(...e){let t=[],n=[],u=!1;for(let p of e.flat())typeof p=="string"&&(p===""&&(u=!0),t.push([p,p]),n.push(p));super(...n);a(this,"infered",{Union:void 0,Tuple:void 0,Mutable:void 0,Unsorted:void 0});a(this,"enum");a(this,"hasEmpty",!1);this.hasEmpty=u,this.enum=Object.fromEntries(t),this.hasEmpty&&(this.enum[""]=""),Object.freeze(this.enum)}concat(...e){return i(this,new r(...super.concat.apply(this,e.flat())))}concatList(e){return this.concat(...e)}toSorted(){return i(this,new r(...super.toSorted.apply(this,arguments)))}toReversed(){return i(this,new r(...super.toReversed.apply(this,arguments)))}reverse(){return i(this,new r(...super.reverse.apply(this,arguments)))}toSpliced(){return i(this,new r(...super.toSpliced.apply(this,arguments)))}slice(){return i(this,new r(...super.slice.apply(this,arguments)))}without(){let e=Array.from(arguments).flatMap(t=>Array.isArray(t)?t.filter(n=>typeof n=="string"):typeof t=="string"?[t]:[]);return i(this,new r(...this.filter(t=>!e.includes(t))))}withTrim(){return i(this,new r(...super.map(e=>e.trim())))}withPrefix(e=""){return i(this,new r(...super.map(t=>`${e}${t}`)))}withSuffix(e=""){return i(this,new r(...super.map(t=>`${t}${e}`)))}withReplace(e,t=void 0){return i(this,new r(...super.map(n=>n.replace(e,t))))}withReplaceAll(e,t=void 0){return i(this,new r(...super.map(n=>n.replaceAll(e,t))))}toLowerCase(){return i(this,new r(...super.map(e=>e.toLowerCase())))}toUpperCase(){return i(this,new r(...super.map(e=>e.toUpperCase())))}toCapitalize(){return this.withReplace(/\b\w/g,e=>e.toUpperCase())}toUnCapitalize(){return this.withReplace(/\b\w/g,e=>e.toLowerCase())}value(e){if(typeof e=="string"&&(this.enum[e]||this.hasEmpty&&e===""))return e;throw new Error(`Invalid value ${e}`)}mutable(){return Array.from(this)}toRecordValue(e=void 0,...t){return Object.assign({},...t,Object.fromEntries(super.map(n=>[n,e])))}toRecordType(e="any",t=void 0,...n){return Object.assign({},...n,Object.fromEntries(super.map(u=>[u,t])))}asMap(){return new Map(super.map(e=>[e,e]))}asSet(){return new Set(this)}asObject(){return Object.assign({},...super.map(e=>({[e]:e})))}map(){let e=this.mutable();return e.map.apply(e,arguments)}filter(){let e=this.mutable();return e.filter.apply(e,arguments)}reduce(){let e=this.mutable();return e.reduce.apply(e,arguments)}reduceRight(){let e=this.mutable();return e.reduceRight.apply(e,arguments)}flat(){let e=this.mutable();return e.flat.apply(e,arguments)}flatMap(){let e=this.mutable();return e.flatMap.apply(e,arguments)}with(){let e=this.mutable();return e.with.apply(e,arguments)}},y=Object.freeze({push:"push",unshift:"unshift",shift:"shift",copyWithin:"copyWithin",pop:"pop",fill:"fill",splice:"splice"});function l(...r){let s=r,e=r.some(t=>typeof t!="string");return r.length&&e&&(typeof window>"u"&&process&&process.env,s=r.flatMap(t=>Array.isArray(t)?t.filter(n=>typeof n=="string"):typeof t=="string"?[t]:typeof t=="number"?[String(t)]:[])),new o(...s)}var c=l,m=l,E={stringList:c,sl:m};export{E as default,m as sl,c as stringList}; diff --git a/stringList.test.js b/stringList.test.js index 609e2d5..5b57cd5 100644 --- a/stringList.test.js +++ b/stringList.test.js @@ -1787,5 +1787,53 @@ for (const { type, stringList } of functions) { t.end(); }); + + tt.test('stringList: asObject()', (t) => { + const list = stringList('foo', 'bar', 'baz'); + const obj = list.asObject(); + t.match(obj, { + foo: 'foo', + bar: 'bar', + baz: 'baz', + }); + testExpectedArrayValues(t, list, 'foo', 'bar', 'baz'); + t.equal(obj.foo, 'foo'); + t.equal(obj.bar, 'bar'); + t.equal(obj.baz, 'baz'); + t.equal(obj.fooz, undefined); + t.end(); + }); + tt.test('stringList: asMap()', (t) => { + const list = stringList('foo', 'bar', 'baz'); + const map = list.asMap(); + t.match( + map, + new Map([ + ['foo', 'foo'], + ['bar', 'bar'], + ['baz', 'baz'], + ]), + ); + testExpectedArrayValues(t, list, 'foo', 'bar', 'baz'); + t.equal(map.size, 3); + t.equal(map.get('foo'), 'foo'); + t.equal(map.get('bar'), 'bar'); + t.equal(map.get('baz'), 'baz'); + t.equal(map.get('fooz'), undefined); + + t.end(); + }); + tt.test('stringList: asSet()', (t) => { + const list = stringList('foo', 'bar', 'baz'); + const set = list.asSet(); + t.match(set, new Set(['foo', 'bar', 'baz'])); + testExpectedArrayValues(t, list, 'foo', 'bar', 'baz'); + t.equal(set.size, 3); + t.equal(set.has('foo'), true); + t.equal(set.has('bar'), true); + t.equal(set.has('baz'), true); + t.equal(set.has('fooz'), false); + t.end(); + }); }); } diff --git a/stringListFunction.js b/stringListFunction.js index 7894855..7fa1e29 100644 --- a/stringListFunction.js +++ b/stringListFunction.js @@ -11,8 +11,10 @@ export function stringListMutable(...strings) { /* c8 ignore start */ if ( typeof window === 'undefined' && - process?.env?.NODE_ENV !== 'production' && - process?.env?.NODE_ENV !== 'test' + process && + process.env && + process.env.NODE_ENV !== 'production' && + process.env.NODE_ENV !== 'test' ) { console.debug( `Unexpected type in stringList(${typeof invalid}). Casting all arguments to string type.`, diff --git a/types/list.d.ts b/types/list.d.ts index 0e33acb..b94789e 100644 --- a/types/list.d.ts +++ b/types/list.d.ts @@ -76,6 +76,19 @@ declare global { ? MaybeReadonly> : never; + withMapPrefix( + map: Map, + ): keyof Map extends Tuple[number] + ? { + [K in keyof Map]: `${Map[K]}${K}`; + }[keyof Map] extends infer W extends string + ? StringLiteralList.tuple.UnionToTuple extends infer W extends + readonly string[] + ? MaybeReadonly> + : never + : never + : never; + withSuffix

( suffix: P, ): P extends string @@ -88,6 +101,18 @@ declare global { : never : never; + withMapSuffix( + map: Map, + ): keyof Map extends Tuple[number] + ? { + [K in keyof Map]: `${K}${Map[K]}`; + }[keyof Map] extends infer W extends string + ? StringLiteralList.tuple.UnionToTuple extends infer W extends + readonly string[] + ? MaybeReadonly> + : never + : never + : never; // withDerivatedSuffix( // chars: S, // ): @@ -184,14 +209,12 @@ declare global { ? MaybeReadonly> : this; - without( + without( ...arg: readonly (ILiterals | S)[] - ): StringLiteralList.tuple.TupleWithExclude< - Tuple, - S, - [] - > extends infer E extends readonly string[] - ? MaybeReadonly> + ): StringLiteralList.tuple.TupleWithExclude extends infer E + ? E extends readonly string[] + ? MaybeReadonly> + : MaybeReadonly> : never; toSorted( @@ -205,6 +228,12 @@ declare global { ? MaybeReadonly> : never; + asMap(): Map; + + asSet(): Set; + + asObject(): { [K in Tuple[number]]: K }; + /** * @deprecated This method does not support type inference, * use `list.concat('str', 'str')` or `list.concatList(stringList('str','str'))` instead.