Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
refactor: move number-format and time-format to core (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmud authored Aug 20, 2020
1 parent ca4aaab commit de95e68
Show file tree
Hide file tree
Showing 146 changed files with 397 additions and 502 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ npm-shrinkwrap.json
package.json
package-lock.json
tsconfig.json
tsconfig.option.json
tsconfig.options.json
tsconfig.eslint.json
CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe('WithLegend', () => {
<WithLegend debounceTime={1} width={500} height={500} renderChart={renderChart} />,
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();
// Have to delay more than debounceTime (1ms)
return promiseTimeout(() => {
Expand All @@ -50,7 +49,6 @@ describe('WithLegend', () => {
/>,
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();
// Have to delay more than debounceTime (1ms)
return promiseTimeout(() => {
Expand All @@ -66,7 +64,6 @@ describe('WithLegend', () => {
<WithLegend debounceTime={1} renderChart={renderChart} renderLegend={renderLegend} />,
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();
// Have to delay more than debounceTime (1ms)
return promiseTimeout(() => {
Expand All @@ -87,7 +84,6 @@ describe('WithLegend', () => {
/>,
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();
// Have to delay more than debounceTime (1ms)
return promiseTimeout(() => {
Expand All @@ -108,7 +104,6 @@ describe('WithLegend', () => {
/>,
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();
// Have to delay more than debounceTime (1ms)
return promiseTimeout(() => {
Expand All @@ -129,7 +124,6 @@ describe('WithLegend', () => {
/>,
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();
// Have to delay more than debounceTime (1ms)
return promiseTimeout(() => {
Expand All @@ -150,7 +144,6 @@ describe('WithLegend', () => {
/>,
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();
// Have to delay more than debounceTime (1ms)
return promiseTimeout(() => {
Expand All @@ -172,7 +165,6 @@ describe('WithLegend', () => {
/>,
);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();
// Have to delay more than debounceTime (1ms)
return promiseTimeout(() => {
Expand Down
1 change: 0 additions & 1 deletion packages/superset-ui-chart/src/components/SuperChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export default class SuperChart extends React.PureComponent<Props, {}> {
if (
queryData == null ||
queryData.data === null ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
(Array.isArray(queryData.data) && queryData.data.length === 0)
) {
chart = <NoResultsComponent id={id} className={className} height={height} width={width} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ export default function createLoadableRenderer<Props, Exports>(
const { onRenderFailure, onRenderSuccess } = this.props;
if (!loading) {
if (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
(onRenderFailure as Function)(error);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
} else if (loaded && Object.keys(loaded).length > 0) {
(onRenderSuccess as Function)();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ describe('SuperChart', () => {
height="100%"
/>,
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();

return promiseTimeout(() => {
Expand All @@ -244,7 +243,6 @@ describe('SuperChart', () => {
height="125"
/>,
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver([{ contentRect: { height: 125, width: 150 } }]);

return promiseTimeout(() => {
Expand All @@ -266,7 +264,6 @@ describe('SuperChart', () => {
height="25%"
/>,
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver([{ contentRect: { height: 75, width: 50 } }]);

return promiseTimeout(() => {
Expand All @@ -286,7 +283,6 @@ describe('SuperChart', () => {
debounceTime={1}
/>,
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();

return promiseTimeout(() => {
Expand Down Expand Up @@ -340,7 +336,6 @@ describe('SuperChart', () => {
Wrapper={MyWrapper}
/>,
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
triggerResizeObserver();

return promiseTimeout(() => {
Expand Down
2 changes: 0 additions & 2 deletions packages/superset-ui-chart/test/components/reactify.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ describe('reactify(renderFn)', () => {
it('does not try to render if not mounted', () => {
const anotherRenderFn = jest.fn();
const AnotherChart = reactify(anotherRenderFn); // enables valid new AnotherChart() call
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
new AnotherChart({ id: 'test' }).execute();
expect(anotherRenderFn).not.toHaveBeenCalled();
});
Expand Down
7 changes: 7 additions & 0 deletions packages/superset-ui-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@
},
"dependencies": {
"@babel/runtime": "^7.1.2",
"@types/d3-format": "^1.3.0",
"@types/lodash": "^4.14.149",
"@types/d3-time": "^1.0.9",
"@types/d3-time-format": "^2.1.0",
"d3-format": "^1.3.2",
"d3-time": "^1.0.10",
"d3-time-format": "^2.2.0",
"jed": "^1.1.1",
"lodash": "^4.17.11",
"fetch-retry": "^4.0.1",
"pretty-ms": "^7.0.0",
"whatwg-fetch": "^3.0.0"
}
}
2 changes: 2 additions & 0 deletions packages/superset-ui-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export * from './types';
export * from './translation';
export * from './connection';
export * from './query';
export * from './number-format';
export * from './time-format';
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import { ExtensibleFunction, isRequired } from '@superset-ui/core';
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { ExtensibleFunction } from '../models';
import { isRequired } from '../utils';
import { NumberFormatFunction } from './types';

export const PREVIEW_VALUE = 12345.432;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import { RegistryWithDefaultKey, OverwritePolicy } from '@superset-ui/core';
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { RegistryWithDefaultKey } from '../models';
import { OverwritePolicy } from '../types';
import createD3NumberFormatter from './factories/createD3NumberFormatter';
import createSmartNumberFormatter from './factories/createSmartNumberFormatter';
import NumberFormats from './NumberFormats';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { makeSingleton } from '../utils';
import NumberFormatterRegistry from './NumberFormatterRegistry';

const getInstance = makeSingleton(NumberFormatterRegistry);

export default getInstance;

export function getNumberFormatter(format?: string) {
return getInstance().get(format);
}

export function formatNumber(format: string | undefined, value: number | null | undefined) {
return getInstance().format(format, value);
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
## @superset-ui/number-format

[![Version](https://img.shields.io/npm/v/@superset-ui/number-format.svg?style=flat)](https://img.shields.io/npm/v/@superset-ui/number-format.svg?style=flat)
[![David (path)](https://img.shields.io/david/apache-superset/superset-ui.svg?path=packages%2Fsuperset-ui-number-format&style=flat-square)](https://david-dm.org/apache-superset/superset-ui?path=packages/superset-ui-number-format)
## @superset-ui/core/number-format

Description

#### Example usage

Functions `getNumberFormatter` and `formatNumber` should be used instead of calling `d3.format` directly.
Functions `getNumberFormatter` and `formatNumber` should be used instead of calling `d3.format`
directly.

```js
import { getNumberFormatter } from '@superset-ui/number-format';
import { getNumberFormatter } from '@superset-ui/core';
const formatter = getNumberFormatter('.2f');
console.log(formatter(1000));
```

or

```js
import { formatNumber } from '@superset-ui/number-format';
import { formatNumber } from '@superset-ui/core';
console.log(formatNumber('.2f', 1000));
```

It is powered by a registry to support registration of custom formatting, with fallback to `d3.format` and handle error for invalid format string.
It is powered by a registry to support registration of custom formatting, with fallback to
`d3.format` and handle error for invalid format string.

```js
import { getNumberFormatterRegistry, formatNumber, NumberFormatter } from '@superset-ui/number-format';
import { getNumberFormatterRegistry, formatNumber, NumberFormatter } from '@superset-ui/core';

getNumberFormatterRegistry().registerValue('my_format', new NumberFormatter({
id: 'my_format',
Expand All @@ -36,16 +35,18 @@ console.log(formatNumber('my_format', 1000));
// prints 'my special format of 1000'
```
It also define constants for common d3 formats. See the full list of formats in [NumberFormats.js](https://github.com/apache-superset/superset-ui/blob/master/packages/superset-ui-number-format/src/NumberFormats.js).
It also define constants for common d3 formats. See the full list of formats in
[NumberFormats.js](https://github.com/apache-superset/superset-ui/blob/master/packages/superset-ui-number-format/src/NumberFormats.js).
```js
import { NumberFormats } from '@superset-ui-number-format';

NumberFormats.PERCENT // ,.2%
NumberFormats.PERCENT_3_POINT // ,.3%
NumberFormats.PERCENT; // ,.2%
NumberFormats.PERCENT_3_POINT; // ,.3%
```
There is also a formatter based on [pretty-ms](https://www.npmjs.com/package/pretty-ms) that can be used to format time durations:
There is also a formatter based on [pretty-ms](https://www.npmjs.com/package/pretty-ms) that can be
used to format time durations:
```js
import { createDurationFormatter, formatNumber, getNumberFormatterRegistry } from from '@superset-ui-number-format';
Expand All @@ -60,8 +61,3 @@ console.log(formatNumber('my_duration_format', 95500))
`fn(args)`
- Do something
### Development
`@data-ui/build-config` is used to manage the build configuration for this package including babel
builds, jest testing, eslint, and prettier.
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { format as d3Format, formatLocale, FormatLocaleDefinition } from 'd3-format';
import { isRequired } from '@superset-ui/core';
import { isRequired } from '../../utils';
import NumberFormatter from '../NumberFormatter';
import { NumberFormatFunction } from '../types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export {
getNumberFormatter,
} from './NumberFormatterRegistrySingleton';

export { default as NumberFormatterRegistry } from './NumberFormatterRegistry';
export { default as createD3NumberFormatter } from './factories/createD3NumberFormatter';
export { default as createDurationFormatter } from './factories/createDurationFormatter';
export { default as createSiAtMostNDigitFormatter } from './factories/createSiAtMostNDigitFormatter';
export { default as createSmartNumberFormatter } from './factories/createSmartNumberFormatter';
Loading

0 comments on commit de95e68

Please sign in to comment.