Refactoring: move methods out of 'utils.ts'

This commit is contained in:
Pieter Vander Vennet 2025-08-27 00:05:14 +02:00
parent d6b56f4eb3
commit 94fbf1eccf
16 changed files with 140 additions and 136 deletions

View file

@ -1378,66 +1378,6 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
}
element.scrollIntoView({ behavior: "smooth", block: "nearest" })
}
/**
* Returns true if the contents of `a` are the same (and in the same order) as `b`.
* Might have false negatives in some cases
* @param a
* @param b
*/
public static sameList<T>(a: ReadonlyArray<T>, b: ReadonlyArray<T>) {
if (a == b) {
return true
}
if (a === undefined || a === null || b === undefined || b === null) {
return false
}
if (a.length !== b.length) {
return false
}
for (let i = 0; i < a.length; i++) {
const ai = a[i]
const bi = b[i]
if (ai == bi) {
continue
}
if (ai === bi) {
continue
}
return false
}
return true
}
public static SameObject<T>(a: T, b: T, ignoreKeys?: string[]): boolean {
if (a === b) {
return true
}
if (a === undefined || a === null || b === null || b === undefined) {
return false
}
if (typeof a === "object" && typeof b === "object") {
for (const aKey in a) {
if (!(aKey in b)) {
return false
}
}
for (const bKey in b) {
if (!(bKey in a)) {
return false
}
}
for (const k in a) {
if (!Utils.SameObject(a[k], b[k])) {
return false
}
}
return true
}
return false
}
/**
*
* Utils.splitIntoSubstitutionParts("abc") // => [{message: "abc"}]
@ -1510,45 +1450,6 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
filename: path.substring(path.lastIndexOf("/") + 1),
}
}
/**
* Removes accents from a string
* @param str
* @constructor
*
* Utils.RemoveDiacritics("bâtiments") // => "batiments"
* Utils.RemoveDiacritics(undefined) // => undefined
*/
public static RemoveDiacritics(str?: string): string {
// See #1729
if (!str) {
return str
}
return str.normalize("NFD").replace(/\p{Diacritic}/gu, "")
}
/**
* Simplifies a string to increase the chance of a match
* @param str
* Utils.simplifyStringForSearch("abc def; ghi 564") // => "abcdefghi564"
* Utils.simplifyStringForSearch("âbc déf; ghi 564") // => "abcdefghi564"
* Utils.simplifyStringForSearch(undefined) // => undefined
*/
public static simplifyStringForSearch(str: string): string {
return Utils.RemoveDiacritics(str)
?.toLowerCase()
?.replace(/[^a-z0-9]/g, "")
}
public static randomString(length: number): string {
let result = ""
for (let i = 0; i < length; i++) {
const chr = Math.random().toString(36).substr(2, 3)
result += chr
}
return result
}
/**
* Recursively rewrites all keys from `+key`, `key+` and `=key` into `key
*