randex 0.17.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.randomArrayItem = void 0;
|
|
4
|
+
var number_1 = require("./number");
|
|
5
|
+
function randomArrayItem(array, defaultValue) {
|
|
6
|
+
if (array === null || array === void 0 ? void 0 : array.length) {
|
|
7
|
+
var arrayCount = array.length - 1;
|
|
8
|
+
var index = (0, number_1.randomNumber)(0, arrayCount);
|
|
9
|
+
return array[index];
|
|
10
|
+
}
|
|
11
|
+
else if (defaultValue) {
|
|
12
|
+
return defaultValue;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
throw "Array is empty";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.randomArrayItem = randomArrayItem;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var array_item_1 = require("../custom/array-item");
|
|
4
|
+
var test_util_1 = require("./test-util");
|
|
5
|
+
describe("randomArrayItem", function () {
|
|
6
|
+
it("param: number", function () {
|
|
7
|
+
var array = [1, 2, 3, 4, 5, 6];
|
|
8
|
+
var resultArray = {};
|
|
9
|
+
test_util_1.TestUtil.loop(function () {
|
|
10
|
+
var value = (0, array_item_1.randomArrayItem)(array);
|
|
11
|
+
resultArray[value] = (resultArray[value] || 0) + 1;
|
|
12
|
+
});
|
|
13
|
+
for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
|
|
14
|
+
var item = array_1[_i];
|
|
15
|
+
expect(resultArray[item]).toBeTruthy();
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|