UNPKG

2.15 kBJavaScriptView Raw
1import { as as asIterable } from './as';
2import { _initialize as _initializeFrom } from './from';
3import { bindCallback } from '../util/bindcallback';
4import { isReadableNodeStream, isWritableNodeStream } from '../util/isiterable';
5/**
6 * This class serves as the base for all operations which support [Symbol.iterator].
7 */
8export class IterableX {
9 forEach(projection, thisArg) {
10 const fn = bindCallback(projection, thisArg, 2);
11 let i = 0;
12 for (const item of this) {
13 fn(item, i++);
14 }
15 }
16 pipe(...args) {
17 let i = -1;
18 const n = args.length;
19 let acc = this;
20 while (++i < n) {
21 acc = args[i](asIterable(acc));
22 }
23 return acc;
24 }
25}
26IterableX.prototype[Symbol.toStringTag] = 'IterableX';
27Object.defineProperty(IterableX, Symbol.hasInstance, {
28 writable: true,
29 configurable: true,
30 value(inst) {
31 return !!(inst && inst[Symbol.toStringTag] === 'IterableX');
32 },
33});
34_initializeFrom(IterableX);
35try {
36 ((isBrowser) => {
37 if (isBrowser) {
38 return;
39 }
40 IterableX.prototype['pipe'] = nodePipe;
41 const readableOpts = (x, opts = x._writableState || { objectMode: true }) => opts;
42 function nodePipe(...args) {
43 let i = -1;
44 let end;
45 const n = args.length;
46 let prev = this;
47 let next;
48 while (++i < n) {
49 next = args[i];
50 if (typeof next === 'function') {
51 prev = next(asIterable(prev));
52 }
53 else if (isWritableNodeStream(next)) {
54 ({ end = true } = args[i + 1] || {});
55 // prettier-ignore
56 return isReadableNodeStream(prev) ? prev.pipe(next, { end }) :
57 asIterable(prev).toNodeStream(readableOpts(next)).pipe(next, { end });
58 }
59 }
60 return prev;
61 }
62 })(typeof window === 'object' && typeof document === 'object' && document.nodeType === 9);
63}
64catch (e) {
65 /* */
66}
67
68//# sourceMappingURL=iterablex.mjs.map