-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
043000c
commit 46149ff
Showing
5 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { sameJointAndDelimiterErrorMessage, beautifyNumber } from "../beautifyNumber"; | ||
|
||
describe("beautifyNumber", () => { | ||
describe("beautifyNumber with no options passed", () => { | ||
test.each([ | ||
[1, "1"], | ||
[100, "100"], | ||
[1000, "1 000"], | ||
[100000, "100 000"], | ||
[123456789, "123 456 789"], | ||
])('converts "%s" into "%s"', (value, result) => { | ||
expect(beautifyNumber(value)).toBe(result); | ||
}); | ||
}); | ||
|
||
describe('beautifyNumber with the "," joint', () => { | ||
test.each([ | ||
[1, "1"], | ||
[100, "100"], | ||
[1000, "1,000"], | ||
[100000, "100,000"], | ||
[123456789, "123,456,789"], | ||
])('converts "%s" into "%s"', (value, result) => { | ||
expect(beautifyNumber(value, { joint: "," })).toBe(result); | ||
}); | ||
}); | ||
|
||
describe("beautifyNumber for floating numbers", () => { | ||
test.each([ | ||
[1.12, "1.12"], | ||
[100.123, "100.123"], | ||
[1000.1234, "1 000.1234"], | ||
[100000.12345, "100 000.12345"], | ||
[123456789.0123456, "123 456 789.0123456"], | ||
])('converts "%s" into "%s"', (value, result) => { | ||
expect(beautifyNumber(value)).toBe(result); | ||
}); | ||
}); | ||
|
||
describe('beautifyNumber for floating numbers with the "_" joint, and "," delimiter', () => { | ||
test.each([ | ||
[1.12, "1,12"], | ||
[100.123, "100,123"], | ||
[1000.1234, "1_000,1234"], | ||
[100000.12345, "100_000,12345"], | ||
[123456789.0123456, "123_456_789,0123456"], | ||
])('converts "%s" into "%s"', (value, result) => { | ||
expect(beautifyNumber(value, { joint: "_", delimiter: "," })).toBe(result); | ||
}); | ||
}); | ||
|
||
describe("beautifyNumber for negative floating numbers", () => { | ||
test.each([ | ||
[-1.12, "-1.12"], | ||
[-100.123, "-100.123"], | ||
[-1000.1234, "-1 000.1234"], | ||
[-100000.12345, "-100 000.12345"], | ||
[-123456789.0123456, "-123 456 789.0123456"], | ||
])('converts "%s" into "%s"', (value, result) => { | ||
expect(beautifyNumber(value)).toBe(result); | ||
}); | ||
}); | ||
|
||
test('works for "_" number notation', () => { | ||
expect(beautifyNumber(12_001_123)).toBe("12 001 123"); | ||
}); | ||
|
||
test("throws the error if joint and delimiter are equal", () => { | ||
try { | ||
beautifyNumber(12_001_123, { joint: "", delimiter: "" }); | ||
} catch (error) { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
expect(error.message).toContain(sameJointAndDelimiterErrorMessage); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
interface IBeautifyNumberOptions { | ||
delimiter?: string; | ||
joint?: string; | ||
} | ||
|
||
interface IBeautifyNumber { | ||
(value: number, options?: IBeautifyNumberOptions): string; | ||
} | ||
|
||
const sameJointAndDelimiterErrorMessage = "Please provide different delimiter and joint values"; | ||
|
||
const DEFAULT_DELIMITER = "."; | ||
const DEFAULT_JOINT = " "; | ||
|
||
/** | ||
* | ||
* @param value number to format based on options | ||
* @param options "joint" symbol to add between thousands, and "delimiter" symbol to join whole and floating parts | ||
* @returns string produced out of a formatted number | ||
*/ | ||
const beautifyNumber: IBeautifyNumber = (value, options = {}) => { | ||
const { delimiter = DEFAULT_DELIMITER, joint = DEFAULT_JOINT } = options; | ||
|
||
if (delimiter === joint) { | ||
const error = new Error(sameJointAndDelimiterErrorMessage); | ||
|
||
throw error; | ||
} | ||
|
||
const [whole, decimal] = String(value).split("."); | ||
const wholeWithJoint = whole.replace(/\B(?=(\d{3})+(?!\d))/g, joint); | ||
|
||
return decimal ? `${wholeWithJoint}${delimiter}${decimal}` : wholeWithJoint; | ||
}; | ||
|
||
export type { IBeautifyNumber, IBeautifyNumberOptions }; | ||
export { sameJointAndDelimiterErrorMessage, beautifyNumber }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters