@infra-blocks/types 0.6.0-alpha.1 → 0.6.0-alpha.2
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/README.md +4 -4
- package/lib/cjs/events.d.ts +39 -0
- package/lib/cjs/events.js +28 -0
- package/lib/cjs/events.js.map +1 -0
- package/lib/cjs/func.d.ts +21 -0
- package/lib/cjs/func.js +3 -0
- package/lib/cjs/func.js.map +1 -0
- package/lib/cjs/guard.d.ts +44 -0
- package/lib/cjs/guard.js +61 -0
- package/lib/cjs/guard.js.map +1 -0
- package/lib/cjs/index.d.ts +5 -65
- package/lib/cjs/index.js +17 -58
- package/lib/cjs/index.js.map +1 -1
- package/lib/esm/events.d.ts +39 -0
- package/lib/esm/events.js +21 -0
- package/lib/esm/events.js.map +1 -0
- package/lib/esm/func.d.ts +21 -0
- package/lib/esm/func.js +2 -0
- package/lib/esm/func.js.map +1 -0
- package/lib/esm/guard.d.ts +44 -0
- package/lib/esm/guard.js +54 -0
- package/lib/esm/guard.js.map +1 -0
- package/lib/esm/index.d.ts +5 -65
- package/lib/esm/index.js +3 -53
- package/lib/esm/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# ts-types
|
|
2
|
-
[](https://github.com/infra-blocks/ts-types/actions/workflows/build.yml)
|
|
3
|
+
[](https://github.com/infra-blocks/ts-types/actions/workflows/release.yml)
|
|
4
|
+
[](https://github.com/infra-blocks/ts-types/actions/workflows/update-from-template.yml)
|
|
5
|
+
[](https://codecov.io/gh/infra-blocks/ts-types)
|
|
6
6
|
|
|
7
7
|
Types utility library for Typescript.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import EventEmitter from "node:events";
|
|
3
|
+
/**
|
|
4
|
+
* Describes the interface that "Event" types must implement.
|
|
5
|
+
*
|
|
6
|
+
* Every key must resolve to a callable that describes the type of the
|
|
7
|
+
* handler for the event.
|
|
8
|
+
*/
|
|
9
|
+
export type Events = {
|
|
10
|
+
[key: string]: (...args: any[]) => void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* An interface for classes that behave like event emitters.
|
|
14
|
+
*
|
|
15
|
+
* Those typically allow subscription to a very specific set of events but do not export
|
|
16
|
+
* the emitting capabilities.
|
|
17
|
+
*/
|
|
18
|
+
export interface EmitterLike<E extends Events> {
|
|
19
|
+
/**
|
|
20
|
+
* Subscribes a handler to the specific event type.
|
|
21
|
+
* @param event
|
|
22
|
+
* @param handler
|
|
23
|
+
*/
|
|
24
|
+
on<Event extends keyof E>(event: Event, handler: E[Event]): this;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A generic implementation of {@link EmitterLike}.
|
|
28
|
+
*
|
|
29
|
+
* Classes that want to implement the {@link EmitterLike} interface using a NodeJs
|
|
30
|
+
* event emitter can do so by extending this class.
|
|
31
|
+
*
|
|
32
|
+
* It simply delegates `on` calls to the underlying event emitter. It also provides
|
|
33
|
+
* an homologous implementation for `emit` that subclasses can use to emit events.
|
|
34
|
+
*/
|
|
35
|
+
export declare class EmitterLikeBase<E extends Events> implements EmitterLike<E> {
|
|
36
|
+
protected readonly emitter: EventEmitter;
|
|
37
|
+
on<Event extends keyof E>(event: Event, handler: E[Event]): this;
|
|
38
|
+
protected emit<Event extends keyof E>(event: Event, ...args: Parameters<E[Event]>): boolean;
|
|
39
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.EmitterLikeBase = void 0;
|
|
7
|
+
const node_events_1 = __importDefault(require("node:events"));
|
|
8
|
+
/**
|
|
9
|
+
* A generic implementation of {@link EmitterLike}.
|
|
10
|
+
*
|
|
11
|
+
* Classes that want to implement the {@link EmitterLike} interface using a NodeJs
|
|
12
|
+
* event emitter can do so by extending this class.
|
|
13
|
+
*
|
|
14
|
+
* It simply delegates `on` calls to the underlying event emitter. It also provides
|
|
15
|
+
* an homologous implementation for `emit` that subclasses can use to emit events.
|
|
16
|
+
*/
|
|
17
|
+
class EmitterLikeBase {
|
|
18
|
+
emitter = new node_events_1.default();
|
|
19
|
+
on(event, handler) {
|
|
20
|
+
this.emitter.on(event.toString(), handler);
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
emit(event, ...args) {
|
|
24
|
+
return this.emitter.emit(event.toString(), ...args);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.EmitterLikeBase = EmitterLikeBase;
|
|
28
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/events.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAuC;AA4BvC;;;;;;;;GAQG;AACH,MAAa,eAAe;IACP,OAAO,GAAG,IAAI,qBAAY,EAAE,CAAC;IAEhD,EAAE,CAAwB,KAAY,EAAE,OAAiB;QACvD,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAC;QAC3C,OAAO,IAAI,CAAC;IACd,CAAC;IAES,IAAI,CACZ,KAAY,EACZ,GAAG,IAA0B;QAE7B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;IACtD,CAAC;CACF;AAdD,0CAcC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convenient type to easily represent "callable" types and to avoid using
|
|
3
|
+
* "any" in client code.
|
|
4
|
+
*/
|
|
5
|
+
export type Callable = (...args: never[]) => unknown;
|
|
6
|
+
/**
|
|
7
|
+
* Convenient type alias for constructors.
|
|
8
|
+
*/
|
|
9
|
+
export type Constructor<R = object, A extends any[] = any[]> = new (...args: A) => R;
|
|
10
|
+
/**
|
|
11
|
+
* A type alias for single element predicate functions.
|
|
12
|
+
*/
|
|
13
|
+
export type Predicate<T> = (item: T) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* A type alias for functions that return a value of a given type without arguments.
|
|
16
|
+
*/
|
|
17
|
+
export type Provider<T> = () => T;
|
|
18
|
+
/**
|
|
19
|
+
* A convenient type declaration for handlers used to resolve "error" type events.
|
|
20
|
+
*/
|
|
21
|
+
export type ErrorHandler<T extends Error = Error> = (err: T) => void;
|
package/lib/cjs/func.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"func.js","sourceRoot":"","sources":["../../src/func.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A convenient Typescript type guard function to assess that a value is a string.
|
|
3
|
+
*
|
|
4
|
+
* @remark
|
|
5
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
6
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
7
|
+
*
|
|
8
|
+
* @param value - The value to test.
|
|
9
|
+
* @returns Whether or not the value is a string.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isString(value: unknown): value is string;
|
|
12
|
+
/**
|
|
13
|
+
* A convenient Typescript type guard function to assess that a value is a symbol.
|
|
14
|
+
*
|
|
15
|
+
* @remark
|
|
16
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
17
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
18
|
+
*
|
|
19
|
+
* @param value - The value to test.
|
|
20
|
+
* @returns Whether or not the value is a symbol.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isSymbol(value: unknown): value is symbol;
|
|
23
|
+
/**
|
|
24
|
+
* A convenient Typescript type guard function to assess that a value is a number.
|
|
25
|
+
*
|
|
26
|
+
* @remark
|
|
27
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
28
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
29
|
+
*
|
|
30
|
+
* @param value - The value to test.
|
|
31
|
+
* @returns Whether or not the value is a number.
|
|
32
|
+
*/
|
|
33
|
+
export declare function isNumber(value: unknown): value is number;
|
|
34
|
+
/**
|
|
35
|
+
* A convenient Typescript type guard function to assess that a value is a function.
|
|
36
|
+
*
|
|
37
|
+
* @remark
|
|
38
|
+
* * This function is mostly meant as a Typescript type guard. See:
|
|
39
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
40
|
+
*
|
|
41
|
+
* @param value - The value to test.
|
|
42
|
+
* @returns Whether or not the value is a function.
|
|
43
|
+
*/
|
|
44
|
+
export declare function isFunction(value: unknown): value is Function;
|
package/lib/cjs/guard.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isFunction = exports.isNumber = exports.isSymbol = exports.isString = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A convenient Typescript type guard function to assess that a value is a string.
|
|
6
|
+
*
|
|
7
|
+
* @remark
|
|
8
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
9
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
10
|
+
*
|
|
11
|
+
* @param value - The value to test.
|
|
12
|
+
* @returns Whether or not the value is a string.
|
|
13
|
+
*/
|
|
14
|
+
function isString(value) {
|
|
15
|
+
return typeof value === "string";
|
|
16
|
+
}
|
|
17
|
+
exports.isString = isString;
|
|
18
|
+
/**
|
|
19
|
+
* A convenient Typescript type guard function to assess that a value is a symbol.
|
|
20
|
+
*
|
|
21
|
+
* @remark
|
|
22
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
23
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
24
|
+
*
|
|
25
|
+
* @param value - The value to test.
|
|
26
|
+
* @returns Whether or not the value is a symbol.
|
|
27
|
+
*/
|
|
28
|
+
function isSymbol(value) {
|
|
29
|
+
return typeof value === "symbol";
|
|
30
|
+
}
|
|
31
|
+
exports.isSymbol = isSymbol;
|
|
32
|
+
/**
|
|
33
|
+
* A convenient Typescript type guard function to assess that a value is a number.
|
|
34
|
+
*
|
|
35
|
+
* @remark
|
|
36
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
37
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
38
|
+
*
|
|
39
|
+
* @param value - The value to test.
|
|
40
|
+
* @returns Whether or not the value is a number.
|
|
41
|
+
*/
|
|
42
|
+
function isNumber(value) {
|
|
43
|
+
return typeof value === "number";
|
|
44
|
+
}
|
|
45
|
+
exports.isNumber = isNumber;
|
|
46
|
+
/**
|
|
47
|
+
* A convenient Typescript type guard function to assess that a value is a function.
|
|
48
|
+
*
|
|
49
|
+
* @remark
|
|
50
|
+
* * This function is mostly meant as a Typescript type guard. See:
|
|
51
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
52
|
+
*
|
|
53
|
+
* @param value - The value to test.
|
|
54
|
+
* @returns Whether or not the value is a function.
|
|
55
|
+
*/
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
57
|
+
function isFunction(value) {
|
|
58
|
+
return typeof value === "function";
|
|
59
|
+
}
|
|
60
|
+
exports.isFunction = isFunction;
|
|
61
|
+
//# sourceMappingURL=guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.js","sourceRoot":"","sources":["../../src/guard.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;GASG;AACH,SAAgB,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACnC,CAAC;AAFD,4BAEC;AAED;;;;;;;;;GASG;AACH,SAAgB,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACnC,CAAC;AAFD,4BAEC;AAED;;;;;;;;;GASG;AACH,SAAgB,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACnC,CAAC;AAFD,4BAEC;AAED;;;;;;;;;GASG;AACH,wDAAwD;AACxD,SAAgB,UAAU,CAAC,KAAc;IACvC,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;AACrC,CAAC;AAFD,gCAEC"}
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,30 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
* "
|
|
4
|
-
*/
|
|
5
|
-
export type Callable = (...args: never[]) => unknown;
|
|
6
|
-
/**
|
|
7
|
-
* Convenient type alias for constructors.
|
|
8
|
-
*/
|
|
9
|
-
export type Constructor<R = object, A extends any[] = any[]> = new (...args: A) => R;
|
|
1
|
+
export * from "./func.js";
|
|
2
|
+
export * from "./guard.js";
|
|
3
|
+
export * from "./events.js";
|
|
10
4
|
/**
|
|
11
5
|
* Convenient type alias to regroup a type that can be T, null or undefined.
|
|
12
6
|
*
|
|
13
7
|
* Semantically the opposite of {@link NonNullable}.
|
|
14
8
|
*/
|
|
15
9
|
export type Nullable<T> = T | null | undefined;
|
|
16
|
-
/**
|
|
17
|
-
* A type alias for single element predicate functions.
|
|
18
|
-
*/
|
|
19
|
-
export type Predicate<T> = (item: T) => boolean;
|
|
20
|
-
/**
|
|
21
|
-
* A type alias for functions that return a value of a given type without arguments.
|
|
22
|
-
*/
|
|
23
|
-
export type Provider<T> = () => T;
|
|
24
|
-
/**
|
|
25
|
-
* A convenient type declaration for handlers used to resolve "error" type events.
|
|
26
|
-
*/
|
|
27
|
-
export type ErrorHandler<T extends Error = Error> = (err: T) => void;
|
|
28
10
|
/**
|
|
29
11
|
* A convenience type extractor to get the inner type of an array.
|
|
30
12
|
*
|
|
@@ -39,6 +21,8 @@ export type UnpackedArray<T> = T extends (infer U)[] ? U : never;
|
|
|
39
21
|
* It will cause compilation errors if T isn't a promise.
|
|
40
22
|
*
|
|
41
23
|
* See here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
|
|
24
|
+
*
|
|
25
|
+
* @deprecated Use built-in {@link Awaited} instead.
|
|
42
26
|
*/
|
|
43
27
|
export type UnpackedPromise<T> = T extends Promise<infer U> ? U : never;
|
|
44
28
|
/**
|
|
@@ -58,47 +42,3 @@ export type TransitivePartial<T> = {
|
|
|
58
42
|
export type KeyOfType<T, U> = {
|
|
59
43
|
[P in keyof T]: T[P] extends U ? P : never;
|
|
60
44
|
}[keyof T];
|
|
61
|
-
/**
|
|
62
|
-
* A convenient Typescript type guard function to assess that a value is a string.
|
|
63
|
-
*
|
|
64
|
-
* @remark
|
|
65
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
66
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
67
|
-
*
|
|
68
|
-
* @param value - The value to test.
|
|
69
|
-
* @returns Whether or not the value is a string.
|
|
70
|
-
*/
|
|
71
|
-
export declare function isString(value: unknown): value is string;
|
|
72
|
-
/**
|
|
73
|
-
* A convenient Typescript type guard function to assess that a value is a symbol.
|
|
74
|
-
*
|
|
75
|
-
* @remark
|
|
76
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
77
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
78
|
-
*
|
|
79
|
-
* @param value - The value to test.
|
|
80
|
-
* @returns Whether or not the value is a symbol.
|
|
81
|
-
*/
|
|
82
|
-
export declare function isSymbol(value: unknown): value is symbol;
|
|
83
|
-
/**
|
|
84
|
-
* A convenient Typescript type guard function to assess that a value is a number.
|
|
85
|
-
*
|
|
86
|
-
* @remark
|
|
87
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
88
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
89
|
-
*
|
|
90
|
-
* @param value - The value to test.
|
|
91
|
-
* @returns Whether or not the value is a number.
|
|
92
|
-
*/
|
|
93
|
-
export declare function isNumber(value: unknown): value is number;
|
|
94
|
-
/**
|
|
95
|
-
* A convenient Typescript type guard function to assess that a value is a function.
|
|
96
|
-
*
|
|
97
|
-
* @remark
|
|
98
|
-
* * This function is mostly meant as a Typescript type guard. See:
|
|
99
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
100
|
-
*
|
|
101
|
-
* @param value - The value to test.
|
|
102
|
-
* @returns Whether or not the value is a function.
|
|
103
|
-
*/
|
|
104
|
-
export declare function isFunction(value: unknown): value is Function;
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,61 +1,20 @@
|
|
|
1
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
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
* @remark
|
|
8
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
9
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
10
|
-
*
|
|
11
|
-
* @param value - The value to test.
|
|
12
|
-
* @returns Whether or not the value is a string.
|
|
13
|
-
*/
|
|
14
|
-
function isString(value) {
|
|
15
|
-
return typeof value === "string";
|
|
16
|
-
}
|
|
17
|
-
exports.isString = isString;
|
|
18
|
-
/**
|
|
19
|
-
* A convenient Typescript type guard function to assess that a value is a symbol.
|
|
20
|
-
*
|
|
21
|
-
* @remark
|
|
22
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
23
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
24
|
-
*
|
|
25
|
-
* @param value - The value to test.
|
|
26
|
-
* @returns Whether or not the value is a symbol.
|
|
27
|
-
*/
|
|
28
|
-
function isSymbol(value) {
|
|
29
|
-
return typeof value === "symbol";
|
|
30
|
-
}
|
|
31
|
-
exports.isSymbol = isSymbol;
|
|
32
|
-
/**
|
|
33
|
-
* A convenient Typescript type guard function to assess that a value is a number.
|
|
34
|
-
*
|
|
35
|
-
* @remark
|
|
36
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
37
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
38
|
-
*
|
|
39
|
-
* @param value - The value to test.
|
|
40
|
-
* @returns Whether or not the value is a number.
|
|
41
|
-
*/
|
|
42
|
-
function isNumber(value) {
|
|
43
|
-
return typeof value === "number";
|
|
44
|
-
}
|
|
45
|
-
exports.isNumber = isNumber;
|
|
46
|
-
/**
|
|
47
|
-
* A convenient Typescript type guard function to assess that a value is a function.
|
|
48
|
-
*
|
|
49
|
-
* @remark
|
|
50
|
-
* * This function is mostly meant as a Typescript type guard. See:
|
|
51
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
52
|
-
*
|
|
53
|
-
* @param value - The value to test.
|
|
54
|
-
* @returns Whether or not the value is a function.
|
|
55
|
-
*/
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
57
|
-
function isFunction(value) {
|
|
58
|
-
return typeof value === "function";
|
|
59
|
-
}
|
|
60
|
-
exports.isFunction = isFunction;
|
|
17
|
+
__exportStar(require("./func.js"), exports);
|
|
18
|
+
__exportStar(require("./guard.js"), exports);
|
|
19
|
+
__exportStar(require("./events.js"), exports);
|
|
61
20
|
//# sourceMappingURL=index.js.map
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,6CAA2B;AAC3B,8CAA4B"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import EventEmitter from "node:events";
|
|
3
|
+
/**
|
|
4
|
+
* Describes the interface that "Event" types must implement.
|
|
5
|
+
*
|
|
6
|
+
* Every key must resolve to a callable that describes the type of the
|
|
7
|
+
* handler for the event.
|
|
8
|
+
*/
|
|
9
|
+
export type Events = {
|
|
10
|
+
[key: string]: (...args: any[]) => void;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* An interface for classes that behave like event emitters.
|
|
14
|
+
*
|
|
15
|
+
* Those typically allow subscription to a very specific set of events but do not export
|
|
16
|
+
* the emitting capabilities.
|
|
17
|
+
*/
|
|
18
|
+
export interface EmitterLike<E extends Events> {
|
|
19
|
+
/**
|
|
20
|
+
* Subscribes a handler to the specific event type.
|
|
21
|
+
* @param event
|
|
22
|
+
* @param handler
|
|
23
|
+
*/
|
|
24
|
+
on<Event extends keyof E>(event: Event, handler: E[Event]): this;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A generic implementation of {@link EmitterLike}.
|
|
28
|
+
*
|
|
29
|
+
* Classes that want to implement the {@link EmitterLike} interface using a NodeJs
|
|
30
|
+
* event emitter can do so by extending this class.
|
|
31
|
+
*
|
|
32
|
+
* It simply delegates `on` calls to the underlying event emitter. It also provides
|
|
33
|
+
* an homologous implementation for `emit` that subclasses can use to emit events.
|
|
34
|
+
*/
|
|
35
|
+
export declare class EmitterLikeBase<E extends Events> implements EmitterLike<E> {
|
|
36
|
+
protected readonly emitter: EventEmitter;
|
|
37
|
+
on<Event extends keyof E>(event: Event, handler: E[Event]): this;
|
|
38
|
+
protected emit<Event extends keyof E>(event: Event, ...args: Parameters<E[Event]>): boolean;
|
|
39
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import EventEmitter from "node:events";
|
|
2
|
+
/**
|
|
3
|
+
* A generic implementation of {@link EmitterLike}.
|
|
4
|
+
*
|
|
5
|
+
* Classes that want to implement the {@link EmitterLike} interface using a NodeJs
|
|
6
|
+
* event emitter can do so by extending this class.
|
|
7
|
+
*
|
|
8
|
+
* It simply delegates `on` calls to the underlying event emitter. It also provides
|
|
9
|
+
* an homologous implementation for `emit` that subclasses can use to emit events.
|
|
10
|
+
*/
|
|
11
|
+
export class EmitterLikeBase {
|
|
12
|
+
emitter = new EventEmitter();
|
|
13
|
+
on(event, handler) {
|
|
14
|
+
this.emitter.on(event.toString(), handler);
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
emit(event, ...args) {
|
|
18
|
+
return this.emitter.emit(event.toString(), ...args);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/events.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,aAAa,CAAC;AA4BvC;;;;;;;;GAQG;AACH,MAAM,OAAO,eAAe;IACP,OAAO,GAAG,IAAI,YAAY,EAAE,CAAC;IAEhD,EAAE,CAAwB,KAAY,EAAE,OAAiB;QACvD,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAC;QAC3C,OAAO,IAAI,CAAC;IACd,CAAC;IAES,IAAI,CACZ,KAAY,EACZ,GAAG,IAA0B;QAE7B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;IACtD,CAAC;CACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convenient type to easily represent "callable" types and to avoid using
|
|
3
|
+
* "any" in client code.
|
|
4
|
+
*/
|
|
5
|
+
export type Callable = (...args: never[]) => unknown;
|
|
6
|
+
/**
|
|
7
|
+
* Convenient type alias for constructors.
|
|
8
|
+
*/
|
|
9
|
+
export type Constructor<R = object, A extends any[] = any[]> = new (...args: A) => R;
|
|
10
|
+
/**
|
|
11
|
+
* A type alias for single element predicate functions.
|
|
12
|
+
*/
|
|
13
|
+
export type Predicate<T> = (item: T) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* A type alias for functions that return a value of a given type without arguments.
|
|
16
|
+
*/
|
|
17
|
+
export type Provider<T> = () => T;
|
|
18
|
+
/**
|
|
19
|
+
* A convenient type declaration for handlers used to resolve "error" type events.
|
|
20
|
+
*/
|
|
21
|
+
export type ErrorHandler<T extends Error = Error> = (err: T) => void;
|
package/lib/esm/func.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"func.js","sourceRoot":"","sources":["../../src/func.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A convenient Typescript type guard function to assess that a value is a string.
|
|
3
|
+
*
|
|
4
|
+
* @remark
|
|
5
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
6
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
7
|
+
*
|
|
8
|
+
* @param value - The value to test.
|
|
9
|
+
* @returns Whether or not the value is a string.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isString(value: unknown): value is string;
|
|
12
|
+
/**
|
|
13
|
+
* A convenient Typescript type guard function to assess that a value is a symbol.
|
|
14
|
+
*
|
|
15
|
+
* @remark
|
|
16
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
17
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
18
|
+
*
|
|
19
|
+
* @param value - The value to test.
|
|
20
|
+
* @returns Whether or not the value is a symbol.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isSymbol(value: unknown): value is symbol;
|
|
23
|
+
/**
|
|
24
|
+
* A convenient Typescript type guard function to assess that a value is a number.
|
|
25
|
+
*
|
|
26
|
+
* @remark
|
|
27
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
28
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
29
|
+
*
|
|
30
|
+
* @param value - The value to test.
|
|
31
|
+
* @returns Whether or not the value is a number.
|
|
32
|
+
*/
|
|
33
|
+
export declare function isNumber(value: unknown): value is number;
|
|
34
|
+
/**
|
|
35
|
+
* A convenient Typescript type guard function to assess that a value is a function.
|
|
36
|
+
*
|
|
37
|
+
* @remark
|
|
38
|
+
* * This function is mostly meant as a Typescript type guard. See:
|
|
39
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
40
|
+
*
|
|
41
|
+
* @param value - The value to test.
|
|
42
|
+
* @returns Whether or not the value is a function.
|
|
43
|
+
*/
|
|
44
|
+
export declare function isFunction(value: unknown): value is Function;
|
package/lib/esm/guard.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A convenient Typescript type guard function to assess that a value is a string.
|
|
3
|
+
*
|
|
4
|
+
* @remark
|
|
5
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
6
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
7
|
+
*
|
|
8
|
+
* @param value - The value to test.
|
|
9
|
+
* @returns Whether or not the value is a string.
|
|
10
|
+
*/
|
|
11
|
+
export function isString(value) {
|
|
12
|
+
return typeof value === "string";
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A convenient Typescript type guard function to assess that a value is a symbol.
|
|
16
|
+
*
|
|
17
|
+
* @remark
|
|
18
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
19
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
20
|
+
*
|
|
21
|
+
* @param value - The value to test.
|
|
22
|
+
* @returns Whether or not the value is a symbol.
|
|
23
|
+
*/
|
|
24
|
+
export function isSymbol(value) {
|
|
25
|
+
return typeof value === "symbol";
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A convenient Typescript type guard function to assess that a value is a number.
|
|
29
|
+
*
|
|
30
|
+
* @remark
|
|
31
|
+
* This function is mostly meant as a Typescript type guard. See:
|
|
32
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
33
|
+
*
|
|
34
|
+
* @param value - The value to test.
|
|
35
|
+
* @returns Whether or not the value is a number.
|
|
36
|
+
*/
|
|
37
|
+
export function isNumber(value) {
|
|
38
|
+
return typeof value === "number";
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A convenient Typescript type guard function to assess that a value is a function.
|
|
42
|
+
*
|
|
43
|
+
* @remark
|
|
44
|
+
* * This function is mostly meant as a Typescript type guard. See:
|
|
45
|
+
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
46
|
+
*
|
|
47
|
+
* @param value - The value to test.
|
|
48
|
+
* @returns Whether or not the value is a function.
|
|
49
|
+
*/
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
51
|
+
export function isFunction(value) {
|
|
52
|
+
return typeof value === "function";
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.js","sourceRoot":"","sources":["../../src/guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACnC,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACnC,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACnC,CAAC;AAED;;;;;;;;;GASG;AACH,wDAAwD;AACxD,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC;AACrC,CAAC"}
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,30 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
* "
|
|
4
|
-
*/
|
|
5
|
-
export type Callable = (...args: never[]) => unknown;
|
|
6
|
-
/**
|
|
7
|
-
* Convenient type alias for constructors.
|
|
8
|
-
*/
|
|
9
|
-
export type Constructor<R = object, A extends any[] = any[]> = new (...args: A) => R;
|
|
1
|
+
export * from "./func.js";
|
|
2
|
+
export * from "./guard.js";
|
|
3
|
+
export * from "./events.js";
|
|
10
4
|
/**
|
|
11
5
|
* Convenient type alias to regroup a type that can be T, null or undefined.
|
|
12
6
|
*
|
|
13
7
|
* Semantically the opposite of {@link NonNullable}.
|
|
14
8
|
*/
|
|
15
9
|
export type Nullable<T> = T | null | undefined;
|
|
16
|
-
/**
|
|
17
|
-
* A type alias for single element predicate functions.
|
|
18
|
-
*/
|
|
19
|
-
export type Predicate<T> = (item: T) => boolean;
|
|
20
|
-
/**
|
|
21
|
-
* A type alias for functions that return a value of a given type without arguments.
|
|
22
|
-
*/
|
|
23
|
-
export type Provider<T> = () => T;
|
|
24
|
-
/**
|
|
25
|
-
* A convenient type declaration for handlers used to resolve "error" type events.
|
|
26
|
-
*/
|
|
27
|
-
export type ErrorHandler<T extends Error = Error> = (err: T) => void;
|
|
28
10
|
/**
|
|
29
11
|
* A convenience type extractor to get the inner type of an array.
|
|
30
12
|
*
|
|
@@ -39,6 +21,8 @@ export type UnpackedArray<T> = T extends (infer U)[] ? U : never;
|
|
|
39
21
|
* It will cause compilation errors if T isn't a promise.
|
|
40
22
|
*
|
|
41
23
|
* See here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
|
|
24
|
+
*
|
|
25
|
+
* @deprecated Use built-in {@link Awaited} instead.
|
|
42
26
|
*/
|
|
43
27
|
export type UnpackedPromise<T> = T extends Promise<infer U> ? U : never;
|
|
44
28
|
/**
|
|
@@ -58,47 +42,3 @@ export type TransitivePartial<T> = {
|
|
|
58
42
|
export type KeyOfType<T, U> = {
|
|
59
43
|
[P in keyof T]: T[P] extends U ? P : never;
|
|
60
44
|
}[keyof T];
|
|
61
|
-
/**
|
|
62
|
-
* A convenient Typescript type guard function to assess that a value is a string.
|
|
63
|
-
*
|
|
64
|
-
* @remark
|
|
65
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
66
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
67
|
-
*
|
|
68
|
-
* @param value - The value to test.
|
|
69
|
-
* @returns Whether or not the value is a string.
|
|
70
|
-
*/
|
|
71
|
-
export declare function isString(value: unknown): value is string;
|
|
72
|
-
/**
|
|
73
|
-
* A convenient Typescript type guard function to assess that a value is a symbol.
|
|
74
|
-
*
|
|
75
|
-
* @remark
|
|
76
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
77
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
78
|
-
*
|
|
79
|
-
* @param value - The value to test.
|
|
80
|
-
* @returns Whether or not the value is a symbol.
|
|
81
|
-
*/
|
|
82
|
-
export declare function isSymbol(value: unknown): value is symbol;
|
|
83
|
-
/**
|
|
84
|
-
* A convenient Typescript type guard function to assess that a value is a number.
|
|
85
|
-
*
|
|
86
|
-
* @remark
|
|
87
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
88
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
89
|
-
*
|
|
90
|
-
* @param value - The value to test.
|
|
91
|
-
* @returns Whether or not the value is a number.
|
|
92
|
-
*/
|
|
93
|
-
export declare function isNumber(value: unknown): value is number;
|
|
94
|
-
/**
|
|
95
|
-
* A convenient Typescript type guard function to assess that a value is a function.
|
|
96
|
-
*
|
|
97
|
-
* @remark
|
|
98
|
-
* * This function is mostly meant as a Typescript type guard. See:
|
|
99
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
100
|
-
*
|
|
101
|
-
* @param value - The value to test.
|
|
102
|
-
* @returns Whether or not the value is a function.
|
|
103
|
-
*/
|
|
104
|
-
export declare function isFunction(value: unknown): value is Function;
|
package/lib/esm/index.js
CHANGED
|
@@ -1,54 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @remark
|
|
5
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
6
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
7
|
-
*
|
|
8
|
-
* @param value - The value to test.
|
|
9
|
-
* @returns Whether or not the value is a string.
|
|
10
|
-
*/
|
|
11
|
-
export function isString(value) {
|
|
12
|
-
return typeof value === "string";
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* A convenient Typescript type guard function to assess that a value is a symbol.
|
|
16
|
-
*
|
|
17
|
-
* @remark
|
|
18
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
19
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
20
|
-
*
|
|
21
|
-
* @param value - The value to test.
|
|
22
|
-
* @returns Whether or not the value is a symbol.
|
|
23
|
-
*/
|
|
24
|
-
export function isSymbol(value) {
|
|
25
|
-
return typeof value === "symbol";
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* A convenient Typescript type guard function to assess that a value is a number.
|
|
29
|
-
*
|
|
30
|
-
* @remark
|
|
31
|
-
* This function is mostly meant as a Typescript type guard. See:
|
|
32
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
33
|
-
*
|
|
34
|
-
* @param value - The value to test.
|
|
35
|
-
* @returns Whether or not the value is a number.
|
|
36
|
-
*/
|
|
37
|
-
export function isNumber(value) {
|
|
38
|
-
return typeof value === "number";
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* A convenient Typescript type guard function to assess that a value is a function.
|
|
42
|
-
*
|
|
43
|
-
* @remark
|
|
44
|
-
* * This function is mostly meant as a Typescript type guard. See:
|
|
45
|
-
* https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
|
|
46
|
-
*
|
|
47
|
-
* @param value - The value to test.
|
|
48
|
-
* @returns Whether or not the value is a function.
|
|
49
|
-
*/
|
|
50
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
51
|
-
export function isFunction(value) {
|
|
52
|
-
return typeof value === "function";
|
|
53
|
-
}
|
|
1
|
+
export * from "./func.js";
|
|
2
|
+
export * from "./guard.js";
|
|
3
|
+
export * from "./events.js";
|
|
54
4
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infra-blocks/types",
|
|
3
|
-
"version": "0.6.0-alpha.
|
|
3
|
+
"version": "0.6.0-alpha.2",
|
|
4
4
|
"description": "Typescript types utility package.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"type",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
],
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/
|
|
11
|
+
"url": "https://github.com/infra-blocks/ts-types.git"
|
|
12
12
|
},
|
|
13
13
|
"license": "ISC",
|
|
14
14
|
"author": "",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"test:unit": "mocha --config test/unit/.mocharc.cjs 'test/unit/**/*.spec.ts'"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@infra-blocks/test": "^0.
|
|
39
|
+
"@infra-blocks/test": "^0.4.0",
|
|
40
40
|
"@types/mocha": "^10.0.1",
|
|
41
41
|
"@types/node": "^20.10.3",
|
|
42
42
|
"@typescript-eslint/eslint-plugin": "^5.59.8",
|