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

Add @superset-ui/time-format #38

Merged
merged 9 commits into from
Nov 29, 2018
Merged

Add @superset-ui/time-format #38

merged 9 commits into from
Nov 29, 2018

Conversation

kristw
Copy link
Contributor

@kristw kristw commented Nov 18, 2018

🏆 Enhancements

Add @superset-ui/time-format, a module for formatting time. Functions getTimeFormatter and formatTime should be used instead of calling d3.format directly.

import { getTimeFormatter } from '@superset-ui/time-format';
const formatter = getTimeFormatter('%Y-%m-d');
console.log(formatter(new Date()));

or

import { formatTime } from '@superset-ui/time-format';
console.log(formatTime('%Y-%m-d', new Date()));

It is powered by a registry to support registration of custom formatting, with fallback to d3.utcFormat or d3.timeFormat (if the formatId starts with local!)

import { getTimeFormatterRegistry, formatTime, TimeFormatter } from '@superset-ui/time-format';

getTimeFormatterRegistry().registerValue('my_format', new TimeFormatter({
  id: 'my_format',
  formatFunc: v => `my special format of ${utcFormat('%Y')(v)}`
});

console.log(formatTime('my_format', new Date(2018)));
// prints 'my special format of 2018'

It also define constants for common d3 time formats. See TimeFormats.js.

import { TimeFormats } from '@superset-ui/time-format';

TimeFormats.DATABASE_DATETIME // '%Y-%m-%d %H:%M:%S'
TimeFormats.US_DATE // '%m/%d/%Y'

@kristw kristw added the WIP Work in progress label Nov 18, 2018
@kristw kristw requested a review from a team as a code owner November 18, 2018 09:20
@codecov
Copy link

codecov bot commented Nov 18, 2018

Codecov Report

Merging #38 into master will increase coverage by 0.17%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #38      +/-   ##
==========================================
+ Coverage   99.11%   99.28%   +0.17%     
==========================================
  Files          48       57       +9     
  Lines         450      560     +110     
  Branches       39       39              
==========================================
+ Hits          446      556     +110     
  Misses          2        2              
  Partials        2        2
Impacted Files Coverage Δ
...kages/superset-ui-time-format/src/TimeFormatter.js 100% <100%> (ø)
...perset-ui-time-format/src/TimeFormatterRegistry.js 100% <100%> (ø)
...-time-format/src/factories/createMultiFormatter.js 100% <100%> (ø)
...-ui-time-format/src/formatters/smartDateVerbose.js 100% <100%> (ø)
...-time-format/src/TimeFormatterRegistrySingleton.js 100% <100%> (ø)
...time-format/src/factories/createD3TimeFormatter.js 100% <100%> (ø)
...ackages/superset-ui-time-format/src/TimeFormats.js 100% <100%> (ø)
...uperset-ui-time-format/src/formatters/smartDate.js 100% <100%> (ø)
packages/superset-ui-time-format/src/utils.js 100% <100%> (ø)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 431b7b2...2893ec3. Read the comment docs.

@kristw kristw added reviewable Ready for review and removed WIP Work in progress labels Nov 21, 2018
@kristw
Copy link
Contributor Author

kristw commented Nov 21, 2018

Just made updates and this one should be ready to review.

Copy link
Contributor

@williaster williaster left a comment

Choose a reason for hiding this comment

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

overall lgtm! had a couple minor comments.

packages/superset-ui-time-format/README.md Show resolved Hide resolved
packages/superset-ui-time-format/src/TimeFormatter.js Outdated Show resolved Hide resolved
}

// Create new formatter if does not exist
const useLocalTime = targetFormat.startsWith(LOCAL_PREFIX);
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think about having the user pass useLocalTime directly as a parameter instead of encoding in the format string? It seems a little more clear api-wise and would simplify splicing local! out, but also makes the format(format, value) signature become more complex.

given those tradeoffs I'd be okay with either, just thought I'd mention it. also not sure if Superset already uses !local and in any case it would probably be pretty straight forward to refactor in the future, too, if it was an issue. If we do keep !local, could we add a test for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are factories which will take useLocalTime directly as parameter, which is the underlying way to construct a formatter.

I intended for the interface of the registry's format to be closest to current superset and provide backwards compatibility to how d3FormatString is stored as a single string in the database. If we introduce useLocalTime as another field then we need a separate control and another field to store in db.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah I didn't realize this complexity, makes sense 👍

Squashed commits:
[5fae1ed] fix lint
[7672544] support local and utc
[c33fae7] update unit tests
[97fdc0d] fix all unit tests
[88e8029] add formatter code
[a33c76c] initialize
@kristw kristw added this to the v0.7.0 milestone Nov 29, 2018
@kristw kristw merged commit e34d318 into master Nov 29, 2018
@delete-merged-branch delete-merged-branch bot deleted the kristw--time-format branch November 29, 2018 21:02
@kristw kristw added #enhancement New feature or request and removed reviewable Ready for review labels Nov 29, 2018
@kristw kristw removed the reviewable Ready for review label Dec 10, 2018
@kristw kristw removed the reviewable Ready for review label Dec 10, 2018
zhaoyongjie pushed a commit to zhaoyongjie/superset-ui that referenced this pull request Sep 23, 2021
* chore: add tags and description

* Update packages/superset-ui-legacy-preset-chart-deckgl/src/layers/Arc/index.js

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>

* Update packages/superset-ui-legacy-preset-chart-deckgl/src/layers/Hex/index.js

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>

* Update packages/superset-ui-legacy-preset-chart-deckgl/src/layers/Scatter/index.js

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>

* Update packages/superset-ui-legacy-preset-chart-deckgl/src/layers/Screengrid/index.js

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>
zhaoyongjie pushed a commit to zhaoyongjie/superset-ui that referenced this pull request Sep 24, 2021
* chore: add tags and description

* Update packages/superset-ui-legacy-preset-chart-deckgl/src/layers/Arc/index.js

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>

* Update packages/superset-ui-legacy-preset-chart-deckgl/src/layers/Hex/index.js

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>

* Update packages/superset-ui-legacy-preset-chart-deckgl/src/layers/Scatter/index.js

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>

* Update packages/superset-ui-legacy-preset-chart-deckgl/src/layers/Screengrid/index.js

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>

Co-authored-by: David Aaron Suddjian <1858430+suddjian@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
#enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants