UX: fix #2416, small code cleanup

This commit is contained in:
Pieter Vander Vennet 2025-05-13 16:13:05 +02:00
parent a4fe5a11ed
commit 77afd8b426
8 changed files with 57 additions and 66 deletions

View file

@ -175,10 +175,10 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
}
public static NoNull<T>(array: ReadonlyArray<T> | undefined): T[] | undefined
public static NoNull<T>(array: undefined): undefined
public static NoNull(array: undefined): undefined
public static NoNull<T>(array: ReadonlyArray<T>): T[]
public static NoNull<T>(array: ReadonlyArray<T>): NonNullable<T>[] {
return <any>array?.filter((o) => o !== undefined && o !== null)
return <NonNullable<T>[]><unknown>array?.filter((o) => o !== undefined && o !== null)
}
public static Hist(array: ReadonlyArray<string>): Map<string, number> {
@ -332,7 +332,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
* @param toKey
* @constructor
*/
public static DedupOnId<T = { id: string }>(arr: T[], toKey?: (t: T) => string): T[] {
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) {
@ -342,10 +342,21 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
if (!img) {
continue
}
const k = toKey(img)
if (!seen.has(k)) {
seen.add(k)
uniq.push(img)
const ks = toKey(img)
if (typeof ks === "string") {
if (!seen.has(ks)) {
seen.add(ks)
uniq.push(img)
}
} else {
const ksNoNull = Utils.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
@ -1657,7 +1668,7 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
return obj
}
const newObj = {}
for (let objKey in obj) {
for (const objKey in obj) {
let cleanKey = objKey
if (objKey.startsWith("+") || objKey.startsWith("=")) {
cleanKey = objKey.substring(1)
@ -1794,19 +1805,6 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
href = href.replaceAll(/ /g, "%20")
return href
}
/** Randomize array in-place using Durstenfeld shuffle algorithm
* Source: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
* */
static shuffle(array: any[]) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
const temp = array[i]
array[i] = array[j]
array[j] = temp
}
}
private static emojiRegex = /[\p{Extended_Pictographic}🛰]/u
/**
@ -1826,4 +1824,8 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
public static isEmojiFlag(string: string) {
return /[🇦-🇿]{2}/u.test(string) // flags, see https://stackoverflow.com/questions/53360006/detect-with-regex-if-emoji-is-country-flag
}
public static concat<T>(param: T[][]): T[] {
return [].concat(...param)
}
}