@tim-code/my-util 0.0.10 → 0.0.11

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": "@tim-code/my-util",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "",
package/src/math.js CHANGED
@@ -1,9 +1,26 @@
1
1
  /**
2
- * Module safe for negative numbers i.e. mod(-1, 3) === 2 (not -1 as in -1 % 3).
3
- * @param {number} n
4
- * @param {number} m If 0, returns NaN as does n % m.
2
+ * Gives the remainder when the number is divided by modulus.
3
+ * The sign of the remainder is always the same as the modulus i.e. mod(-1, 3) === 2 (not -1 as in -1 % 3).
4
+ * @param {number} number
5
+ * @param {number} modulus If 0, returns NaN as does n % m.
5
6
  * @returns {number}
6
7
  */
7
- export function mod(n, m) {
8
- return ((n % m) + m) % m
8
+ export function mod(number, modulus) {
9
+ return ((number % modulus) + modulus) % modulus
10
+ }
11
+
12
+ /**
13
+ * Convert a number to a string and prepend a plus if the number is positive.
14
+ * @param {number} number
15
+ * @param {Object} $1
16
+ * @param {boolean=} $1.zero If true, prepends a plus to zero as well.
17
+ * @param {number=} $1.decimals If a number, uses .toFixed(decimals) instead of .toString()
18
+ * @returns {string}
19
+ */
20
+ export function formatPlus(number, { zero = false, decimals = undefined } = {}) {
21
+ const string = typeof decimals === "number" ? number.toFixed(decimals) : number.toString()
22
+ if (number > 0 || (number === 0 && zero)) {
23
+ return `+${string}`
24
+ }
25
+ return `${string}`
9
26
  }
package/src/math.test.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from "@jest/globals"
2
- const { mod } = await import("./math.js")
2
+ const { mod, formatPlus } = await import("./math.js")
3
3
 
4
4
  describe("mod", () => {
5
5
  it("returns n when n is less than m and n is non-negative", () => {
@@ -34,4 +34,68 @@ describe("mod", () => {
34
34
  expect(mod(0, 0)).toBeNaN()
35
35
  expect(mod(-3, 0)).toBeNaN()
36
36
  })
37
+
38
+ // All sign combinations for number and modulus
39
+ it("handles all sign combinations for number and modulus", () => {
40
+ // number > 0, modulus > 0
41
+ expect(mod(5, 3)).toBe(2)
42
+ // number > 0, modulus < 0
43
+ expect(mod(5, -3)).toBe(-1)
44
+ // number < 0, modulus > 0
45
+ expect(mod(-5, 3)).toBe(1)
46
+ // number < 0, modulus < 0
47
+ expect(mod(-5, -3)).toBe(-2)
48
+ // number = 0, modulus > 0
49
+ expect(mod(0, 3)).toBe(0)
50
+ // number = 0, modulus < 0
51
+ expect(mod(0, -3)).toBe(-0)
52
+ // number > 0, modulus = 0
53
+ expect(mod(5, 0)).toBeNaN()
54
+ // number < 0, modulus = 0
55
+ expect(mod(-5, 0)).toBeNaN()
56
+ // number = 0, modulus = 0
57
+ expect(mod(0, 0)).toBeNaN()
58
+ })
59
+ })
60
+
61
+ describe("formatPlus", () => {
62
+ it("prepends a plus for positive numbers", () => {
63
+ expect(formatPlus(1)).toBe("+1")
64
+ expect(formatPlus(123.45)).toBe("+123.45")
65
+ })
66
+
67
+ it("does not prepend a plus for negative numbers", () => {
68
+ expect(formatPlus(-1)).toBe("-1")
69
+ expect(formatPlus(-123.45)).toBe("-123.45")
70
+ })
71
+
72
+ it("does not prepend a plus for zero by default", () => {
73
+ expect(formatPlus(0)).toBe("0")
74
+ })
75
+
76
+ it("prepends a plus for zero if zero option is true", () => {
77
+ expect(formatPlus(0, { zero: true })).toBe("+0")
78
+ })
79
+
80
+ it("uses toFixed when decimals option is provided", () => {
81
+ expect(formatPlus(1.234, { decimals: 2 })).toBe("+1.23")
82
+ expect(formatPlus(-1.234, { decimals: 1 })).toBe("-1.2")
83
+ expect(formatPlus(0, { decimals: 3, zero: true })).toBe("+0.000")
84
+ expect(formatPlus(0, { decimals: 3 })).toBe("0.000")
85
+ })
86
+
87
+ it("handles zero with decimals and zero=true", () => {
88
+ expect(formatPlus(0, { decimals: 2, zero: true })).toBe("+0.00")
89
+ })
90
+
91
+ it("handles undefined options argument", () => {
92
+ expect(formatPlus(5)).toBe("+5")
93
+ expect(formatPlus(-5)).toBe("-5")
94
+ expect(formatPlus(0)).toBe("0")
95
+ })
96
+
97
+ it("does not use toFixed if decimals is 0 (current behavior)", () => {
98
+ expect(formatPlus(1.99, { decimals: 0 })).toBe("+2")
99
+ expect(formatPlus(-1.99, { decimals: 0 })).toBe("-2")
100
+ })
37
101
  })