From 2e53d66d6e7a0e5a71f95bc12afc213e59421cc6 Mon Sep 17 00:00:00 2001 From: Gregor Becker Date: Tue, 26 Nov 2024 11:18:41 +0100 Subject: [PATCH] fix(pinia-orm): UID Decorator: Setting Custom Alphabet Results in "undefined" Instead of Characters in UIDs --- packages/pinia-orm/src/support/Utils.ts | 2 +- .../tests/unit/model/Model_Attrs_UID.spec.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/pinia-orm/src/support/Utils.ts b/packages/pinia-orm/src/support/Utils.ts index 17e04f2ed..0b8e13bc9 100644 --- a/packages/pinia-orm/src/support/Utils.ts +++ b/packages/pinia-orm/src/support/Utils.ts @@ -227,7 +227,7 @@ export function generateId (size: number, alphabet: string) { let i = size while (i--) { // `| 0` is more compact and faster than `Math.floor()`. - id += alphabet[(Math.random() * 64) | 0] + id += alphabet[(Math.random() * alphabet.length) | 0] } return id } diff --git a/packages/pinia-orm/tests/unit/model/Model_Attrs_UID.spec.ts b/packages/pinia-orm/tests/unit/model/Model_Attrs_UID.spec.ts index 279975478..18cb43c7a 100644 --- a/packages/pinia-orm/tests/unit/model/Model_Attrs_UID.spec.ts +++ b/packages/pinia-orm/tests/unit/model/Model_Attrs_UID.spec.ts @@ -18,6 +18,18 @@ describe('unit/model/Model_Attrs_UID', () => { expect(new User().id).toBe('uid1') }) + it('creates a random Uid with correct alphabet usage', () => { + class User extends Model { + static entity = 'users' + + @Uid({ alphabet: '123456789', size: 5 }) + id!: string + } + + expect(new User().id.length).toBe(5) + expect(typeof Number(new User().id)).toBe('number') + }) + it('creates a random Uid with options', () => { class User extends Model { static entity = 'users'