Przeglądaj źródła

Add ArrayIsOnlyDuplicates function

Added a function to check if an array contains only one unique value.
ApisNecros 1 rok temu
rodzic
commit
85373d08ec
1 zmienionych plików z 15 dodań i 0 usunięć
  1. 15 0
      common.ts

+ 15 - 0
common.ts

@@ -49,4 +49,19 @@ export function DeepClone(value: any): any {
  */
 export function ExtractNumbers(str: string): number[] {
 	return str.split(/[^\-0-9]/).filter((n) => n.length).map((n) => Number(n));
+}
+
+/**
+ * Check if an array only contains one unique value
+ *
+ * @see https://stackoverflow.com/a/14438954/2708601
+ * @param arr An array of any type
+ * @returns Whether the array only contains duplicates or not
+ */
+export function ArrayIsOnlyDuplicates(arr: Array<any>): boolean {
+	if (arr.length === 1) { return true; }
+
+	return arr.filter((val, idx, array) => {
+		return array.indexOf(val) === idx;
+	}).length == 1;
 }