From c2dd832e5c538d95947a4534953f66b1e2acfc3d Mon Sep 17 00:00:00 2001 From: Peter Fajdiga Date: Fri, 25 Oct 2024 21:39:33 +0200 Subject: [PATCH] move functions from math.ts to collections.ts --- src/lib/utils/collections.ts | 30 ++++++++++++++++++++++++++++++ src/lib/utils/math.ts | 30 ------------------------------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/lib/utils/collections.ts b/src/lib/utils/collections.ts index 0d684eb..b91a118 100644 --- a/src/lib/utils/collections.ts +++ b/src/lib/utils/collections.ts @@ -1,3 +1,20 @@ +function union(array0: T[], array1: T[]) { + const set = new Set([...array0, ...array1]); + return [...set]; +} + +function uniq(sortedArray: any[]) { + const filtered = []; + let lastItem; + for (const item of sortedArray) { + if (item !== lastItem) { + filtered.push(item); + lastItem = item; + } + } + return filtered; +} + function mapGetOrInit(map: Map, key: K, defaultItem: V) { const item = map.get(key); if (item !== undefined) { @@ -7,3 +24,16 @@ function mapGetOrInit(map: Map, key: K, defaultItem: V) { return defaultItem; } } + +function findMinPositive(items: T[], evaluate: (item: T) => number) { + let bestScore = Infinity; + let bestItem = undefined; + for (const item of items) { + const score = evaluate(item); + if (score > 0 && score < bestScore) { + bestScore = score; + bestItem = item; + } + } + return bestItem; +} diff --git a/src/lib/utils/math.ts b/src/lib/utils/math.ts index 7a202aa..ea6b7fa 100644 --- a/src/lib/utils/math.ts +++ b/src/lib/utils/math.ts @@ -12,36 +12,6 @@ function sum(...list: number[]) { return list.reduce((acc, val) => acc + val); } -function union(array0: T[], array1: T[]) { - const set = new Set([...array0, ...array1]); - return [...set]; -} - -function uniq(sortedArray: T[]) { - const filtered = []; - let lastItem; - for (const item of sortedArray) { - if (item !== lastItem) { - filtered.push(item); - lastItem = item; - } - } - return filtered; -} - -function findMinPositive(items: T[], evaluate: (item: T) => number) { - let bestScore = Infinity; - let bestItem = undefined; - for (const item of items) { - const score = evaluate(item); - if (score > 0 && score < bestScore) { - bestScore = score; - bestItem = item; - } - } - return bestItem; -} - function rectEquals(a: QmlRect, b: QmlRect) { return a.x === b.x && a.y === b.y &&