Переглянути джерело

Add ExtractNumbers function

ApisNecros 1 рік тому
батько
коміт
af7eb178cf
1 змінених файлів з 14 додано та 0 видалено
  1. 14 0
      common.ts

+ 14 - 0
common.ts

@@ -35,4 +35,18 @@ export function Inspect(value: any): void {
  */
 export function DeepClone(value: any): any {
 	return JSON.parse(JSON.stringify(value));
+}
+
+/**
+ * Extracts all numbers from a string
+ *
+ * If there were no numbers in the string, then an empty array is returned.
+ *
+ * Numbers in the output are in the order they appeared in in the input
+ *
+ * @param {string} str A string
+ * @returns {number[]} An array of numbers
+ */
+export function ExtractNumbers(str: string): number[] {
+	return str.split(/\D/).filter((n) => n.length).map((n) => Number(n));
 }