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
|
* 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:');
|
export const isLocalId = (id) => id.startsWith('local:');
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ import { isLocalId } from './local-id';
|
|||||||
|
|
||||||
describe('isLocalId', () => {
|
describe('isLocalId', () => {
|
||||||
test('is valid', () => {
|
test('is valid', () => {
|
||||||
expect(isLocalId('local:1234567890')).toBeTruthy();
|
expect(isLocalId('local:1234567890-0000')).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is invalid', () => {
|
test('is invalid', () => {
|
||||||
expect(isLocalId('1234567890')).toBeFalsy();
|
expect(isLocalId('1234567890-0000')).toBeFalsy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user