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

feat: add @trezor/utils package #4666

Merged
merged 8 commits into from
Jan 28, 2022
Merged

feat: add @trezor/utils package #4666

merged 8 commits into from
Jan 28, 2022

Conversation

mroz22
Copy link
Contributor

@mroz22 mroz22 commented Dec 15, 2021

Does it sound reasonable to have a general utils package that would:

  1. hold sharable utils from across trezor-suite packages and it will likely contribute to reducing duplicities
  2. help with transferring trezor-connect into monorepo by starting with connect utils and porting them back to connect via @trezor/utils package

?

Possible candidates:

Notes:

  • target es2017?
  • inspiration from utxo lib

@mroz22 mroz22 force-pushed the trezor-utils branch 3 times, most recently from 0abb99a to 1d8e21f Compare December 22, 2021 12:11
@mroz22 mroz22 force-pushed the trezor-utils branch 3 times, most recently from c7dcb6c to e1d94f9 Compare December 22, 2021 21:00
@karliatto karliatto force-pushed the trezor-utils branch 3 times, most recently from f570e73 to 3340270 Compare January 20, 2022 12:08
@karliatto karliatto marked this pull request as ready for review January 20, 2022 12:52
@karliatto karliatto self-requested a review January 20, 2022 12:52
@mroz22 mroz22 added the READY FOR REVIEW Developed pull request ready to be reviewed by another developer label Jan 20, 2022
packages/blockchain-link/package.json Outdated Show resolved Hide resolved
packages/blockchain-link/src/index.ts Outdated Show resolved Hide resolved
packages/suite/src/actions/suite/metadataActions.ts Outdated Show resolved Hide resolved
packages/suite/src/actions/suite/modalActions.ts Outdated Show resolved Hide resolved
packages/transport/src/lowlevel/withSharedConnections.ts Outdated Show resolved Hide resolved
packages/transport/src/lowlevel/sharedConnectionWorker.ts Outdated Show resolved Hide resolved
packages/suite/src/hooks/suite/useAsyncDebounce.ts Outdated Show resolved Hide resolved
packages/suite/src/reducers/wallet/discoveryReducer.ts Outdated Show resolved Hide resolved
packages/transport/src/lowlevel/withSharedConnections.ts Outdated Show resolved Hide resolved
packages/transport/src/lowlevel/withSharedConnections.ts Outdated Show resolved Hide resolved
packages/transport/src/lowlevel/withSharedConnections.ts Outdated Show resolved Hide resolved
packages/utils/src/promise.ts Outdated Show resolved Hide resolved
packages/utils/.eslintrc.js Outdated Show resolved Hide resolved
packages/utils/.gitignore Outdated Show resolved Hide resolved
packages/utils/jest.config.js Outdated Show resolved Hide resolved
packages/utils/src/validators.ts Outdated Show resolved Hide resolved
packages/utils/src/validators.ts Outdated Show resolved Hide resolved
packages/utils/tests/validators.test.ts Outdated Show resolved Hide resolved
packages/utils/src/validators.ts Outdated Show resolved Hide resolved
packages/utils/src/index.ts Outdated Show resolved Hide resolved
packages/utils/src/file.ts Outdated Show resolved Hide resolved
@Nodonisko
Copy link
Contributor

Great work with package! Small first step to de-aliased and flatten structure.

packages/utils/.eslintrc.js Show resolved Hide resolved
packages/utils/CHANGELOG.md Outdated Show resolved Hide resolved
packages/suite/src/utils/wallet/ethUtils.ts Outdated Show resolved Hide resolved
Copy link
Contributor Author

@mroz22 mroz22 left a comment

Choose a reason for hiding this comment

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

few comments

packages/suite/src/actions/suite/analyticsActions.ts Outdated Show resolved Hide resolved
packages/suite/src/utils/suite/oauth.ts Outdated Show resolved Hide resolved
packages/utils/CHANGELOG.md Outdated Show resolved Hide resolved
packages/utils/src/index.ts Outdated Show resolved Hide resolved
@Nodonisko
Copy link
Contributor

Nodonisko commented Jan 25, 2022

I am really sorry but I have probably one more thing, that we should change. I don't like the file structure/naming right now. I thinked about it a lot a will try to explain what I mean.

What will be most common usage flow with current structure?

  1. I will find out that I need some utility function that will give me count bytes in string for my feature.
  2. I will go to utils package and take a look what I see, lot of files with names that could be related for my usage like string.ts , file.ts ...
  3. I will open file.ts and now I will have to go over all function names (imagine there could be tens of functions), ignoring implementation just looking for function witch name that could do what you want.... and you do see only other functions to work with bytes, but not that one you want... maybe we don't have one? Should I add one here?
  4. Lets try go to next file string.ts and you do the same, iterate over all function names and there it is, you found function countBytesInString . You can only wonder what is rule to decice if function will be in file.ts or string.ts.
  5. Now just briefly check implementation, and I can use it.

Now imagine same flow where is single function per file and file name is same like function name. So you have folder with files like mergeObject.ts , arrayPartion.ts , countBytesInString.ts , getRandomString.ts , getRandomNumber.ts . So flow will be like:

  1. I will find out that I need some utility function that will give me random string for my feature.
  2. I will go to utils package and I will take a look to files here (also I don't need to finding function names inside lot of other code because there are just their names) and I see countBytesInString.ts.
  3. I will open that file and check implementation and I can use it.

As you can see second flow is much simpler, faster, efficient and gives really great developer experience.

And there is more, imagine you are adding new function to current structure. With some functions like bytesToHumanReadable it could be really tricky, because you will have to figure out to which file you should add it, maybe it could go string.ts because there is already function countBytesInString or maybe you can create new file called file.ts and move it here. As you can see it's super hard to decide and you never will be sure that you did it right. So how to avoid this?

Use structure I mentioned in second flow. You will want add function? Just create file bytesToHumanReadable.ts put it here and it's done.

This also raises one question: What if there will be like hundred files in future? Will be hard to orient in these files?
Answer is that it always will be much better than few files with tens of functions inside. You don't have to guess in which file your function can be, you can just slide your eyes over filenames nicely alphabetically ordered without any implementation noise. I can you can even use CMD+P and try to guess name and it will work!

This is just small part of utils package that I used before:
Screenshot 2022-01-25 at 12 45 01

In total there were hundreds of files + test files in same folder and no one ever had issue to orient in it, maintain it, or
add new function here.

So lets make this package awesome!

@mroz22
Copy link
Contributor Author

mroz22 commented Jan 25, 2022

@Nodonisko's approach might be useful for handling of multiple environments (browser/node/native), we could have a base file for node and then override files for let's say browser. Just an idea that crossed my mind.

@karliatto
Copy link
Member

@Nodonisko I think your suggestion is great, if I understand well it reminds me to the structure of this famous utility library https://github.com/lodash/lodash

But I am not sure about having the test files in the same folder, I kind of like the idea of having a test folder where you have the tests, so those files don't bother you when you are trying to find the function you need.

@Nodonisko
Copy link
Contributor

@karliatto Yea, we can place test files to it's own folder, as we do everywhere in repo.

@karliatto karliatto force-pushed the trezor-utils branch 2 times, most recently from e779ce6 to b8ececc Compare January 26, 2022 08:07
@Nodonisko
Copy link
Contributor

It looks awesome now!

ci/test.yml Show resolved Hide resolved
packages/utils/src/isAscii.ts Outdated Show resolved Hide resolved
Copy link
Contributor Author

@mroz22 mroz22 left a comment

Choose a reason for hiding this comment

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

One last requirement from me. And lets merge it 🚀 🚀 🚀

ci/test.yml Show resolved Hide resolved
packages/transport/src/lowlevel/sharedConnectionWorker.ts Outdated Show resolved Hide resolved
packages/utils/tsconfig.json Outdated Show resolved Hide resolved
Copy link
Contributor

@szymonlesisz szymonlesisz left a comment

Choose a reason for hiding this comment

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

perfecto 👌

@matejkriz matejkriz merged commit 45016a2 into develop Jan 28, 2022
@matejkriz matejkriz deleted the trezor-utils branch January 28, 2022 13:31
@bosomt
Copy link
Contributor

bosomt commented Jan 31, 2022

@matejkriz anything that can/needs ti be checked on our side
or regress test of whole Suite after tomorrow freeze is good enough ?

@karliatto
Copy link
Member

or regress test of whole Suite after tomorrow freeze is good enough ?

Exactly this is just code organization improvements, so regress test should be enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
READY FOR REVIEW Developed pull request ready to be reviewed by another developer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants