@tim-code/my-util 0.0.11 → 0.0.13

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,11 +1,14 @@
1
1
  {
2
2
  "name": "@tim-code/my-util",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "",
7
7
  "license": "MIT",
8
8
  "main": "src/index.js",
9
+ "exports": {
10
+ "./fs": "./src/fs.js"
11
+ },
9
12
  "scripts": {
10
13
  "test": "node --no-warnings --experimental-vm-modules node_modules/.bin/jest"
11
14
  },
package/src/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from "./array.js"
2
- export * from "./fs.js"
3
2
  export * from "./math.js"
4
3
  export * from "./promise.js"
5
4
  export * from "./run.js"
package/src/math.js CHANGED
@@ -10,17 +10,23 @@ export function mod(number, modulus) {
10
10
  }
11
11
 
12
12
  /**
13
- * Convert a number to a string and prepend a plus if the number is positive.
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
- * @param {number=} $1.decimals If a number, uses .toFixed(decimals) instead of .toString()
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, decimals = undefined } = {}) {
21
- const string = typeof decimals === "number" ? number.toFixed(decimals) : number.toString()
22
- if (number > 0 || (number === 0 && zero)) {
23
- return `+${string}`
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 `${string}`
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
- 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")
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
  })