Skip to content

Commit

Permalink
chore: Add methods to convert StringLiteralList to Map, Set, and Object
Browse files Browse the repository at this point in the history
  • Loading branch information
rawpixel-vincent committed Aug 13, 2024
1 parent 4644a73 commit d17ba20
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<literal, type | typeof value>` 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.

Expand Down
12 changes: 12 additions & 0 deletions StringLiteralList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion strict.cjs
Original file line number Diff line number Diff line change
@@ -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};
2 changes: 1 addition & 1 deletion strict.min.cjs
Original file line number Diff line number Diff line change
@@ -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};
Loading

0 comments on commit d17ba20

Please sign in to comment.