Skip to content

Commit

Permalink
optimize types
Browse files Browse the repository at this point in the history
  • Loading branch information
rawpixel-vincent committed Mar 16, 2024
1 parent 8575d34 commit 7509705
Show file tree
Hide file tree
Showing 21 changed files with 1,800 additions and 6,615 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
]
},
"overrides": [],
"ignorePatterns": ["*.cjs"],
"ignorePatterns": ["*.cjs", "*.min.js"],
"parserOptions": {
"ecmaVersion": 14,
"sourceType": "module"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

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

steps:
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The StringList class extends the Array with new methods and supercharges the arr
- `withTrim()`: trim all the words.
- `withReplace(search, replacement)`: call the String.prototype.replace on all.
- `withReplaceAll(search, replacement)`: call the String.prototype.replaceAll on all.
- `to"Case"()` methods for case transform, camel, pascal, snake, capitalize, etc...
- `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.

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 Expand Up @@ -135,9 +135,6 @@ const scored = sl(' has ', 'spaces', ' between ', ' o r', 'in the words')

const Upper = sl('foo', 'bar').toUpperCase();
// SL<["FOO","BAR"]>

const Pascal = sl('foo', 'bar').withPrefix('pascal_').toPascalCase();
// SL<["PascalFoo","PascalBar"]>
```

#### list.concat(...(string)[]) and list.concatList(sl)
Expand Down
50 changes: 1 addition & 49 deletions StringLiteralList.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import 'core-js/actual/array/to-reversed.js';
import 'core-js/actual/array/to-sorted.js';
import 'core-js/actual/array/to-spliced.js';
import 'core-js/actual/array/find-last-index.js';
import 'core-js/actual/array/with.js';

const SeparatorsRegexp = /[[|\]{}()\\/\-_\\ .,\]]+/g;

const freezeIfImmutable = (source, target) => {
if (source && target && Object.isFrozen(source)) {
/* c8 ignore next 3 @preserve */
return Object.freeze(target);
}
return target;
Expand Down Expand Up @@ -188,47 +181,6 @@ export class SL extends Array {
return this.withReplace(/\b\w/g, (char) => char.toLowerCase());
}

toPascalCase() {
return freezeIfImmutable(
this,
new SL(
...super.map((e) =>
e
.trim()
.replace(SeparatorsRegexp, '_')
.replace(/[^a-z0-9_]+/gi, '')
.replace(/[_]+/g, '_')
.replace(/(?:^|_)(\w)/g, (_, char) => char.toUpperCase())
.replace(/[\s_]+/g, '')
.trim(),
),
),
).toCapitalize();
}

toCamelCase() {
return this.withPrefix('_').toPascalCase().toUnCapitalize();
}

toSnakeCase() {
return freezeIfImmutable(
this,
new SL(
...super.map((e) =>
e
.trim()
.replace(SeparatorsRegexp, '_')
.replace(/[^a-z0-9_]+/gi, '')
.replace(/([A-Z])/g, (_, char) => `_${char.toLowerCase()}`)
.replace(/[\s_]+/g, '_')
.replace(/^[_]+/g, '')
.toLowerCase()
.trim(),
),
),
);
}

value(value) {
if (
typeof value === 'string' &&
Expand Down
12 changes: 10 additions & 2 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
"traceResolution": false,
"allowSyntheticDefaultImports": true
},
"include": ["*.js", "*.d.ts", "*.ts", "types/*.d.ts"],
"exclude": ["node_modules", "*.cjs", "**/*.cjs"]
"include": [
"*.d.ts",
"types/*.d.ts",
"*.ts",
"strict.js",
"stringList.js",
"StringLiteralList.js",
"stringListFunction.js"
],
"exclude": ["node_modules", "*.cjs", "**/*.cjs", "*.min.js", "**/*.min.js"]
}
24 changes: 8 additions & 16 deletions package-lock.json

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

56 changes: 35 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,58 @@
{
"name": "string-literal-list",
"version": "1.17.0",
"version": "1.18.0",
"description": "an array for string literal",
"main": "stringList.js",
"main": "stringList.cjs",
"module": "stringList.js",
"browser": {
"./stringList.cjs": "./stringList.min.cjs",
"./stringList.js": "./stringList.min.js"
},
"types": "stringList.d.ts",
"exports": {
".": {
"import": "./stringList.js",
"require": "./stringList.cjs"
"types": "./stringList.js",
"require": "./stringList.cjs",
"node": "./stringList.js",
"import": "./stringList.min.js",
"default": "./stringList.js"
},
"./strict.js": {
"import": "./strict.js",
"require": "./strict.cjs"
"types": "./strict.js",
"require": "./strict.cjs",
"node": "./strict.js",
"import": "./strict.min.js",
"default": "./strict.js"
},
"./types.js": {
"import": "./types/index.js",
"require": "./types/index.js"
"default": "./types/index.js"
},
"./stringList.js": {
"import": "./stringList.js",
"require": "./stringList.cjs"
"types": "./stringList.js",
"require": "./stringList.cjs",
"node": "./stringList.js",
"import": "./stringList.min.js",
"default": "./stringList.js"
}
},
"engines": {
"node": ">=16"
"node": ">=18"
},
"packageManager": "[email protected]",
"license": "MIT",
"scripts": {
"test": "npm run lint:ci && npm run test:checkJs && npm run test:unit",
"test:unit": "NODE_ENV=test tap run && tap report --show-full-coverage",
"test:unit": "NODE_ENV=test tap run",
"test:checkJs": "tsc --checkJs --project ./jsconfig.json",
"prettier": "prettier --write \"**/*.{js,ts}\"",
"lint": "eslint --fix \"./*.js\"",
"lint:ci": "eslint . --ext .js",
"build": "npm run build:main && npm run build:strict",
"build:strict": "esbuild ./strict.js --bundle --format=cjs --keep-names --outfile=strict.cjs --target=node16",
"build:main": "esbuild ./stringList.js --bundle --format=cjs --keep-names --outfile=stringList.cjs --target=node16",
"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: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:cjs": "esbuild ./stringList.js --bundle --format=cjs --outfile=stringList.cjs --target=node18 --minify --legal-comments=inline",
"prepare": "husky"
},
"author": "Vincent Baronnet",
Expand All @@ -46,7 +62,9 @@
"url": "https://github.com/rawpixel-vincent/string-literal-list.git"
},
"devDependencies": {
"esbuild": "^0.20.1",
"@types/node": "latest",
"core-js": "latest",
"esbuild": "latest",
"eslint": "latest",
"eslint-config-prettier": "latest",
"eslint-config-standard": "latest",
Expand All @@ -55,9 +73,5 @@
"prettier": "latest",
"tap": "latest",
"typescript": "latest"
},
"dependencies": {
"core-js": "latest",
"string-ts": "^2.0.0"
}
}
Loading

0 comments on commit 7509705

Please sign in to comment.