@cspell/cspell-pipe 8.10.2 → 8.11.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.
@@ -0,0 +1,3 @@
1
+ export type ForkedIterables<T> = [Iterable<T>, Iterable<T>];
2
+ export declare function fork<T>(iterable: Iterable<T>): ForkedIterables<T>;
3
+ //# sourceMappingURL=fork.d.ts.map
@@ -0,0 +1,51 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ const emptyArray = [];
3
+ Object.freeze(emptyArray);
4
+ export function fork(iterable) {
5
+ let active = 3;
6
+ const bufA = { buf: [] };
7
+ const bufB = { buf: [] };
8
+ let iterator = undefined;
9
+ function getIterator() {
10
+ if (iterator) {
11
+ return iterator;
12
+ }
13
+ return (iterator = iterable[Symbol.iterator]());
14
+ }
15
+ function* gen(mask, a, b) {
16
+ const cur = a.buf;
17
+ const other = b.buf;
18
+ const iter = getIterator();
19
+ try {
20
+ // We have to loop through the current buffer first.
21
+ // It is necessary to use a loop in case the buffer is updated between yields.
22
+ for (let i = 0; i < cur.length; i++) {
23
+ yield cur[i];
24
+ }
25
+ cur.length = 0;
26
+ let n;
27
+ while (!(n = iter.next()).done) {
28
+ if (active & mask) {
29
+ other.push(n.value);
30
+ }
31
+ yield n.value;
32
+ }
33
+ }
34
+ catch (e) {
35
+ if (iter.throw) {
36
+ return iter.throw(e);
37
+ }
38
+ throw e;
39
+ }
40
+ finally {
41
+ active &= mask;
42
+ cur.length = 0;
43
+ a.buf = emptyArray;
44
+ if (!active) {
45
+ iterator?.return?.();
46
+ }
47
+ }
48
+ }
49
+ return [gen(~1, bufA, bufB), gen(~2, bufB, bufA)];
50
+ }
51
+ //# sourceMappingURL=fork.js.map
@@ -1,4 +1,5 @@
1
1
  export { toDistributableIterable } from './distribute.js';
2
+ export { fork } from './fork.js';
2
3
  export { interleave } from './interleave.js';
3
4
  export { asyncIteratorToAsyncIterable, iteratorToIterable } from './iteratorToIterable.js';
4
5
  export { toArray } from './toArray.js';
@@ -1,4 +1,5 @@
1
1
  export { toDistributableIterable } from './distribute.js';
2
+ export { fork } from './fork.js';
2
3
  export { interleave } from './interleave.js';
3
4
  export { asyncIteratorToAsyncIterable, iteratorToIterable } from './iteratorToIterable.js';
4
5
  export { toArray } from './toArray.js';
@@ -1,13 +1,37 @@
1
1
  export function* iteratorToIterable(iterator) {
2
- let n;
3
- while (!(n = iterator.next()).done) {
4
- yield n.value;
2
+ try {
3
+ let n;
4
+ while (!(n = iterator.next()).done) {
5
+ yield n.value;
6
+ }
7
+ }
8
+ catch (e) {
9
+ if (iterator.throw) {
10
+ return iterator.throw(e);
11
+ }
12
+ throw e;
13
+ }
14
+ finally {
15
+ // ensure that clean up happens.
16
+ iterator.return?.();
5
17
  }
6
18
  }
7
19
  export async function* asyncIteratorToAsyncIterable(iterator) {
8
- let n;
9
- while (!(n = await iterator.next()).done) {
10
- yield n.value;
20
+ try {
21
+ let n;
22
+ while (!(n = await iterator.next()).done) {
23
+ yield n.value;
24
+ }
25
+ }
26
+ catch (e) {
27
+ if (iterator.throw) {
28
+ return iterator.throw(e);
29
+ }
30
+ throw e;
31
+ }
32
+ finally {
33
+ // ensure that clean up happens.
34
+ iterator.return?.();
11
35
  }
12
36
  }
13
37
  //# sourceMappingURL=iteratorToIterable.js.map
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _helpers from './helpers/index.js';
2
2
  import * as _operators from './operators/index.js';
3
- export { interleave, isAsyncIterable, toArray, toAsyncIterable, toDistributableIterable } from './helpers/index.js';
3
+ export { fork, interleave, isAsyncIterable, toArray, toAsyncIterable, toDistributableIterable, } from './helpers/index.js';
4
4
  export { opAppend, opAwaitAsync, opBuffer, opConcatMap, opFilter, opFirst, opFlatten, opJoinStrings, opLast, opMap, opSkip, opTake, opTap, opUnique, } from './operators/index.js';
5
5
  export { pipeAsync, pipeSync } from './pipe.js';
6
6
  export { reduce, reduceAsync, reduceSync } from './reduce.js';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _helpers from './helpers/index.js';
2
2
  import * as _operators from './operators/index.js';
3
- export { interleave, isAsyncIterable, toArray, toAsyncIterable, toDistributableIterable } from './helpers/index.js';
3
+ export { fork, interleave, isAsyncIterable, toArray, toAsyncIterable, toDistributableIterable, } from './helpers/index.js';
4
4
  export { opAppend, opAwaitAsync, opBuffer, opConcatMap, opFilter, opFirst, opFlatten, opJoinStrings, opLast, opMap, opSkip, opTake, opTap, opUnique, } from './operators/index.js';
5
5
  export { pipeAsync, pipeSync } from './pipe.js';
6
6
  export { reduce, reduceAsync, reduceSync } from './reduce.js';
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "8.10.2",
6
+ "version": "8.11.0",
7
7
  "description": "Library to make working with Iterators/AsyncIterators easier.",
8
8
  "keywords": [
9
9
  "cspell",
@@ -126,5 +126,5 @@
126
126
  "devDependencies": {
127
127
  "globby": "^14.0.2"
128
128
  },
129
- "gitHead": "1a1f15b593a0b25419c1e74eeb53d0b2839762cc"
129
+ "gitHead": "2b85b2b458b1117870a4f0aee18fb45ce991848d"
130
130
  }