silentium 0.0.172 → 0.0.173
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 +7 -0
- package/dist/silentium.cjs +10 -4
- package/dist/silentium.cjs.map +1 -1
- package/dist/silentium.d.ts +7 -4
- package/dist/silentium.js +10 -5
- package/dist/silentium.js.map +1 -1
- package/dist/silentium.min.js +1 -1
- package/dist/silentium.min.mjs +1 -1
- package/dist/silentium.min.mjs.map +1 -1
- package/dist/silentium.mjs +10 -5
- package/dist/silentium.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/Piped.test.ts +167 -0
- package/src/components/Piped.ts +18 -0
- package/src/global.d.ts +6 -2
- package/src/helpers/DevTools.ts +4 -5
- package/src/index.ts +1 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { Piped } from "components/Piped";
|
|
3
|
+
import { Of } from "base/Of";
|
|
4
|
+
import { Message } from "base/Message";
|
|
5
|
+
import { Applied } from "components/Applied";
|
|
6
|
+
|
|
7
|
+
describe("Piped.test", () => {
|
|
8
|
+
test("basic pipe with single constructor", async () => {
|
|
9
|
+
const $base = Of(10);
|
|
10
|
+
const double = ($msg: MessageType<number>) =>
|
|
11
|
+
Applied($msg, (v: number) => v * 2);
|
|
12
|
+
|
|
13
|
+
const $result = Piped($base, double);
|
|
14
|
+
const result = await $result;
|
|
15
|
+
|
|
16
|
+
expect(result).toBe(20);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("pipe with multiple constructors", async () => {
|
|
20
|
+
const $base = Of(5);
|
|
21
|
+
const addTwo = ($msg: MessageType<number>) =>
|
|
22
|
+
Applied($msg, (v: number) => v + 2);
|
|
23
|
+
const multiplyThree = ($msg: MessageType<number>) =>
|
|
24
|
+
Applied($msg, (v: number) => v * 3);
|
|
25
|
+
const subtractOne = ($msg: MessageType<number>) =>
|
|
26
|
+
Applied($msg, (v: number) => v - 1);
|
|
27
|
+
|
|
28
|
+
const $result = Piped($base, addTwo, multiplyThree, subtractOne);
|
|
29
|
+
const result = await $result;
|
|
30
|
+
|
|
31
|
+
expect(result).toBe((5 + 2) * 3 - 1); // 7 * 3 - 1 = 20
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("pipe with raw value instead of message", async () => {
|
|
35
|
+
const rawValue = 10;
|
|
36
|
+
const double = ($msg: MessageType<number>) =>
|
|
37
|
+
Applied($msg, (v: number) => v * 2);
|
|
38
|
+
|
|
39
|
+
const $result = Piped(rawValue, double);
|
|
40
|
+
const result = await $result;
|
|
41
|
+
|
|
42
|
+
expect(result).toBe(20);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("pipe with mixed raw value and message", async () => {
|
|
46
|
+
const rawValue = 5;
|
|
47
|
+
const addTwo = ($msg: MessageType<number>) =>
|
|
48
|
+
Applied($msg, (v: number) => v + 2);
|
|
49
|
+
const multiplyThree = ($msg: MessageType<number>) =>
|
|
50
|
+
Applied($msg, (v: number) => v * 3);
|
|
51
|
+
|
|
52
|
+
const $result = Piped(rawValue, addTwo, multiplyThree);
|
|
53
|
+
const result = await $result;
|
|
54
|
+
|
|
55
|
+
expect(result).toBe((5 + 2) * 3); // 21
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("pipe with string transformations", async () => {
|
|
59
|
+
const $base = Of("hello");
|
|
60
|
+
const toUpperCase = ($msg: MessageType<string>) =>
|
|
61
|
+
Applied($msg, (v: string) => v.toUpperCase());
|
|
62
|
+
const addExclamation = ($msg: MessageType<string>) =>
|
|
63
|
+
Applied($msg, (v: string) => v + "!");
|
|
64
|
+
|
|
65
|
+
const $result = Piped($base, toUpperCase, addExclamation);
|
|
66
|
+
const result = await $result;
|
|
67
|
+
|
|
68
|
+
expect(result).toBe("HELLO!");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("pipe with object transformations", async () => {
|
|
72
|
+
const $base = Of({ name: "John", age: 30 });
|
|
73
|
+
const addCountry = ($msg: MessageType<{ name: string; age: number }>) =>
|
|
74
|
+
Applied($msg, (v) => ({ ...v, country: "USA" }));
|
|
75
|
+
const incrementAge = (
|
|
76
|
+
$msg: MessageType<{ name: string; age: number; country: string }>,
|
|
77
|
+
) => Applied($msg, (v) => ({ ...v, age: v.age + 1 }));
|
|
78
|
+
|
|
79
|
+
const $result = Piped($base, addCountry, incrementAge);
|
|
80
|
+
const result = await $result;
|
|
81
|
+
|
|
82
|
+
expect(result).toEqual({ name: "John", age: 31, country: "USA" });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("pipe with error handling in constructor", () => {
|
|
86
|
+
const $base = Of(10);
|
|
87
|
+
const errorConstructor = ($msg: MessageType<number>) =>
|
|
88
|
+
Message<number>((resolve, reject) => {
|
|
89
|
+
$msg.then(() => reject("error from constructor"));
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const $result = Piped($base, errorConstructor);
|
|
93
|
+
|
|
94
|
+
let caught = false;
|
|
95
|
+
$result.then(() => {
|
|
96
|
+
throw new Error("should not resolve");
|
|
97
|
+
});
|
|
98
|
+
$result.catch((e: unknown) => {
|
|
99
|
+
expect(e).toBe("error from constructor");
|
|
100
|
+
caught = true;
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
expect(caught).toBe(true);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("pipe with no constructors returns original message", () => {
|
|
107
|
+
const $base = Of(42);
|
|
108
|
+
|
|
109
|
+
const $result = Piped($base) as any;
|
|
110
|
+
|
|
111
|
+
const g = vi.fn();
|
|
112
|
+
$result.then(g);
|
|
113
|
+
|
|
114
|
+
expect(g).toBeCalledWith(42);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("pipe with raw value and no constructors", async () => {
|
|
118
|
+
const rawValue = 42;
|
|
119
|
+
|
|
120
|
+
const $result = Piped(rawValue) as any;
|
|
121
|
+
|
|
122
|
+
// When no constructors are provided, Pipe returns the raw value directly
|
|
123
|
+
expect(await $result).toBe(42);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("pipe with complex type transformations", async () => {
|
|
127
|
+
const $base = Of([1, 2, 3]);
|
|
128
|
+
const sumArray = ($msg: MessageType<number[]>) =>
|
|
129
|
+
Applied($msg, (v: number[]) => v.reduce((a, b) => a + b, 0));
|
|
130
|
+
const doubleSum = ($msg: MessageType<number>) =>
|
|
131
|
+
Applied($msg, (v: number) => v * 2);
|
|
132
|
+
const toString = ($msg: MessageType<number>) =>
|
|
133
|
+
Applied($msg, (v: number) => `Sum: ${v}`);
|
|
134
|
+
|
|
135
|
+
const $result = Piped($base, sumArray, doubleSum, toString);
|
|
136
|
+
const result = await $result;
|
|
137
|
+
|
|
138
|
+
expect(result).toBe("Sum: 12"); // (1+2+3)*2 = 12
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("pipe preserves message chainability", () => {
|
|
142
|
+
const $base = Of(10);
|
|
143
|
+
const addFive = ($msg: MessageType<number>) =>
|
|
144
|
+
Applied($msg, (v: number) => v + 5);
|
|
145
|
+
|
|
146
|
+
const $result = Piped($base, addFive) as any;
|
|
147
|
+
|
|
148
|
+
// Should be able to chain then/catch
|
|
149
|
+
let resolved = false;
|
|
150
|
+
let rejected = false;
|
|
151
|
+
|
|
152
|
+
$result.then((v: number) => {
|
|
153
|
+
expect(v).toBe(15);
|
|
154
|
+
resolved = true;
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
$result.catch(() => {
|
|
158
|
+
rejected = true;
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
expect(resolved).toBe(true);
|
|
162
|
+
expect(rejected).toBe(false);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
import { MessageType } from "types/MessageType";
|
|
167
|
+
import { vi } from "vitest";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ActualMessage } from "base/ActualMessage";
|
|
2
|
+
import { MaybeMessage } from "types/MessageType";
|
|
3
|
+
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
5
|
+
type Last<T extends readonly any[]> = T extends readonly [...infer _, infer L]
|
|
6
|
+
? L extends (...args: any) => any
|
|
7
|
+
? L
|
|
8
|
+
: never
|
|
9
|
+
: never;
|
|
10
|
+
|
|
11
|
+
export function Piped<T extends ((...vars: any) => MaybeMessage)[]>(
|
|
12
|
+
$m: MaybeMessage,
|
|
13
|
+
...c: T
|
|
14
|
+
) {
|
|
15
|
+
return c.reduce((msg, Constructor) => {
|
|
16
|
+
return ActualMessage(Constructor(msg));
|
|
17
|
+
}, ActualMessage($m)) as ReturnType<Last<T>>;
|
|
18
|
+
}
|
package/src/global.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import { MessageType } from "types/MessageType";
|
|
2
|
+
|
|
1
3
|
export {};
|
|
2
4
|
|
|
3
5
|
declare global {
|
|
4
6
|
interface GlobalThis {
|
|
5
|
-
silentiumValue: (
|
|
7
|
+
silentiumValue: ($message: MessageType) => unknown;
|
|
8
|
+
silentiumPrint: (...messages: MessageType[]) => void;
|
|
6
9
|
}
|
|
7
10
|
|
|
8
|
-
function silentiumValue(
|
|
11
|
+
function silentiumValue($message: MessageType): unknown;
|
|
12
|
+
function silentiumPrint(...messages: MessageType[]): void;
|
|
9
13
|
}
|
package/src/helpers/DevTools.ts
CHANGED
|
@@ -6,16 +6,15 @@ import { MessageType } from "types/MessageType";
|
|
|
6
6
|
|
|
7
7
|
declare global {
|
|
8
8
|
interface GlobalThis {
|
|
9
|
-
silentiumPrint: (...messages: MessageType[]) => MessageType;
|
|
10
9
|
silentiumValue: ($message: MessageType) => unknown;
|
|
10
|
+
silentiumPrint: (...messages: MessageType[]) => void;
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
const silentiumPrint = (...messages: MessageType[]) => {
|
|
15
|
-
Applied(All(...messages.map((e) => Shared(e))),
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
})).then(console.table);
|
|
15
|
+
Applied(All(...messages.map((e) => Shared(e))), JSON.stringify).then(
|
|
16
|
+
console.log,
|
|
17
|
+
);
|
|
19
18
|
};
|
|
20
19
|
const silentiumValue = ($message: MessageType) =>
|
|
21
20
|
Primitive($message).primitive();
|
package/src/index.ts
CHANGED
|
@@ -28,6 +28,7 @@ export * from "./components/FromEvent";
|
|
|
28
28
|
export * from "./components/Late";
|
|
29
29
|
export * from "./components/Map";
|
|
30
30
|
export * from "./components/Once";
|
|
31
|
+
export * from "./components/Piped";
|
|
31
32
|
export * from "./components/Primitive";
|
|
32
33
|
export * from "./components/Process";
|
|
33
34
|
export * from "./components/Race";
|