randex 0.34.0 → 0.35.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.
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/test/picker.test.d.ts +1 -0
- package/lib/test/picker.test.js +30 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +17 -0
- package/lib/utils/picker.d.ts +7 -0
- package/lib/utils/picker.js +39 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -17,5 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
var custom_1 = require("./custom");
|
|
18
18
|
__exportStar(require("./interfaces"), exports);
|
|
19
19
|
__exportStar(require("./custom"), exports);
|
|
20
|
+
__exportStar(require("./utils"), exports);
|
|
20
21
|
// eslint-disable-next-line import/no-default-export
|
|
21
22
|
exports.default = custom_1.Randex;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var set_1 = require("../basic/set");
|
|
4
|
+
var picker_1 = require("../utils/picker");
|
|
5
|
+
describe("RandexPicker", function () {
|
|
6
|
+
it("picks from temp items without replacement", function () {
|
|
7
|
+
var picker = new picker_1.RandexPicker(["a", "b", "c"]);
|
|
8
|
+
var randomSpy = jest.spyOn(set_1.RandexSetUtil, "randomSingleNumber");
|
|
9
|
+
randomSpy.mockReturnValueOnce(1).mockReturnValueOnce(0).mockReturnValueOnce(0);
|
|
10
|
+
expect(picker.pick(3)).toEqual(["b", "a", "c"]);
|
|
11
|
+
expect(picker._tempItems).toEqual([]);
|
|
12
|
+
randomSpy.mockRestore();
|
|
13
|
+
});
|
|
14
|
+
it("keeps refilling until it returns the requested count", function () {
|
|
15
|
+
var picker = new picker_1.RandexPicker([1, 2]);
|
|
16
|
+
var randomSpy = jest.spyOn(set_1.RandexSetUtil, "randomSingleNumber");
|
|
17
|
+
randomSpy.mockReturnValueOnce(0).mockReturnValueOnce(0).mockReturnValueOnce(1).mockReturnValueOnce(0).mockReturnValueOnce(0);
|
|
18
|
+
expect(picker.pick(5)).toEqual([1, 2, 2, 1, 1]);
|
|
19
|
+
expect(picker._tempItems).toEqual([2]);
|
|
20
|
+
randomSpy.mockRestore();
|
|
21
|
+
});
|
|
22
|
+
it("refills temp items until the requested count is reached", function () {
|
|
23
|
+
var picker = new picker_1.RandexPicker(["x", "y"]);
|
|
24
|
+
var randomSpy = jest.spyOn(set_1.RandexSetUtil, "randomSingleNumber");
|
|
25
|
+
randomSpy.mockReturnValueOnce(1).mockReturnValueOnce(0).mockReturnValueOnce(0);
|
|
26
|
+
expect(picker.pick(3)).toEqual(["y", "x", "x"]);
|
|
27
|
+
expect(picker._tempItems).toEqual(["y"]);
|
|
28
|
+
randomSpy.mockRestore();
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./picker";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./picker"), exports);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.RandexPicker = void 0;
|
|
13
|
+
var set_1 = require("../basic/set");
|
|
14
|
+
var RandexPicker = /** @class */ (function () {
|
|
15
|
+
function RandexPicker(items) {
|
|
16
|
+
if (items === void 0) { items = []; }
|
|
17
|
+
this.items = items;
|
|
18
|
+
this._items = items;
|
|
19
|
+
this._tempItems = __spreadArray([], items, true);
|
|
20
|
+
}
|
|
21
|
+
RandexPicker.prototype.pick = function (count) {
|
|
22
|
+
if (count === void 0) { count = 1; }
|
|
23
|
+
if (!this._items.length || count <= 0) {
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
26
|
+
var result = [];
|
|
27
|
+
for (var i = 0; i < count; i++) {
|
|
28
|
+
if (!this._tempItems.length) {
|
|
29
|
+
this._tempItems = __spreadArray([], this._items, true);
|
|
30
|
+
}
|
|
31
|
+
var index = set_1.RandexSetUtil.randomSingleNumber(this._tempItems.length);
|
|
32
|
+
var item = this._tempItems.splice(index, 1)[0];
|
|
33
|
+
result.push(item);
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
return RandexPicker;
|
|
38
|
+
}());
|
|
39
|
+
exports.RandexPicker = RandexPicker;
|