UNPKG

@barchart/common-js

Version:
293 lines (291 loc) 9.24 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var array_exports = {}; __export(array_exports, { batchBy: () => batchBy, binarySearch: () => binarySearch, difference: () => difference, differenceBy: () => differenceBy, differenceSymmetric: () => differenceSymmetric, differenceSymmetricBy: () => differenceSymmetricBy, dropLeft: () => dropLeft, dropRight: () => dropRight, first: () => first, flatten: () => flatten, groupBy: () => groupBy, indexBy: () => indexBy, insert: () => insert, intersection: () => intersection, intersectionBy: () => intersectionBy, last: () => last, partition: () => partition, remove: () => remove, union: () => union, unionBy: () => unionBy, unique: () => unique, uniqueBy: () => uniqueBy }); module.exports = __toCommonJS(array_exports); var assert = __toESM(require("./assert.js")); var is = __toESM(require("./is.js")); function unique(a) { assert.argumentIsArray(a, "a"); return uniqueBy(a, (item) => item); } function uniqueBy(a, keySelector) { assert.argumentIsArray(a, "a"); return a.filter((item, index, array) => { const key = keySelector(item); return array.findIndex((candidate) => key === keySelector(candidate)) === index; }); } function groupBy(a, keySelector) { assert.argumentIsArray(a, "a"); assert.argumentIsRequired(keySelector, "keySelector", Function); return a.reduce((groups, item) => { const key = keySelector(item); if (!Object.prototype.hasOwnProperty.call(groups, key)) { groups[key] = []; } groups[key].push(item); return groups; }, {}); } function batchBy(a, keySelector) { assert.argumentIsArray(a, "a"); assert.argumentIsRequired(keySelector, "keySelector", Function); let currentKey = null; let currentBatch = null; return a.reduce((batches, item) => { const key = keySelector(item); if (currentBatch === null || currentKey !== key) { currentKey = key; currentBatch = []; batches.push(currentBatch); } currentBatch.push(item); return batches; }, []); } function indexBy(a, keySelector) { assert.argumentIsArray(a, "a"); assert.argumentIsRequired(keySelector, "keySelector", Function); return a.reduce((map, item) => { const key = keySelector(item); if (Object.prototype.hasOwnProperty.call(map, key)) { throw new Error("Unable to index array. A duplicate key exists."); } map[key] = item; return map; }, {}); } function dropLeft(a) { assert.argumentIsArray(a, "a"); let returnRef = Array.from(a); if (returnRef.length !== 0) { returnRef.shift(); } return returnRef; } function dropRight(a) { assert.argumentIsArray(a, "a"); let returnRef = Array.from(a); if (returnRef.length !== 0) { returnRef.pop(); } return returnRef; } function first(a) { assert.argumentIsArray(a, "a"); let returnRef; if (a.length !== 0) { returnRef = a[0]; } else { returnRef = void 0; } return returnRef; } function last(a) { assert.argumentIsArray(a, "a"); let returnRef; if (a.length !== 0) { returnRef = a[a.length - 1]; } else { returnRef = void 0; } return returnRef; } function flatten(a, recursive) { assert.argumentIsArray(a, "a"); assert.argumentIsOptional(recursive, "recursive", Boolean); const empty = []; let flat = empty.concat.apply(empty, a); if (recursive && flat.some((x) => is.array(x))) { flat = flatten(flat, true); } return flat; } function partition(a, size) { assert.argumentIsArray(a, "a"); assert.argumentIsOptional(size, "size", Number); const copy = a.slice(0); const partitions = []; while (copy.length !== 0) { partitions.push(copy.splice(0, size)); } return partitions; } function difference(a, b) { return differenceBy(a, b, (item) => item); } function differenceBy(a, b, keySelector) { assert.argumentIsArray(a, "a"); assert.argumentIsArray(b, "b"); assert.argumentIsRequired(keySelector, "keySelector", Function); const returnRef = []; a.forEach((candidate) => { const candidateKey = keySelector(candidate); const exclude = b.some((comparison) => candidateKey === keySelector(comparison)); if (!exclude) { returnRef.push(candidate); } }); return returnRef; } function differenceSymmetric(a, b) { return differenceSymmetricBy(a, b, (item) => item); } function differenceSymmetricBy(a, b, keySelector) { return unionBy(differenceBy(a, b, keySelector), differenceBy(b, a, keySelector), keySelector); } function union(a, b) { return unionBy(a, b, (item) => item); } function unionBy(a, b, keySelector) { assert.argumentIsArray(a, "a"); assert.argumentIsArray(b, "b"); assert.argumentIsRequired(keySelector, "keySelector", Function); const returnRef = a.slice(); b.forEach((candidate) => { const candidateKey = keySelector(candidate); const exclude = returnRef.some((comparison) => candidateKey === keySelector(comparison)); if (!exclude) { returnRef.push(candidate); } }); return returnRef; } function intersection(a, b) { return intersectionBy(a, b, (item) => item); } function intersectionBy(a, b, keySelector) { assert.argumentIsArray(a, "a"); assert.argumentIsArray(b, "b"); const returnRef = []; a.forEach((candidate) => { const candidateKey = keySelector(candidate); const include = b.some((comparison) => candidateKey === keySelector(comparison)); if (include) { returnRef.push(candidate); } }); return returnRef; } function remove(a, predicate) { assert.argumentIsArray(a, "a"); assert.argumentIsRequired(predicate, "predicate", Function); const index = a.findIndex(predicate); const found = !(index < 0); if (found) { a.splice(index, 1); } return found; } function insert(a, item, comparator) { assert.argumentIsArray(a, "a"); assert.argumentIsRequired(comparator, "comparator", Function); if (a.length === 0 || !(comparator(item, a[a.length - 1]) < 0)) { a.push(item); } else if (comparator(item, a[0]) < 0) { a.unshift(item); } else { a.splice(binarySearchForInsert(a, item, comparator, 0, a.length - 1), 0, item); } return a; } function binarySearch(a, key, comparator, start, end) { assert.argumentIsArray(a, "a"); assert.argumentIsRequired(comparator, "comparator", Function); assert.argumentIsOptional(start, "start", Number); assert.argumentIsOptional(end, "end", Number); if (a.length === 0) { return null; } return binarySearchForMatch(a, key, comparator, start || 0, end || a.length - 1); } function binarySearchForMatch(a, key, comparator, start, end) { const size = end - start; const midpointIndex = start + Math.floor(size / 2); const midpointItem = a[midpointIndex]; const comparison = comparator(key, midpointItem); if (comparison === 0) { return midpointItem; } else if (size < 2) { const finalIndex = a.length - 1; const finalItem = a[finalIndex]; if (end === finalIndex && comparator(key, finalItem) === 0) { return finalItem; } else { return null; } } else if (comparison > 0) { return binarySearchForMatch(a, key, comparator, midpointIndex, end); } else { return binarySearchForMatch(a, key, comparator, start, midpointIndex); } } function binarySearchForInsert(a, item, comparator, start, end) { const size = end - start; const midpointIndex = start + Math.floor(size / 2); const midpointItem = a[midpointIndex]; const comparison = comparator(item, midpointItem); if (size < 2) { if (comparison > 0) { const finalIndex = a.length - 1; if (end === finalIndex && comparator(item, a[finalIndex]) > 0) { return end + 1; } return end; } return start; } if (comparison > 0) { return binarySearchForInsert(a, item, comparator, midpointIndex, end); } return binarySearchForInsert(a, item, comparator, start, midpointIndex); }