ix
Version:
The Interactive Extensions for JavaScript
122 lines (120 loc) • 3.82 kB
JavaScript
import { identity } from '../util/identity.mjs';
import { bindCallback } from '../util/bindcallback.mjs';
import { isArrayLike, isIterable, isIterator, isReadableNodeStream, isWritableNodeStream, } from '../util/isiterable.mjs';
import { toLength } from '../util/tolength.mjs';
/**
* This class serves as the base for all operations which support [Symbol.iterator].
*/
export class IterableX {
/** @nocollapse */
forEach(projection, thisArg) {
const fn = bindCallback(projection, thisArg, 2);
let i = 0;
for (const item of this) {
fn(item, i++);
}
}
pipe(...args) {
let i = -1;
const n = args.length;
let acc = this;
while (++i < n) {
acc = args[i](IterableX.as(acc));
}
return acc;
}
/** @nocollapse */
static as(source) {
if (source instanceof IterableX) {
return source;
}
if (typeof source === 'string') {
return new FromIterable([source], identity);
}
if (isIterable(source) || isArrayLike(source)) {
return new FromIterable(source, identity);
}
return new FromIterable([source], identity);
}
/** @nocollapse */
static from(source, selector = identity, thisArg) {
const fn = bindCallback(selector, thisArg, 2);
if (isIterable(source)) {
return new FromIterable(source, fn);
}
if (isArrayLike(source)) {
return new FromIterable(source, fn);
}
if (isIterator(source)) {
return new FromIterable({ [Symbol.iterator]: () => source }, fn);
}
throw new TypeError('Input type not supported');
}
}
IterableX.prototype[Symbol.toStringTag] = 'IterableX';
Object.defineProperty(IterableX, Symbol.hasInstance, {
writable: true,
configurable: true,
value(inst) {
return !!(inst && inst[Symbol.toStringTag] === 'IterableX');
},
});
/** @ignore */
export class FromIterable extends IterableX {
constructor(source, fn) {
super();
this._source = source;
this._fn = fn;
}
*[Symbol.iterator]() {
const iterable = isIterable(this._source);
let i = 0;
if (iterable) {
for (const item of this._source) {
yield this._fn(item, i++);
}
}
else {
const length = toLength(this._source.length);
while (i < length) {
const val = this._source[i];
yield this._fn(val, i++);
}
}
}
}
try {
((isBrowser) => {
if (isBrowser) {
return;
}
IterableX.prototype['pipe'] = nodePipe;
const readableOpts = (x, opts = x._writableState || { objectMode: true }) => opts;
function nodePipe(...args) {
let i = -1;
let end;
const n = args.length;
let prev = this;
let next;
while (++i < n) {
next = args[i];
if (typeof next === 'function') {
prev = next(IterableX.as(prev));
}
else if (isWritableNodeStream(next)) {
({ end = true } = args[i + 1] || {});
// prettier-ignore
return isReadableNodeStream(prev) ? prev.pipe(next, { end }) :
IterableX.as(prev).toNodeStream(readableOpts(next)).pipe(next, { end });
}
}
return prev;
}
})(typeof window === 'object' && typeof document === 'object' && document.nodeType === 9);
}
catch (e) {
/* */
}
export const as = IterableX.as;
export const from = IterableX.from;
//# sourceMappingURL=iterablex.mjs.map