@js-data-tools/js-helpers
Version:
A set of JavaScript / TypeScript helper functions for parsing, converting, transforming and formatting data.
186 lines (185 loc) • 4.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.skipUntilAsync = exports.skipWhileAsync = exports.skipAsync = exports.takeUntilAsync = exports.takeWhileAsync = exports.takeAsync = exports.filterAsync = exports.skipUntil = exports.skipWhile = exports.skip = exports.takeUntil = exports.takeWhile = exports.take = exports.filter = void 0;
function* filter(from, predicate) {
if (!from) {
return;
}
if (!predicate) {
yield* from;
return;
}
for (const item of from) {
if (predicate(item)) {
yield item;
}
}
}
exports.filter = filter;
function* take(from, count) {
if (!from) {
return;
}
let remained = count || 0;
for (const item of from) {
if (remained > 0) {
yield item;
--remained;
}
else {
return;
}
}
}
exports.take = take;
function* takeWhile(from, condition) {
if (!from) {
return;
}
if (!condition) {
yield* from;
return;
}
for (const item of from) {
if (condition(item)) {
yield item;
}
else {
return;
}
}
}
exports.takeWhile = takeWhile;
function takeUntil(from, condition) {
return takeWhile(from, condition ? (x) => !condition(x) : condition);
}
exports.takeUntil = takeUntil;
function* skip(from, count) {
if (!from) {
return;
}
let skip = count;
for (const item of from) {
if (skip > 0) {
--skip;
continue;
}
yield item;
}
}
exports.skip = skip;
function* skipWhile(from, condition) {
if (!from) {
return;
}
if (!condition) {
yield* from;
return;
}
let continueSkipping = true;
for (const item of from) {
if (continueSkipping) {
if (condition(item)) {
continue;
}
continueSkipping = false;
}
yield item;
}
}
exports.skipWhile = skipWhile;
function skipUntil(from, condition) {
return skipWhile(from, condition ? (x) => !condition(x) : condition);
}
exports.skipUntil = skipUntil;
async function* filterAsync(from, predicate) {
if (!from) {
return;
}
if (!predicate) {
yield* from;
return;
}
for await (const item of from) {
if (predicate(item)) {
yield item;
}
}
}
exports.filterAsync = filterAsync;
async function* takeAsync(from, count) {
if (!from) {
return;
}
let remained = count || 0;
for await (const item of from) {
if (remained > 0) {
yield item;
--remained;
}
else {
return;
}
}
}
exports.takeAsync = takeAsync;
async function* takeWhileAsync(from, condition) {
if (!from) {
return;
}
if (!condition) {
yield* from;
return;
}
for await (const item of from) {
if (condition(item)) {
yield item;
}
else {
return;
}
}
}
exports.takeWhileAsync = takeWhileAsync;
function takeUntilAsync(from, condition) {
return takeWhileAsync(from, condition ? (x) => !condition(x) : condition);
}
exports.takeUntilAsync = takeUntilAsync;
async function* skipAsync(from, count) {
if (!from) {
return;
}
let skip = count;
for await (const item of from) {
if (skip > 0) {
--skip;
continue;
}
yield item;
}
}
exports.skipAsync = skipAsync;
async function* skipWhileAsync(from, condition) {
if (!from) {
return;
}
if (!condition) {
yield* from;
return;
}
let continueSkipping = true;
for await (const item of from) {
if (continueSkipping) {
if (condition(item)) {
continue;
}
continueSkipping = false;
}
yield item;
}
}
exports.skipWhileAsync = skipWhileAsync;
function skipUntilAsync(from, condition) {
return skipWhileAsync(from, condition ? (x) => !condition(x) : condition);
}
exports.skipUntilAsync = skipUntilAsync;