-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from quadratichq/development-number-formatting
feat: Number Formatting
- Loading branch information
Showing
26 changed files
with
480 additions
and
222 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,21 @@ | ||
export const DEFAULT_NUMBER_OF_DECIMAL_PLACES = 2; | ||
|
||
export type CellTextFormat = | ||
| { | ||
type: 'NUMBER'; | ||
decimalPlaces?: number; | ||
} | ||
| { | ||
type: 'CURRENCY'; | ||
display: 'CURRENCY'; | ||
symbol?: string; | ||
decimalPlaces?: number; | ||
} | ||
| { | ||
type: 'PERCENTAGE'; | ||
decimalPlaces?: number; | ||
} | ||
| { | ||
type: 'EXPONENTIAL'; | ||
decimalPlaces?: number; | ||
}; |
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,59 @@ | ||
import { Cell, CellFormat } from '../gridDB/gridTypes'; | ||
import { CellTextFormat } from './cellTextFormat'; | ||
import { CellTextFormatter } from './cellTextFormatter'; | ||
|
||
const generateCell = (value: string) => { | ||
return { | ||
x: 0, | ||
y: 0, | ||
type: 'TEXT', | ||
value: value, | ||
} as Cell; | ||
}; | ||
|
||
const generateFormat = (textFormat: CellTextFormat, decimals?: number) => { | ||
// allows tests to fit on one line below | ||
let format = { | ||
textFormat, | ||
} as CellFormat; | ||
if (decimals && format.textFormat) { | ||
format.textFormat.decimalPlaces = decimals; | ||
} | ||
return format; | ||
}; | ||
|
||
test('CellTextFormatter', () => { | ||
// format undefined | ||
expect(CellTextFormatter(generateCell('$1'), undefined)).toBe('$1'); | ||
expect(CellTextFormatter(generateCell('100%'), undefined)).toBe('100%'); | ||
expect(CellTextFormatter(generateCell('hello'), undefined)).toBe('hello'); | ||
|
||
// format number | ||
expect(CellTextFormatter(generateCell('1'), generateFormat({ type: 'NUMBER' }))).toBe('1.00'); | ||
expect(CellTextFormatter(generateCell('1'), generateFormat({ type: 'NUMBER', decimalPlaces: 0 }))).toBe('1'); | ||
expect(CellTextFormatter(generateCell('1'), generateFormat({ type: 'NUMBER', decimalPlaces: 1 }))).toBe('1.0'); | ||
expect(CellTextFormatter(generateCell('1'), generateFormat({ type: 'NUMBER', decimalPlaces: 4 }))).toBe('1.0000'); | ||
expect(CellTextFormatter(generateCell('0.009'), generateFormat({ type: 'NUMBER', decimalPlaces: 2 }))).toBe('0.01'); | ||
expect(CellTextFormatter(generateCell('1000000'), generateFormat({ type: 'NUMBER' }))).toBe('1,000,000.00'); | ||
|
||
// format currency | ||
const currencyFormat = generateFormat({ type: 'CURRENCY', display: 'CURRENCY', symbol: 'USD' }); | ||
expect(CellTextFormatter(generateCell('1'), currencyFormat)).toBe('$1.00'); | ||
expect(CellTextFormatter(generateCell('.01'), currencyFormat)).toBe('$0.01'); | ||
expect(CellTextFormatter(generateCell('.009'), currencyFormat)).toBe('$0.01'); | ||
expect(CellTextFormatter(generateCell('1000'), currencyFormat)).toBe('$1,000.00'); | ||
|
||
// format percentage | ||
expect(CellTextFormatter(generateCell('1'), generateFormat({ type: 'PERCENTAGE' }))).toBe('100.00%'); | ||
expect(CellTextFormatter(generateCell('.1'), generateFormat({ type: 'PERCENTAGE' }))).toBe('10.00%'); | ||
expect(CellTextFormatter(generateCell('.01'), generateFormat({ type: 'PERCENTAGE' }))).toBe('1.00%'); | ||
expect(CellTextFormatter(generateCell('.00009'), generateFormat({ type: 'PERCENTAGE' }))).toBe('0.01%'); | ||
|
||
// format exponential | ||
expect(CellTextFormatter(generateCell('1'), generateFormat({ type: 'EXPONENTIAL' }))).toBe('1.00e+0'); | ||
expect(CellTextFormatter(generateCell('1'), generateFormat({ type: 'EXPONENTIAL', decimalPlaces: 0 }))).toBe('1e+0'); | ||
expect(CellTextFormatter(generateCell('1'), generateFormat({ type: 'EXPONENTIAL' }, 1))).toBe('1.0e+0'); | ||
expect(CellTextFormatter(generateCell('1'), generateFormat({ type: 'EXPONENTIAL' }, 4))).toBe('1.0000e+0'); | ||
expect(CellTextFormatter(generateCell('0.009'), generateFormat({ type: 'EXPONENTIAL' }, 2))).toBe('9.00e-3'); | ||
expect(CellTextFormatter(generateCell('1000000'), generateFormat({ type: 'EXPONENTIAL' }))).toBe('1.00e+6'); | ||
}); |
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,39 @@ | ||
import { format as formatNumber } from 'numerable'; | ||
import { Cell, CellFormat } from '../gridDB/gridTypes'; | ||
import { CellTextFormat, DEFAULT_NUMBER_OF_DECIMAL_PLACES } from './cellTextFormat'; | ||
|
||
// function that checks if a string is a number | ||
const IsNumeric = (num: string) => /^-{0,1}\d*\.{0,1}\d+$/.test(num); | ||
|
||
const getDecimalPlacesString = (format: CellTextFormat, number_of_decimals: number) => { | ||
// returns a string of the format '.00' for the number of decimal places | ||
if (number_of_decimals === 0) return ''; | ||
let decimalString = '.'; | ||
for (let i = 0; i < number_of_decimals; i++) { | ||
decimalString += '0'; | ||
} | ||
return decimalString; | ||
}; | ||
|
||
export const CellTextFormatter = (cell: Cell, format: CellFormat | undefined) => { | ||
if (!format || !format.textFormat) return cell.value; | ||
|
||
const number_of_decimals = format.textFormat.decimalPlaces ?? DEFAULT_NUMBER_OF_DECIMAL_PLACES; | ||
const decimal_string = getDecimalPlacesString(format.textFormat, number_of_decimals); | ||
|
||
try { | ||
if (format.textFormat.type === 'CURRENCY' && IsNumeric(cell.value)) { | ||
return formatNumber(cell.value, `$0,0${decimal_string}`, { currency: format.textFormat.symbol }); | ||
} else if (format.textFormat.type === 'PERCENTAGE' && IsNumeric(cell.value)) { | ||
return formatNumber(Number(cell.value), `0,0${decimal_string}%`); | ||
} else if (format.textFormat.type === 'NUMBER' && IsNumeric(cell.value)) { | ||
return formatNumber(cell.value, `0,0${decimal_string}`); | ||
} else if (format.textFormat.type === 'EXPONENTIAL' && IsNumeric(cell.value)) { | ||
return Number(cell.value).toExponential(number_of_decimals); | ||
} | ||
} catch (e) { | ||
console.error('Caught error in CellTextFormatter: ', e); | ||
} | ||
|
||
return cell.value; | ||
}; |
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
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
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
Oops, something went wrong.