Skip to content

Commit

Permalink
Add unit tests for ids
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Sep 2, 2024
1 parent c6f7c5d commit 9ba6e12
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
22 changes: 22 additions & 0 deletions src/lib/utils/ids.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, it, expect } from 'vitest';
import {localId, uuid} from './ids';


describe('ids', () => {
describe('localId', () => {
it('should generate a new local ID', () => {
expect(localId()).toMatch(/local-unique-id-\d+/);
})
it('should generate a new local ID with a prefix', () => {
expect(localId('test')).toMatch(/test-\d+/);
})
})
describe('uuid', () => {
it('should generate a new UUID', () => {
expect(uuid()).toMatch(/[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[\da-f]{4}-[\da-f]{12}/);
})
it('should generate a new UUID with a prefix', () => {
expect(uuid('test')).toMatch(/test-[\da-f]{8}-[\da-f]{4}-4[\da-f]{3}-[\da-f]{4}-[\da-f]{12}/);
})
})
});
45 changes: 45 additions & 0 deletions src/lib/utils/ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export { v4 as uuidv4, v3 as uuidv3, v5 as uuidv5 } from 'uuid';
import { v4 as uuidv4 } from 'uuid';

let idCount = 0;

/**
* @deprecated Use `localId` instead.
*/
export function newLocalId(baseName?: string) {
idCount += 1;
return `${baseName ?? 'local-unique-id'}-${idCount}`;
}

/**
* Generates a new local ID, with an optional prefix.
*
* Don't use it for persisted IDs.
* @param baseName prefix to add to the ID
*/
export function localId(baseName?: string) {
return `${baseName ?? 'local-unique-id'}-${idCount++}`;
}

/**
* @deprecated Use `uuid` instead.
*/
export function newUuid(baseName?: string) {
if (baseName) {
return `${baseName}-${uuidv4()}`;
} else {
return uuidv4();
}
}

/**
* Generates a new v4 UUID, with an optional prefix.
* @param baseName prefix to add to the UUID
*/
export function uuid(baseName?: string) {
if (baseName) {
return `${baseName}-${uuidv4()}`;
} else {
return uuidv4();
}
}
32 changes: 2 additions & 30 deletions src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,5 @@ export * as Promise from './promise';
export * from './promise';
export * as Layout from './layout';
export * from './layout';
export { v4 as uuidv4 } from 'uuid';
import { v4 as uuidv4 } from 'uuid';
let idCount = 0;
export function newLocalId(baseName?: string) {
idCount += 1;
return `${baseName ?? 'local-unique-id'}-${idCount}`;
}

/**
* @deprecated Use `uuid` instead.
*/
export function newUuid(baseName?: string) {
if (baseName) {
return `${baseName}-${uuidv4()}`;
} else {
return uuidv4();
}
}

/**
* Generates a new v4 UUID, with an optional prefix.
* @param baseName prefix to add to the UUID
*/
export function uuid(baseName?: string) {
if (baseName) {
return `${baseName}-${uuidv4()}`;
} else {
return uuidv4();
}
}
export * as Ids from './ids';
export * from './ids';

0 comments on commit 9ba6e12

Please sign in to comment.