Skip to content

Commit d400e21

Browse files
authored
fix(pinia-orm): UID Decorator: Setting Custom Alphabet Results in "undefined" Instead of Characters in UIDs (#1956)
resolves #1928
1 parent dc9d711 commit d400e21

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/pinia-orm/src/support/Utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export function generateId (size: number, alphabet: string) {
227227
let i = size
228228
while (i--) {
229229
// `| 0` is more compact and faster than `Math.floor()`.
230-
id += alphabet[(Math.random() * 64) | 0]
230+
id += alphabet[(Math.random() * alphabet.length) | 0]
231231
}
232232
return id
233233
}

packages/pinia-orm/tests/unit/model/Model_Attrs_UID.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ describe('unit/model/Model_Attrs_UID', () => {
1818
expect(new User().id).toBe('uid1')
1919
})
2020

21+
it('creates a random Uid with correct alphabet usage', () => {
22+
class User extends Model {
23+
static entity = 'users'
24+
25+
@Uid({ alphabet: '123456789', size: 5 })
26+
id!: string
27+
}
28+
29+
expect(new User().id.length).toBe(5)
30+
expect(typeof Number(new User().id)).toBe('number')
31+
})
32+
2133
it('creates a random Uid with options', () => {
2234
class User extends Model {
2335
static entity = 'users'

0 commit comments

Comments
 (0)