@infra-blocks/types 0.1.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/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # ts-types
2
+
3
+ Small types utility library for Typescript.
4
+
5
+ ## Development
6
+
7
+ ### Repo init
8
+
9
+ This repository leverages [nvm](https://github.com/nvm-sh/nvm) and users should have it installed in their local environment.
10
+ In addition, it is recommended that users install a [shell hook](https://github.com/nvm-sh/nvm#deeper-shell-integration)
11
+ so that `nvm use` is run upon changing into a project that utilises `nvm`.
12
+
13
+ Upon checking out the repository, run the following commands:
14
+ ```shell
15
+ nvm install
16
+ npm install
17
+ npm run compile
18
+ npm run lint
19
+ npm run test
20
+ ```
21
+
22
+ ### Package publication
23
+
24
+ Package publication is done manually at the moment.
package/lib/index.d.ts ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Convenient type alias to regroup a type that can be T, null or undefined.
3
+ *
4
+ * Semantically the opposite of {@link NonNullable}.
5
+ */
6
+ export type Nullable<T> = T | null | undefined;
7
+ /**
8
+ * A type alias for single element predicate functions.
9
+ */
10
+ export type Predicate<T> = (item: T) => boolean;
11
+ /**
12
+ * A convenient type declaration for handlers used to resolve "error" type events.
13
+ */
14
+ export type ErrorHandler<T extends Error = Error> = (err: T) => void;
15
+ /**
16
+ * A convenience type extractor to get the inner type of an array.
17
+ *
18
+ * It will cause compilation errors if T isn't an array.
19
+ *
20
+ * See here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
21
+ */
22
+ export type UnpackedArray<T> = T extends (infer U)[] ? U : never;
23
+ /**
24
+ * A convenience type extractor to get the inner type of a promise.
25
+ *
26
+ * It will cause compilation errors if T isn't a promise.
27
+ *
28
+ * See here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
29
+ */
30
+ export type UnpackedPromise<T> = T extends Promise<infer U> ? U : never;
31
+ /**
32
+ * A convenience type mapping that transitively make partial fields optional.
33
+ *
34
+ * The built-in Partial type doesn't cover nested objects, this one does.
35
+ */
36
+ export type TransitivePartial<T> = {
37
+ [K in keyof T]?: T[K] extends object ? TransitivePartial<T[K]> : T[K];
38
+ };
39
+ /**
40
+ * A convenience type mapping to extract keys of a type that are of a given type.
41
+ *
42
+ * For example, if you would like all the types of an object that are numbers, you could do
43
+ * KeyOfType<TheType, number>
44
+ */
45
+ export type KeyOfType<T, U> = {
46
+ [P in keyof T]: T[P] extends U ? P : never;
47
+ }[keyof T];
48
+ /**
49
+ * A convenient Typescript type guard function to assess that a value is a string.
50
+ *
51
+ * @remark
52
+ * This function is mostly meant as a Typescript type guard. See:
53
+ * https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
54
+ *
55
+ * @param value - The value to test.
56
+ * @returns Whether or not the value is a string.
57
+ */
58
+ export declare function isString(value: unknown): value is string;
59
+ /**
60
+ * A convenient Typescript type guard function to assess that a value is a symbol.
61
+ *
62
+ * @remark
63
+ * This function is mostly meant as a Typescript type guard. See:
64
+ * https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
65
+ *
66
+ * @param value - The value to test.
67
+ * @returns Whether or not the value is a symbol.
68
+ */
69
+ export declare function isSymbol(value: unknown): value is symbol;
70
+ /**
71
+ * A convenient Typescript type guard function to assess that a value is a number.
72
+ *
73
+ * @remark
74
+ * This function is mostly meant as a Typescript type guard. See:
75
+ * https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
76
+ *
77
+ * @param value - The value to test.
78
+ * @returns Whether or not the value is a number.
79
+ */
80
+ export declare function isNumber(value: unknown): value is number;
81
+ /**
82
+ * A convenient Typescript type guard function to assess that a value is a function.
83
+ *
84
+ * @remark
85
+ * * This function is mostly meant as a Typescript type guard. See:
86
+ * https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards
87
+ *
88
+ * @param value - The value to test.
89
+ * @returns Whether or not the value is a function.
90
+ */
91
+ export declare function isFunction(value: unknown): value is Function;
package/lib/index.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=index.js.map
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@infra-blocks/types",
3
+ "version": "0.1.0",
4
+ "description": "Typescript types utility package.",
5
+ "exports": "./lib/index.js",
6
+ "engines": {
7
+ "node": ">=18.0.0"
8
+ },
9
+ "files": [
10
+ "lib/**/*.{js,d.ts}"
11
+ ],
12
+ "type": "module",
13
+ "scripts": {
14
+ "prepack": "npm run build",
15
+ "build": "npm run clean && tsc --project tsconfig.build.json",
16
+ "compile": "tsc",
17
+ "clean": "rm -rf lib && rm -f breather-infra-*.tgz",
18
+ "lint": "eslint --ext .js,.ts --max-warnings 0 .",
19
+ "test": "npm run test:unit",
20
+ "test:coverage": "nyc npm run test",
21
+ "test:coverage:lcov": "npx nyc --reporter=lcov npm run test",
22
+ "test:unit": "mocha --config test/unit/.mocharc.cjs 'test/unit/**/*.spec.ts'",
23
+ "test:integration": "mocha --config test/integration/.mocharc.js 'test/integration/**/*.spec.ts'"
24
+ },
25
+ "devDependencies": {
26
+ "@istanbuljs/nyc-config-typescript": "^1.0.2",
27
+ "@types/chai": "^4.3.5",
28
+ "@types/chai-as-promised": "^7.1.5",
29
+ "@types/mocha": "^10.0.1",
30
+ "@types/node": "^18.16.16",
31
+ "@types/sinon": "^10.0.15",
32
+ "@types/sinon-chai": "^3.2.9",
33
+ "@typescript-eslint/eslint-plugin": "^5.59.8",
34
+ "@typescript-eslint/parser": "^5.59.8",
35
+ "chai": "^4.3.7",
36
+ "chai-as-promised": "^7.1.1",
37
+ "eslint": "^8.41.0",
38
+ "eslint-config-prettier": "^8.8.0",
39
+ "eslint-plugin-prettier": "^4.2.1",
40
+ "mocha": "^10.2.0",
41
+ "nyc": "^15.1.0",
42
+ "prettier": "^2.8.8",
43
+ "sinon": "^15.1.0",
44
+ "sinon-chai": "^3.7.0",
45
+ "ts-node": "^10.9.1",
46
+ "typescript": "^5.0.4"
47
+ },
48
+ "author": "",
49
+ "license": "ISC"
50
+ }