Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(number-formatter): upgrade pretty-ms to 9.1.0 #30599

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion superset-frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module.exports = {
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
snapshotSerializers: ['@emotion/jest/enzyme-serializer'],
transformIgnorePatterns: [
'node_modules/(?!d3-(interpolate|color|time)|remark-gfm|markdown-table|micromark-*.|decode-named-character-reference|character-entities|mdast-util-*.|unist-util-*.|ccount|escape-string-regexp|nanoid|@rjsf/*.|sinon|echarts|zrender|fetch-mock)',
'node_modules/(?!d3-(interpolate|color|time)|remark-gfm|markdown-table|micromark-*.|decode-named-character-reference|character-entities|mdast-util-*.|unist-util-*.|ccount|escape-string-regexp|nanoid|@rjsf/*.|sinon|echarts|zrender|fetch-mock|pretty-ms|parse-ms)',
],
globals: {
__DEV__: true,
Expand Down
67 changes: 35 additions & 32 deletions superset-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion superset-frontend/packages/superset-ui-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"jed": "^1.1.1",
"lodash": "^4.17.21",
"math-expression-evaluator": "^1.3.8",
"pretty-ms": "^7.0.0",
"pretty-ms": "^9.1.0",
"react-error-boundary": "^1.2.5",
"react-markdown": "^8.0.7",
"rehype-raw": "^7.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import prettyMsFormatter from 'pretty-ms';
import prettyMilliseconds, { Options } from 'pretty-ms';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming the default export to prettyMilliseconds, as that's what it's called in the source, and pull in Options via the newly exposed named export.

import NumberFormatter from '../NumberFormatter';

export default function createDurationFormatter(
Expand All @@ -26,13 +26,14 @@ export default function createDurationFormatter(
id?: string;
label?: string;
multiplier?: number;
} & prettyMsFormatter.Options = {},
} & Options = {},
) {
const { description, id, label, multiplier = 1, ...prettyMsOptions } = config;

return new NumberFormatter({
description,
formatFunc: value => prettyMsFormatter(value * multiplier, prettyMsOptions),
formatFunc: value =>
prettyMilliseconds(value * multiplier, prettyMsOptions),
id: id ?? 'duration_format',
label: label ?? `Duration formatter`,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,41 @@

import { NumberFormatter, createDurationFormatter } from '@superset-ui/core';

describe('createDurationFormatter()', () => {
it('creates an instance of NumberFormatter', () => {
const formatter = createDurationFormatter();
expect(formatter).toBeInstanceOf(NumberFormatter);
});
it('format milliseconds in human readable format with default options', () => {
const formatter = createDurationFormatter();
expect(formatter(0)).toBe('0ms');
expect(formatter(1000)).toBe('1s');
expect(formatter(1337)).toBe('1.3s');
expect(formatter(10500)).toBe('10.5s');
expect(formatter(60 * 1000)).toBe('1m');
expect(formatter(90 * 1000)).toBe('1m 30s');
test('creates an instance of NumberFormatter', () => {
const formatter = createDurationFormatter();
expect(formatter).toBeInstanceOf(NumberFormatter);
});
test('format milliseconds in human readable format with default options', () => {
const formatter = createDurationFormatter();
expect(formatter(-1000)).toBe('-1s');
Copy link
Member Author

@villebro villebro Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note the added negative duration tests that previously weren't supported.

expect(formatter(0)).toBe('0ms');
expect(formatter(1000)).toBe('1s');
expect(formatter(1337)).toBe('1.3s');
expect(formatter(10500)).toBe('10.5s');
expect(formatter(60 * 1000)).toBe('1m');
expect(formatter(90 * 1000)).toBe('1m 30s');
});
test('format seconds in human readable format with default options', () => {
const formatter = createDurationFormatter({ multiplier: 1000 });
expect(formatter(-0.5)).toBe('-500ms');
expect(formatter(0.5)).toBe('500ms');
expect(formatter(1)).toBe('1s');
expect(formatter(30)).toBe('30s');
expect(formatter(60)).toBe('1m');
expect(formatter(90)).toBe('1m 30s');
});
test('format milliseconds in human readable format with additional pretty-ms options', () => {
const colonNotationFormatter = createDurationFormatter({
colonNotation: true,
});
it('format seconds in human readable format with default options', () => {
const formatter = createDurationFormatter({ multiplier: 1000 });
expect(formatter(0.5)).toBe('500ms');
expect(formatter(1)).toBe('1s');
expect(formatter(30)).toBe('30s');
expect(formatter(60)).toBe('1m');
expect(formatter(90)).toBe('1m 30s');
expect(colonNotationFormatter(-10500)).toBe('-0:10.5');
expect(colonNotationFormatter(10500)).toBe('0:10.5');
const zeroDecimalFormatter = createDurationFormatter({
secondsDecimalDigits: 0,
});
it('format milliseconds in human readable format with additional pretty-ms options', () => {
const colonNotationFormatter = createDurationFormatter({
colonNotation: true,
});
expect(colonNotationFormatter(10500)).toBe('0:10.5');
const zeroDecimalFormatter = createDurationFormatter({
secondsDecimalDigits: 0,
});
expect(zeroDecimalFormatter(10500)).toBe('10s');
const subMillisecondFormatter = createDurationFormatter({
formatSubMilliseconds: true,
});
expect(subMillisecondFormatter(100.40008)).toBe('100ms 400µs 80ns');
expect(zeroDecimalFormatter(10500)).toBe('10s');
const subMillisecondFormatter = createDurationFormatter({
formatSubMilliseconds: true,
});
expect(subMillisecondFormatter(100.40008)).toBe('100ms 400µs 80ns');
});
Loading