@kottetall/random 0.0.2 → 0.0.4

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,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
- static letter(casing: "ALL" | "UPPERCASE" | "LOWERCASE"): string;
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
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,qBAAa,MAAM;IACjB,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IAO1C,MAAM,CAAC,OAAO;IAId,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,GAAG,CAAC;IAMnC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,GAAG,WAAW,GAAG,WAAW;CAWxD"}
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
- static letter(casing) {
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 (casing === "LOWERCASE" || casing === "ALL") {
81
+ if (letterCasing === random_constant_1.casing.LOWER || !letterCasing) {
23
82
  source.push(...chars_constant_1.alphabetLowercase);
24
83
  }
25
- if (casing === "UPPERCASE" || casing === "ALL") {
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,4 @@
1
+ import { booleanString, casing } from "../constants/random.constant";
2
+ export type Casing = (typeof casing)[keyof typeof casing];
3
+ export type BooleanString = (typeof booleanString)[keyof typeof booleanString];
4
+ //# sourceMappingURL=random.type.d.ts.map
@@ -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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export type ObjectValues<T extends Object> = T[keyof T];
2
+ export type ObjectValuesArray<T extends Object> = ObjectValues<T>[];
3
+ //# sourceMappingURL=utils.type.d.ts.map
@@ -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"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kottetall/random",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -14,7 +14,20 @@
14
14
  "type": "git",
15
15
  "url": "git+https://github.com/kottetall/random.git"
16
16
  },
17
- "keywords": [],
17
+ "keywords": [
18
+ "random",
19
+ "random-utils",
20
+ "utility",
21
+ "utils",
22
+ "helpers",
23
+ "javascript",
24
+ "node",
25
+ "array",
26
+ "object",
27
+ "picker",
28
+ "random-picker",
29
+ "random-value"
30
+ ],
18
31
  "author": "",
19
32
  "license": "ISC",
20
33
  "type": "commonjs",
package/readme.md ADDED
@@ -0,0 +1,56 @@
1
+ # Random
2
+
3
+ A lightweight utility library for generating pseudo-random values in JavaScript based of `Math.random()`.
4
+
5
+ This package was originally created for personal use in my own projects, but it is published as an open tool that anyone can use. It focuses on simple, practical helpers for working with randomness, such as picking random values from arrays or objects.
6
+
7
+ ## Features
8
+
9
+ - Get a random value from an array
10
+ - Get a random value from an object
11
+ - Get a random letter
12
+ - Get a random truthy/falsy value
13
+ - And more...
14
+ - Simple and minimal API
15
+ - Zero dependencies
16
+ - Designed for everyday use in JavaScript and Node.js projects
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @kottetall/random
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ### Random value from an array
27
+
28
+ ```js
29
+ import { Random } from "@kottetall/random";
30
+
31
+ const items = ["apple", "banana", "orange"];
32
+ const result = Random.fromArray(items);
33
+
34
+ console.log(result);
35
+ ```
36
+
37
+ ### Random value from an object
38
+
39
+ ```js
40
+ import { Random } from "@kottetall/random";
41
+
42
+ const obj = {
43
+ a: 1,
44
+ b: 2,
45
+ c: 3,
46
+ };
47
+
48
+ const result = Random.fromObject(obj);
49
+
50
+ console.log(result);
51
+ ```
52
+
53
+ ## Why this package?
54
+
55
+ Sometimes you just need small, reusable helpers instead of rewriting the same logic in every project.
56
+ This package aims to provide clean and simple functions for common random-related tasks.