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
feat: add functions for parsing formatters from encoding #205
Merged
Merged
Changes from 3 commits
Commits
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
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
14 changes: 14 additions & 0 deletions
14
packages/superset-ui-encodeable/src/parsers/createFormatterFromChannelDef.ts
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,14 @@ | ||
import { ChannelDef } from '../types/ChannelDef'; | ||
import { isTypedFieldDef } from '../typeGuards/ChannelDef'; | ||
import fallbackFormatter from './fallbackFormatter'; | ||
import createFormatterFromFieldTypeAndFormat from './createFormatterFromFieldTypeAndFormat'; | ||
|
||
export default function createFormatterFromChannelDef(definition: ChannelDef) { | ||
if (isTypedFieldDef(definition)) { | ||
const { type, format = '' } = definition; | ||
|
||
return createFormatterFromFieldTypeAndFormat(type, format); | ||
} | ||
|
||
return fallbackFormatter; | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/superset-ui-encodeable/src/parsers/createFormatterFromFieldTypeAndFormat.ts
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,22 @@ | ||
import { getNumberFormatter } from '@superset-ui/number-format'; | ||
import { getTimeFormatter } from '@superset-ui/time-format'; | ||
import { Type } from '../types/VegaLite'; | ||
import { Formatter } from '../types/ChannelDef'; | ||
import fallbackFormatter from './fallbackFormatter'; | ||
|
||
export default function createFormatterFromFieldTypeAndFormat( | ||
type: Type, | ||
format: string, | ||
): Formatter { | ||
if (type === 'quantitative') { | ||
const formatter = getNumberFormatter(format); | ||
|
||
return (value: any) => formatter(value); | ||
} else if (type === 'temporal') { | ||
const formatter = getTimeFormatter(format); | ||
|
||
return (value: any) => formatter(value); | ||
} | ||
|
||
return fallbackFormatter; | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/superset-ui-encodeable/src/parsers/createGetterFromChannelDef.ts
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,14 @@ | ||
import { get } from 'lodash/fp'; | ||
import identity from '../utils/identity'; | ||
import { ChannelDef } from '../types/ChannelDef'; | ||
import { isValueDef } from '../typeGuards/ChannelDef'; | ||
|
||
export default function createGetterFromChannelDef(definition: ChannelDef): (x?: any) => any { | ||
if (isValueDef(definition)) { | ||
return () => definition.value; | ||
} else if (typeof definition.field !== 'undefined') { | ||
return get(definition.field); | ||
} | ||
|
||
return identity; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/superset-ui-encodeable/src/parsers/fallbackFormatter.ts
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,3 @@ | ||
export default function fallbackFormatter(v: any) { | ||
return `${v}`; | ||
} |
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
27 changes: 27 additions & 0 deletions
27
packages/superset-ui-encodeable/test/parsers/createFormatterFromChannelDef.test.ts
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,27 @@ | ||
import createFormatterFromChannelDef from '../../src/parsers/createFormatterFromChannelDef'; | ||
|
||
describe('createFormatterFromChannelDef(type, format)', () => { | ||
it('handles when format is defined', () => { | ||
const formatter = createFormatterFromChannelDef({ | ||
field: 'lunchTime', | ||
type: 'temporal', | ||
format: '%b %d, %Y', | ||
}); | ||
expect(formatter(new Date(Date.UTC(2019, 5, 20)))).toEqual('Jun 20, 2019'); | ||
}); | ||
it('handles when format is not defined', () => { | ||
const formatter = createFormatterFromChannelDef({ | ||
field: 'lunchTime', | ||
type: 'temporal', | ||
}); | ||
expect(formatter(new Date(Date.UTC(2019, 5, 20)))).toEqual('2019-06-20 00:00:00'); | ||
}); | ||
it('uses fallback for other cases', () => { | ||
const formatter = createFormatterFromChannelDef({ type: 'nominal', field: 'restaurantName' }); | ||
expect(formatter('Lazy Burger')).toEqual('Lazy Burger'); | ||
}); | ||
it('uses fallback for channel definitions without type', () => { | ||
const formatter = createFormatterFromChannelDef({ value: 'Lettuce' }); | ||
expect(formatter('Lazy Burger')).toEqual('Lazy Burger'); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/superset-ui-encodeable/test/parsers/createFormatterFromFieldTypeAndFormat.test.ts
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,16 @@ | ||
import createFormatterFromFieldTypeAndFormat from '../../src/parsers/createFormatterFromFieldTypeAndFormat'; | ||
|
||
describe('createFormatterFromFieldTypeAndFormat(type, format)', () => { | ||
it('handles quantitative field type', () => { | ||
const formatter = createFormatterFromFieldTypeAndFormat('quantitative', '.2f'); | ||
expect(formatter(200)).toEqual('200.00'); | ||
}); | ||
it('handles temporal field type', () => { | ||
const formatter = createFormatterFromFieldTypeAndFormat('temporal', '%b %d, %Y'); | ||
expect(formatter(new Date(Date.UTC(2019, 5, 20)))).toEqual('Jun 20, 2019'); | ||
}); | ||
it('uses fallback for other cases', () => { | ||
const formatter = createFormatterFromFieldTypeAndFormat('nominal', ''); | ||
expect(formatter('cat')).toEqual('cat'); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/superset-ui-encodeable/test/parsers/createGetterFromChannelDef.test.ts
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 @@ | ||
import createGetterFromChannelDef from '../../src/parsers/createGetterFromChannelDef'; | ||
|
||
describe('createGetterFromChannelDef(definition)', () => { | ||
it('handles ValueDef', () => { | ||
const getter = createGetterFromChannelDef({ value: 1 }); | ||
expect(getter()).toBe(1); | ||
}); | ||
it('handleFieldDef', () => { | ||
const getter = createGetterFromChannelDef({ field: 'cost' }); | ||
expect(getter({ cost: 10 })).toBe(10); | ||
}); | ||
it('handleFieldDef with nested field', () => { | ||
const getter = createGetterFromChannelDef({ field: 'fuel.cost' }); | ||
expect(getter({ fuel: { cost: 10 } })).toBe(10); | ||
}); | ||
it('otherwise return identity', () => { | ||
// @ts-ignore | ||
const getter = createGetterFromChannelDef({}); | ||
expect(getter(300)).toBe(300); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
packages/superset-ui-encodeable/test/parsers/fallbackFormatter.test.ts
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,22 @@ | ||
import fallbackFormatter from '../../src/parsers/fallbackFormatter'; | ||
|
||
describe('fallbackFormatter(v: any)', () => { | ||
it('handles primitive types', () => { | ||
expect(fallbackFormatter(undefined)).toEqual('undefined'); | ||
expect(fallbackFormatter(null)).toEqual('null'); | ||
expect(fallbackFormatter(true)).toEqual('true'); | ||
expect(fallbackFormatter(false)).toEqual('false'); | ||
expect(fallbackFormatter(0)).toEqual('0'); | ||
expect(fallbackFormatter(1)).toEqual('1'); | ||
expect(fallbackFormatter(-1)).toEqual('-1'); | ||
}); | ||
it('handles arrays', () => { | ||
expect(fallbackFormatter([])).toEqual(''); | ||
expect(fallbackFormatter(['def'])).toEqual('def'); | ||
expect(fallbackFormatter(['def', 'ghi'])).toEqual('def,ghi'); | ||
}); | ||
it('handles objects', () => { | ||
expect(fallbackFormatter({})).toEqual('[object Object]'); | ||
expect(fallbackFormatter({ abc: 1 })).toEqual('[object Object]'); | ||
}); | ||
}); |
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.
Could this function have more explicit typing? If not, would be good to leave a comment documenting expected input/output