forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert number-format to TypeScript (apache#75)
* Convert number-format to TypeScript * return string * unit test 100% * Allow developer to specify return type for loader * add doc
- Loading branch information
Showing
21 changed files
with
217 additions
and
158 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
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 |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
}, | ||
"dependencies": { | ||
"@superset-ui/core": "^0.8.0", | ||
"@types/d3-format": "^1.3.0", | ||
"d3-format": "^1.3.2" | ||
} | ||
} |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ExtensibleFunction, isRequired } from '@superset-ui/core'; | ||
import { NumberFormatFunction } from './types'; | ||
|
||
export const PREVIEW_VALUE = 12345.432; | ||
|
||
export interface NumberFormatterConfig { | ||
id: string; | ||
label?: string; | ||
description?: string; | ||
formatFunc: NumberFormatFunction; | ||
isInvalid?: boolean; | ||
} | ||
|
||
export default class NumberFormatter extends ExtensibleFunction { | ||
id: string; | ||
label: string; | ||
description: string; | ||
formatFunc: NumberFormatFunction; | ||
isInvalid: boolean; | ||
|
||
constructor(config: NumberFormatterConfig) { | ||
super((value: number) => this.format(value)); | ||
|
||
const { | ||
id = isRequired('config.id'), | ||
label, | ||
description = '', | ||
formatFunc = isRequired('config.formatFunc'), | ||
isInvalid = false, | ||
} = config; | ||
this.id = id; | ||
this.label = label || id; | ||
this.description = description; | ||
this.formatFunc = formatFunc; | ||
this.isInvalid = isInvalid; | ||
} | ||
|
||
format(value: number | null | undefined) { | ||
if (value === null || value === undefined || Number.isNaN(value)) { | ||
return `${value}`; | ||
} else if (value === Number.POSITIVE_INFINITY) { | ||
return '∞'; | ||
} else if (value === Number.NEGATIVE_INFINITY) { | ||
return '-∞'; | ||
} | ||
|
||
return this.formatFunc(value); | ||
} | ||
|
||
preview(value: number = PREVIEW_VALUE) { | ||
return `${value} => ${this.format(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
10 changes: 9 additions & 1 deletion
10
...actories/createSiAtMostNDigitFormatter.js → ...actories/createSiAtMostNDigitFormatter.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
File renamed without changes.
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,2 @@ | ||
/* eslint-disable-next-line import/prefer-default-export */ | ||
export type NumberFormatFunction = (value: number) => string; |
Oops, something went wrong.