@laudebugs/types 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +44 -0
- package/LICENSE +7 -0
- package/README.md +26 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/lib/constructable.d.ts +1 -0
- package/dist/lib/constructable.js +2 -0
- package/dist/lib/deep-partial.d.ts +7 -0
- package/dist/lib/deep-partial.js +2 -0
- package/dist/lib/filtered-keys.d.ts +4 -0
- package/dist/lib/filtered-keys.js +8 -0
- package/dist/lib/filtered-type.d.ts +3 -0
- package/dist/lib/filtered-type.js +2 -0
- package/dist/lib/get-constructor-params.d.ts +8 -0
- package/dist/lib/get-constructor-params.js +95 -0
- package/dist/lib/get-enum-values.d.ts +18 -0
- package/dist/lib/get-enum-values.js +32 -0
- package/dist/lib/index.d.ts +7 -0
- package/dist/lib/index.js +23 -0
- package/dist/lib/value-of.d.ts +1 -0
- package/dist/lib/value-of.js +2 -0
- package/package.json +15 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
Entries up to 0.2.5 were generated by [@jscutlery/semver](https://github.com/jscutlery/semver); later ones are written by hand.
|
|
4
|
+
|
|
5
|
+
## 0.4.0
|
|
6
|
+
|
|
7
|
+
### Minor Changes
|
|
8
|
+
|
|
9
|
+
- 2a1fae1: The utilities now do what their names say. Each of these can change what compiles or what you get back:
|
|
10
|
+
|
|
11
|
+
- `DeepPartial<T>` makes keys and array elements optional at every depth; before, nothing became optional. Use `T` where keys must be required. Primitives, functions and built-in objects such as `Date` and `Map` are kept whole.
|
|
12
|
+
- `filterKeys` is typed as returning the object's key names, numeric ones as strings, and no longer takes a second type argument. A global or sticky pattern no longer skips keys.
|
|
13
|
+
- `getEnumValues` no longer returns the names of numeric members.
|
|
14
|
+
- `getConstructorParams` reads the class's own constructor, keeps a default value containing commas in one piece, removes comments, and returns an empty list for a class that declares none.
|
|
15
|
+
|
|
16
|
+
## 0.3.0 (2026-12-02)
|
|
17
|
+
|
|
18
|
+
- The package has its code again: 0.2.1 on npm held only `package.json`.
|
|
19
|
+
- Files moved under `dist/`, and the build is CommonJS only. Importing the package by name works from `require` and from ES modules; imports of files inside 0.1.0 no longer resolve.
|
|
20
|
+
- `getEnumValues` is available from `latest` for the first time.
|
|
21
|
+
- Known problems: `DeepPartial` does not make keys optional, `filterKeys` declares the wrong return type, and `getEnumValues` also returns the names of a numeric enum.
|
|
22
|
+
|
|
23
|
+
## [0.2.5](https://github.com/laudebugs/laudebugs/compare/types-0.2.4...types-0.2.5) (2024-02-05)
|
|
24
|
+
|
|
25
|
+
### ⚙️ Chores
|
|
26
|
+
|
|
27
|
+
- update types ([31b6aa4](https://github.com/laudebugs/laudebugs/commit/31b6aa478f0bc763061b61f4d2e587dab062a63f))
|
|
28
|
+
|
|
29
|
+
## [0.2.4](https://github.com/laudebugs/laudebugs/compare/types-0.2.3...types-0.2.4) (2024-02-05)
|
|
30
|
+
|
|
31
|
+
### 🤖 Continuous Integration
|
|
32
|
+
|
|
33
|
+
- update publish script ([6865019](https://github.com/laudebugs/laudebugs/commit/686501943eccc27cfa56db55288e51648cf21838))
|
|
34
|
+
|
|
35
|
+
## [0.2.3](https://github.com/laudebugs/laudebugs/compare/types-0.2.2...types-0.2.3) (2024-02-05)
|
|
36
|
+
|
|
37
|
+
### ⚙️ Chores
|
|
38
|
+
|
|
39
|
+
- run nx repair ([839afa5](https://github.com/laudebugs/laudebugs/commit/839afa5132d6fcf61c123deeb0b095812690fc01))
|
|
40
|
+
- **types:** release version 0.2.3 ([32e0879](https://github.com/laudebugs/laudebugs/commit/32e0879d9f84b6e55fa130ff1bc23b3b07e5389e))
|
|
41
|
+
|
|
42
|
+
### 🤖 Continuous Integration
|
|
43
|
+
|
|
44
|
+
- **types:** update project.json ([a1a1696](https://github.com/laudebugs/laudebugs/commit/a1a16966d4f625c3201ccae6af27cafd8a311d58))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2023-present, Laudebugs
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# TypeScript type utilities
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm i @laudebugs/types
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Version 0.2.1 on npm was published without its code; use 0.3.0 or later.
|
|
10
|
+
|
|
11
|
+
## Utility types
|
|
12
|
+
|
|
13
|
+
| Type | What it is |
|
|
14
|
+
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
15
|
+
| `Constructable<T>` | A class or constructor that builds a `T` |
|
|
16
|
+
| `DeepPartial<T>` | `T` with every key optional, at every depth, array elements included. Primitives, functions and built-in objects such as `Date` and `Map` stay as they are |
|
|
17
|
+
| `FilteredType<T, C>` | The keys of `T` whose values extend `C` |
|
|
18
|
+
| `ValueOf<T>` | The union of the values of `T` |
|
|
19
|
+
|
|
20
|
+
## Helpers
|
|
21
|
+
|
|
22
|
+
| Function | What it returns |
|
|
23
|
+
| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
24
|
+
| `getEnumValues(enum)` | The values of an enum, numeric, string or mixed |
|
|
25
|
+
| `filterKeys(object, pattern)` | The names of the object's keys that match the regular expression |
|
|
26
|
+
| `getConstructorParams(class)` | The parameters of the class's own constructor, read from the running code: defaults included, types gone, minified names if minified. Comments are removed; empty if it declares none. A regular expression holding quotes, backticks, brackets or `/*`, or a template literal nested in another, can mislead it |
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./lib"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Constructable<T> = new (...args: any[]) => T;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
type Whole = Function | Date | RegExp | Map<any, any> | Set<any> | ReadonlyMap<any, any> | ReadonlySet<any> | Error | WeakMap<any, any> | WeakSet<any> | Promise<any> | ArrayBuffer | ArrayBufferView;
|
|
2
|
+
type Primitive = string | number | boolean | bigint | symbol | null | undefined;
|
|
3
|
+
/** `T` with every key optional, at every depth. Primitives, functions and built-in objects are kept as they are. */
|
|
4
|
+
export type DeepPartial<T> = T extends Whole | Primitive ? T : T extends object ? {
|
|
5
|
+
[K in keyof T]?: DeepPartial<T[K]>;
|
|
6
|
+
} : T;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
type KeyName<T> = T extends unknown ? `${Extract<keyof T, string | number>}` : never;
|
|
2
|
+
/** The names of the object's own keys that match the pattern. */
|
|
3
|
+
export declare function filterKeys<T extends Record<string, any>>(source: T, regExp: RegExp): KeyName<T>[];
|
|
4
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.filterKeys = filterKeys;
|
|
4
|
+
/** The names of the object's own keys that match the pattern. */
|
|
5
|
+
function filterKeys(source, regExp) {
|
|
6
|
+
// search, unlike test, ignores and keeps the position a global pattern remembers between calls.
|
|
7
|
+
return Object.keys(source).filter((key) => key.search(regExp) !== -1);
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Constructable } from './constructable';
|
|
2
|
+
/**
|
|
3
|
+
* The parameters of a class's own constructor, read from the source text of the running code: a default value is
|
|
4
|
+
* included, types are gone, and minified code gives minified names. Empty if the class declares no constructor.
|
|
5
|
+
* Comments are removed. A regular expression literal holding quotes, backticks, brackets or `/*`, or a template
|
|
6
|
+
* literal nested in another, can still mislead it.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getConstructorParams<T>(target: Constructable<T>): string[];
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getConstructorParams = getConstructorParams;
|
|
4
|
+
const OPENING = '([{';
|
|
5
|
+
const CLOSING = ')]}';
|
|
6
|
+
// Escapes outside strings (in a regular expression, say), comments and string literals, in the order they start.
|
|
7
|
+
const NOT_CODE = /\\.|\/\/[^\n]*|\/\*[\s\S]*?\*\/|'(?:\\.|[^'\\\n])*'|"(?:\\.|[^"\\\n])*"|`(?:\\[\s\S]|[^`\\])*`/g;
|
|
8
|
+
// A method named constructor, not a call such as this.constructor() or a static method of that name.
|
|
9
|
+
const CONSTRUCTOR = /(?<![.\w$#])(?<!static\s+)constructor\s*\(/y;
|
|
10
|
+
/**
|
|
11
|
+
* Two copies of the source, the same length as it: `text` with comments blanked, and `code` with the insides of
|
|
12
|
+
* strings blanked too, so that brackets and commas can be counted.
|
|
13
|
+
*/
|
|
14
|
+
function blank(source) {
|
|
15
|
+
let text = source;
|
|
16
|
+
let code = source;
|
|
17
|
+
const put = (into, at, value) => into.slice(0, at) + value + into.slice(at + value.length);
|
|
18
|
+
for (const match of source.matchAll(NOT_CODE)) {
|
|
19
|
+
const [found] = match;
|
|
20
|
+
// Matching an escape keeps `\/\/` from reading as a comment, and an escaped bracket from being counted.
|
|
21
|
+
if (found.startsWith('\\')) {
|
|
22
|
+
code = put(code, match.index, '__');
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const spaces = ' '.repeat(found.length);
|
|
26
|
+
if (found.startsWith('/')) {
|
|
27
|
+
text = put(text, match.index, spaces);
|
|
28
|
+
code = put(code, match.index, spaces);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
code = put(code, match.index, found[0] + '_'.repeat(found.length - 2) + found[0]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { text, code };
|
|
35
|
+
}
|
|
36
|
+
/** Where the parameter list of the class's own constructor begins, just after its parenthesis. */
|
|
37
|
+
function constructorStart(code) {
|
|
38
|
+
let depth = 0;
|
|
39
|
+
let inBody = false;
|
|
40
|
+
for (let i = 0; i < code.length; i++) {
|
|
41
|
+
const character = code[i];
|
|
42
|
+
// The class body is the first brace outside any bracket; a brace in `extends mix({})` is inside one.
|
|
43
|
+
if (!inBody && depth === 0 && character === '{')
|
|
44
|
+
inBody = true;
|
|
45
|
+
if (inBody && depth === 1) {
|
|
46
|
+
CONSTRUCTOR.lastIndex = i;
|
|
47
|
+
if (CONSTRUCTOR.test(code))
|
|
48
|
+
return CONSTRUCTOR.lastIndex;
|
|
49
|
+
}
|
|
50
|
+
if (OPENING.includes(character))
|
|
51
|
+
depth++;
|
|
52
|
+
else if (CLOSING.includes(character))
|
|
53
|
+
depth--;
|
|
54
|
+
if (inBody && depth === 0)
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
/** The list from `start` to the parenthesis that closes it, split at its own commas. */
|
|
60
|
+
function parameters(text, code, start) {
|
|
61
|
+
const found = [];
|
|
62
|
+
let depth = 0;
|
|
63
|
+
let from = start;
|
|
64
|
+
for (let i = start; i < code.length; i++) {
|
|
65
|
+
const character = code[i];
|
|
66
|
+
if (OPENING.includes(character))
|
|
67
|
+
depth++;
|
|
68
|
+
else if (CLOSING.includes(character)) {
|
|
69
|
+
if (depth === 0)
|
|
70
|
+
return [...found, text.slice(from, i)];
|
|
71
|
+
depth--;
|
|
72
|
+
}
|
|
73
|
+
else if (character === ',' && depth === 0) {
|
|
74
|
+
found.push(text.slice(from, i));
|
|
75
|
+
from = i + 1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return found;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The parameters of a class's own constructor, read from the source text of the running code: a default value is
|
|
82
|
+
* included, types are gone, and minified code gives minified names. Empty if the class declares no constructor.
|
|
83
|
+
* Comments are removed. A regular expression literal holding quotes, backticks, brackets or `/*`, or a template
|
|
84
|
+
* literal nested in another, can still mislead it.
|
|
85
|
+
*/
|
|
86
|
+
function getConstructorParams(target) {
|
|
87
|
+
const { text, code } = blank(target.toString());
|
|
88
|
+
const opening = code.indexOf('(') + 1;
|
|
89
|
+
const start = code.startsWith('class') ? constructorStart(code) : opening || undefined;
|
|
90
|
+
if (start === undefined)
|
|
91
|
+
return [];
|
|
92
|
+
return parameters(text, code, start)
|
|
93
|
+
.map((parameter) => parameter.trim())
|
|
94
|
+
.filter(Boolean);
|
|
95
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get an array of enum values
|
|
3
|
+
*
|
|
4
|
+
* A numeric member of a TypeScript enum is also stored the other way round, from its value to its name. Those
|
|
5
|
+
* entries are left out.
|
|
6
|
+
*
|
|
7
|
+
* @param enumObject An enum object
|
|
8
|
+
* @example
|
|
9
|
+
* enum MyEnum {
|
|
10
|
+
* First = 'First',
|
|
11
|
+
* Second = 2,
|
|
12
|
+
* }
|
|
13
|
+
*
|
|
14
|
+
* const values = getEnumValues(MyEnum)
|
|
15
|
+
* // values = ['First', 2]
|
|
16
|
+
* @returns An array of enum values
|
|
17
|
+
*/
|
|
18
|
+
export declare const getEnumValues: <E extends Record<string, any>>(enumObject: E) => E[keyof E][];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getEnumValues = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Get an array of enum values
|
|
6
|
+
*
|
|
7
|
+
* A numeric member of a TypeScript enum is also stored the other way round, from its value to its name. Those
|
|
8
|
+
* entries are left out.
|
|
9
|
+
*
|
|
10
|
+
* @param enumObject An enum object
|
|
11
|
+
* @example
|
|
12
|
+
* enum MyEnum {
|
|
13
|
+
* First = 'First',
|
|
14
|
+
* Second = 2,
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* const values = getEnumValues(MyEnum)
|
|
18
|
+
* // values = ['First', 2]
|
|
19
|
+
* @returns An array of enum values
|
|
20
|
+
*/
|
|
21
|
+
const getEnumValues = (enumObject) => {
|
|
22
|
+
const record = enumObject;
|
|
23
|
+
const mapsBack = (key) => {
|
|
24
|
+
const name = record[key];
|
|
25
|
+
// Object.is, so that a member whose value is NaN is recognised too.
|
|
26
|
+
return typeof name === 'string' && Object.is(record[name], Number(key));
|
|
27
|
+
};
|
|
28
|
+
return Object.keys(record)
|
|
29
|
+
.filter((key) => !mapsBack(key))
|
|
30
|
+
.map((key) => record[key]);
|
|
31
|
+
};
|
|
32
|
+
exports.getEnumValues = getEnumValues;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./constructable"), exports);
|
|
18
|
+
__exportStar(require("./deep-partial"), exports);
|
|
19
|
+
__exportStar(require("./filtered-keys"), exports);
|
|
20
|
+
__exportStar(require("./filtered-type"), exports);
|
|
21
|
+
__exportStar(require("./get-constructor-params"), exports);
|
|
22
|
+
__exportStar(require("./get-enum-values"), exports);
|
|
23
|
+
__exportStar(require("./value-of"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ValueOf<T> = T[keyof T];
|
package/package.json
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@laudebugs/types",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Small TypeScript utility types and helpers.",
|
|
4
5
|
"type": "commonjs",
|
|
5
6
|
"main": "dist/index.js",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
7
8
|
"files": [
|
|
8
|
-
"dist"
|
|
9
|
+
"dist",
|
|
10
|
+
"CHANGELOG.md"
|
|
9
11
|
],
|
|
10
12
|
"repository": {
|
|
11
13
|
"type": "git",
|
|
12
|
-
"url": "https://github.com/laudebugs/laudebugs
|
|
14
|
+
"url": "git+https://github.com/laudebugs/laudebugs.git",
|
|
15
|
+
"directory": "packages/types"
|
|
13
16
|
},
|
|
14
17
|
"keywords": [
|
|
15
18
|
"typescript",
|
|
@@ -21,5 +24,13 @@
|
|
|
21
24
|
"bugs": {
|
|
22
25
|
"url": "https://github.com/laudebugs/laudebugs/issues"
|
|
23
26
|
},
|
|
24
|
-
"homepage": "https://github.com/laudebugs/laudebugs/tree/main/packages/types"
|
|
27
|
+
"homepage": "https://github.com/laudebugs/laudebugs/tree/main/packages/types",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",
|
|
33
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
34
|
+
"test": "vitest run"
|
|
35
|
+
}
|
|
25
36
|
}
|