Skip to content

Commit

Permalink
Merge pull request #3 from rawpixel-vincent/add-value-method
Browse files Browse the repository at this point in the history
add `value(T):T` method to get a constant from the list
  • Loading branch information
rawpixel-vincent authored Mar 4, 2024
2 parents 76ba628 + 6096db6 commit bfc6b78
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)[])
Expand Down
1 change: 1 addition & 0 deletions StringLiteralList.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface IStringList<T extends unknown>
withSuffix<P extends string>(
suffix: P,
): IStringList<sl.utils.StringConcat<T extends string ? T : string, P>>;
value<V extends T & string>(val: V): V extends T ? T : never;
mutable(): string[];

// Implemented methods to return the frozen array, typed as IStringList.
Expand Down
7 changes: 7 additions & 0 deletions StringLiteralList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
5 changes: 5 additions & 0 deletions stringList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down Expand Up @@ -283,3 +286,5 @@ t.test('stringList mutable', (t) => {
});
t.end();
});

stringList('d').value('d');
3 changes: 2 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export namespace sl {
| 'withPrefix'
| 'withSuffix'
| 'toReversed'
| 'toSorted';
| 'toSorted'
| 'value';

/**
* @description
Expand Down

0 comments on commit bfc6b78

Please sign in to comment.