randex 0.14.0 → 0.16.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/README.md +5 -0
- package/lib/custom/number.d.ts +5 -1
- package/lib/custom/number.js +30 -5
- package/lib/test/number.test.js +28 -2
- package/package.json +1 -1
- package/todo +4 -0
package/README.md
CHANGED
|
@@ -315,6 +315,11 @@ randomNumber(3);
|
|
|
315
315
|
randomNumber([3, 7]);
|
|
316
316
|
```
|
|
317
317
|
|
|
318
|
+
Options:
|
|
319
|
+
| Name | Type| Description|
|
|
320
|
+
| ------------------------ | ---- |------------ |
|
|
321
|
+
|decimals| `number` | Number of decimal digits |
|
|
322
|
+
|
|
318
323
|
## randomNumberArray
|
|
319
324
|
|
|
320
325
|
Generates random array with values from a range.
|
package/lib/custom/number.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import { RandexLength } from "../interfaces";
|
|
2
|
-
export
|
|
2
|
+
export interface RandomNumberOptions {
|
|
3
|
+
decimals?: number;
|
|
4
|
+
}
|
|
5
|
+
export declare function randomNumber(length: RandexLength, options?: RandomNumberOptions): number;
|
|
6
|
+
export declare function randomNumber(from: number, to: number, options?: RandomNumberOptions): number;
|
package/lib/custom/number.js
CHANGED
|
@@ -2,13 +2,38 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.randomNumber = void 0;
|
|
4
4
|
var set_1 = require("../basic/set");
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
function getBase(decimals) {
|
|
6
|
+
return decimals === 1 ? 10 : Math.pow(10, decimals);
|
|
7
|
+
}
|
|
8
|
+
function getValue(value, decimals) {
|
|
9
|
+
var result = value;
|
|
10
|
+
if (typeof decimals === "number") {
|
|
11
|
+
result = result * getBase(decimals);
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
function innerRandomNumber(from, to, decimals) {
|
|
16
|
+
var fromV = getValue(from, decimals);
|
|
17
|
+
var toV = getValue(to, decimals);
|
|
18
|
+
var result = set_1.RandexSetUtil.randomNumber(toV - fromV + 1) + fromV;
|
|
19
|
+
if (typeof decimals === "number") {
|
|
20
|
+
result = result / getBase(decimals);
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
function randomNumber(p1, p2, p3) {
|
|
25
|
+
if (typeof p1 === "number" && typeof p2 === "number") {
|
|
26
|
+
return innerRandomNumber(p1, p2, p3 === null || p3 === void 0 ? void 0 : p3.decimals);
|
|
8
27
|
}
|
|
9
28
|
else {
|
|
10
|
-
var
|
|
11
|
-
|
|
29
|
+
var decimals = p2 === null || p2 === void 0 ? void 0 : p2.decimals;
|
|
30
|
+
if (typeof p1 === "number") {
|
|
31
|
+
return innerRandomNumber(0, p1, decimals);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
var min = p1[0], max = p1[1];
|
|
35
|
+
return innerRandomNumber(min, max, decimals);
|
|
36
|
+
}
|
|
12
37
|
}
|
|
13
38
|
}
|
|
14
39
|
exports.randomNumber = randomNumber;
|
package/lib/test/number.test.js
CHANGED
|
@@ -7,9 +7,10 @@ function testNumber(max) {
|
|
|
7
7
|
expect(value).toBeGreaterThanOrEqual(0);
|
|
8
8
|
expect(value).toBeLessThanOrEqual(max);
|
|
9
9
|
}
|
|
10
|
-
function testNumberWithArray(range) {
|
|
10
|
+
function testNumberWithArray(range, two) {
|
|
11
|
+
if (two === void 0) { two = false; }
|
|
11
12
|
var min = range[0], max = range[1];
|
|
12
|
-
var value = (0, number_1.randomNumber)(range);
|
|
13
|
+
var value = two ? (0, number_1.randomNumber)(range) : (0, number_1.randomNumber)(min, max);
|
|
13
14
|
expect(value).toBeGreaterThanOrEqual(min);
|
|
14
15
|
expect(value).toBeLessThanOrEqual(max);
|
|
15
16
|
}
|
|
@@ -35,4 +36,29 @@ describe("randomNumber", function () {
|
|
|
35
36
|
testNumberWithArray([10, 15]);
|
|
36
37
|
});
|
|
37
38
|
});
|
|
39
|
+
it("param: two numbers", function () {
|
|
40
|
+
test_util_1.TestUtil.loop(function () {
|
|
41
|
+
testNumberWithArray([10, 15], true);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
it("two numbers with decimals", function () {
|
|
45
|
+
var count = 0;
|
|
46
|
+
test_util_1.TestUtil.loop(function () {
|
|
47
|
+
var value = (0, number_1.randomNumber)(1, 10, { decimals: 2 });
|
|
48
|
+
if (/\.\d{2}$/g.test(value.toString())) {
|
|
49
|
+
count++;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
expect(count).toBeGreaterThan(1);
|
|
53
|
+
});
|
|
54
|
+
it("a number with decimals", function () {
|
|
55
|
+
var count = 0;
|
|
56
|
+
test_util_1.TestUtil.loop(function () {
|
|
57
|
+
var value = (0, number_1.randomNumber)(10, { decimals: 2 });
|
|
58
|
+
if (/\.\d{2}$/g.test(value.toString())) {
|
|
59
|
+
count++;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
expect(count).toBeGreaterThan(1);
|
|
63
|
+
});
|
|
38
64
|
});
|
package/package.json
CHANGED
package/todo
ADDED