123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- function recordFromArray(array, mainKey) {
- const record = Object.create(null);
- for (const value of array) {
- const key = value[mainKey];
- // istanbul ignore next
- if (record[key]) {
- throw new Error(`Duplicate ${mainKey} ${JSON.stringify(key)}`);
- }
- // @ts-ignore
- record[key] = value;
- }
- return record;
- }
- exports.recordFromArray = recordFromArray;
- function mapFromArray(array, mainKey) {
- const map = new Map();
- for (const value of array) {
- const key = value[mainKey];
- // istanbul ignore next
- if (map.has(key)) {
- throw new Error(`Duplicate ${mainKey} ${JSON.stringify(key)}`);
- }
- map.set(key, value);
- }
- return map;
- }
- exports.mapFromArray = mapFromArray;
- function createAutoChecklist() {
- const map = Object.create(null);
- return (id) => {
- const idString = JSON.stringify(id);
- if (map[idString]) {
- return true;
- }
- map[idString] = true;
- return false;
- };
- }
- exports.createAutoChecklist = createAutoChecklist;
- function partition(array, predicate) {
- const trueArray = [];
- const falseArray = [];
- for (const value of array) {
- if (predicate(value)) {
- trueArray.push(value);
- }
- else {
- falseArray.push(value);
- }
- }
- return [trueArray, falseArray];
- }
- exports.partition = partition;
- function isInt(value) {
- return value === Math.floor(value);
- }
- exports.isInt = isInt;
- function comparePrimitive(a, b) {
- if (a === b) {
- return 0;
- }
- const typeofA = typeof a;
- const typeofB = typeof b;
- const orders = [
- 'undefined',
- 'object',
- 'boolean',
- 'number',
- 'string',
- ];
- if (typeofA !== typeofB) {
- return orders.indexOf(typeofA) - orders.indexOf(typeofB);
- }
- if (typeofA !== 'string') {
- return Number(a) - Number(b);
- }
- return a.localeCompare(b);
- }
- exports.comparePrimitive = comparePrimitive;
- function normalizeDefaultResult(result) {
- return result === undefined ? {} : result;
- }
- exports.normalizeDefaultResult = normalizeDefaultResult;
- function normalizeValidateResult(result, value) {
- return result === true ? true : result === false ? { value } : result;
- }
- exports.normalizeValidateResult = normalizeValidateResult;
- function normalizeDeprecatedResult(result, value, doNotNormalizeTrue = false) {
- return result === false
- ? false
- : result === true
- ? doNotNormalizeTrue
- ? true
- : [{ value }]
- : 'value' in result
- ? [result]
- : result.length === 0
- ? false
- : result;
- }
- exports.normalizeDeprecatedResult = normalizeDeprecatedResult;
- function normalizeTransferResult(result, value) {
- return typeof result === 'string' || 'key' in result
- ? { from: value, to: result }
- : 'from' in result
- ? { from: result.from, to: result.to }
- : { from: value, to: result.to };
- }
- exports.normalizeTransferResult = normalizeTransferResult;
- function normalizeForwardResult(result, value) {
- return result === undefined
- ? []
- : Array.isArray(result)
- ? result.map(transferResult => normalizeTransferResult(transferResult, value))
- : [normalizeTransferResult(result, value)];
- }
- exports.normalizeForwardResult = normalizeForwardResult;
- function normalizeRedirectResult(result, value) {
- const redirect = normalizeForwardResult(typeof result === 'object' && 'redirect' in result
- ? result.redirect
- : result, value);
- return redirect.length === 0
- ? { remain: value, redirect }
- : typeof result === 'object' && 'remain' in result
- ? { remain: result.remain, redirect }
- : { redirect };
- }
- exports.normalizeRedirectResult = normalizeRedirectResult;
|