Created
January 31, 2018 07:13
-
-
Save on3iro/ef51db9d91d91c6be5016a18ae4cbfa2 to your computer and use it in GitHub Desktop.
Converting a number to a formatted string
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
// @flow | |
/** | |
* Adds a separator between triples of numbers. | |
* For examples have a look at the test suite | |
*/ | |
export const addThousands = (numString: string, separator: string = '.'): string => { | |
const re = /\d(?=(?:\d{3})+(?!\d))/g | |
const convert = (match = numString.search(re), acc = numString) => { | |
if (match === -1) return acc | |
const head = acc.slice(0, match + 1) | |
const tail = acc.slice(match + 1) | |
const newStr = `${head}${separator}${tail}` | |
return convert(newStr.search(re), newStr) | |
} | |
return convert() | |
} | |
// TESTS | |
describe('addThousands()', () => { | |
it('should handle default', () => { | |
expect(addThousands('1000')).toBe('1.000') | |
}) | |
it('should handle different separator', () => { | |
expect(addThousands('1000', ',')).toBe('1,000') | |
}) | |
it('should add separator for each triple before decimals', () => { | |
expect(addThousands('1000000')).toBe('1.000.000') | |
}) | |
it('should handle decimal dot on num', () => { | |
expect(addThousands('1000.50')).toBe('1.000.50') | |
}) | |
}) | |
type FormattedStrConfig = {| | |
decimalSeparator?: string, | |
thousandsSeparator?: string, | |
unit?: string | |
|} | |
/** | |
* Converts a number into a formatted string | |
* For examples have a look at the test suite. | |
*/ | |
export const numToFormattedString = ( | |
num: number, | |
{ | |
decimalSeparator = ',', | |
thousandsSeparator = '.', | |
unit = '€' | |
}: FormattedStrConfig | |
): string => { | |
const numStrWithDecimals = num | |
.toFixed(2) | |
.toString() | |
.replace('.', decimalSeparator) | |
const numStr = addThousands(numStrWithDecimals, thousandsSeparator) | |
const finalStr = `${numStr}${unit}` | |
return finalStr | |
} | |
describe('numToFormattedString()', () => { | |
it('should num with default formatting', () => { | |
const result = numToFormattedString(1000.50, {}) | |
expect(result).toBe('1.000,50€') | |
}) | |
it('should handle decimal overwrite', () => { | |
const result = numToFormattedString(1000.50, { | |
decimalSeparator: ':' | |
}) | |
expect(result).toBe('1.000:50€') | |
}) | |
it('should handle thousands overwrite', () => { | |
const result = numToFormattedString(1000.50, { | |
thousandsSeparator: ':' | |
}) | |
expect(result).toBe('1:000,50€') | |
}) | |
it('should handle unit overwrite', () => { | |
const result = numToFormattedString(1000.50, { | |
unit: '$' | |
}) | |
expect(result).toBe('1.000,50$') | |
}) | |
it('should always have two decimals', () => { | |
const result = numToFormattedString(1000, {}) | |
expect(result).toBe('1.000,00€') | |
const result2 = numToFormattedString(1000.555, {}) | |
expect(result2).toBe('1.000,55€') | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment