@ptolemy2002/ts-utils 1.1.0 → 1.3.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 +30 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +27 -0
- package/package.json +8 -2
package/README.md
CHANGED
|
@@ -30,8 +30,37 @@ This type returns an object with all the properties of `T` and any number of oth
|
|
|
30
30
|
### KeysMatching<T, V>
|
|
31
31
|
This type returns a union of all the keys in `T` that have a value assignable to `V`.
|
|
32
32
|
|
|
33
|
+
### PartialBy<T, K extends keyof T>
|
|
34
|
+
This type returns a type that is the same as `T` except that the keys in `K` are optional.
|
|
35
|
+
|
|
36
|
+
### ValueCondition<T>
|
|
37
|
+
This type allows the user to specify a condition for matching a subset of type `T`. It can be one of the following:
|
|
38
|
+
- A value of type `T`
|
|
39
|
+
- An object with keys `include` and `exclude` that are both either objects of type `T`, functions that take a value of `T` and return a boolean, or arrays of objects of any of the preceding types or `false`. In addition, there is a `match` key that takes 2 values of type `T` and returns a boolean. Each of these keys is optional.
|
|
40
|
+
- An array of objects of type `T | ValueCondition<T> | false`.
|
|
41
|
+
- A function that takes a value of type `T` and returns a boolean.
|
|
42
|
+
|
|
43
|
+
The condition can then be passed to the `valueConditionMatches` function along with a value of type `T` to determine if the condition is met for that value.
|
|
44
|
+
|
|
45
|
+
### OptionalValueCondition<T>
|
|
46
|
+
This type is the same as `ValueCondition<T>` except that the value can also be `null`, indicating any value is acceptable.
|
|
47
|
+
|
|
48
|
+
## Functions
|
|
49
|
+
The following functions are available in the library:
|
|
50
|
+
|
|
51
|
+
### valueConditionMatches<T>
|
|
52
|
+
#### Description
|
|
53
|
+
This function takes a value of type `T` and a condition of type `OptionalValueCondition<T>` and returns a boolean indicating whether the value meets the condition. If the condition is `null`, the function will always return `true`. Any instance of the value `false` will be filtered out of lists. If `match` is not specified, it will default to `Object.is`. If `include` is not specified or empty, it will be ignored and `exclude` will act as the only constraint. If `exclude` is not specified, it will be assumed to be empty.
|
|
54
|
+
|
|
55
|
+
#### Parameters
|
|
56
|
+
- `value` (`T`) - The value to check against the condition.
|
|
57
|
+
- `condition` (`OptionalValueCondition<T>`) - The condition to check against the value.
|
|
58
|
+
|
|
59
|
+
#### Returns
|
|
60
|
+
- `boolean` - Whether the value meets the condition.
|
|
61
|
+
|
|
33
62
|
## Peer Dependencies
|
|
34
|
-
|
|
63
|
+
- `is-callable^1.2.7`
|
|
35
64
|
|
|
36
65
|
## Commands
|
|
37
66
|
The following commands exist in the project:
|
package/dist/index.d.ts
CHANGED
|
@@ -8,3 +8,11 @@ export type TAndOthers<T, K extends keyof any = PropertyKey> = Record<K, any> &
|
|
|
8
8
|
export type KeysMatching<T, V> = {
|
|
9
9
|
[K in keyof T]-?: T[K] extends V ? K : never;
|
|
10
10
|
}[keyof T];
|
|
11
|
+
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
12
|
+
export type ValueCondition<T> = Partial<{
|
|
13
|
+
include: T | ((v: T) => boolean) | (T | ((v: T) => boolean) | false)[];
|
|
14
|
+
exclude: T | ((v: T) => boolean) | (T | ((v: T) => boolean) | false)[];
|
|
15
|
+
match: (a: T, b: T) => boolean;
|
|
16
|
+
}> | T | ((v: T) => boolean) | (ValueCondition<T> | false)[];
|
|
17
|
+
export type OptionalValueCondition<T> = ValueCondition<T> | null;
|
|
18
|
+
export declare function valueConditionMatches<T>(value: T, condition: OptionalValueCondition<T>): boolean;
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.valueConditionMatches = valueConditionMatches;
|
|
4
|
+
const isCallable = require("is-callable");
|
|
5
|
+
function valueConditionMatches(value, condition) {
|
|
6
|
+
if (condition === null)
|
|
7
|
+
return true;
|
|
8
|
+
if (Object.is(value, condition))
|
|
9
|
+
return true;
|
|
10
|
+
if (Array.isArray(condition))
|
|
11
|
+
return condition.some(c => c !== false && valueConditionMatches(value, c));
|
|
12
|
+
if (isCallable(condition))
|
|
13
|
+
return condition(value);
|
|
14
|
+
let { include = [], exclude = [], match = Object.is } = condition;
|
|
15
|
+
if (!Array.isArray(include))
|
|
16
|
+
include = [include];
|
|
17
|
+
if (!Array.isArray(exclude))
|
|
18
|
+
exclude = [exclude];
|
|
19
|
+
const included = (v) => {
|
|
20
|
+
return include.some(i => i !== false && (isCallable(i) ? i(v) : match(value, i)));
|
|
21
|
+
};
|
|
22
|
+
const excluded = (v) => {
|
|
23
|
+
return exclude.some(e => e !== false && (isCallable(e) ? e(v) : match(value, e)));
|
|
24
|
+
};
|
|
25
|
+
// If there are no includes, the last condition will unexpectedly fail, so we add a separate case for this
|
|
26
|
+
if (include.length === 0)
|
|
27
|
+
return !excluded(value);
|
|
28
|
+
return included(value) && !excluded(value);
|
|
29
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ptolemy2002/ts-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -26,5 +26,11 @@
|
|
|
26
26
|
"release-minor": "bash ./scripts/release.sh minor",
|
|
27
27
|
"release-major": "bash ./scripts/release.sh major"
|
|
28
28
|
},
|
|
29
|
-
"
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"is-callable": "^1.2.7"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/is-callable": "~1.1.2",
|
|
34
|
+
"is-callable": "^1.2.7"
|
|
35
|
+
}
|
|
30
36
|
}
|