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: parse more scales and add more test
- Loading branch information
Showing
10 changed files
with
428 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
31 changes: 31 additions & 0 deletions
31
packages/superset-ui-encodeable/src/parsers/parseDateTime.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,31 @@ | ||
import { parse, codegen } from 'vega-expression'; | ||
import { dateTimeExpr } from 'vega-lite/build/src/datetime'; | ||
import { DateTime } from '../types/VegaLite'; | ||
|
||
export default function parseDateTime(dateTime: string | number | DateTime) { | ||
if (typeof dateTime === 'number' || typeof dateTime === 'string') { | ||
return new Date(dateTime); | ||
} | ||
|
||
const expression = dateTimeExpr(dateTime, true) as string; | ||
const code = codegen({ globalvar: 'window' })(parse(expression)).code as string; | ||
// Technically the "code" here is safe to eval(), | ||
// but we will use more conservative approach and manually parse at the moment. | ||
const isUtc = code.startsWith('Date.UTC'); | ||
|
||
const dateParts = code | ||
.replace(/^(Date[.]UTC|new[ ]Date)\(/, '') | ||
.replace(/\)$/, '') | ||
.split(',') | ||
.map((chunk: string) => Number(chunk.trim())) as [ | ||
number, | ||
number, | ||
number, | ||
number, | ||
number, | ||
number, | ||
number, | ||
]; | ||
|
||
return isUtc ? new Date(Date.UTC(...dateParts)) : new Date(...dateParts); | ||
} |
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
59 changes: 59 additions & 0 deletions
59
packages/superset-ui-encodeable/src/parsers/scale/createScaleFromScaleType.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,59 @@ | ||
import { | ||
scaleLinear, | ||
scaleLog, | ||
scalePow, | ||
scaleSqrt, | ||
scaleTime, | ||
scaleUtc, | ||
scaleQuantile, | ||
scaleQuantize, | ||
scaleThreshold, | ||
scaleOrdinal, | ||
scalePoint, | ||
scaleBand, | ||
} from 'd3-scale'; | ||
import { HasToString } from '../../types/Base'; | ||
import { ScaleConfig } from '../../types/Scale'; | ||
import { ScaleType, Value } from '../../types/VegaLite'; | ||
|
||
// eslint-disable-next-line complexity | ||
export default function createScaleFromScaleType<Output extends Value>( | ||
config: ScaleConfig<Output>, | ||
) { | ||
switch (config.type) { | ||
default: | ||
case ScaleType.LINEAR: | ||
return scaleLinear<Output>(); | ||
case ScaleType.LOG: | ||
return typeof config.base === 'undefined' | ||
? scaleLog<Output>() | ||
: scaleLog<Output>().base(config.base); | ||
case ScaleType.POW: | ||
return typeof config.exponent === 'undefined' | ||
? scalePow<Output>() | ||
: scalePow<Output>().exponent(config.exponent); | ||
case ScaleType.SQRT: | ||
return scaleSqrt<Output>(); | ||
case ScaleType.SYMLOG: | ||
// TODO: d3-scale typings does not include scaleSymlog yet | ||
// needs to patch the declaration file before continue. | ||
throw new Error('"scale.type = symlog" is not implemented yet.'); | ||
case ScaleType.TIME: | ||
return scaleTime<Output>(); | ||
case ScaleType.UTC: | ||
return scaleUtc<Output>(); | ||
case ScaleType.QUANTILE: | ||
return scaleQuantile<Output>(); | ||
case ScaleType.QUANTIZE: | ||
return scaleQuantize<Output>(); | ||
case ScaleType.THRESHOLD: | ||
return scaleThreshold<number | string | Date, Output>(); | ||
case ScaleType.BIN_ORDINAL: | ||
case ScaleType.ORDINAL: | ||
return scaleOrdinal<HasToString, Output>(); | ||
case ScaleType.POINT: | ||
return scalePoint<HasToString>(); | ||
case ScaleType.BAND: | ||
return scaleBand<HasToString>(); | ||
} | ||
} |
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,24 @@ | ||
import { CategoricalColorScale } from '@superset-ui/color'; | ||
import { ScaleTime } from 'd3-scale'; | ||
import { D3Scale } from '../types/Scale'; | ||
import { Value, ScaleType } from '../types/VegaLite'; | ||
import { timeScaleTypesSet } from '../parsers/scale/scaleCategories'; | ||
|
||
export function isCategoricalColorScale<Output extends Value = Value>( | ||
scale: D3Scale<Output> | CategoricalColorScale, | ||
): scale is CategoricalColorScale { | ||
return scale instanceof CategoricalColorScale; | ||
} | ||
|
||
export function isD3Scale<Output extends Value = Value>( | ||
scale: D3Scale<Output> | CategoricalColorScale, | ||
): scale is D3Scale<Output> { | ||
return !isCategoricalColorScale(scale); | ||
} | ||
|
||
export function isTimeScale<Output extends Value = Value>( | ||
scale: D3Scale<Output> | CategoricalColorScale, | ||
scaleType: ScaleType, | ||
): scale is ScaleTime<Output, Output> { | ||
return scale && timeScaleTypesSet.has(scaleType); | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/superset-ui-encodeable/src/utils/inferElementTypeFromUnionOfArrayTypes.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,10 @@ | ||
type ArrayElement<A> = A extends Array<infer Elem> ? Elem : never; | ||
|
||
/** | ||
* Type workaround for https://github.com/Microsoft/TypeScript/issues/7294#issuecomment-465794460 | ||
* to avoid error "Cannot invoke an expression whose type lacks a call signature" | ||
* when using array.map | ||
*/ | ||
export default function inferElementTypeFromUnionOfArrayTypes<T>(array: T): ArrayElement<T>[] { | ||
return array as any; | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/superset-ui-encodeable/test/parsers/parseDateTime.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,29 @@ | ||
import parseDateTime from '../../src/parsers/parseDateTime'; | ||
|
||
describe('parseDateTime(dateTime)', () => { | ||
it('parses number', () => { | ||
expect(parseDateTime(1560384000000)).toEqual(new Date(Date.UTC(2019, 5, 13))); | ||
}); | ||
it('parses string', () => { | ||
expect(parseDateTime('2019-01-01')).toEqual(new Date('2019-01-01')); | ||
}); | ||
it('parse DateTime object', () => { | ||
expect( | ||
parseDateTime({ | ||
year: 2019, | ||
month: 6, | ||
date: 14, | ||
}), | ||
).toEqual(new Date(2019, 5, 14)); | ||
}); | ||
it('handles utc correctly', () => { | ||
expect( | ||
parseDateTime({ | ||
year: 2019, | ||
month: 6, | ||
date: 14, | ||
utc: true, | ||
}), | ||
).toEqual(new Date(Date.UTC(2019, 5, 14))); | ||
}); | ||
}); |
Oops, something went wrong.