randex 0.33.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.
@@ -4,20 +4,21 @@ on:
4
4
  push:
5
5
  branches: [master]
6
6
 
7
+ permissions:
8
+ id-token: write
9
+ contents: read
10
+
7
11
  jobs:
8
12
  publish:
9
13
  runs-on: ubuntu-latest
10
14
  steps:
11
- - uses: actions/checkout@v2
12
- # Setup .npmrc file to publish to npm
13
- - uses: actions/setup-node@v2
14
- with:
15
- node-version: '18.x'
16
- registry-url: 'https://registry.npmjs.org'
17
- - run: npm install
18
- - run: npm run lint
19
- - run: npm run test
20
- - run: npm run build
21
- - run: npm publish
22
- env:
23
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
15
+ - uses: actions/checkout@v4
16
+ - uses: actions/setup-node@v4
17
+ with:
18
+ node-version: '24.x'
19
+ registry-url: 'https://registry.npmjs.org'
20
+ - run: npm install
21
+ - run: npm run lint
22
+ - run: npm run test
23
+ - run: npm run build
24
+ - run: npm publish
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Randex } from "./custom";
2
2
  export * from "./interfaces";
3
3
  export * from "./custom";
4
+ export * from "./utils";
4
5
  export default Randex;
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,7 @@
1
+ export declare class RandexPicker {
2
+ readonly items: any[];
3
+ private readonly _items;
4
+ private _tempItems;
5
+ constructor(items?: any[]);
6
+ pick(count?: number): any[];
7
+ }
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "randex",
3
- "version": "0.33.0",
3
+ "version": "0.35.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",