Skip to content

Commit 93cfb61

Browse files
committed
Add a method to create a unique list
1 parent 847e11c commit 93cfb61

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/ts/Utils.ts

+23
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,27 @@ class Utils {
2020
return arg1[Utils.random(0, arg1.length)];
2121
}
2222
}
23+
24+
/**
25+
* Creates an array of unique items
26+
* @param data The array of data to get elements from
27+
* @param amount The number of elements in the unique array
28+
* @param limit The max number of operations before the unique array is returned (default 1000)
29+
*/
30+
public static createUniqueList(data: any[], amount: number, limit?: number): any[] {
31+
const result: any[] = [];
32+
33+
let counter: number = 0;
34+
while (result.length < amount && counter < (limit || 100)) {
35+
const item: any = data[Utils.random(0, data.length)];
36+
37+
if (!result.includes(item)) {
38+
result.push(item);
39+
}
40+
41+
counter++;
42+
}
43+
44+
return result;
45+
}
2346
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"baseUrl": "./src/ts/",
55
"outFile": "./src/output/build.js",
66
"lib": [
7-
"es2015",
7+
"es2016",
88
"dom"
99
],
1010
"module": "system",

0 commit comments

Comments
 (0)