@inglorious/utils 3.4.0 → 3.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/utils",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "A set of general-purpose utility functions designed with functional programming principles in mind.",
5
5
  "author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
package/src/math/rng.js CHANGED
@@ -16,20 +16,32 @@ export function choose(...values) {
16
16
  /**
17
17
  * Generates a random number.
18
18
  * - If no arguments are provided, returns a random float between 0 (inclusive) and 1 (exclusive).
19
- * - If one argument is provided, returns a random integer between 0 and the given number (inclusive).
20
- * - If two or more arguments are provided, returns a random integer within the specified range and step.
21
- * @param {number} [to] - The upper bound (inclusive) if one argument is provided.
22
- * @param {number} [from] - The lower bound (inclusive) if two arguments are provided.
19
+ * - If one argument (`to`) is provided, returns a random integer between 0 and `to` (inclusive).
20
+ * - If two arguments (`from`, `to`) are provided:
21
+ * - If they are integers, returns a random integer between `from` and `to` (inclusive).
22
+ * - If they are floats, returns a random float between `from` (inclusive) and `to` (exclusive).
23
+ * - If three arguments (`from`, `to`, `step`) are provided, returns a random integer within the specified range and step.
24
+ * @param {number} [to] - The upper bound.
25
+ * @param {number} [from] - The lower bound.
23
26
  * @param {number} [step=1] - The step size if two or more arguments are provided.
24
27
  * @returns {number} A random number based on the provided arguments.
25
28
  */
26
29
  export function random(...args) {
27
- let step, from, to
28
-
29
30
  if (!args.length) {
30
31
  return Math.random()
31
32
  }
32
33
 
34
+ if (
35
+ args.length > 1 &&
36
+ !Number.isInteger(args[0]) &&
37
+ !Number.isInteger(args[1])
38
+ ) {
39
+ const [from, to] = args
40
+ return Math.random() * (to - from) + from
41
+ }
42
+
43
+ let step, from, to
44
+
33
45
  if (args.length === 1) {
34
46
  step = 1
35
47
  from = 0
@@ -38,6 +38,15 @@ test("it should create a random even number in the range [2, 6]", () => {
38
38
  expect(randomNumber % 2).toBe(0)
39
39
  })
40
40
 
41
+ test("it should create a random float in the range [1.5, 6.5)", () => {
42
+ const from = 1.5
43
+ const to = 6.5
44
+ const randomNumber = random(from, to)
45
+
46
+ expect(randomNumber).toBeGreaterThanOrEqual(from)
47
+ expect(randomNumber).toBeLessThan(to)
48
+ })
49
+
41
50
  test("it should create a random number in the range (-1, 1)", () => {
42
51
  const randomNumber = randomBinomial()
43
52