@webdeveric/utils 1.6.0 → 1.7.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/dist/cjs/predicate/factory/allOf.js +1 -1
- package/dist/cjs/predicate/factory/anyOf.js +1 -1
- package/dist/cjs/predicate/factory/index.js +1 -0
- package/dist/cjs/predicate/factory/is.js +1 -1
- package/dist/cjs/predicate/factory/oneOf.js +23 -0
- package/dist/mjs/predicate/factory/allOf.js +1 -1
- package/dist/mjs/predicate/factory/anyOf.js +1 -1
- package/dist/mjs/predicate/factory/index.js +1 -0
- package/dist/mjs/predicate/factory/is.js +1 -1
- package/dist/mjs/predicate/factory/oneOf.js +19 -0
- package/dist/types/predicate/factory/index.d.ts +1 -0
- package/dist/types/predicate/factory/oneOf.d.ts +16 -0
- package/dist/types/types/branded.d.ts +3 -0
- package/dist/types/types/common.d.ts +2 -2
- package/dist/types/types/utils.d.ts +28 -2
- package/package.json +18 -17
- package/dist/mjs/package.json +0 -1
|
@@ -14,7 +14,7 @@ exports.allOf = void 0;
|
|
|
14
14
|
*/
|
|
15
15
|
const allOf = (...predicates) => {
|
|
16
16
|
if (!predicates.length) {
|
|
17
|
-
throw new
|
|
17
|
+
throw new TypeError('Predicate functions not provided');
|
|
18
18
|
}
|
|
19
19
|
return (input) => predicates.every((predicate) => predicate(input));
|
|
20
20
|
};
|
|
@@ -14,7 +14,7 @@ exports.anyOf = void 0;
|
|
|
14
14
|
*/
|
|
15
15
|
const anyOf = (...predicates) => {
|
|
16
16
|
if (!predicates.length) {
|
|
17
|
-
throw new
|
|
17
|
+
throw new TypeError('Predicate functions not provided');
|
|
18
18
|
}
|
|
19
19
|
return (input) => predicates.some((predicate) => predicate(input));
|
|
20
20
|
};
|
|
@@ -26,6 +26,7 @@ __exportStar(require("./maybeArray.js"), exports);
|
|
|
26
26
|
__exportStar(require("./nonNullable.js"), exports);
|
|
27
27
|
__exportStar(require("./nullable.js"), exports);
|
|
28
28
|
__exportStar(require("./nullish.js"), exports);
|
|
29
|
+
__exportStar(require("./oneOf.js"), exports);
|
|
29
30
|
__exportStar(require("./optional.js"), exports);
|
|
30
31
|
__exportStar(require("./range.js"), exports);
|
|
31
32
|
__exportStar(require("./record.js"), exports);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.oneOf = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Create a type predicate function that passes if `input` is one of the given `values`.
|
|
6
|
+
*
|
|
7
|
+
* Values are compared using [SameValueZero](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality)
|
|
8
|
+
* (like `Array.prototype.includes()`), so `NaN` matches `NaN` and `0` matches `-0`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const isPrimary = oneOf('red', 'green', 'blue');
|
|
13
|
+
* isPrimary('red'); // true
|
|
14
|
+
* isPrimary('yellow'); // false
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
const oneOf = (...values) => {
|
|
18
|
+
if (!values.length) {
|
|
19
|
+
throw new TypeError('Values not provided');
|
|
20
|
+
}
|
|
21
|
+
return (input) => values.includes(input);
|
|
22
|
+
};
|
|
23
|
+
exports.oneOf = oneOf;
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
export const allOf = (...predicates) => {
|
|
13
13
|
if (!predicates.length) {
|
|
14
|
-
throw new
|
|
14
|
+
throw new TypeError('Predicate functions not provided');
|
|
15
15
|
}
|
|
16
16
|
return (input) => predicates.every((predicate) => predicate(input));
|
|
17
17
|
};
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
export const anyOf = (...predicates) => {
|
|
13
13
|
if (!predicates.length) {
|
|
14
|
-
throw new
|
|
14
|
+
throw new TypeError('Predicate functions not provided');
|
|
15
15
|
}
|
|
16
16
|
return (input) => predicates.some((predicate) => predicate(input));
|
|
17
17
|
};
|
|
@@ -10,6 +10,7 @@ export * from './maybeArray.js';
|
|
|
10
10
|
export * from './nonNullable.js';
|
|
11
11
|
export * from './nullable.js';
|
|
12
12
|
export * from './nullish.js';
|
|
13
|
+
export * from './oneOf.js';
|
|
13
14
|
export * from './optional.js';
|
|
14
15
|
export * from './range.js';
|
|
15
16
|
export * from './record.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a type predicate function that passes if `input` is one of the given `values`.
|
|
3
|
+
*
|
|
4
|
+
* Values are compared using [SameValueZero](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality)
|
|
5
|
+
* (like `Array.prototype.includes()`), so `NaN` matches `NaN` and `0` matches `-0`.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const isPrimary = oneOf('red', 'green', 'blue');
|
|
10
|
+
* isPrimary('red'); // true
|
|
11
|
+
* isPrimary('yellow'); // false
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export const oneOf = (...values) => {
|
|
15
|
+
if (!values.length) {
|
|
16
|
+
throw new TypeError('Values not provided');
|
|
17
|
+
}
|
|
18
|
+
return (input) => values.includes(input);
|
|
19
|
+
};
|
|
@@ -10,6 +10,7 @@ export * from './maybeArray.js';
|
|
|
10
10
|
export * from './nonNullable.js';
|
|
11
11
|
export * from './nullable.js';
|
|
12
12
|
export * from './nullish.js';
|
|
13
|
+
export * from './oneOf.js';
|
|
13
14
|
export * from './optional.js';
|
|
14
15
|
export * from './range.js';
|
|
15
16
|
export * from './record.js';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { NonEmptyArray } from '../../types/arrays.js';
|
|
2
|
+
import type { TypePredicateFn } from '../../types/functions.js';
|
|
3
|
+
/**
|
|
4
|
+
* Create a type predicate function that passes if `input` is one of the given `values`.
|
|
5
|
+
*
|
|
6
|
+
* Values are compared using [SameValueZero](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality)
|
|
7
|
+
* (like `Array.prototype.includes()`), so `NaN` matches `NaN` and `0` matches `-0`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const isPrimary = oneOf('red', 'green', 'blue');
|
|
12
|
+
* isPrimary('red'); // true
|
|
13
|
+
* isPrimary('yellow'); // false
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare const oneOf: <const Values extends NonEmptyArray<unknown>>(...values: Values) => TypePredicateFn<Values[number]>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { NotPromise } from './utils.js';
|
|
1
|
+
import type { NotPromise, Pretty } from './utils.js';
|
|
2
2
|
export type Primitive = string | number | boolean | bigint | symbol | undefined | null;
|
|
3
3
|
/**
|
|
4
4
|
* This is any number or bigint that has been stringified.
|
|
@@ -10,7 +10,6 @@ export type JsonObject = {
|
|
|
10
10
|
};
|
|
11
11
|
export type JsonArray = JsonValue[];
|
|
12
12
|
export type JsonValue = string | number | boolean | null | object | JsonObject | JsonArray;
|
|
13
|
-
export type TypeOf = 'undefined' | 'object' | 'boolean' | 'number' | 'bigint' | 'string' | 'symbol' | 'function';
|
|
14
13
|
export type TypeOfMapping = {
|
|
15
14
|
undefined: undefined;
|
|
16
15
|
object: object;
|
|
@@ -21,6 +20,7 @@ export type TypeOfMapping = {
|
|
|
21
20
|
symbol: symbol;
|
|
22
21
|
function: Function;
|
|
23
22
|
};
|
|
23
|
+
export type TypeOf = Pretty<keyof TypeOfMapping>;
|
|
24
24
|
export type Builtin = Date | Error | Function | Primitive | RegExp;
|
|
25
25
|
export type AnyFunction = (...args: any[]) => any;
|
|
26
26
|
export type AnySyncFunction = (...args: any[]) => NotPromise<any>;
|
|
@@ -70,8 +70,34 @@ export type DeepPartial<Type> = Type extends unknown[] ? DeepPartial<Type[number
|
|
|
70
70
|
[Property in keyof Type]?: DeepPartial<Type[Property]>;
|
|
71
71
|
} : Type;
|
|
72
72
|
export type UnionToIntersection<Union> = (Union extends any ? (union: Union) => void : never) extends (union: infer Type extends Union) => void ? Type : never;
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Tail-recursive so TypeScript can evaluate long tuples (up to ~1000 elements)
|
|
75
|
+
* without hitting the instantiation depth limit.
|
|
76
|
+
*
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
export type IntersectionOfAccumulator<T extends readonly unknown[], Result> = T extends readonly [
|
|
80
|
+
infer First,
|
|
81
|
+
...infer Rest
|
|
82
|
+
] ? IntersectionOfAccumulator<Rest, Result & First> : Result;
|
|
83
|
+
/**
|
|
84
|
+
* Intersection of the element types of `T`.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* type Example = IntersectionOf<[{ a: 1 }, { b: 2 }]>; // { a: 1 } & { b: 2 }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export type IntersectionOf<T extends readonly unknown[]> = IntersectionOfAccumulator<T, unknown>;
|
|
92
|
+
/**
|
|
93
|
+
* Union of the element types of `T`.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* type Example = UnionOf<[1, 'a', true]>; // 1 | 'a' | true
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export type UnionOf<T extends readonly unknown[]> = T[number];
|
|
75
101
|
export type Pretty<Type> = {
|
|
76
102
|
[Property in keyof Type]: Type[Property];
|
|
77
103
|
} & unknown;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdeveric/utils",
|
|
3
3
|
"description": "Just another collection of utilities.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.7.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -25,13 +25,13 @@
|
|
|
25
25
|
"devEngines": {
|
|
26
26
|
"packageManager": {
|
|
27
27
|
"name": "pnpm",
|
|
28
|
-
"version": "
|
|
28
|
+
"version": "12.23.0",
|
|
29
29
|
"onFail": "warn"
|
|
30
30
|
},
|
|
31
31
|
"runtime": [
|
|
32
32
|
{
|
|
33
33
|
"name": "node",
|
|
34
|
-
"version": "^26.
|
|
34
|
+
"version": "^26.13.1",
|
|
35
35
|
"onFail": "download"
|
|
36
36
|
}
|
|
37
37
|
]
|
|
@@ -67,6 +67,7 @@
|
|
|
67
67
|
"require": "./dist/cjs/*.js",
|
|
68
68
|
"import": "./dist/mjs/*.js"
|
|
69
69
|
},
|
|
70
|
+
"./internal/*": null,
|
|
70
71
|
"./package.json": "./package.json"
|
|
71
72
|
},
|
|
72
73
|
"files": [
|
|
@@ -81,11 +82,11 @@
|
|
|
81
82
|
},
|
|
82
83
|
"homepage": "https://github.com/webdeveric/utils/#readme",
|
|
83
84
|
"scripts": {
|
|
84
|
-
"clean": "rimraf ./dist/ ./cache/ ./coverage/",
|
|
85
|
+
"clean": "rimraf ./dist/ ./docs/ ./cache/ ./coverage/",
|
|
85
86
|
"prebuild": "pnpm clean",
|
|
86
87
|
"build": "tsc --build tsconfig.cjs.json tsconfig.mjs.json --force",
|
|
87
88
|
"build:docs": "typedoc",
|
|
88
|
-
"postbuild": "echo '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json
|
|
89
|
+
"postbuild": "echo '{\"type\":\"commonjs\"}' > ./dist/cjs/package.json",
|
|
89
90
|
"typecheck": "tsc --build --verbose --noEmit",
|
|
90
91
|
"lint": "eslint ./*.{js,cjs,mjs,ts,cts,mts} ./src/ ./bench/ ./test/ --ext .ts,.mjs,.cjs",
|
|
91
92
|
"bench": "vitest bench",
|
|
@@ -99,31 +100,31 @@
|
|
|
99
100
|
},
|
|
100
101
|
"prettier": "@webdeveric/prettier-config",
|
|
101
102
|
"devDependencies": {
|
|
102
|
-
"@commitlint/config-conventional": "^21.2.
|
|
103
|
+
"@commitlint/config-conventional": "^21.2.4",
|
|
103
104
|
"@commitlint/types": "^21.2.3",
|
|
104
|
-
"@types/node": "^
|
|
105
|
+
"@types/node": "^26.6.11",
|
|
105
106
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
106
|
-
"@vitest/coverage-v8": "^
|
|
107
|
+
"@vitest/coverage-v8": "^5.1.2",
|
|
107
108
|
"@webdeveric/eslint-config-ts": "^0.12.1",
|
|
108
109
|
"@webdeveric/prettier-config": "^0.4.0",
|
|
109
110
|
"@webdeveric/ts-data-structures": "^1.0.1",
|
|
110
|
-
"commitlint": "^21.2.
|
|
111
|
+
"commitlint": "^21.2.4",
|
|
111
112
|
"commitlint-plugin-cspell": "^0.9.5",
|
|
112
113
|
"conventional-changelog-conventionalcommits": "^9.3.1",
|
|
113
|
-
"cspell": "^10.
|
|
114
|
+
"cspell": "^10.5.2",
|
|
114
115
|
"eslint": "^8.57.1",
|
|
115
|
-
"eslint-config-prettier": "^10.1.
|
|
116
|
+
"eslint-config-prettier": "^10.1.9",
|
|
116
117
|
"eslint-import-resolver-typescript": "^4.4.5",
|
|
117
118
|
"eslint-plugin-import": "^2.32.0",
|
|
118
119
|
"husky": "^9.1.7",
|
|
119
|
-
"jsdom": "^
|
|
120
|
-
"lint-staged": "^17.6.
|
|
121
|
-
"prettier": "^3.
|
|
122
|
-
"rimraf": "^6.1.
|
|
120
|
+
"jsdom": "^31.0.1",
|
|
121
|
+
"lint-staged": "^17.6.2",
|
|
122
|
+
"prettier": "^3.10.1",
|
|
123
|
+
"rimraf": "^6.1.4",
|
|
123
124
|
"semantic-release": "^25.0.9",
|
|
124
125
|
"typedoc": "^0.28.20",
|
|
125
126
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
126
|
-
"validate-package-exports": "^1.
|
|
127
|
-
"vitest": "^
|
|
127
|
+
"validate-package-exports": "^1.7.0",
|
|
128
|
+
"vitest": "^5.1.2"
|
|
128
129
|
}
|
|
129
130
|
}
|
package/dist/mjs/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|