forked from MapComplete/MapComplete
Refactoring: move functions out of Utils
This commit is contained in:
parent
442f5a2923
commit
5ea2414204
16 changed files with 126 additions and 145 deletions
101
src/Utils.ts
101
src/Utils.ts
|
@ -252,99 +252,6 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
|
|||
}
|
||||
return str
|
||||
}
|
||||
public static DedupT<T>(arr: T[]): T[] {
|
||||
if (!arr) {
|
||||
return arr
|
||||
}
|
||||
const items = []
|
||||
for (const item of arr) {
|
||||
if (items.indexOf(item) < 0) {
|
||||
items.push(item)
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
/**
|
||||
* Deduplicates the given array based on some ID-properties.
|
||||
* Removes all falsey values
|
||||
* @param arr
|
||||
* @param toKey
|
||||
* @constructor
|
||||
*/
|
||||
public static DedupOnId<T = { id: string }>(
|
||||
arr: T[],
|
||||
toKey?: (t: T) => string | string[]
|
||||
): T[] {
|
||||
const uniq: T[] = []
|
||||
const seen = new Set<string>()
|
||||
if (toKey === undefined) {
|
||||
toKey = (item) => item["id"]
|
||||
}
|
||||
for (const img of arr) {
|
||||
if (!img) {
|
||||
continue
|
||||
}
|
||||
const ks = toKey(img)
|
||||
if (typeof ks === "string") {
|
||||
if (!seen.has(ks)) {
|
||||
seen.add(ks)
|
||||
uniq.push(img)
|
||||
}
|
||||
} else if (ks) {
|
||||
const ksNoNull = Lists.noNull(ks)
|
||||
const hasBeenSeen = ksNoNull.some((k) => seen.has(k))
|
||||
if (!hasBeenSeen) {
|
||||
uniq.push(img)
|
||||
}
|
||||
for (const k of ksNoNull) {
|
||||
seen.add(k)
|
||||
}
|
||||
}
|
||||
}
|
||||
return uniq
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all duplicates in a list of strings
|
||||
*
|
||||
* Utils.Duplicates(["a", "b", "c"]) // => []
|
||||
* Utils.Duplicates(["a", "b","c","b"] // => ["b"]
|
||||
* Utils.Duplicates(["a", "b","c","b","b"] // => ["b"]
|
||||
*
|
||||
*/
|
||||
public static Duplicates(arr: string[]): string[] {
|
||||
if (arr === undefined) {
|
||||
return undefined
|
||||
}
|
||||
const seen = new Set<string>()
|
||||
const duplicates = new Set<string>()
|
||||
for (const string of arr) {
|
||||
if (seen.has(string)) {
|
||||
duplicates.add(string)
|
||||
}
|
||||
seen.add(string)
|
||||
}
|
||||
return Array.from(duplicates)
|
||||
}
|
||||
|
||||
/**
|
||||
* In the given list, all values which are lists will be merged with the values, e.g.
|
||||
*
|
||||
* Utils.Flatten([ [1,2], 3, [4, [5 ,6]] ]) // => [1, 2, 3, 4, [5, 6]]
|
||||
*/
|
||||
public static Flatten<T>(list: (T | T[])[]): T[] {
|
||||
const result = []
|
||||
for (const value of list) {
|
||||
if (Array.isArray(value)) {
|
||||
result.push(...value)
|
||||
} else {
|
||||
result.push(value)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Utils.Identical([1,2], [1,2]) // => true
|
||||
* Utils.Identical([1,2,3], [1,2,4}]) // => false
|
||||
|
@ -1787,14 +1694,6 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
|
|||
return href
|
||||
}
|
||||
|
||||
public static sum(list: number[]): number {
|
||||
let total = 0
|
||||
for (const number of list) {
|
||||
total += number
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* JSON.stringify(Utils.reorder({b: "0", a: "1"}, ["a", "b"])) // => '{"a":"1","b":"0"}'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue