@clipboard-health/util-ts 2.11.3 → 2.12.1
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 +3 -0
- package/package.json +12 -3
- package/src/lib/errors/cbhError.d.ts +1 -2
- package/src/lib/errors/cbhError.js +2 -2
- package/src/lib/errors/cbhError.js.map +1 -1
- package/src/lib/errors/serviceError.d.ts +1 -1
- package/src/lib/functional/either.d.ts +32 -6
- package/src/lib/functional/either.js +3 -3
- package/src/lib/functional/either.js.map +1 -1
- package/src/lib/functional/option.d.ts +28 -2
- package/src/lib/functional/option.js.map +1 -1
- package/src/lib/functional/pipe.d.ts +0 -11
- package/src/lib/functional/pipe.js +26 -0
- package/src/lib/functional/pipe.js.map +1 -1
package/README.md
CHANGED
|
@@ -156,6 +156,7 @@ ok(E.isRight(validateUser({ email: "user@example.com", phone: "555-555-5555" }))
|
|
|
156
156
|
```ts
|
|
157
157
|
// ./examples/pipe.ts
|
|
158
158
|
|
|
159
|
+
// packages/util-ts/src/lib/functional/pipe.ts
|
|
159
160
|
import { equal } from "node:assert/strict";
|
|
160
161
|
|
|
161
162
|
import { pipe } from "@clipboard-health/util-ts";
|
|
@@ -178,6 +179,7 @@ equal(result, "Hello World");
|
|
|
178
179
|
```ts
|
|
179
180
|
// ./examples/option.ts
|
|
180
181
|
|
|
182
|
+
// packages/util-ts/src/lib/functional/option.ts
|
|
181
183
|
import { equal } from "node:assert/strict";
|
|
182
184
|
|
|
183
185
|
import { option as O, pipe } from "@clipboard-health/util-ts";
|
|
@@ -210,6 +212,7 @@ equal(result, "Result is 0.1");
|
|
|
210
212
|
```ts
|
|
211
213
|
// ./examples/either.ts
|
|
212
214
|
|
|
215
|
+
// packages/util-ts/src/lib/functional/either.ts
|
|
213
216
|
import { equal } from "node:assert/strict";
|
|
214
217
|
|
|
215
218
|
import { either as E, pipe } from "@clipboard-health/util-ts";
|
package/package.json
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/util-ts",
|
|
3
3
|
"description": "TypeScript utilities.",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.12.1",
|
|
5
|
+
"bugs": "https://github.com/ClipboardHealth/core-utils/issues",
|
|
5
6
|
"dependencies": {
|
|
6
7
|
"tslib": "2.8.0"
|
|
7
8
|
},
|
|
8
9
|
"devDependencies": {
|
|
9
|
-
"type-fest": "4.
|
|
10
|
+
"type-fest": "4.27.0",
|
|
10
11
|
"zod": "3.23.8"
|
|
11
12
|
},
|
|
12
|
-
"keywords": [
|
|
13
|
+
"keywords": [
|
|
14
|
+
"typescript",
|
|
15
|
+
"utility"
|
|
16
|
+
],
|
|
13
17
|
"license": "MIT",
|
|
14
18
|
"main": "./src/index.js",
|
|
15
19
|
"publishConfig": {
|
|
16
20
|
"access": "public"
|
|
17
21
|
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"directory": "packages/util-ts",
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/ClipboardHealth/core-utils.git"
|
|
26
|
+
},
|
|
18
27
|
"scripts": {
|
|
19
28
|
"embed": "embedme README.md"
|
|
20
29
|
},
|
|
@@ -5,7 +5,7 @@ import * as E from "../functional/either";
|
|
|
5
5
|
*
|
|
6
6
|
* @see {@link https://www.notion.so/BP-REST-API-f769b7fe745c4cf38f6eca2e9ad8a843?pvs=4#e0b4e9ea30f041409ce39505650098ea}
|
|
7
7
|
*/
|
|
8
|
-
declare const ERROR_STATUS_CODES: readonly [400, 401, 403, 404, 409, 422, 429, 500];
|
|
8
|
+
export declare const ERROR_STATUS_CODES: readonly [400, 401, 403, 404, 409, 422, 429, 500];
|
|
9
9
|
/**
|
|
10
10
|
* @deprecated Use {@link ErrorCode} instead.
|
|
11
11
|
*/
|
|
@@ -30,4 +30,3 @@ export declare class CbhError extends Error {
|
|
|
30
30
|
* @deprecated Use {@link failure} instead.
|
|
31
31
|
*/
|
|
32
32
|
export declare function toLeft<A = never>(issues: Arrayable<CbhIssue> | string): E.Either<CbhError, A>;
|
|
33
|
-
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CbhError = void 0;
|
|
3
|
+
exports.CbhError = exports.ERROR_STATUS_CODES = void 0;
|
|
4
4
|
exports.toLeft = toLeft;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const E = tslib_1.__importStar(require("../functional/either"));
|
|
@@ -9,7 +9,7 @@ const E = tslib_1.__importStar(require("../functional/either"));
|
|
|
9
9
|
*
|
|
10
10
|
* @see {@link https://www.notion.so/BP-REST-API-f769b7fe745c4cf38f6eca2e9ad8a843?pvs=4#e0b4e9ea30f041409ce39505650098ea}
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
exports.ERROR_STATUS_CODES = [400, 401, 403, 404, 409, 422, 429, 500];
|
|
13
13
|
/**
|
|
14
14
|
* @deprecated Use {@link ServiceError} instead.
|
|
15
15
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cbhError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/cbhError.ts"],"names":[],"mappings":";;;AAqDA,wBAEC;;AArDD,gEAA0C;AAE1C;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"cbhError.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/errors/cbhError.ts"],"names":[],"mappings":";;;AAqDA,wBAEC;;AArDD,gEAA0C;AAE1C;;;;GAIG;AACU,QAAA,kBAAkB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAU,CAAC;AAiBpF;;GAEG;AACH,MAAa,QAAS,SAAQ,KAAK;IACjB,MAAM,CAAsB;IAE5C,YAAY,eAA6C;QACvD,MAAM,EAAE,GACN,OAAO,eAAe,KAAK,QAAQ;YACjC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,eAAe,EAAE,CAAC;YAChC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,eAAe,CAAC;gBAC9B,CAAC,CAAC,eAAe;gBACjB,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC;QAE1B,MAAM,KAAK,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,OAAO,EAAE,eAAe,EAAE,CAAC;QACpD,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC;QAC7E,6FAA6F;QAC7F,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAElD,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;IACzB,CAAC;CACF;AAnBD,4BAmBC;AAED;;GAEG;AACH,SAAgB,MAAM,CAAY,MAAoC;IACpE,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -71,7 +71,7 @@ declare const ERROR_METADATA: {
|
|
|
71
71
|
};
|
|
72
72
|
};
|
|
73
73
|
type Status = (typeof ERROR_METADATA)[keyof typeof ERROR_METADATA]["status"];
|
|
74
|
-
interface Issue extends ServiceIssue {
|
|
74
|
+
export interface Issue extends ServiceIssue {
|
|
75
75
|
code: Required<ServiceIssue>["code"];
|
|
76
76
|
title: string;
|
|
77
77
|
}
|
|
@@ -9,12 +9,38 @@ export type Right<A> = Readonly<{
|
|
|
9
9
|
/**
|
|
10
10
|
* A value of `Either` type `Left<E>` or type `Right<A>`; a disjoint union.
|
|
11
11
|
*
|
|
12
|
-
* A common use case is as an alternative to
|
|
12
|
+
* A common use case is as an alternative to `Option` where `Left<E>` contains useful
|
|
13
13
|
* information. Convention dictates that `Left<E>` is used for failure and `Right<A>` for success.
|
|
14
14
|
* To help remember, the success case is "right"; it's the result you want.
|
|
15
15
|
*
|
|
16
|
-
* @
|
|
17
|
-
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* // packages/util-ts/examples/either.ts
|
|
19
|
+
* import { equal } from "node:assert/strict";
|
|
20
|
+
*
|
|
21
|
+
* import { either as E, pipe } from "@clipboard-health/util-ts";
|
|
22
|
+
*
|
|
23
|
+
* function double(n: number): number {
|
|
24
|
+
* return n * 2;
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* function inverse(n: number): E.Either<string, number> {
|
|
28
|
+
* return n === 0 ? E.left("Division by zero") : E.right(1 / n);
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* const result = pipe(
|
|
32
|
+
* E.right(5),
|
|
33
|
+
* E.map(double),
|
|
34
|
+
* E.flatMap(inverse),
|
|
35
|
+
* E.match(
|
|
36
|
+
* (error) => `Error: ${error}`,
|
|
37
|
+
* (result) => `Result is ${result}`,
|
|
38
|
+
* ),
|
|
39
|
+
* );
|
|
40
|
+
*
|
|
41
|
+
* equal(result, "Result is 0.1");
|
|
42
|
+
*
|
|
43
|
+
* ```
|
|
18
44
|
*/
|
|
19
45
|
export type Either<E, A> = Left<E> | Right<A>;
|
|
20
46
|
/**
|
|
@@ -27,7 +53,7 @@ export declare function left<E, A = never>(left: E): Either<E, A>;
|
|
|
27
53
|
/**
|
|
28
54
|
* Constructs an `Either` holding a `Right<A>`, representing a success.
|
|
29
55
|
*
|
|
30
|
-
* @param
|
|
56
|
+
* @param right - The value to wrap in a `Right`
|
|
31
57
|
* @returns A `Right` containing the value
|
|
32
58
|
*/
|
|
33
59
|
export declare function right<A, E = never>(right: A): Either<E, A>;
|
|
@@ -72,8 +98,8 @@ export declare function flatMap<E, A, B>(f: (right: A) => Either<E, B>): (either
|
|
|
72
98
|
* Safely extracts the value from an `Either` with a fallback. Use this function when you need to
|
|
73
99
|
* convert an `Either<E, A>` to an `A`, providing a default value for the `Left` case.
|
|
74
100
|
*
|
|
75
|
-
* @param
|
|
76
|
-
* @returns The contained value if `Right`, `
|
|
101
|
+
* @param onLeft - The value to return if the `Either` is `Left`
|
|
102
|
+
* @returns The contained value if `Right`, the result of `onLeft` if `Left`
|
|
77
103
|
*/
|
|
78
104
|
export declare function getOrElse<E, A>(onLeft: (left: E) => A): (either: Either<E, A>) => A;
|
|
79
105
|
/**
|
|
@@ -21,7 +21,7 @@ function left(left) {
|
|
|
21
21
|
/**
|
|
22
22
|
* Constructs an `Either` holding a `Right<A>`, representing a success.
|
|
23
23
|
*
|
|
24
|
-
* @param
|
|
24
|
+
* @param right - The value to wrap in a `Right`
|
|
25
25
|
* @returns A `Right` containing the value
|
|
26
26
|
*/
|
|
27
27
|
function right(right) {
|
|
@@ -78,8 +78,8 @@ function flatMap(f) {
|
|
|
78
78
|
* Safely extracts the value from an `Either` with a fallback. Use this function when you need to
|
|
79
79
|
* convert an `Either<E, A>` to an `A`, providing a default value for the `Left` case.
|
|
80
80
|
*
|
|
81
|
-
* @param
|
|
82
|
-
* @returns The contained value if `Right`, `
|
|
81
|
+
* @param onLeft - The value to return if the `Either` is `Left`
|
|
82
|
+
* @returns The contained value if `Right`, the result of `onLeft` if `Left`
|
|
83
83
|
*/
|
|
84
84
|
function getOrElse(onLeft) {
|
|
85
85
|
return (either) => (isRight(either) ? either.right : onLeft(either.left));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"either.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/either.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"either.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/either.ts"],"names":[],"mappings":";;AAsDA,oBAEC;AAQD,sBAEC;AAQD,wBAEC;AAQD,0BAEC;AAUD,kBAEC;AAOD,0BAEC;AASD,0BAIC;AASD,8BAEC;AASD,sBAKC;AAjGD;;;;;GAKG;AACH,SAAgB,IAAI,CAAe,IAAO;IACxC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAClC,CAAC;AAED;;;;;GAKG;AACH,SAAgB,KAAK,CAAe,KAAQ;IAC1C,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AAClC,CAAC;AAED;;;;;GAKG;AACH,SAAgB,MAAM,CAAO,MAAoB;IAC/C,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC;AACzB,CAAC;AAED;;;;;GAKG;AACH,SAAgB,OAAO,CAAO,MAAoB;IAChD,OAAO,MAAM,CAAC,OAAO,CAAC;AACxB,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,GAAG,CAAO,CAAkB;IAC1C,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AACzE,CAAC;AAED;;;;GAIG;AACH,SAAgB,OAAO,CAAO,CAAiB;IAC7C,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AACtE,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,OAAO,CACrB,CAA6B;IAE7B,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AAClE,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,SAAS,CAAO,MAAsB;IACpD,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AAC5E,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,KAAK,CACnB,MAAsB,EACtB,OAAwB;IAExB,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACrF,CAAC"}
|
|
@@ -8,8 +8,34 @@ export type Some<A> = Readonly<{
|
|
|
8
8
|
/**
|
|
9
9
|
* An optional value. If the value exists, it's of type `Some<A>`, otherwise it's of type `None`.
|
|
10
10
|
*
|
|
11
|
-
* @
|
|
12
|
-
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // packages/util-ts/examples/option.ts
|
|
14
|
+
* import { equal } from "node:assert/strict";
|
|
15
|
+
*
|
|
16
|
+
* import { option as O, pipe } from "@clipboard-health/util-ts";
|
|
17
|
+
*
|
|
18
|
+
* function double(n: number) {
|
|
19
|
+
* return n * 2;
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* function inverse(n: number): O.Option<number> {
|
|
23
|
+
* return n === 0 ? O.none : O.some(1 / n);
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* const result = pipe(
|
|
27
|
+
* O.some(5),
|
|
28
|
+
* O.map(double),
|
|
29
|
+
* O.flatMap(inverse),
|
|
30
|
+
* O.match(
|
|
31
|
+
* () => "No result",
|
|
32
|
+
* (n) => `Result is ${n}`,
|
|
33
|
+
* ),
|
|
34
|
+
* );
|
|
35
|
+
*
|
|
36
|
+
* equal(result, "Result is 0.1");
|
|
37
|
+
*
|
|
38
|
+
* ```
|
|
13
39
|
*/
|
|
14
40
|
export type Option<A> = None | Some<A>;
|
|
15
41
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"option.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/option.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"option.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/option.ts"],"names":[],"mappings":";;;AAwDA,oBAEC;AAQD,wBAEC;AAQD,wBAEC;AASD,kBAEC;AASD,0BAEC;AASD,8BAEC;AASD,sBAKC;AAUD,oCAEC;AAzID,wCAAmC;AA6CnC;;GAEG;AACU,QAAA,IAAI,GAAS,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AAE5C;;;;;GAKG;AACH,SAAgB,IAAI,CAAI,KAAQ;IAC9B,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AACjC,CAAC;AAED;;;;;GAKG;AACH,SAAgB,MAAM,CAAI,MAAiB;IACzC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;AACxB,CAAC;AAED;;;;;GAKG;AACH,SAAgB,MAAM,CAAI,MAAiB;IACzC,OAAO,MAAM,CAAC,MAAM,CAAC;AACvB,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,GAAG,CAAO,CAAc;IACtC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,YAAI,CAAC,CAAC;AACrE,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,OAAO,CAAO,CAAsB;IAClD,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,YAAI,CAAC,CAAC;AAC/D,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,SAAS,CAAI,YAAe;IAC1C,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;AACpE,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,KAAK,CACnB,MAAe,EACf,MAAuB;IAEvB,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;AACxE,CAAC;AAED;;;;;;GAMG;AACH,wDAAwD;AACxD,SAAgB,YAAY,CAAI,KAA2B;IACzD,OAAO,IAAA,eAAK,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,YAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC3C,CAAC"}
|
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pipes a value through a series of functions from left to right. Currently supports up to 10
|
|
3
|
-
* functions.
|
|
4
|
-
*
|
|
5
|
-
* @includeExample ./packages/util-ts/examples/pipe.ts
|
|
6
|
-
* @see [Usage example](../../util-ts/examples/pipe.ts)
|
|
7
|
-
*
|
|
8
|
-
* @param a - The initial value to transform
|
|
9
|
-
* @param fs - Functions to apply sequentially
|
|
10
|
-
* @returns The final transformed value
|
|
11
|
-
*/
|
|
12
1
|
export declare function pipe<A>(a: A): A;
|
|
13
2
|
export declare function pipe<A, B>(a: A, ab: (a: A) => B): B;
|
|
14
3
|
export declare function pipe<A, B, C>(a: A, ab: (a: A) => B, bc: (b: B) => C): C;
|
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.pipe = pipe;
|
|
4
|
+
/**
|
|
5
|
+
* Pipes a value through a series of functions from left to right. Currently supports up to 10
|
|
6
|
+
* functions.
|
|
7
|
+
*
|
|
8
|
+
* @param a - The initial value to transform
|
|
9
|
+
* @param fs - Functions to apply sequentially
|
|
10
|
+
* @returns The final transformed value
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // packages/util-ts/examples/pipe.ts
|
|
14
|
+
* import { equal } from "node:assert/strict";
|
|
15
|
+
*
|
|
16
|
+
* import { pipe } from "@clipboard-health/util-ts";
|
|
17
|
+
*
|
|
18
|
+
* const result = pipe(
|
|
19
|
+
* " hello world ",
|
|
20
|
+
* (s) => s.trim(),
|
|
21
|
+
* (s) => s.split(" "),
|
|
22
|
+
* (array) => array.map((word) => word.charAt(0).toUpperCase() + word.slice(1)),
|
|
23
|
+
* (array) => array.join(" "),
|
|
24
|
+
* );
|
|
25
|
+
*
|
|
26
|
+
* equal(result, "Hello World");
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
4
30
|
function pipe(a, ...fs) {
|
|
5
31
|
return fs.reduce((accumulator, f) => f(accumulator), a);
|
|
6
32
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pipe.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/pipe.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"pipe.js","sourceRoot":"","sources":["../../../../../../packages/util-ts/src/lib/functional/pipe.ts"],"names":[],"mappings":";;AAuFA,oBAEC;AA5BD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,SAAgB,IAAI,CAAC,CAAU,EAAE,GAAG,EAAkC;IACpE,OAAO,EAAE,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC,CAAC;AAC1D,CAAC"}
|