From b7a88ced709e1b84834364340102353c49cfa8f2 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Tue, 24 Oct 2023 21:40:34 +0200 Subject: [PATCH] Docs: improve documentation of duplicates and UIEventSource --- src/Logic/UIEventSource.ts | 2 +- src/Utils.ts | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Logic/UIEventSource.ts b/src/Logic/UIEventSource.ts index 5beca660df..35f0f0ef85 100644 --- a/src/Logic/UIEventSource.ts +++ b/src/Logic/UIEventSource.ts @@ -669,7 +669,7 @@ export class UIEventSource extends Store implements Writable { ) } - static asBoolean(stringUIEventSource: UIEventSource) { + static asBoolean(stringUIEventSource: UIEventSource): UIEventSource { return stringUIEventSource.sync( (str) => str === "true", [], diff --git a/src/Utils.ts b/src/Utils.ts index 180c727cbc..25da56512a 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -387,19 +387,27 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be return newArr } + /** + * 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 newArr = [] const seen = new Set() + const duplicates = new Set() for (const string of arr) { if (seen.has(string)) { - newArr.push(string) + duplicates.add(string) } seen.add(string) } - return newArr + return Array.from(duplicates) } /**