@ptolemy2002/ts-utils 1.3.0 → 1.3.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 CHANGED
@@ -36,7 +36,7 @@ This type returns a type that is the same as `T` except that the keys in `K` are
36
36
  ### ValueCondition<T>
37
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
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.
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. The one required key is `_isCondition` which should be set to `true`. This allows this condition object to be distinguished from a regular object of type `T`.
40
40
  - An array of objects of type `T | ValueCondition<T> | false`.
41
41
  - A function that takes a value of type `T` and returns a boolean.
42
42
 
package/dist/index.d.ts CHANGED
@@ -9,10 +9,12 @@ export type KeysMatching<T, V> = {
9
9
  [K in keyof T]-?: T[K] extends V ? K : never;
10
10
  }[keyof T];
11
11
  export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
12
- export type ValueCondition<T> = Partial<{
12
+ export type ValueCondition<T> = ({
13
+ _isCondition: true;
14
+ } & Partial<{
13
15
  include: T | ((v: T) => boolean) | (T | ((v: T) => boolean) | false)[];
14
16
  exclude: T | ((v: T) => boolean) | (T | ((v: T) => boolean) | false)[];
15
17
  match: (a: T, b: T) => boolean;
16
- }> | T | ((v: T) => boolean) | (ValueCondition<T> | false)[];
18
+ }>) | T | ((v: T) => boolean) | (ValueCondition<T> | false)[];
17
19
  export type OptionalValueCondition<T> = ValueCondition<T> | null;
18
20
  export declare function valueConditionMatches<T>(value: T, condition: OptionalValueCondition<T>): boolean;
package/dist/index.js CHANGED
@@ -5,12 +5,13 @@ const isCallable = require("is-callable");
5
5
  function valueConditionMatches(value, condition) {
6
6
  if (condition === null)
7
7
  return true;
8
- if (Object.is(value, condition))
9
- return true;
10
8
  if (Array.isArray(condition))
11
9
  return condition.some(c => c !== false && valueConditionMatches(value, c));
12
10
  if (isCallable(condition))
13
11
  return condition(value);
12
+ // If the condition value here is not a condition object, it must be of type T, so we can directly compare it
13
+ if (condition._isCondition !== true)
14
+ return Object.is(value, condition);
14
15
  let { include = [], exclude = [], match = Object.is } = condition;
15
16
  if (!Array.isArray(include))
16
17
  include = [include];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/ts-utils",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",