Browse Source

Add ExtractNumbers function

ApisNecros 1 year ago
parent
commit
af7eb178cf
1 changed files with 14 additions and 0 deletions
  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));
 }