@laudebugs/types 0.3.0 → 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 CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  Entries up to 0.2.5 were generated by [@jscutlery/semver](https://github.com/jscutlery/semver); later ones are written by hand.
4
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
+
5
16
  ## 0.3.0 (2026-12-02)
6
17
 
7
18
  - The package has its code again: 0.2.1 on npm held only `package.json`.
package/README.md CHANGED
@@ -10,17 +10,17 @@ Version 0.2.1 on npm was published without its code; use 0.3.0 or later.
10
10
 
11
11
  ## Utility types
12
12
 
13
- | Type | What it is |
14
- | -------------------- | ------------------------------------------------------------------------------------ |
15
- | `Constructable<T>` | A class or constructor that builds a `T` |
16
- | `DeepPartial<T>` | Meant to make every nested key optional. At present it returns the same shape as `T` |
17
- | `FilteredType<T, C>` | The keys of `T` whose values extend `C` |
18
- | `ValueOf<T>` | The union of the values of `T` |
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
19
 
20
20
  ## Helpers
21
21
 
22
- | Function | What it returns |
23
- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
24
- | `getEnumValues(enum)` | The values of a string enum. For a numeric enum it also returns the names |
25
- | `filterKeys(object, pattern)` | The keys of the object that match the regular expression. Its declared return type is wrong for most objects |
26
- | `getConstructorParams(class)` | The names inside the first parentheses of the class's source text. That is the constructor's parameters only when nothing with parentheses comes before it |
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 |
@@ -1,3 +1,7 @@
1
- export type DeepPartial<T> = T extends Record<string, any> ? {
2
- [K in keyof T]: DeepPartial<T[K]>;
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]>;
3
6
  } : T;
7
+ export {};
@@ -1,2 +1,4 @@
1
- import type { FilteredType } from './filtered-type';
2
- export declare function filterKeys<T extends Record<string, any>, K extends RegExp>(source: T, regExp: K): FilteredType<T, K>[];
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 {};
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.filterKeys = filterKeys;
4
+ /** The names of the object's own keys that match the pattern. */
4
5
  function filterKeys(source, regExp) {
5
- return Object.keys(source).filter((key) => regExp.test(key));
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);
6
8
  }
@@ -1,2 +1,8 @@
1
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
+ */
2
8
  export declare function getConstructorParams<T>(target: Constructable<T>): string[];
@@ -1,7 +1,95 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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
+ */
4
86
  function getConstructorParams(target) {
5
- const params = target.toString().match(/\(([^)]*)\)/)?.[1];
6
- return params?.split(',').map((param) => param.trim()) ?? [];
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);
7
95
  }
@@ -1,16 +1,18 @@
1
1
  /**
2
2
  * Get an array of enum values
3
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
+ *
4
7
  * @param enumObject An enum object
5
8
  * @example
6
9
  * enum MyEnum {
7
10
  * First = 'First',
8
- * Second = 'Second',
9
- * Third = 'Third',
11
+ * Second = 2,
10
12
  * }
11
13
  *
12
14
  * const values = getEnumValues(MyEnum)
13
- * // values = ['First', 'Second', 'Third']
15
+ * // values = ['First', 2]
14
16
  * @returns An array of enum values
15
17
  */
16
18
  export declare const getEnumValues: <E extends Record<string, any>>(enumObject: E) => E[keyof E][];
@@ -1,20 +1,32 @@
1
1
  "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEnumValues = void 0;
2
4
  /**
3
5
  * Get an array of enum values
4
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
+ *
5
10
  * @param enumObject An enum object
6
11
  * @example
7
12
  * enum MyEnum {
8
13
  * First = 'First',
9
- * Second = 'Second',
10
- * Third = 'Third',
14
+ * Second = 2,
11
15
  * }
12
16
  *
13
17
  * const values = getEnumValues(MyEnum)
14
- * // values = ['First', 'Second', 'Third']
18
+ * // values = ['First', 2]
15
19
  * @returns An array of enum values
16
20
  */
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.getEnumValues = void 0;
19
- const getEnumValues = (enumObject) => Object.keys(enumObject).map((key) => enumObject[key]);
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
+ };
20
32
  exports.getEnumValues = getEnumValues;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@laudebugs/types",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Small TypeScript utility types and helpers.",
5
5
  "type": "commonjs",
6
6
  "main": "dist/index.js",