mirror of
https://github.com/plankanban/planka.git
synced 2026-07-15 21:48:11 +03:00
fix: Prevent collisions when generating local ids
This commit is contained in:
@@ -3,6 +3,16 @@
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
export const createLocalId = () => `local:${Date.now()}`;
|
||||
let lastTimestamp = 0;
|
||||
let sequence = 0;
|
||||
|
||||
export const createLocalId = () => {
|
||||
const timestamp = Date.now();
|
||||
|
||||
sequence = timestamp <= lastTimestamp ? sequence + 1 : 0;
|
||||
lastTimestamp = timestamp;
|
||||
|
||||
return `local:${timestamp}-${String(sequence).padStart(4, '0')}`;
|
||||
};
|
||||
|
||||
export const isLocalId = (id) => id.startsWith('local:');
|
||||
|
||||
@@ -2,10 +2,10 @@ import { isLocalId } from './local-id';
|
||||
|
||||
describe('isLocalId', () => {
|
||||
test('is valid', () => {
|
||||
expect(isLocalId('local:1234567890')).toBeTruthy();
|
||||
expect(isLocalId('local:1234567890-0000')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('is invalid', () => {
|
||||
expect(isLocalId('1234567890')).toBeFalsy();
|
||||
expect(isLocalId('1234567890-0000')).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user