@tim-code/my-util 0.0.11 → 0.0.12
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 +1 -1
- package/src/math.js +15 -9
- package/src/math.test.js +28 -14
package/package.json
CHANGED
package/src/math.js
CHANGED
|
@@ -10,17 +10,23 @@ export function mod(number, modulus) {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
* @param {number} number
|
|
13
|
+
* Prepend a plus to a number or string if positive.
|
|
14
|
+
* @param {number|string} number Or string
|
|
15
15
|
* @param {Object} $1
|
|
16
16
|
* @param {boolean=} $1.zero If true, prepends a plus to zero as well.
|
|
17
|
-
* @
|
|
18
|
-
* @returns {string}
|
|
17
|
+
* @returns {string|undefined} Returns undefined if number is not a number or string. NaN return undefined.
|
|
19
18
|
*/
|
|
20
|
-
export function formatPlus(number, { zero = false
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
export function formatPlus(number, { zero = false } = {}) {
|
|
20
|
+
if (typeof number === "number" && !isNaN(number)) {
|
|
21
|
+
if (number > 0 || (zero && number === 0)) {
|
|
22
|
+
return `+${number}`
|
|
23
|
+
}
|
|
24
|
+
return `${number}`
|
|
25
|
+
} else if (typeof number === "string") {
|
|
26
|
+
if (number === "0" ? zero : number[0] !== "-") {
|
|
27
|
+
return `+${number}`
|
|
28
|
+
}
|
|
29
|
+
return number
|
|
24
30
|
}
|
|
25
|
-
return
|
|
31
|
+
return undefined
|
|
26
32
|
}
|
package/src/math.test.js
CHANGED
|
@@ -77,25 +77,39 @@ describe("formatPlus", () => {
|
|
|
77
77
|
expect(formatPlus(0, { zero: true })).toBe("+0")
|
|
78
78
|
})
|
|
79
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
80
|
it("handles undefined options argument", () => {
|
|
92
81
|
expect(formatPlus(5)).toBe("+5")
|
|
93
82
|
expect(formatPlus(-5)).toBe("-5")
|
|
94
83
|
expect(formatPlus(0)).toBe("0")
|
|
95
84
|
})
|
|
96
85
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
expect(formatPlus(
|
|
86
|
+
// String input cases
|
|
87
|
+
it("prepends a plus for positive string numbers", () => {
|
|
88
|
+
expect(formatPlus("5")).toBe("+5")
|
|
89
|
+
expect(formatPlus("123.45")).toBe("+123.45")
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it("does not prepend a plus for negative string numbers", () => {
|
|
93
|
+
expect(formatPlus("-5")).toBe("-5")
|
|
94
|
+
expect(formatPlus("-123.45")).toBe("-123.45")
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it("prepends a plus for string zero if zero option is true", () => {
|
|
98
|
+
expect(formatPlus("0", { zero: true })).toBe("+0")
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it("does not prepend a plus for string zero if zero option is false", () => {
|
|
102
|
+
expect(formatPlus("0")).toBe("0")
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it("returns undefined for non-number, non-string input", () => {
|
|
106
|
+
expect(formatPlus(undefined)).toBeUndefined()
|
|
107
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
108
|
+
expect(formatPlus(null)).toBeUndefined()
|
|
109
|
+
expect(formatPlus({})).toBeUndefined()
|
|
110
|
+
expect(formatPlus([])).toBeUndefined()
|
|
111
|
+
expect(formatPlus(true)).toBeUndefined()
|
|
112
|
+
expect(formatPlus(Symbol("x"))).toBeUndefined()
|
|
113
|
+
expect(formatPlus(NaN)).toBeUndefined()
|
|
100
114
|
})
|
|
101
115
|
})
|