-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6f7c5d
commit 9ba6e12
Showing
3 changed files
with
69 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}/); | ||
}) | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters