Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"gsap": "^3.13.0",
"is-plain-object": "^5.0.0",
"jsonpath-plus": "^10.3.0",
"nanoid": "^5.1.6",
"pixi-viewport": "^6.0.3",
"zod": "^3.25.67",
"zod-validation-error": "^3.4.0"
Expand All @@ -71,7 +72,6 @@
"rollup-plugin-copy": "^3.5.0",
"standard-version": "^9.5.0",
"typescript": "^5.9.3",
"uuid": "^13.0.0",
"vitest": "^4.0.8"
}
}
2 changes: 1 addition & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ export default [
],
}),
],
external: ['pixi.js'],
external: ['pixi.js', 'nanoid'],
},
];
11 changes: 9 additions & 2 deletions src/utils/uuid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { v4 as uuidv4 } from 'uuid';
import { customAlphabet } from 'nanoid';

export const uid = () => uuidv4().replace(/-/g, '').substring(0, 12);
// uuidv4 provides completely random values. (CSPRNG)
// nanoid includes uppercase alphabets, making it more secure (completely random like uuidv4).
// nanoid allows direct length adjustment in the function.
export const uid = () =>
customAlphabet(
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
15,
)();
Comment on lines +1 to +10

Choose a reason for hiding this comment

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

high

Calling customAlphabet inside the uid function is inefficient because it recreates the generator on every invocation. For better performance, you should create the nanoid generator once at the module level and reuse it. This will be particularly beneficial if uid is called in a loop or frequently.

Suggested change
import { customAlphabet } from 'nanoid';
export const uid = () => uuidv4().replace(/-/g, '').substring(0, 12);
// uuidv4 provides completely random values. (CSPRNG)
// nanoid includes uppercase alphabets, making it more secure (completely random like uuidv4).
// nanoid allows direct length adjustment in the function.
export const uid = () =>
customAlphabet(
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
15,
)();
import { customAlphabet } from 'nanoid';
// uuidv4 provides completely random values. (CSPRNG)
// nanoid includes uppercase alphabets, making it more secure (completely random like uuidv4).
// nanoid allows direct length adjustment in the function.
const nanoid = customAlphabet(
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
15,
);
export const uid = () => nanoid();

7 changes: 4 additions & 3 deletions src/utils/uuid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('uuid', () => {
describe('createUUID', () => {
it('should create a UUID string of length 8', () => {
const uuid = uid();
expect(uuid).toHaveLength(12);
expect(uuid).toHaveLength(15);

Choose a reason for hiding this comment

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

medium

While this assertion is correct, the test description on line 6 is misleading. It states should create a UUID string of length 8, but the test checks for a length of 15. Please update the test description to should create a UUID string of length 15 for consistency.

});

it('should create a UUID string containing only alphanumeric characters', () => {
Expand All @@ -20,11 +20,12 @@ describe('uuid', () => {
});

it('should create a new UUID every time it is called.', () => {
const size = 100000;
const uuids = new Set();
for (let i = 0; i < 1000; i++) {
for (let i = 0; i < size; i++) {
uuids.add(uid());
}
expect(uuids.size).toBe(1000);
expect(uuids.size).toBe(size);
});
});
});