-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Number Formatting #238
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6ef50fc
cell formatting progress
davidkircos 3d1ed57
add number, currency, percentage, date, and exponential formatting
davidkircos 718a354
implement all types of cell text formatting
davidkircos ad2a4ab
add cell TextFormat tests
davidkircos dde689a
add formatting to FloatingContextMenu, clean NumberFormattingMenu
davidkircos 04cb350
remove unused imports
davidkircos 552b09e
Merge branch 'development' into development-number-formatting
davidkircos 66a5892
Merge branch 'development' into development-number-formatting
davidkircos 0155f50
menu style updates
jimniels 7cf92e5
Merge branch 'development-number-formatting' of github.com:quadratich…
jimniels 1673b3a
add command palette formatting items
jimniels 1ff1924
Merge branch 'development-number-formatting' of https://github.com/qu…
davidkircos ae46e9c
wire up plain text
davidkircos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: this code comment should probably be more generic so it doesn't fall out of date with the code above it (e.g. "check for special characters to begin taking input")