This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create factory functions for creating D3 formatters. BREAKING CHANGE: Remove D3NumberFormatter class
- Loading branch information
Showing
11 changed files
with
108 additions
and
98 deletions.
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
29 changes: 29 additions & 0 deletions
29
packages/superset-ui-number-format/src/factories/createD3NumberFormatter.js
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,29 @@ | ||
import { format as d3Format } from 'd3-format'; | ||
import { isRequired } from '@superset-ui/core'; | ||
import NumberFormatter from '../NumberFormatter'; | ||
|
||
export default function createD3NumberFormatter({ | ||
description, | ||
formatString = isRequired('formatString'), | ||
label, | ||
} = {}) { | ||
let formatFunc; | ||
let isInvalid = false; | ||
|
||
try { | ||
formatFunc = d3Format(formatString); | ||
} catch (e) { | ||
formatFunc = value => `${value} (Invalid format: ${formatString})`; | ||
isInvalid = true; | ||
} | ||
|
||
const id = formatString; | ||
|
||
return new NumberFormatter({ | ||
description, | ||
formatFunc, | ||
id, | ||
isInvalid, | ||
label, | ||
}); | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/superset-ui-number-format/src/factories/createSiAtMostNDigitFormatter.js
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,18 @@ | ||
import { format as d3Format } from 'd3-format'; | ||
import NumberFormatter from '../NumberFormatter'; | ||
|
||
export default function createSiAtMostNDigitFormatter({ description, n = 3, id, label } = {}) { | ||
const siFormatter = d3Format(`.${n}s`); | ||
|
||
return new NumberFormatter({ | ||
description, | ||
formatFunc: value => { | ||
const si = siFormatter(value); | ||
|
||
// Removing trailing `.00` if any | ||
return si.slice(-1) < 'A' ? parseFloat(si).toString() : si; | ||
}, | ||
id: id || `si_at_most_${n}_digit`, | ||
label: label || `SI with at most ${n} significant digits`, | ||
}); | ||
} |
42 changes: 0 additions & 42 deletions
42
packages/superset-ui-number-format/src/formatters/D3NumberFormatter.js
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
packages/superset-ui-number-format/src/formatters/SiAtMostNDigitFormatter.js
This file was deleted.
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
49 changes: 49 additions & 0 deletions
49
packages/superset-ui-number-format/test/factories/createD3NumberFormatter.test.js
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,49 @@ | ||
import createD3NumberFormatter from '../../src/factories/createD3NumberFormatter'; | ||
|
||
describe('D3NumberFormatter', () => { | ||
describe('new D3NumberFormatter(config)', () => { | ||
it('requires config.formatString', () => { | ||
expect(() => createD3NumberFormatter()).toThrow(); | ||
}); | ||
describe('config.formatString', () => { | ||
it('creates a NumberFormatter with the formatString as id', () => { | ||
const formatter = createD3NumberFormatter({ formatString: '.2f' }); | ||
expect(formatter.id).toEqual('.2f'); | ||
}); | ||
describe('if it is valid d3 formatString', () => { | ||
it('uses d3.format(config.formatString) as format function', () => { | ||
const formatter = createD3NumberFormatter({ formatString: '.2f' }); | ||
expect(formatter.format(100)).toEqual('100.00'); | ||
}); | ||
}); | ||
describe('if it is invalid d3 formatString', () => { | ||
it('The format function displays error message', () => { | ||
const formatter = createD3NumberFormatter({ formatString: 'i-am-groot' }); | ||
expect(formatter.format(12345.67)).toEqual('12345.67 (Invalid format: i-am-groot)'); | ||
}); | ||
it('also set formatter.isInvalid to true', () => { | ||
const formatter = createD3NumberFormatter({ formatString: 'i-am-groot' }); | ||
expect(formatter.isInvalid).toEqual(true); | ||
}); | ||
}); | ||
}); | ||
describe('config.label', () => { | ||
it('set label if specified', () => { | ||
const formatter = createD3NumberFormatter({ | ||
formatString: '.2f', | ||
label: 'float formatter', | ||
}); | ||
expect(formatter.label).toEqual('float formatter'); | ||
}); | ||
}); | ||
describe('config.description', () => { | ||
it('set decription if specified', () => { | ||
const formatter = createD3NumberFormatter({ | ||
description: 'lorem ipsum', | ||
formatString: '.2f', | ||
}); | ||
expect(formatter.description).toEqual('lorem ipsum'); | ||
}); | ||
}); | ||
}); | ||
}); |
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
28 changes: 0 additions & 28 deletions
28
packages/superset-ui-number-format/test/formatters/D3NumberFormatter.test.js
This file was deleted.
Oops, something went wrong.