@emmveqz/js-utils
Version:
Utilities for JavaScript
158 lines (157 loc) • 6.63 kB
JavaScript
;
//
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withAsyncTryCatch = exports.withTryCatch = exports.ctxAsyncTryCatch = exports.asyncTryCatch = exports.ctxTryCatch = exports.tryCatch = exports.extractEnumNumbers = exports.arrayUnique = exports.arrayFilterHalt = exports.arraySum = exports.arrayUniqueByProp = exports.getPromiseState = void 0;
exports.racePromisesIterator = racePromisesIterator;
//
/**
* This relies blindly that `Promise.race(promises)` will resolve with `promises[0]`
*
* even if all `promises` were resolved when passed.
*/
const getPromiseState = (promise) => {
const pending = Symbol('pending');
return Promise
.race([promise, Promise.resolve(pending)])
.then((value) => (value === pending ? 'pending' : 'fulfilled'))
.catch(() => 'rejected');
};
exports.getPromiseState = getPromiseState;
/**
* Returns an iterator, yielding the value of `'fulfilled'` promises first.
*
* In case a promise is rejected, the iterator will yield an `Error` instance instead.
*/
function racePromisesIterator(promises) {
return __asyncGenerator(this, arguments, function* racePromisesIterator_1() {
const pending = promises.concat([]);
while (pending.length) {
try {
yield __await(Promise.race(pending));
}
catch (ex) {
//
}
for (let idx = 0; idx < pending.length; idx++) {
const state = yield __await((0, exports.getPromiseState)(pending[idx]));
if (state === 'pending') {
continue;
}
let nextRequest;
try {
nextRequest = yield yield __await(yield __await(pending[idx]));
}
catch (ex) {
nextRequest = yield yield __await(new Error(ex));
}
if (nextRequest === null || nextRequest === void 0 ? void 0 : nextRequest.abort) {
return yield __await(void 0);
}
pending.splice(idx, 1);
yield __await((nextRequest === null || nextRequest === void 0 ? void 0 : nextRequest.waitFor));
break;
}
}
});
}
const arrayUniqueByProp = (arr, prop) => {
return arr.filter((val, idx, arr2) => arr2
.findIndex((val2) => val2[prop] === val[prop]) === idx);
};
exports.arrayUniqueByProp = arrayUniqueByProp;
/**
* If you are not certain of the types for the `arr` values,
*
* consider validating the result with `Number.isNaN(result)`
*/
const arraySum = (arr) => {
return !arr.length ? 0 : arr.reduce((total, val) => (Number.isSafeInteger(val) ? (total || 0) + val : total), Number.isSafeInteger(arr[0]) ? 0 : NaN);
};
exports.arraySum = arraySum;
/**
* Returns a copy of the `arr` which halts at the first `false` occurence for `predicate`.
*/
const arrayFilterHalt = (arr, predicate) => {
const idx = arr.findIndex(predicate);
return arr.slice(0, idx);
};
exports.arrayFilterHalt = arrayFilterHalt;
/**
* Consider using `new Set(arr)`
*/
const arrayUnique = (arr) => {
return arr.filter((val, idx, arr2) => arr2.indexOf(val) === idx);
};
exports.arrayUnique = arrayUnique;
const extractEnumNumbers = (en) => {
return Object
.values(en)
.filter((val) => typeof val === typeof 1);
};
exports.extractEnumNumbers = extractEnumNumbers;
const tryCatch = (func, ...args) => {
try {
return func(...args);
}
catch (ex) {
return new Error(ex.message);
}
};
exports.tryCatch = tryCatch;
const ctxTryCatch = (ctx, func, ...args) => {
try {
return func.call(ctx, ...args);
}
catch (ex) {
return new Error(ex.message);
}
};
exports.ctxTryCatch = ctxTryCatch;
const asyncTryCatch = (func, ...args) => __awaiter(void 0, void 0, void 0, function* () {
try {
return yield func(...args);
}
catch (ex) {
return new Error(ex.message);
}
});
exports.asyncTryCatch = asyncTryCatch;
const ctxAsyncTryCatch = (ctx, func, ...args) => __awaiter(void 0, void 0, void 0, function* () {
try {
return yield func.call(ctx, ...args);
}
catch (ex) {
return new Error(ex.message);
}
});
exports.ctxAsyncTryCatch = ctxAsyncTryCatch;
const withTryCatch = (func) => {
return (...args) => (0, exports.tryCatch)(func, ...args);
};
exports.withTryCatch = withTryCatch;
const withAsyncTryCatch = (func) => {
return (...args) => (0, exports.asyncTryCatch)(func, ...args);
};
exports.withAsyncTryCatch = withAsyncTryCatch;