Skip to content

Commit 6fefae0

Browse files
committed
fix: formatNumber now accepts floating-point numbers
1 parent 2490722 commit 6fefae0

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@acelords/js-utils",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "Common utils and helpers used on node projects",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,16 @@ export const randomString = (length = 6, includeNumbers = false) => {
9191
* - isNumeric('1234') // true
9292
* - isNumeric('1234n') // true
9393
* - isNumeric('123.4') // false
94+
* - isNumeric('123.4', true) // true
9495
* - isNumeric('') // false
9596
* - isNumeric(undefined) // false
9697
* - isNumeric(null) // false
9798
*/
98-
export function isNumeric(value: string | number | undefined | null): boolean {
99+
export function isNumeric(value: string | number | undefined | null, allowFloat = false): boolean {
99100
if (!value) return false;
100-
return /^-?\d+$/.test(value.toString());
101+
return allowFloat
102+
? /^-?\d+\.?\d*$/.test(value.toString())
103+
: /^-?\d+$/.test(value.toString());
101104
}
102105

103106
/**
@@ -136,7 +139,7 @@ export const substring = (str: string | null | undefined, end: number): string =
136139
export const numberFormat = (value: string | number | undefined | null, toInt = false): string => {
137140
const format = toInt ? '0,0' : "0,0.00"
138141
if (value === 0 || value === "0") return numeral(value).format(format)
139-
if (!value || !isNumeric(value)) return ""
142+
if (!value || !isNumeric(value, true)) return ""
140143
return numeral(value).format(format)
141144
}
142145

@@ -167,7 +170,7 @@ export const formatNumber = (value: string | number | undefined | null, toInt =
167170
export const formatCurrency = (value: string | number | undefined | null): string => {
168171
const format = "0,0.00"
169172
if (value === 0 || value === "0") return numeral(0).format(format)
170-
if (!value || !isNumeric(value)) return ""
173+
if (!value || !isNumeric(value, true)) return ""
171174
const amount = Number(value.toString()) / 100;
172175
return numeral(amount).format(format)
173176
}

src/utils.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ test("isNumeric - check if a string|number is numeric", () => {
119119
expect(isNumeric('1234567890')).toBeTruthy();
120120
expect(isNumeric('-23')).toBeTruthy();
121121
expect(isNumeric('1234')).toBeTruthy();
122-
// expect(isNumeric('1234e')).toBeTruthy();
123122
expect(isNumeric('123.4')).toBeFalsy();
123+
expect(isNumeric('123.4', true)).toBeTruthy();
124+
expect(isNumeric('1234e')).toBeFalsy();
124125
expect(isNumeric('')).toBeFalsy();
125126
expect(isNumeric(undefined)).toBeFalsy();
126127
expect(isNumeric(null)).toBeFalsy();
@@ -160,6 +161,10 @@ test("numberFormat - can format numeric strings and numbers", () => {
160161
expect(numberFormat(0, true)).toBe("0");
161162
expect(numberFormat('123456')).toBe("123,456.00");
162163
expect(numberFormat('123456', true)).toBe("123,456");
164+
expect(numberFormat('1.3333333333')).toBe("1.33");
165+
expect(numberFormat('-1.3333333333')).toBe("-1.33");
166+
expect(numberFormat('1.3333333333', true)).toBe("1");
167+
expect(numberFormat('-1.3333333333', true)).toBe("-1");
163168
});
164169

165170
test("formatNumber - can format numeric strings and numbers", () => {
@@ -172,6 +177,10 @@ test("formatNumber - can format numeric strings and numbers", () => {
172177
expect(formatNumber(0, true)).toBe("0");
173178
expect(formatNumber('123456')).toBe("123,456.00");
174179
expect(formatNumber('123456', true)).toBe("123,456");
180+
expect(formatNumber('1.3333333333')).toBe("1.33");
181+
expect(formatNumber('-1.3333333333')).toBe("-1.33");
182+
expect(formatNumber('1.3333333333', true)).toBe("1");
183+
expect(formatNumber('-1.3333333333', true)).toBe("-1");
175184
});
176185

177186
/*======== formatCurrency =============*/
@@ -183,6 +192,9 @@ test("formatCurrency - can format numeric strings and numbers to currency", () =
183192
expect(formatCurrency(0)).toBe("0.00");
184193
expect(formatCurrency('123456')).toBe("1,234.56");
185194
expect(formatCurrency('12345600')).toBe("123,456.00");
195+
expect(formatCurrency('-12345600')).toBe("-123,456.00");
196+
expect(formatCurrency('-100.333333333')).toBe("-1.00");
197+
expect(formatCurrency('-10.333333333')).toBe("-0.10");
186198
});
187199

188200
/*======== slugify =============*/
@@ -434,8 +446,12 @@ test("countWordsFromHtml - can get number of words in a string", () => {
434446

435447
/*======== birthdayFromNow =============*/
436448
test("birthdayFromNow - can get days to next birthday", () => {
437-
expect(birthdayFromNow(dayjs('2023-08-23').subtract(4, 'days').toDate())).toBe(361);
438-
expect(birthdayFromNow(dayjs('2023-08-23').add(4, 'days').toDate())).toBe(3);
449+
expect(birthdayFromNow(dayjs().subtract(4, 'days').toDate())).toBeGreaterThanOrEqual(361);
450+
expect(birthdayFromNow(dayjs().subtract(4, 'days').toDate())).toBeLessThanOrEqual(362);
451+
452+
expect(birthdayFromNow(dayjs().add(4, 'days').toDate())).toBeGreaterThanOrEqual(3);
453+
expect(birthdayFromNow(dayjs().add(4, 'days').toDate())).toBeLessThanOrEqual(4);
454+
439455
expect(birthdayFromNow(dayjs().toDate())).toBe(0);
440456
expect(birthdayFromNow(null)).toBe(null);
441457
expect(birthdayFromNow(undefined)).toBe(null);

0 commit comments

Comments
 (0)