@thi.ng/compare 2.1.37 → 2.2.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-08-22T14:39:27Z
3
+ - **Last updated**: 2023-09-15T12:33:37Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/compare@2.2.0) (2023-09-15)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add comparison operators/predicates ([5a627b1](https://github.com/thi-ng/umbrella/commit/5a627b1))
17
+
12
18
  ## [2.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/compare@2.1.0) (2021-11-17)
13
19
 
14
20
  #### 🚀 Features
package/README.md CHANGED
@@ -58,7 +58,7 @@ For Node.js REPL:
58
58
  const compare = await import("@thi.ng/compare");
59
59
  ```
60
60
 
61
- Package sizes (brotli'd, pre-treeshake): ESM: 393 bytes
61
+ Package sizes (brotli'd, pre-treeshake): ESM: 598 bytes
62
62
 
63
63
  ## Dependencies
64
64
 
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./compare.js";
2
2
  export * from "./keys.js";
3
3
  export * from "./numeric.js";
4
+ export * from "./ops.js";
4
5
  export * from "./reverse.js";
5
6
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./compare.js";
2
2
  export * from "./keys.js";
3
3
  export * from "./numeric.js";
4
+ export * from "./ops.js";
4
5
  export * from "./reverse.js";
package/ops.d.ts ADDED
@@ -0,0 +1,74 @@
1
+ import type { Predicate2 } from "@thi.ng/api";
2
+ export type Operator = "=" | "!=" | "<" | "<=" | ">=" | ">";
3
+ export declare const OPERATORS: Record<Operator, Predicate2<string> | Predicate2<number>>;
4
+ /**
5
+ * Takes a comparison operator (either as string alias or function) and the RHS arg
6
+ * for it, returns a new function for given operator which only takes a single
7
+ * arg (i.e. the LHS of the operator) and always coerces it to a string before
8
+ * applying the operator.
9
+ *
10
+ * @remarks
11
+ * See {@link numericOp} for related functionality.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * const equalsABC = stringOp("=", "abc");
16
+ *
17
+ * ["xyz", "abc", "def"].map(equalsABC)
18
+ * // [ false, true, false ]
19
+ *
20
+ * class X {
21
+ * constructor(public body: string) {}
22
+ *
23
+ * toString() {
24
+ * return this.body;
25
+ * }
26
+ * }
27
+ *
28
+ * equalsABC(new X("abc"))
29
+ * // true
30
+ * ```
31
+ *
32
+ * @param op
33
+ * @param x
34
+ */
35
+ export declare const stringOp: (op: Operator | Predicate2<string>, x: string) => (y: any) => boolean;
36
+ /**
37
+ * Similar to {@link stringOp}, but for numeric args. Takes a comparison
38
+ * operator (either as string alias or function) and the RHS arg for it, returns
39
+ * a new function for given operator which only takes a single arg (i.e. the LHS
40
+ * of the operator) and then checks if that arg is a number before applying the
41
+ * operator. For non-numeric args, the returned function will always return
42
+ * false.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * const lessThan42 = numericOp("<", 42);
47
+ *
48
+ * lessThan42(41)
49
+ * // true
50
+ *
51
+ * lessThan42("41")
52
+ * // false
53
+ *
54
+ * lessThan42([41])
55
+ * // false
56
+ * ```
57
+ *
58
+ * @param op
59
+ * @param x
60
+ */
61
+ export declare const numericOp: (op: Operator | Predicate2<number>, x: number) => (y: any) => boolean;
62
+ export declare function eq(a: string, b: string): boolean;
63
+ export declare function eq(a: number, b: number): boolean;
64
+ export declare function neq(a: string, b: string): boolean;
65
+ export declare function neq(a: number, b: number): boolean;
66
+ export declare function lt(a: string, b: string): boolean;
67
+ export declare function lt(a: number, b: number): boolean;
68
+ export declare function lte(a: string, b: string): boolean;
69
+ export declare function lte(a: number, b: number): boolean;
70
+ export declare function gte(a: string, b: string): boolean;
71
+ export declare function gte(a: number, b: number): boolean;
72
+ export declare function gt(a: string, b: string): boolean;
73
+ export declare function gt(a: number, b: number): boolean;
74
+ //# sourceMappingURL=ops.d.ts.map
package/ops.js ADDED
@@ -0,0 +1,99 @@
1
+ export const OPERATORS = {
2
+ "=": eq,
3
+ "!=": neq,
4
+ "<": lt,
5
+ "<=": lte,
6
+ ">=": gte,
7
+ ">": gt,
8
+ };
9
+ const __ensure = (op) => {
10
+ if (typeof op === "string") {
11
+ if (op in OPERATORS)
12
+ return OPERATORS[op];
13
+ else
14
+ throw new Error(`invalid operator: ${op}`);
15
+ }
16
+ return op;
17
+ };
18
+ /**
19
+ * Takes a comparison operator (either as string alias or function) and the RHS arg
20
+ * for it, returns a new function for given operator which only takes a single
21
+ * arg (i.e. the LHS of the operator) and always coerces it to a string before
22
+ * applying the operator.
23
+ *
24
+ * @remarks
25
+ * See {@link numericOp} for related functionality.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const equalsABC = stringOp("=", "abc");
30
+ *
31
+ * ["xyz", "abc", "def"].map(equalsABC)
32
+ * // [ false, true, false ]
33
+ *
34
+ * class X {
35
+ * constructor(public body: string) {}
36
+ *
37
+ * toString() {
38
+ * return this.body;
39
+ * }
40
+ * }
41
+ *
42
+ * equalsABC(new X("abc"))
43
+ * // true
44
+ * ```
45
+ *
46
+ * @param op
47
+ * @param x
48
+ */
49
+ export const stringOp = (op, x) => {
50
+ const impl = __ensure(op);
51
+ return (y) => impl(String(y), x);
52
+ };
53
+ /**
54
+ * Similar to {@link stringOp}, but for numeric args. Takes a comparison
55
+ * operator (either as string alias or function) and the RHS arg for it, returns
56
+ * a new function for given operator which only takes a single arg (i.e. the LHS
57
+ * of the operator) and then checks if that arg is a number before applying the
58
+ * operator. For non-numeric args, the returned function will always return
59
+ * false.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * const lessThan42 = numericOp("<", 42);
64
+ *
65
+ * lessThan42(41)
66
+ * // true
67
+ *
68
+ * lessThan42("41")
69
+ * // false
70
+ *
71
+ * lessThan42([41])
72
+ * // false
73
+ * ```
74
+ *
75
+ * @param op
76
+ * @param x
77
+ */
78
+ export const numericOp = (op, x) => {
79
+ const impl = __ensure(op);
80
+ return (y) => typeof y === "number" && impl(y, x);
81
+ };
82
+ export function eq(a, b) {
83
+ return a === b;
84
+ }
85
+ export function neq(a, b) {
86
+ return a !== b;
87
+ }
88
+ export function lt(a, b) {
89
+ return a < b;
90
+ }
91
+ export function lte(a, b) {
92
+ return a <= b;
93
+ }
94
+ export function gte(a, b) {
95
+ return a >= b;
96
+ }
97
+ export function gt(a, b) {
98
+ return a > b;
99
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/compare",
3
- "version": "2.1.37",
3
+ "version": "2.2.0",
4
4
  "description": "Comparators with support for types implementing the @thi.ng/api/ICompare interface",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,15 +34,15 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.9.4"
37
+ "@thi.ng/api": "^8.9.5"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@microsoft/api-extractor": "^7.36.4",
41
- "@thi.ng/testament": "^0.3.22",
41
+ "@thi.ng/testament": "^0.3.23",
42
42
  "rimraf": "^5.0.1",
43
43
  "tools": "^0.0.1",
44
- "typedoc": "^0.24.8",
45
- "typescript": "^5.1.6"
44
+ "typedoc": "^0.25.0",
45
+ "typescript": "^5.2.2"
46
46
  },
47
47
  "keywords": [
48
48
  "comparator",
@@ -72,9 +72,12 @@
72
72
  "./numeric": {
73
73
  "default": "./numeric.js"
74
74
  },
75
+ "./ops": {
76
+ "default": "./ops.js"
77
+ },
75
78
  "./reverse": {
76
79
  "default": "./reverse.js"
77
80
  }
78
81
  },
79
- "gitHead": "74cfe3fb8de5bfcbfc1109e67604541cbd7b77aa\n"
82
+ "gitHead": "b2ef5a1b8932d067af4ec2fc7da03d59d6868dc7\n"
80
83
  }