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

Convert number-format to TypeScript #75

Merged
merged 5 commits into from
Jan 15, 2019
Merged

Conversation

kristw
Copy link
Contributor

@kristw kristw commented Jan 9, 2019

🏠 Internal

  • Convert @superset-ui/number-format to TypeScript

🏆 Enhancements

  • BREAKING CHANGE: Make formatter always returns string. This is different from previous behavior.
Value formatted value (before) formatted value (after)
null null 'null'
undefined undefined 'undefined'
NaN NaN 'NaN'
  • Add second generic to Registry, changing from Registry<V> to Registry<V, W>
    • V is type of value in the registry
    • W is type of value returned from loader function when using .registerLoader(key, loader).
    • W can be either V, Promise<V> or V | Promise<V>
    • Set W=V when does not support asynchronous loader. Making return type of .get() become V instead of Promise<V>
    • By default, W is set to V | Promise<V> to support both synchronous and asynchronous loaders.

@kristw kristw requested a review from a team as a code owner January 9, 2019 16:40
@codecov
Copy link

codecov bot commented Jan 9, 2019

Codecov Report

Merging #75 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #75   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          67     67           
  Lines         744    754   +10     
  Branches      130    146   +16     
=====================================
+ Hits          744    754   +10
Impacted Files Coverage Δ
...ges/superset-ui-number-format/src/NumberFormats.ts 100% <ø> (ø)
...erset-ui-core/src/models/RegistryWithDefaultKey.ts 100% <ø> (ø) ⬆️
...ber-format/src/NumberFormatterRegistrySingleton.ts 100% <ø> (ø)
packages/superset-ui-core/src/models/Registry.ts 100% <100%> (ø) ⬆️
...s/superset-ui-number-format/src/NumberFormatter.ts 100% <100%> (ø)
...et-ui-number-format/src/NumberFormatterRegistry.ts 100% <100%> (ø)
...mat/src/factories/createSiAtMostNDigitFormatter.ts 100% <100%> (ø)
...er-format/src/factories/createD3NumberFormatter.ts 100% <100%> (ø)

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 b40189c...38e9048. Read the comment docs.

@kristw kristw added #code-quality #enhancement New feature or request reviewable Ready for review labels Jan 10, 2019
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.

looks good overall, left some comments about the added W type. If you and @xtinec think it'll enable us to be more specific I'm totally on board, but curious if the added complexity is needed vs just V | Promise<V>. Seems potentially useful in some cases?

* By default W is set to V | Promise<V> to support
* both synchronous and asynchronous loaders.
*/
export default class Registry<V, W extends V | Promise<V> = V | Promise<V>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

for simplicity could we just say that this has to be V | Promise<V>? it seems like we shouldn't really need to support anything beyond that since ultimately it should match V right?

Copy link
Contributor Author

@kristw kristw Jan 14, 2019

Choose a reason for hiding this comment

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

I agree with the added complexity. The high-level idea is to be able to define synchronous registry. I am considering adding a subclass of Registry just to alias and hide this multiple generics.

class SyncRegistry<V> extends Registry<V, V>{}

W affect the .get() function in particular if the returned value is always the value or can also be a promise of the value. If it can be either promise of value then the subclass has to always consider if the value is promise or not. Using the W generic add these benefits:

  • Check to make sure loader is not async.
  • Knowing that the returned type of .get is not a promise simplifies the subclass operations. (Not having to manually override/cast)

@@ -109,7 +120,7 @@ export default class Registry<V> {
}
const item = this.get(key);
if (item !== undefined) {
const newPromise = Promise.resolve(item);
const newPromise = Promise.resolve(item) as Promise<V>;
Copy link
Contributor

Choose a reason for hiding this comment

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

should this and the function return type not be W? I guess either is okay technically.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line is always a promise.
The function return type has to be V|W|undefined because if W is set to Promise<V>, W won't include V.

export default class NumberFormatterRegistry extends RegistryWithDefaultKey<NumberFormatter> {
export default class NumberFormatterRegistry extends RegistryWithDefaultKey<
NumberFormatter,
NumberFormatter
Copy link
Contributor

Choose a reason for hiding this comment

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

does the = default specified for the generics in the Registry not allow you to just default to this? I guess you are saying it is returning the value not a promise of the value.

it seems a little burdensome for the users to always have to specify the same type 2wice 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you set RegistryWithDefaultKey<NumberFormatter>
it defaults to RegistryWithDefaultKey<NumberFormatter, NumberFormatter | Promise<NumberFormatter>.

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.

Got it, thanks for elaborating. I think this is a win overall 👍

The additional class seems potentially useful to obfuscate some complexity, but I'm okay with doing that now or punting and seeing how this goes.

@kristw kristw merged commit ac24d30 into master Jan 15, 2019
@delete-merged-branch delete-merged-branch bot deleted the kristw--number-format-ts branch January 15, 2019 18:49
@kristw kristw added this to the v0.9.0 milestone Feb 1, 2019
kristw added a commit that referenced this pull request Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
#code-quality #enhancement New feature or request reviewable Ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants