Skip to content

Commit

Permalink
Merge pull request #1 from rawpixel-vincent/test-ci
Browse files Browse the repository at this point in the history
clean tests, exports and ci
  • Loading branch information
rawpixel-vincent authored Mar 3, 2024
2 parents 15e7a3c + 387fa88 commit d47766e
Show file tree
Hide file tree
Showing 14 changed files with 6,961 additions and 606 deletions.
13 changes: 13 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": ["eslint:recommended", "prettier"],
"overrides": [],
"parserOptions": {
"ecmaVersion": 14,
"sourceType": "module"
}
}
4 changes: 1 addition & 3 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: npm

on:
Expand All @@ -19,6 +16,7 @@ jobs:
node-version: '20'
registry-url: 'https://registry.npmjs.org/'
- run: npm ci
- run: npm test
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
26 changes: 26 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: tests

on:
push:
branches: ['main']
pull_request:
branches: ['main']

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: ['20', 'latest']
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install --include=dev
- run: npm run test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.tap
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 80,
"singleQuote": true,
"trailingComma": "all"
}
99 changes: 99 additions & 0 deletions StringLiteralList.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { sl } from './types.js';

export interface IStringList<T extends unknown>
extends Omit<
Array<T>,
| sl.specs.ImplementedMethod
| sl.specs.OmitedMutableMethod
| sl.specs.NativeMethodWithTypeOverride
> {
// Custom Methods
withPrefix<P extends string>(
prefix: P,
): IStringList<sl.utils.StringConcat<P, T extends string ? T : string>>;
withSuffix<P extends string>(
suffix: P,
): IStringList<sl.utils.StringConcat<T extends string ? T : string, P>>;
mutable(): string[];

// Implemented methods to return the frozen array, typed as IStringList.
toSorted(compareFn?: (a: T, b: T) => number): IStringList<T>;
toReversed(): IStringList<T>;
concat<PP extends T, P extends string = string>(
...arg: P[]
): IStringList<Record<P, P>[keyof Record<P, P>] | PP>;

// Readonly overrides
readonly length: number;
readonly [n: number]: T | undefined;

// Supported Methods
at(n: number): T;

// Type override to prevent string not in type T issue
includes<PP = T>(val: PP, fromIndex?: number): boolean;
indexOf<PP = T>(searchElement: PP, fromIndex?: number): number;
lastIndexOf<PP = T>(searchElement: PP, fromIndex?: number): number;

find<PP = T>(
predictate: (
val: PP extends T ? T : PP,
i: number,
obj: IStringList<T>,
) => val is PP extends T ? T : PP,
): T;
find(
predictate: (
val: string | undefined,
i: number,
obj: IStringList<T>,
) => boolean,
): T;
findIndex<PP = T>(
predictate: (val: PP extends T ? PP : string) => boolean,
): number;
findIndex(
predictate: (
val: string | undefined,
i: number,
obj: IStringList<T>,
) => boolean,
): number;

some<PP = T>(
predictate: (val: PP extends T ? PP : string) => boolean,
): boolean;
every<PP = T>(
predictate: (val: PP extends T ? PP : string) => boolean,
): boolean;

// Return Type overrides
filter<S extends T>(
predicate: (value: T, index: number, array: IStringList<T>) => value is S,
thisArg?: any,
): S[];
filter(
predicate: (value: string, index: number, array: IStringList<T>) => boolean,
thisArg?: any,
): string[];
toSpliced(start: number, deleteCount: number, ...items: string[]): string[];
}

export class StringLiteralList<T extends string> {
constructor(...list: T[]);
}

export interface ArrayInPlaceMutation {
push: 'push';
slice: 'slice';
sort: 'sort';
unshift: 'unshift';
shift: 'shift';
copyWithin: 'copyWithin';
pop: 'pop';
fill: 'fill';
splice: 'splice';
reverse: 'reverse';
}

export const ARRAY_IN_PLACE_MUTATION: ArrayInPlaceMutation;
84 changes: 84 additions & 0 deletions StringLiteralList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
export class StringLiteralList extends Array {
concat() {
return Object.freeze(
new StringLiteralList(...super.concat.apply(this, arguments)),
);
}

toSorted() {
return Object.freeze(
new StringLiteralList(...super.toSorted.apply(this, arguments)),
);
}

toReversed() {
return Object.freeze(
new StringLiteralList(...super.toReversed.apply(this, arguments)),
);
}

withPrefix(prefix) {
return Object.freeze(
new StringLiteralList(...super.map((e) => `${prefix}${e}`)),
);
}

withSuffix(suffix) {
return Object.freeze(
new StringLiteralList(...super.map((e) => `${e}${suffix}`)),
);
}

// Get the native array
mutable() {
return Array.from(this);
}

// Methods returning the native array
map() {
const mut = this.mutable();
return mut.map.apply(mut, arguments);
}
filter() {
const mut = this.mutable();
return mut.filter.apply(mut, arguments);
}
reduce() {
const mut = this.mutable();
return mut.reduce.apply(mut, arguments);
}
reduceRight() {
const mut = this.mutable();
return mut.reduceRight.apply(mut, arguments);
}
flat() {
const mut = this.mutable();
return mut.flat.apply(mut, arguments);
}
flatMap() {
const mut = this.mutable();
return mut.flatMap.apply(mut, arguments);
}
toSpliced() {
const mut = this.mutable();
return mut.toSpliced.apply(mut, arguments);
}
}

export const ARRAY_IN_PLACE_MUTATION = Object.freeze({
push: 'push',
slice: 'slice',
sort: 'sort',
unshift: 'unshift',
shift: 'shift',
copyWithin: 'copyWithin',
pop: 'pop',
fill: 'fill',
splice: 'splice',
reverse: 'reverse',
});
Object.values(ARRAY_IN_PLACE_MUTATION).forEach((el) => {
StringLiteralList.prototype[el] = () => {
throw new Error(`Array method ${el} is not supported by StringLiteralList`);
};
});
5 changes: 2 additions & 3 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
"lib": ["ESNext", "DOM"],
"moduleResolution": "nodenext",
"resolveJsonModule": false,
"esModuleInterop": true,
"esModuleInterop": false,
"checkJs": true,
"skipLibCheck": false,
"maxNodeModuleJsDepth": 0,
"allowJs": true,
"traceResolution": false
},
"include": ["types.d.ts", "stringList.js", "stringList.test.js"]
"include": ["*.js", "*.d.ts"]
}
Loading

0 comments on commit d47766e

Please sign in to comment.