-
Notifications
You must be signed in to change notification settings - Fork 188
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
refactor: generate currency source files #670
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit b9467fc:
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit c144f5f:
|
Since api-extractor was added before this PR, we can be petty confident that this didn't make any changes to the existing api, because it didn't modify the |
To verify the generation makes no changes to existing files, I replaced all the original source files from the main branch. After I modified the |
b6a34a2
to
869363a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really cool change, that's gonna be useful.
One early thought before I review this more thoroughly—way the script is designed, if we add a genCurrenciesNumber
script, we'll read and parse the JSON file twice (since we'll call genCurrencies
in each script). We can avoid that by creating configuration objects instead that we pass to a generator. The generator reads and parses once, and maps over configurations to which it passes the data. That's how Rollup works, and it makes the final script easy to read and to update.
For example, we could have the entry point look like this:
const numberCurrencyConfig = createCurrencyConfig(
({ currency, ...description }) => ({
...currency,
description,
genericType: 'number',
})
);
const bigintCurrencyConfig = createCurrencyConfig(
({ currency, ...description }) => ({
...currency,
description,
base: `${base}n`,
exponent: `${exponent}n`,
genericType: 'bigint',
})
);
const configs = [numberCurrencyConfig, bigintCurrencyConfig];
export function build() {
// load data
configs.forEach((config) => {
// do stuff
});
}
Beautiful. I'll incorporate that. |
scripts/genCurrenciesNumber.mjs
Outdated
/** | ||
* ${description}. | ||
*/ | ||
export const ${code}: Currency<number> = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could easily be adopted to be even more generic (abstractions all the way!), By letting the number
part (and the formatting of the numbers down below), be configured.
This is just a pop-up thought, and might just be a bit too much abstraction layering though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm working through some thoughts @sarahdayan about better abstractions. I should have it up in a few days.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@reckter checkout out the abstraction now. Creating the currency generation config adds a mapNumber
function which converts the JS number type of the json data file into a string that will be output into the generated source files.
Example for bigint
createCurrencyConfig({
genericType: 'bigint',
mapNumber: (n) => `${n}n`,
}
@@ -20,54 +22,103 @@ import { getCurrencyData } from './getCurrencyData.mjs'; | |||
// eslint-disable-next-line import/no-named-as-default-member | |||
const { format, resolveConfig } = prettier; | |||
|
|||
const baseOutputDir = path.resolve( | |||
url.fileURLToPath(new URL('.', import.meta.url)), // __dirname es module style | |||
'../packages/currencies/src/iso4217/amendments/168' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually we would want this broken out into another layer of abstraction, so it will auto generate other amendments. But I think leaving that for another day is okay.
I'm unsure why the |
@@ -1,2 +1,2 @@ | |||
export * from './iso4217/amendments/168'; | |||
export * from './iso4217/amendments/168/number'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the default to load the number currency library. Currently the API is identical to the previous, the bigint
currency files aren't exported by the package.
4b9041d
to
0220263
Compare
// eslint-disable-next-line promise/always-return | ||
.then(() => { | ||
const elapsed = Math.floor((performance.now() - start) / 10) / 100; | ||
const msg = `Dinero.js currency generation successful\n😸 Done in ${elapsed}s`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this to emulate api-extractors output. It's helpful to see in the CI output. Also added cat face, if too cute I can remove it.
Merging #672 fixed the issue I was having with |
Do you want to change the base branch to |
Sorry @sarahdayan its only letting me choose from the difference branches of the |
b796265
to
da40053
Compare
da40053
to
d62f7d6
Compare
d62f7d6
to
d58dabb
Compare
d58dabb
to
22413bd
Compare
22413bd
to
b35c74f
Compare
b35c74f
to
b6c050d
Compare
@sarahdayan are you still interested in generating currency files? |
The amendments data file is formatted so each currency is on a single line, which makes it much easier to read. Though it also needs to be ignored by prettier to make sure it isn't formatted by the format script.
Also modified `build:clean` to include `lib` and `.turbo` directories.
b6c050d
to
ce9a8b0
Compare
5f555d9
to
e08d87b
Compare
Is there a reason this wasn't merged in the end? Did I just not see replacement for this? If I just completely missed it, my bad :) But as #582 is still open, we probably need a solution for this topic |
@reckter I hadn't heard from Sarah on whether or not she want to use it, so I closed it. |
As a first step to add currencies of different
TAmounts
, I created scripts to generate source files from a shared json data file. This should make implementing PR #582 trivial.Other changes
Currency<number>
source filesgenerate
script to@dinero.js/currencies
prepare
script of@dinero.js/currencies
to generate sources files before building@public
comment to allCurrency<number>
objects for api-extractor@public
comment toCurrency<TAmount>
type for api-extractor