@kottetall/random 0.0.2 → 0.0.3
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/constants/random.constant.d.ts +13 -0
- package/lib/constants/random.constant.d.ts.map +1 -0
- package/lib/constants/random.constant.js +40 -0
- package/lib/index.d.ts +45 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +69 -3
- package/lib/types/random.type.d.ts +4 -0
- package/lib/types/random.type.d.ts.map +1 -0
- package/lib/types/random.type.js +2 -0
- package/lib/types/utils.type.d.ts +3 -0
- package/lib/types/utils.type.d.ts.map +1 -0
- package/lib/types/utils.type.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const casing: {
|
|
2
|
+
readonly LOWER: "lower";
|
|
3
|
+
readonly UPPER: "upper";
|
|
4
|
+
};
|
|
5
|
+
export declare const booleanString: {
|
|
6
|
+
readonly TRUE_LOWERCASE: "true";
|
|
7
|
+
readonly FALSE_LOWERCASE: "false";
|
|
8
|
+
readonly TRUE_UPPERCASE: "TRUE";
|
|
9
|
+
readonly FALSE_UPPERCASE: "FALSE";
|
|
10
|
+
};
|
|
11
|
+
export declare const falsyValues: readonly [null, undefined, false, number, 0, 0, 0n, ""];
|
|
12
|
+
export declare const truthyValues: readonly [true, {}, readonly [], 42, "0", "false", Date, -42, 12n, 3.14, -3.14, number, number];
|
|
13
|
+
//# sourceMappingURL=random.constant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.constant.d.ts","sourceRoot":"","sources":["../../src/constants/random.constant.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM;;;CAGT,CAAC;AAEX,eAAO,MAAM,aAAa;;;;;CAKhB,CAAC;AAEX,eAAO,MAAM,WAAW,yDAUd,CAAC;AAEX,eAAO,MAAM,YAAY,iGAef,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.truthyValues = exports.falsyValues = exports.booleanString = exports.casing = void 0;
|
|
4
|
+
exports.casing = {
|
|
5
|
+
LOWER: "lower",
|
|
6
|
+
UPPER: "upper",
|
|
7
|
+
};
|
|
8
|
+
exports.booleanString = {
|
|
9
|
+
TRUE_LOWERCASE: "true",
|
|
10
|
+
FALSE_LOWERCASE: "false",
|
|
11
|
+
TRUE_UPPERCASE: "TRUE",
|
|
12
|
+
FALSE_UPPERCASE: "FALSE",
|
|
13
|
+
};
|
|
14
|
+
exports.falsyValues = [
|
|
15
|
+
// Based on https://developer.mozilla.org/en-US/docs/Glossary/Falsy
|
|
16
|
+
null,
|
|
17
|
+
undefined,
|
|
18
|
+
false,
|
|
19
|
+
NaN,
|
|
20
|
+
0,
|
|
21
|
+
-0,
|
|
22
|
+
0n,
|
|
23
|
+
"",
|
|
24
|
+
];
|
|
25
|
+
exports.truthyValues = [
|
|
26
|
+
// Based on https://developer.mozilla.org/en-US/docs/Glossary/Truthy
|
|
27
|
+
true,
|
|
28
|
+
{},
|
|
29
|
+
[],
|
|
30
|
+
42,
|
|
31
|
+
"0",
|
|
32
|
+
"false",
|
|
33
|
+
new Date(),
|
|
34
|
+
-42,
|
|
35
|
+
12n,
|
|
36
|
+
3.14,
|
|
37
|
+
-3.14,
|
|
38
|
+
Infinity,
|
|
39
|
+
-Infinity,
|
|
40
|
+
];
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,51 @@
|
|
|
1
|
+
import { Casing } from "./types/random.type";
|
|
2
|
+
import { ObjectValues } from "./types/utils.type";
|
|
1
3
|
export declare class Random {
|
|
2
4
|
static intBetween(min: number, max: number): number;
|
|
3
5
|
static boolean(): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Returns "true" or "false" in lowercase unless casing has been specified
|
|
8
|
+
* @param booleanCasing
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
static booleanString(booleanCasing?: Casing): string;
|
|
12
|
+
/**
|
|
13
|
+
* Returns 1 or 0
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
static booleanInt(): 0 | 1;
|
|
17
|
+
/**
|
|
18
|
+
* Returns a falsy value
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
static falsy(): number | false | "" | 0n | null | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Returns a truthy value
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
static truthy(): number | true | "false" | readonly [] | "0" | Date | 12n | {};
|
|
27
|
+
/**
|
|
28
|
+
* Returns a value from the provided array
|
|
29
|
+
* @param source
|
|
30
|
+
* @returns
|
|
31
|
+
*/
|
|
4
32
|
static fromArray<T>(source: T[]): T;
|
|
5
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Returns a value from the provided object
|
|
35
|
+
* @param source
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
static fromObject<T extends Object>(source: T): ObjectValues<T>;
|
|
39
|
+
/**
|
|
40
|
+
* Returns a letter, can be either lowercase or uppercase unless specified
|
|
41
|
+
* @param letterCasing
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
static letter(letterCasing?: Casing): string;
|
|
45
|
+
/**
|
|
46
|
+
* A wrapper for crypto.randomUUID
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
static uuid(): `${string}-${string}-${string}-${string}-${string}`;
|
|
6
50
|
}
|
|
7
51
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,OAAO,EAAiB,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAqB,MAAM,oBAAoB,CAAC;AAErE,qBAAa,MAAM;IACjB,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IAY1C,MAAM,CAAC,OAAO;IAId;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,aAAa,CAAC,EAAE,MAAM;IAQ3C;;;OAGG;IACH,MAAM,CAAC,UAAU;IAIjB;;;OAGG;IACH,MAAM,CAAC,KAAK;IAIZ;;;OAGG;IACH,MAAM,CAAC,MAAM;IAIb;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,GAAG,CAAC;IAMnC;;;;OAIG;IACH,MAAM,CAAC,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC;IAK/D;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,MAAM;IAYnC;;;OAGG;IACH,MAAM,CAAC,IAAI;CAGZ"}
|
package/lib/index.js
CHANGED
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Random = void 0;
|
|
4
4
|
const chars_constant_1 = require("./constants/chars.constant");
|
|
5
|
+
const random_constant_1 = require("./constants/random.constant");
|
|
5
6
|
class Random {
|
|
6
7
|
static intBetween(min, max) {
|
|
7
8
|
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
|
|
9
|
+
if (max < min) {
|
|
10
|
+
const oldMin = min;
|
|
11
|
+
min = max;
|
|
12
|
+
max = oldMin;
|
|
13
|
+
}
|
|
8
14
|
const minCeiled = Math.ceil(min);
|
|
9
15
|
const maxFloored = Math.floor(max);
|
|
10
16
|
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled);
|
|
@@ -12,20 +18,80 @@ class Random {
|
|
|
12
18
|
static boolean() {
|
|
13
19
|
return Math.random() < 0.5;
|
|
14
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns "true" or "false" in lowercase unless casing has been specified
|
|
23
|
+
* @param booleanCasing
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
26
|
+
static booleanString(booleanCasing) {
|
|
27
|
+
if (!booleanCasing)
|
|
28
|
+
booleanCasing = random_constant_1.casing.LOWER;
|
|
29
|
+
const result = Random.boolean()
|
|
30
|
+
? random_constant_1.booleanString.TRUE_LOWERCASE
|
|
31
|
+
: random_constant_1.booleanString.FALSE_UPPERCASE;
|
|
32
|
+
return booleanCasing === random_constant_1.casing.LOWER ? result : result.toUpperCase();
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Returns 1 or 0
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
static booleanInt() {
|
|
39
|
+
return Random.boolean() ? 1 : 0;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Returns a falsy value
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
static falsy() {
|
|
46
|
+
return Random.fromArray([...random_constant_1.falsyValues]);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns a truthy value
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
static truthy() {
|
|
53
|
+
return Random.fromArray([...random_constant_1.truthyValues]);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns a value from the provided array
|
|
57
|
+
* @param source
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
15
60
|
static fromArray(source) {
|
|
16
61
|
const maxIndex = source.length - 1;
|
|
17
62
|
const randomIndex = Random.intBetween(0, maxIndex);
|
|
18
63
|
return source[randomIndex];
|
|
19
64
|
}
|
|
20
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Returns a value from the provided object
|
|
67
|
+
* @param source
|
|
68
|
+
* @returns
|
|
69
|
+
*/
|
|
70
|
+
static fromObject(source) {
|
|
71
|
+
const sourceValues = Object.values(source);
|
|
72
|
+
return Random.fromArray(sourceValues);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Returns a letter, can be either lowercase or uppercase unless specified
|
|
76
|
+
* @param letterCasing
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
static letter(letterCasing) {
|
|
21
80
|
const source = [];
|
|
22
|
-
if (
|
|
81
|
+
if (letterCasing === random_constant_1.casing.LOWER || !letterCasing) {
|
|
23
82
|
source.push(...chars_constant_1.alphabetLowercase);
|
|
24
83
|
}
|
|
25
|
-
if (
|
|
84
|
+
if (letterCasing === random_constant_1.casing.UPPER || !letterCasing) {
|
|
26
85
|
source.push(...chars_constant_1.alphabetUppercase);
|
|
27
86
|
}
|
|
28
87
|
return Random.fromArray(source);
|
|
29
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* A wrapper for crypto.randomUUID
|
|
91
|
+
* @returns
|
|
92
|
+
*/
|
|
93
|
+
static uuid() {
|
|
94
|
+
return crypto.randomUUID();
|
|
95
|
+
}
|
|
30
96
|
}
|
|
31
97
|
exports.Random = Random;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.type.d.ts","sourceRoot":"","sources":["../../src/types/random.type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAC;AAErE,MAAM,MAAM,MAAM,GAAG,CAAC,OAAO,MAAM,CAAC,CAAC,MAAM,OAAO,MAAM,CAAC,CAAC;AAE1D,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,OAAO,aAAa,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.type.d.ts","sourceRoot":"","sources":["../../src/types/utils.type.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAExD,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,MAAM,IAAI,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC"}
|