UNPKG

minixt

Version:

A tiny fork of Quxt with zero dependencies

284 lines (283 loc) 6.34 kB
// src/index.ts function matches(rule2) { return (object2) => { let match2 = true; for (const [key2, value2] of Object.entries(rule2)) { if (object2[key2] !== value2) { match2 = false; } } return match2; }; } function matchesProperty(key2, value2) { return (object2) => object2[key2] === value2; } function property(key2) { return (object2) => object2[key2]; } function range(start, end, step = 1) { const list = []; if (start > end) { step *= -1; for (let i = start; i > end; i += step) { list.push(i); } } else { for (let i = start; i < end; i += step) { list.push(i); } } return list; } function exclude(...lists) { let all = []; for (const list of lists) { all = all.concat(list); } const track = []; const common = []; for (const item of all) { if (track.includes(item)) common.push(item); else track.push(item); } return all.filter((x) => !common.includes(x)); } function filter(list, fun) { return list.filter(fun); } function intersect(...lists) { let all = []; for (const list of lists) { all = all.concat(list); } return all.filter((x) => { let common = true; for (const list of lists) { if (!list.includes(x)) common = false; } return common; }); } function partition(list, rule2) { if (typeof rule2 === "function") { const yes = list.filter(rule2); const no = list.filter((x) => !rule2(x)); return [yes, no]; } if (Array.isArray(rule2)) { const yes = list.filter((x) => matchesProperty(rule2[0], rule2[1])(x)); const no = list.filter((x) => !matchesProperty(rule2[0], rule2[1])(x)); return [yes, no]; } if (typeof rule2 === "object") { const yes = list.filter((x) => matches(rule2)(x)); const no = list.filter((x) => !matches(rule2)(x)); return [yes, no]; } if (typeof rule2 === "string") { const yes = list.filter((x) => property(rule2)(x)); const no = list.filter((x) => !property(rule2)(x)); return [yes, no]; } throw new TypeError("Rule is an not array, number, function, object, or string"); } function factorial(input) { let n = 1; for (let i = 2; i <= input; i += 1) { n *= i; } return n; } function fibonacci(n) { if (n === 0 || n === 1) { return n; } return fibonacci(n - 1) + fibonacci(n - 2); } function must(rule) { return (object) => { let match = true; for (const [key, value] of Object.entries(rule)) { if (typeof value === "string") { if (object[key] !== value) { match = false; } } else if (Array.isArray(value) && value.length > 1) { let str = ""; const con = value.shift(); value.forEach((item, index) => { if (index === 0) { str += `object[key]${item}`; } else { str += `${con}object[key]${item}`; } }); if (!eval(str)) { match = false; } } else if (typeof value === "boolean") { if (object[key] !== value) { match = false; } } } return match; }; } function factor(d, nums) { for (const num of nums) { if (num % d !== 0) { return false; } } return true; } function frequency(data) { const chart = /* @__PURE__ */ new Map(); for (const item of data) { const value2 = chart.get(item); if (value2) { chart.set(item, value2 + 1); } else { chart.set(item, 1); } } return chart; } function factors(n) { const list = []; for (const i of range(2, Math.floor(Math.sqrt(n)) + 1)) { if (n % i === 0) { list.push(i); list.push(n / i); } } return list; } function gcd(a, b) { if (b) return gcd(b, a % b); return Math.abs(a); } function hcf(...nums) { let p = null; for (const i of range(Math.min(...nums), Math.floor(Math.sqrt(Math.min(...nums))) - 1)) { if (factor(i, nums)) { return i; } if (factor(Math.min(...nums) / i, nums)) { p = Math.min(...nums) / i; } } return p != null ? p : 1; } function prime(n) { for (const i of range(2, Math.floor(Math.sqrt(n)) + 1)) { if (n % i === 0) { return false; } } return true; } function mergeSort(list) { function merge(left, right) { const list2 = []; while (left.length && right.length) { if (left[0] < right[0]) { list2.push(left.shift()); } else { list2.push(right.shift()); } } return [...list, ...left, ...right]; } function sort(list2, half = list.length / 2) { if (list2.length < 2) { return list2; } const left = list2.splice(0, half); return merge(sort(left), sort(list2)); } return sort(list); } function mode(data) { const chart = frequency(data); const max = Math.max(...chart.values()); return [...chart.keys()].filter((x) => chart.get(x) === max); } function quickSort(items) { const { length } = items; if (length <= 1) { return items; } const pivot = items[0]; const greater = []; const less = []; for (let i = 1; i < length; i += 1) { if (items[i] > pivot) { greater.push(items[i]); } else { less.push(items[i]); } } const sorted = [...quickSort(less), pivot, ...quickSort(greater)]; return sorted; } function shellSort(items) { let interval = 1; while (interval < items.length / 3) { interval = interval * 3 + 1; } while (interval > 0) { for (let outer = interval; outer < items.length; outer += 1) { const value2 = items[outer]; let inner = outer; while (inner > interval - 1 && items[inner - interval] >= value2) { items[inner] = items[inner - interval]; inner -= interval; } items[inner] = value2; } interval = (interval - 1) / 3; } return items; } function zip(...lists) { const zipped = []; for (let i = 0; i < lists[0].length; i += 1) { const item = []; for (const list of lists) { item.push(list[i]); } zipped.push(item); } return zipped; } export { exclude, factor, factorial, factors, fibonacci, filter, frequency, gcd, hcf, intersect, matches, matchesProperty, mergeSort, mode, must, partition, prime, property, quickSort, range, shellSort, zip };