Skip to content

Commit 52dae93

Browse files
committed
refactor(utils): Replace GenRandomId with createElementId
- 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 <opensource@fthiessen.de>
1 parent 2b6bcd7 commit 52dae93

File tree

31 files changed

+136
-86
lines changed

31 files changed

+136
-86
lines changed

src/components/NcActionButtonGroup/NcActionButtonGroup.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default {
8787

8888
<script>
8989
import { defineComponent } from 'vue'
90-
import GenRandomId from '../../utils/GenRandomId.js'
90+
import { createElementId } from '../../utils/createElementId.ts'
9191
import { t } from '../../l10n.js'
9292
9393
/**
@@ -116,7 +116,7 @@ export default defineComponent({
116116
117117
setup() {
118118
return {
119-
labelId: `nc-action-button-group-${GenRandomId()}`,
119+
labelId: `nc-action-button-group-${createElementId()}`,
120120
}
121121
},
122122

src/components/NcActionCheckbox/NcActionCheckbox.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ This component is made to be used inside of the [NcActions](#NcActions) componen
3939

4040
<script>
4141
import ActionGlobalMixin from '../../mixins/actionGlobal.js'
42-
import GenRandomId from '../../utils/GenRandomId.js'
42+
import { createElementId } from '../../utils/createElementId.ts'
4343
4444
export default {
4545
name: 'NcActionCheckbox',
@@ -59,7 +59,7 @@ export default {
5959
*/
6060
id: {
6161
type: String,
62-
default: () => 'action-' + GenRandomId(),
62+
default: () => 'action-' + createElementId(),
6363
validator: id => id.trim() !== '',
6464
},
6565

src/components/NcActionInput/NcActionInput.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ import NcPasswordField from '../NcPasswordField/index.js'
246246
import NcSelect from '../NcSelect/index.js'
247247
import NcTextField from '../NcTextField/index.js'
248248
import ActionGlobalMixin from '../../mixins/actionGlobal.js'
249-
import GenRandomId from '../../utils/GenRandomId.js'
249+
import { createElementId } from '../../utils/createElementId.ts'
250250
import { t } from '../../l10n.js'
251251
252252
export default {
@@ -270,15 +270,15 @@ export default {
270270
*/
271271
id: {
272272
type: String,
273-
default: () => 'action-' + GenRandomId(),
273+
default: () => 'action-' + createElementId(),
274274
validator: id => id.trim() !== '',
275275
},
276276
/**
277277
* id attribute of the text input element
278278
*/
279279
inputId: {
280280
type: String,
281-
default: () => 'action-input-' + GenRandomId(),
281+
default: () => 'action-input-' + createElementId(),
282282
validator: id => id.trim() !== '',
283283
},
284284
/**

src/components/NcActionRadio/NcActionRadio.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ So that only one of each name set can be selected at the same time.
6868
<script>
6969
import { useModel } from 'vue'
7070
import ActionGlobalMixin from '../../mixins/actionGlobal.js'
71-
import GenRandomId from '../../utils/GenRandomId.js'
71+
import { createElementId } from '../../utils/createElementId.ts'
7272
7373
export default {
7474
name: 'NcActionRadio',
@@ -88,7 +88,7 @@ export default {
8888
*/
8989
id: {
9090
type: String,
91-
default: () => 'action-' + GenRandomId(),
91+
default: () => 'action-' + createElementId(),
9292
validator: id => id.trim() !== '',
9393
},
9494

src/components/NcActionTextEditable/NcActionTextEditable.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default {
8484

8585
<script>
8686
import ActionTextMixin from '../../mixins/actionText.js'
87-
import GenRandomId from '../../utils/GenRandomId.js'
87+
import { createElementId } from '../../utils/createElementId.ts'
8888
8989
import ArrowLeft from 'vue-material-design-icons/ArrowLeft.vue'
9090
import ArrowRight from 'vue-material-design-icons/ArrowRight.vue'
@@ -107,7 +107,7 @@ export default {
107107
*/
108108
id: {
109109
type: String,
110-
default: () => 'action-' + GenRandomId(),
110+
default: () => 'action-' + createElementId(),
111111
validator: id => id.trim() !== '',
112112
},
113113
/**
@@ -149,7 +149,7 @@ export default {
149149
},
150150
151151
computedId() {
152-
return GenRandomId()
152+
return createElementId()
153153
},
154154
},
155155

src/components/NcActions/NcActions.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ import { t } from '../../l10n.js'
925925
926926
import NcButton from '../NcButton/index.ts'
927927
import NcPopover from '../NcPopover/index.js'
928-
import GenRandomId from '../../utils/GenRandomId.js'
928+
import { createElementId } from '../../utils/createElementId.ts'
929929
import isSlotPopulated from '../../utils/isSlotPopulated.ts'
930930
931931
import IconDotsHorizontal from 'vue-material-design-icons/DotsHorizontal.vue'
@@ -1112,7 +1112,7 @@ export default {
11121112
],
11131113
11141114
setup(props) {
1115-
const randomId = `menu-${GenRandomId()}`
1115+
const randomId = createElementId()
11161116
const triggerRandomId = `trigger-${randomId}`
11171117
11181118
const triggerButton = ref()

src/components/NcAppNavigationItem/NcAppNavigationItem.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ import NcAppNavigationIconCollapsible from './NcAppNavigationIconCollapsible.vue
399399
import { useIsMobile } from '../../composables/useIsMobile/index.js'
400400
import NcInputConfirmCancel from './NcInputConfirmCancel.vue'
401401
import { t } from '../../l10n.js'
402-
import GenRandomId from '../../utils/GenRandomId.js'
402+
import { createElementId } from '../../utils/createElementId.ts'
403403
404404
import Pencil from 'vue-material-design-icons/Pencil.vue'
405405
import Undo from 'vue-material-design-icons/Undo.vue'
@@ -449,7 +449,7 @@ export default {
449449
*/
450450
id: {
451451
type: String,
452-
default: () => 'app-navigation-item-' + GenRandomId(),
452+
default: () => 'app-navigation-item-' + createElementId(),
453453
validator: id => id.trim() !== '',
454454
},
455455

src/components/NcAppNavigationSettings/NcAppNavigationSettings.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import type { Slot } from 'vue'
88
import { onClickOutside } from '@vueuse/core'
99
import { computed, ref, useTemplateRef } from 'vue'
10+
import { createElementId } from '../../utils/createElementId.js'
1011
import { t } from '../../l10n.js'
11-
import GenRandomId from '../../utils/GenRandomId.js'
1212
1313
import IconCog from 'vue-material-design-icons/Cog.vue'
1414
import NcButton from '../NcButton/NcButton.vue'
@@ -37,7 +37,7 @@ defineSlots<{
3737
default: Slot
3838
}>()
3939
40-
const contentId = GenRandomId()
40+
const contentId = createElementId()
4141
/**
4242
* Are the settings open
4343
*/

src/components/NcAppSidebar/NcAppSidebar.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,8 @@ import NcEmptyContent from '../NcEmptyContent/index.js'
712712
import Focus from '../../directives/Focus/index.js'
713713
import Linkify from '../../directives/Linkify/index.js'
714714
import { useIsSmallMobile } from '../../composables/useIsMobile/index.js'
715-
import GenRandomId from '../../utils/GenRandomId.js'
716-
import { getTrapStack } from '../../utils/focusTrap.ts'
715+
import { getTrapStack } from '../../utils/focusTrap.js'
716+
import { createElementId } from '../../utils/createElementId.ts'
717717
import { t } from '../../l10n.js'
718718
import isSlotPopulated from '../../utils/isSlotPopulated.ts'
719719
@@ -924,7 +924,7 @@ export default {
924924
925925
setup() {
926926
return {
927-
uid: GenRandomId(),
927+
uid: createElementId(),
928928
isMobile: useIsSmallMobile(),
929929
}
930930
},

src/components/NcBreadcrumb/NcBreadcrumb.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Renders a button element when given no redirection props, otherwise, renders <a/
6161

6262
<script>
6363
import NcActions from '../NcActions/index.js'
64-
import GenRandomId from '../../utils/GenRandomId.js'
64+
import { createElementId } from '../../utils/createElementId.ts'
6565
import NcButton from '../NcButton/NcButton.vue'
6666
6767
import ChevronRight from 'vue-material-design-icons/ChevronRight.vue'
@@ -172,7 +172,7 @@ export default {
172172
* The unique id of the breadcrumb. Necessary to append the
173173
* Actions menu to the correct crumb.
174174
*/
175-
crumbId: `crumb-id-${GenRandomId()}`,
175+
crumbId: `crumb-id-${createElementId()}`,
176176
}
177177
},
178178
computed: {

0 commit comments

Comments
 (0)