@effectionx/stream-helpers 0.7.1 → 0.7.3

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.
Files changed (53) hide show
  1. package/README.md +149 -0
  2. package/batch.test.ts +2 -2
  3. package/batch.ts +0 -1
  4. package/dist/batch.d.ts.map +1 -1
  5. package/dist/batch.js +0 -1
  6. package/dist/drain.d.ts +24 -0
  7. package/dist/drain.d.ts.map +1 -0
  8. package/dist/drain.js +33 -0
  9. package/dist/first.d.ts +28 -0
  10. package/dist/first.d.ts.map +1 -0
  11. package/dist/first.js +67 -0
  12. package/dist/for-each.d.ts.map +1 -1
  13. package/dist/for-each.js +12 -8
  14. package/dist/last.d.ts +29 -0
  15. package/dist/last.d.ts.map +1 -0
  16. package/dist/last.js +81 -0
  17. package/dist/mod.d.ts +6 -0
  18. package/dist/mod.d.ts.map +1 -1
  19. package/dist/mod.js +6 -0
  20. package/dist/subject.d.ts.map +1 -1
  21. package/dist/subject.js +0 -1
  22. package/dist/take-until.d.ts +46 -0
  23. package/dist/take-until.d.ts.map +1 -0
  24. package/dist/take-until.js +68 -0
  25. package/dist/take-while.d.ts +41 -0
  26. package/dist/take-while.d.ts.map +1 -0
  27. package/dist/take-while.js +64 -0
  28. package/dist/take.d.ts +36 -0
  29. package/dist/take.d.ts.map +1 -0
  30. package/dist/take.js +59 -0
  31. package/dist/test-helpers/faucet.d.ts.map +1 -1
  32. package/dist/test-helpers/faucet.js +28 -24
  33. package/dist/tsconfig.tsbuildinfo +1 -1
  34. package/drain.test.ts +52 -0
  35. package/drain.ts +35 -0
  36. package/first.test.ts +53 -0
  37. package/first.ts +74 -0
  38. package/for-each.ts +12 -8
  39. package/last.test.ts +65 -0
  40. package/last.ts +96 -0
  41. package/mod.ts +6 -0
  42. package/package.json +5 -4
  43. package/subject.ts +0 -1
  44. package/take-until.test.ts +113 -0
  45. package/take-until.ts +76 -0
  46. package/take-while.test.ts +89 -0
  47. package/take-while.ts +74 -0
  48. package/take.test.ts +83 -0
  49. package/take.ts +67 -0
  50. package/test-helpers/faucet.test.ts +3 -3
  51. package/test-helpers/faucet.ts +28 -24
  52. package/tracker.test.ts +4 -4
  53. package/valve.test.ts +2 -2
@@ -0,0 +1,41 @@
1
+ import type { Stream } from "effection";
2
+ /**
3
+ * Creates a stream transformer that yields values from the source stream
4
+ * while the predicate returns true. Closes when the predicate returns false,
5
+ * without including the failing value.
6
+ *
7
+ * When the predicate fails, the stream closes immediately without the failing
8
+ * value. The close value will be `undefined` since we don't have access to
9
+ * the source's close value without draining the entire stream.
10
+ *
11
+ * If the source stream closes before the predicate returns false, the
12
+ * resulting stream closes with the source's close value.
13
+ *
14
+ * @template T - The type of items in the stream
15
+ * @template TClose - The type of the close value
16
+ * @param predicate - A function that returns true to continue taking values
17
+ * @returns A stream transformer that yields values while predicate is true
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * import { takeWhile, streamOf } from "@effectionx/stream-helpers";
22
+ *
23
+ * const stream = streamOf([1, 2, 3, 4, 5]);
24
+ * // yields 1, 2 (stops when value >= 3)
25
+ * const limited = takeWhile((x: number) => x < 3)(stream);
26
+ * ```
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * import { takeWhile, map } from "@effectionx/stream-helpers";
31
+ * import { pipe } from "remeda";
32
+ *
33
+ * const limited = pipe(
34
+ * source,
35
+ * map(function* (x) { return x * 2; }),
36
+ * takeWhile((x) => x < 100),
37
+ * );
38
+ * ```
39
+ */
40
+ export declare function takeWhile<T>(predicate: (item: T) => boolean): <TClose>(stream: Stream<T, TClose>) => Stream<T, TClose | undefined>;
41
+ //# sourceMappingURL=take-while.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"take-while.d.ts","sourceRoot":"","sources":["../take-while.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,SAAS,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,GAC9B,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CA+BtE"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Creates a stream transformer that yields values from the source stream
3
+ * while the predicate returns true. Closes when the predicate returns false,
4
+ * without including the failing value.
5
+ *
6
+ * When the predicate fails, the stream closes immediately without the failing
7
+ * value. The close value will be `undefined` since we don't have access to
8
+ * the source's close value without draining the entire stream.
9
+ *
10
+ * If the source stream closes before the predicate returns false, the
11
+ * resulting stream closes with the source's close value.
12
+ *
13
+ * @template T - The type of items in the stream
14
+ * @template TClose - The type of the close value
15
+ * @param predicate - A function that returns true to continue taking values
16
+ * @returns A stream transformer that yields values while predicate is true
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * import { takeWhile, streamOf } from "@effectionx/stream-helpers";
21
+ *
22
+ * const stream = streamOf([1, 2, 3, 4, 5]);
23
+ * // yields 1, 2 (stops when value >= 3)
24
+ * const limited = takeWhile((x: number) => x < 3)(stream);
25
+ * ```
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * import { takeWhile, map } from "@effectionx/stream-helpers";
30
+ * import { pipe } from "remeda";
31
+ *
32
+ * const limited = pipe(
33
+ * source,
34
+ * map(function* (x) { return x * 2; }),
35
+ * takeWhile((x) => x < 100),
36
+ * );
37
+ * ```
38
+ */
39
+ export function takeWhile(predicate) {
40
+ return (stream) => ({
41
+ *[Symbol.iterator]() {
42
+ const subscription = yield* stream;
43
+ let done = false;
44
+ return {
45
+ *next() {
46
+ if (done) {
47
+ return { done: true, value: undefined };
48
+ }
49
+ const result = yield* subscription.next();
50
+ if (result.done) {
51
+ return result;
52
+ }
53
+ if (!predicate(result.value)) {
54
+ done = true;
55
+ // Close immediately without the failing value
56
+ // We return undefined as we don't drain the stream
57
+ return { done: true, value: undefined };
58
+ }
59
+ return result;
60
+ },
61
+ };
62
+ },
63
+ });
64
+ }
package/dist/take.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ import type { Stream } from "effection";
2
+ /**
3
+ * Creates a stream transformer that yields the first `n` values from the
4
+ * source stream, then closes with the last taken value.
5
+ *
6
+ * If the source stream closes before yielding `n` values, the resulting
7
+ * stream closes with the source's close value.
8
+ *
9
+ * @template T - The type of items in the stream
10
+ * @template TClose - The type of the close value
11
+ * @param n - The number of values to take
12
+ * @returns A stream transformer that yields at most `n` values
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { take, streamOf } from "@effectionx/stream-helpers";
17
+ *
18
+ * const stream = streamOf([1, 2, 3, 4, 5]);
19
+ * // yields 1, 2, then closes with 3
20
+ * const limited = take(3)(stream);
21
+ * ```
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * import { take, map } from "@effectionx/stream-helpers";
26
+ * import { pipe } from "remeda";
27
+ *
28
+ * const limited = pipe(
29
+ * source,
30
+ * map(function* (x) { return x * 2; }),
31
+ * take(5),
32
+ * );
33
+ * ```
34
+ */
35
+ export declare function take<T>(n: number): <TClose>(stream: Stream<T, TClose>) => Stream<T, T | TClose>;
36
+ //# sourceMappingURL=take.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"take.d.ts","sourceRoot":"","sources":["../take.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,IAAI,CAAC,CAAC,EACpB,CAAC,EAAE,MAAM,GACR,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,CA6B9D"}
package/dist/take.js ADDED
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Creates a stream transformer that yields the first `n` values from the
3
+ * source stream, then closes with the last taken value.
4
+ *
5
+ * If the source stream closes before yielding `n` values, the resulting
6
+ * stream closes with the source's close value.
7
+ *
8
+ * @template T - The type of items in the stream
9
+ * @template TClose - The type of the close value
10
+ * @param n - The number of values to take
11
+ * @returns A stream transformer that yields at most `n` values
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { take, streamOf } from "@effectionx/stream-helpers";
16
+ *
17
+ * const stream = streamOf([1, 2, 3, 4, 5]);
18
+ * // yields 1, 2, then closes with 3
19
+ * const limited = take(3)(stream);
20
+ * ```
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * import { take, map } from "@effectionx/stream-helpers";
25
+ * import { pipe } from "remeda";
26
+ *
27
+ * const limited = pipe(
28
+ * source,
29
+ * map(function* (x) { return x * 2; }),
30
+ * take(5),
31
+ * );
32
+ * ```
33
+ */
34
+ export function take(n) {
35
+ return (stream) => ({
36
+ *[Symbol.iterator]() {
37
+ const subscription = yield* stream;
38
+ let count = 0;
39
+ return {
40
+ *next() {
41
+ if (count >= n) {
42
+ // Should not happen if used correctly, but handle gracefully
43
+ return { done: true, value: undefined };
44
+ }
45
+ const result = yield* subscription.next();
46
+ if (result.done) {
47
+ return result;
48
+ }
49
+ count++;
50
+ if (count >= n) {
51
+ // This is the nth value, return it and mark as done
52
+ return { done: true, value: result.value };
53
+ }
54
+ return result;
55
+ },
56
+ };
57
+ },
58
+ });
59
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"faucet.d.ts","sourceRoot":"","sources":["../../test-helpers/faucet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAGvE;;GAEG;AACH,MAAM,WAAW,MAAM,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC;IACjD;;;OAGG;IACH,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,S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CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CA8BzE"}
@@ -1,5 +1,5 @@
1
- import { createChannel } from "effection";
2
1
  import { createBooleanSignal, is } from "@effectionx/signals";
2
+ import { createChannel } from "effection";
3
3
  /**
4
4
  * Creates a stream that can be used to test the behavior of streams that use backpressure.
5
5
  * It's useful in tests where it can be used as a source stream. This function is used to create
@@ -43,30 +43,34 @@ import { createBooleanSignal, is } from "@effectionx/signals";
43
43
  * @param options.open - Whether the faucet is open.
44
44
  * @returns stream of items coming from the faucet
45
45
  */
46
- export function* useFaucet(options) {
47
- let signal = createChannel();
48
- let open = yield* createBooleanSignal(options.open);
46
+ export function useFaucet(options) {
49
47
  return {
50
- [Symbol.iterator]: signal[Symbol.iterator],
51
- *pour(items) {
52
- if (Array.isArray(items)) {
53
- for (let i of items) {
54
- yield* is(open, (open) => open);
55
- yield* signal.send(i);
56
- }
57
- }
58
- else {
59
- yield* items(function* (item) {
60
- yield* is(open, (open) => open);
61
- yield* signal.send(item);
62
- });
63
- }
64
- },
65
- close() {
66
- open.set(false);
67
- },
68
- open() {
69
- open.set(true);
48
+ *[Symbol.iterator]() {
49
+ let signal = createChannel();
50
+ let open = yield* createBooleanSignal(options.open);
51
+ return {
52
+ [Symbol.iterator]: signal[Symbol.iterator],
53
+ *pour(items) {
54
+ if (Array.isArray(items)) {
55
+ for (let i of items) {
56
+ yield* is(open, (open) => open);
57
+ yield* signal.send(i);
58
+ }
59
+ }
60
+ else {
61
+ yield* items(function* (item) {
62
+ yield* is(open, (open) => open);
63
+ yield* signal.send(item);
64
+ });
65
+ }
66
+ },
67
+ close() {
68
+ open.set(false);
69
+ },
70
+ open() {
71
+ open.set(true);
72
+ },
73
+ };
70
74
  },
71
75
  };
72
76
  }