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

refactor(utils): Replace GenRandomId with getRandomId #6425

Open
wants to merge 4 commits into
base: next
Choose a base branch
from

Conversation

susnux
Copy link
Contributor

@susnux susnux commented Jan 23, 2025

☑️ Resolves

  • Use Typescript for correct type annotations
  • Fix length problem (the old implementation sometimes did not yield the expected length, e.g. if a "short" random number was generated)¹
  • Add unit test for the util

¹ Did some benchmarks: Just the method is now ~10% slower. But the old method delivered too short values for ~25% of all cases.

🏁 Checklist

  • ⛑️ Tests are included or are not applicable
  • 📘 Component documentation has been extended, updated or is not applicable
  • 3️⃣ Backport to next requested with a Vue 3 upgrade

@susnux susnux added 3. to review Waiting for reviews vue 3 Related to the vue 3 migration refactor ♻️ Pull request that is neither a fix nor a feature labels Jan 23, 2025
@susnux susnux added this to the 9.0.0-alpha.7 milestone Jan 23, 2025
@susnux susnux force-pushed the chore/refactor-getrandomid branch from 8c8a1c6 to a64d43a Compare January 23, 2025 12:26
Copy link
Contributor

@ShGKme ShGKme left a comment

Choose a reason for hiding this comment

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

In the next branch I'd prefer to use useId: https://vuejs.org/api/composition-api-helpers.html#useid

It is Vue "native" way, and it's stable, unlinke Math.random().

The only problem is that it's uniq only in a single Vue package instance. For multiple apps on the page with multiple Vue it requires setting app.config.idPrefix in an app.

src/utils/getRandomId.ts Outdated Show resolved Hide resolved
@susnux
Copy link
Contributor Author

susnux commented Jan 23, 2025

In the next branch I'd prefer to use useId: https://vuejs.org/api/composition-api-helpers.html#useid
It is Vue "native" way, and it's stable, unlinke Math.random().

For apps yes but for the library we can not be sure that the user has setup the id prefix correctly.

One alternative would be to provide a custom useId (for internal use!) that outputs:

const sanitizedAppName = String(appName).replaceAll(/[^0-9a-z-_]/ig, '')
return `${sanitizedAppName}-${useId()}`

This could be used in the library while apps of course would just use useId from Vue.

- Use Typescript for correct type annotations
- Fix length problem (the old implemenation sometimes did not yield the
  expected length, e.g. if a "short" random number was generated)¹
- Add unit test for the util

¹ Did some benchmarks: Just the method is now ~10% slower. But the old
method delivered too short values for ~25% of all cases.

Signed-off-by: Ferdinand Thiessen <[email protected]>
@susnux susnux force-pushed the chore/refactor-getrandomid branch from 068c557 to 9d885b8 Compare January 23, 2025 18:11
@susnux
Copy link
Contributor Author

susnux commented Jan 23, 2025

Simplified, what do you think?

src/utils/getElementId.ts Outdated Show resolved Hide resolved
src/utils/getElementId.ts Outdated Show resolved Hide resolved
@susnux susnux requested a review from ShGKme January 24, 2025 18:35
@Antreesy
Copy link
Contributor

Antreesy commented Jan 29, 2025

Since you've merged it, please don't forget to replace GenRandomId, after backport is there: https://github.com/nextcloud-libraries/nextcloud-vue/pull/6454/files#diff-c5595ba3afc4a147c5e8b442fe46ed5f41af898aad982d3b732103fe0fedb9f3R149

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3. to review Waiting for reviews refactor ♻️ Pull request that is neither a fix nor a feature vue 3 Related to the vue 3 migration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants