diff --git a/README.md b/README.md index 951c39b..2005d5f 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,9 @@ v.withSuffix('.suffix') => SL<"foo.suffix" | "bar.suffix"> v.concat('zing', 'boom', stringList('zig', 'zag')) => SL<"foo" | "bar" | "zing" | "boom" | "zig" | "zag">; +v.value('foo') => 'foo'; + +v.value('not') => throws; ``` ## Installation @@ -80,6 +83,13 @@ const bothWay = list.withPrefix('data.').withSuffix('.ext'); let val; list.includes(val); // No type error just boolean result. // list implements similar fix for indexOf, lastIndexOf, filter, some, every, findIndex and find methods. + +// Get a copy of the underlying array -> string[] +const arr = list.mutable(); // => ["foo", "bar"] + +// access a value in the list +list.value('foo'); // 'foo' +list.value('n'); // throws error ``` #### list.concat(...(string|StringList)[]) diff --git a/StringLiteralList.d.ts b/StringLiteralList.d.ts index d9cd346..2a822d4 100644 --- a/StringLiteralList.d.ts +++ b/StringLiteralList.d.ts @@ -18,6 +18,7 @@ export interface IStringList withSuffix

( suffix: P, ): IStringList>; + value(val: V): V extends T ? T : never; mutable(): string[]; // Implemented methods to return the frozen array, typed as IStringList. diff --git a/StringLiteralList.js b/StringLiteralList.js index 63ca6db..689ee47 100644 --- a/StringLiteralList.js +++ b/StringLiteralList.js @@ -25,6 +25,13 @@ export class SL extends Array { return Object.freeze(new SL(...super.map((e) => `${e}${suffix}`))); } + value(value) { + if (this.includes(value)) { + return value; + } + throw new Error(`Invalid value ${value}`); + } + // Get the native array mutable() { return Array.from(this); diff --git a/package-lock.json b/package-lock.json index b6dbce1..e51d9aa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "string-literal-list", - "version": "1.4.1", + "version": "1.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "string-literal-list", - "version": "1.4.1", + "version": "1.5.0", "license": "MIT", "dependencies": { "core-js": "^3.36.0" diff --git a/package.json b/package.json index dacddd2..c351e09 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "string-literal-list", - "version": "1.4.1", + "version": "1.5.0", "description": "an array for string literal", "main": "stringList.js", "types": "stringList.d.ts", diff --git a/stringList.test.js b/stringList.test.js index 3a0de8c..9b43b80 100644 --- a/stringList.test.js +++ b/stringList.test.js @@ -32,8 +32,11 @@ const testExpectedArrayValues = (st, list, ...values) => { st.ok(list.findIndex((v) => v === value) === i); st.ok(list.some((v) => v === value) === true); st.ok(list.every((v) => v === value) === (list.length === 1)); + st.ok(list.value(value) === value); } + st.throws(() => list.value(`${Math.random() * 100000}!`)); + st.notOk(list.includes(null)); st.ok(list.at(values.length) === undefined); }; @@ -283,3 +286,5 @@ t.test('stringList mutable', (t) => { }); t.end(); }); + +stringList('d').value('d'); diff --git a/types.d.ts b/types.d.ts index 102df10..5d416fb 100644 --- a/types.d.ts +++ b/types.d.ts @@ -28,7 +28,8 @@ export namespace sl { | 'withPrefix' | 'withSuffix' | 'toReversed' - | 'toSorted'; + | 'toSorted' + | 'value'; /** * @description