Skip to content

Commit

Permalink
Prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Apr 3, 2024
1 parent c22aac1 commit 2de5b21
Showing 1 changed file with 88 additions and 56 deletions.
144 changes: 88 additions & 56 deletions util/RangeIterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ export const SKIP = {};
const DONE = {
value: null,
done: true,
}
const RETURN_DONE = { // we allow this one to be mutated
};
const RETURN_DONE = {
// we allow this one to be mutated
value: null,
done: true,
};
Expand All @@ -27,7 +28,7 @@ export class RangeIterable {
return {
next(resolvedResult) {
try {
let result;
let result;
do {
let iteratorResult;
if (resolvedResult) {
Expand All @@ -37,11 +38,18 @@ export class RangeIterable {
iteratorResult = iterator.next();
if (iteratorResult.then) {
if (!async) {
this.throw(new Error('Can not synchronously iterate with asynchronous values'));
this.throw(
new Error(
'Can not synchronously iterate with asynchronous values',
),
);
}
return iteratorResult.then(iteratorResult => this.next(iteratorResult), (error) => {
this.throw(error);
});
return iteratorResult.then(
(iteratorResult) => this.next(iteratorResult),
(error) => {
this.throw(error);
},
);
}
}
if (iteratorResult.done === true) {
Expand All @@ -52,23 +60,26 @@ export class RangeIterable {
result = func.call(source, iteratorResult.value, i++);
if (result && result.then && async) {
// if async, wait for promise to resolve before returning iterator result
return result.then(result =>
result === SKIP ?
this.next() :
{
value: result
}, (error) => {
this.throw(error);
});
return result.then(
(result) =>
result === SKIP
? this.next()
: {
value: result,
},
(error) => {
this.throw(error);
},
);
}
} while(result === SKIP);
} while (result === SKIP);
if (result === DONE) {
return this.return();
}
return {
value: result
value: result,
};
} catch(error) {
} catch (error) {
this.throw(error);
}
},
Expand All @@ -84,28 +95,29 @@ export class RangeIterable {
throw(error) {
this.return();
throw error;
}
},
};
};
return iterable;
}
[Symbol.asyncIterator]() {
return this.iterator = this.iterate(true);
return (this.iterator = this.iterate(true));
}
[Symbol.iterator]() {
return this.iterator = this.iterate();
return (this.iterator = this.iterate());
}
filter(func) {
return this.map(element => {
return this.map((element) => {
let result = func(element);
// handle promise
if (result?.then) return result.then((result) => result ? element : SKIP);
if (result?.then)
return result.then((result) => (result ? element : SKIP));
else return result ? element : SKIP;
});
}

forEach(callback) {
let iterator = this.iterator = this.iterate();
let iterator = (this.iterator = this.iterate());
let result;
while ((result = iterator.next()).done !== true) {
callback(result.value);
Expand All @@ -114,23 +126,30 @@ export class RangeIterable {
concat(secondIterable) {
let concatIterable = new RangeIterable();
concatIterable.iterate = (async) => {
let iterator = this.iterator = this.iterate(async);
let iterator = (this.iterator = this.iterate(async));
let isFirst = true;
function iteratorDone(result) {
if (isFirst) {
try {
isFirst = false;
iterator = secondIterable[async ? Symbol.asyncIterator : Symbol.iterator]();
iterator =
secondIterable[async ? Symbol.asyncIterator : Symbol.iterator]();
result = iterator.next();
if (concatIterable.onDone) {
if (result.then) {
if (!async) throw new Error('Can not synchronously iterate with asynchronous values');
result.then((result) => {
if (result.done()) concatIterable.onDone();
}, (error) => {
this.return();
throw error;
});
if (!async)
throw new Error(
'Can not synchronously iterate with asynchronous values',
);
result.then(
(result) => {
if (result.done()) concatIterable.onDone();
},
(error) => {
this.return();
throw error;
},
);
} else if (result.done) concatIterable.onDone();
}
} catch (error) {
Expand All @@ -146,7 +165,10 @@ export class RangeIterable {
try {
let result = iterator.next();
if (result.then) {
if (!async) throw new Error('Can synchronously iterate with asynchronous values');
if (!async)
throw new Error(
'Can synchronously iterate with asynchronous values',
);
return result.then((result) => {
if (result.done) return iteratorDone(result);
return result;
Expand All @@ -167,12 +189,11 @@ export class RangeIterable {
iterator.return();
}
return RETURN_DONE;

},
throw(error) {
this.return();
throw error;
}
},
};
};
return concatIterable;
Expand All @@ -181,7 +202,7 @@ export class RangeIterable {
flatMap(callback) {
let mappedIterable = new RangeIterable();
mappedIterable.iterate = (async) => {
let iterator = this.iterator = this.iterate(async);
let iterator = (this.iterator = this.iterate(async));
let isFirst = true;
let currentSubIterator;
return {
Expand All @@ -195,7 +216,10 @@ export class RangeIterable {
resolvedResult = undefined;
} else result = currentSubIterator.next();
if (result.then) {
if (!async) throw new Error('Can not synchronously iterate with asynchronous values');
if (!async)
throw new Error(
'Can not synchronously iterate with asynchronous values',
);
return result.then((result) => this.next(result));
}
if (!result.done) {
Expand All @@ -204,7 +228,10 @@ export class RangeIterable {
}
let result = resolvedResult ?? iterator.next();
if (result.then) {
if (!async) throw new Error('Can not synchronously iterate with asynchronous values');
if (!async)
throw new Error(
'Can not synchronously iterate with asynchronous values',
);
currentSubIterator = undefined;
return result.then((result) => this.next(result));
}
Expand All @@ -214,7 +241,10 @@ export class RangeIterable {
}
let value = callback(result.value);
if (value?.then) {
if (!async) throw new Error('Can not synchronously iterate with asynchronous values');
if (!async)
throw new Error(
'Can not synchronously iterate with asynchronous values',
);
return value.then((value) => {
if (Array.isArray(value) || value instanceof RangeIterable) {
currentSubIterator = value[Symbol.iterator]();
Expand All @@ -223,41 +253,38 @@ export class RangeIterable {
currentSubIterator = null;
return { value };
}
})
});
}
if (Array.isArray(value) || value instanceof RangeIterable)
currentSubIterator = value[Symbol.iterator]();
else {
currentSubIterator = null;
return { value };
}
} while(true);
} while (true);
} catch (error) {
this.return();
throw error;
}
},
return() {
if (mappedIterable.onDone) mappedIterable.onDone();
if (currentSubIterator)
currentSubIterator.return();
if (currentSubIterator) currentSubIterator.return();
return iterator.return();
},
throw() {
if (mappedIterable.onDone) mappedIterable.onDone();
if (currentSubIterator)
currentSubIterator.throw();
if (currentSubIterator) currentSubIterator.throw();
return iterator.throw();
}
},
};
};
return mappedIterable;
}

slice(start, end) {
return this.map((element, i) => {
if (i < start)
return SKIP;
if (i < start) return SKIP;
if (i >= end) {
DONE.value = element;
return DONE;
Expand All @@ -266,22 +293,22 @@ export class RangeIterable {
});
}
next() {
if (!this.iterator)
this.iterator = this.iterate();
if (!this.iterator) this.iterator = this.iterate();
return this.iterator.next();
}
toJSON() {
if (this.asArray && this.asArray.forEach) {
return this.asArray;
}
const error = new Error('Can not serialize async iterables without first calling resolving asArray');
const error = new Error(
'Can not serialize async iterables without first calling resolving asArray',
);
error.resolution = this.asArray;
throw error;
//return Array.from(this)
}
get asArray() {
if (this._asArray)
return this._asArray;
if (this._asArray) return this._asArray;
let promise = new Promise((resolve, reject) => {
let iterator = this.iterate(true);
let array = [];
Expand All @@ -296,7 +323,7 @@ export class RangeIterable {
}
result = iterator.next();
}
resolve(iterable._asArray = array);
resolve((iterable._asArray = array));
}
next(iterator.next());
});
Expand All @@ -306,5 +333,10 @@ export class RangeIterable {
resolveData() {
return this.asArray;
}
at(index) {
for (let entry of this) {
if (index-- === 0) return entry;
}
}
}
RangeIterable.prototype.DONE = DONE;
RangeIterable.prototype.DONE = DONE;

0 comments on commit 2de5b21

Please sign in to comment.