Skip to content

Commit

Permalink
Merge branch 'release/v0.24.21'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Sep 20, 2024
2 parents 6b7e0f6 + 14527fa commit 0022602
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.24.20",
"version": "0.24.21",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
19 changes: 18 additions & 1 deletion src/common/uuid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@

import { useBase } from './data/basex'
import { sleep } from './exec/promise'
import { suid, suidBytesDate, suidDate, uname, uuid, uuid32bit, uuidB32, uuidBytes, uuidDecode, uuidDecodeB32, uuidDecodeV4, uuidEncode, uuidEncodeB32, uuidEncodeV4, uuidv4 } from './uuid'
import { setUuidDefaultEncoding, suid, suidBytesDate, suidDate, uname, uuid, uuid32bit, uuidB32, uuidBytes, uuidDecode, uuidDecodeB32, uuidDecodeV4, uuidEncode, uuidEncodeB32, uuidEncodeV4, uuidv4 } from './uuid'

describe('uuid', () => {
// beforeAll(() => setUuidDefaultEncoding('base32'))

it('should not certain length', () => {
setUuidDefaultEncoding()
expect(uuid().length).toBe(22)
expect(uuidB32().length).toBe(26)
})

it('should encode/decode', () => {
setUuidDefaultEncoding()
const us = uuid()
const ub = uuidDecode(us)
expect(ub.length).toBe(16)
expect(uuidEncode(ub)).toEqual(us)
})

it('should not have collisions', () => {
setUuidDefaultEncoding()
const list = Array.apply(null, Array.from({ length: 100 })).map(uuid)
while (list.length) {
const id = list.pop()
Expand All @@ -29,6 +32,7 @@ describe('uuid', () => {
})

it('should not have collisions v4', () => {
setUuidDefaultEncoding()
const list = Array.apply(null, Array.from({ length: 100 })).map(uuidv4)
while (list.length) {
const id = list.pop()
Expand Down Expand Up @@ -172,13 +176,15 @@ describe('uuid', () => {
})

it('should encode / decode 62', () => {
setUuidDefaultEncoding()
const bytes = uuidBytes()
const b62 = uuidEncode(bytes)
expect(b62).toHaveLength(22)
expect(uuidDecode(b62)).toEqual(bytes)
})

it('should encode / decode 62 from string', () => {
setUuidDefaultEncoding()
const id = uuid()
expect(id).toHaveLength(22)
const b62 = uuidDecode(id)
Expand All @@ -187,6 +193,7 @@ describe('uuid', () => {
})

it('should encode / decode 62 by example', () => {
setUuidDefaultEncoding()
const id = '78MQbFaILcblSYA7WS2OGE'
expect(id).toHaveLength(22)
const b62 = uuidDecode(id)
Expand Down Expand Up @@ -235,4 +242,14 @@ describe('uuid', () => {
const decoded = uuidDecodeV4(uuid)
expect(decoded).toEqual(new Uint8Array([234, 109, 230, 115, 91, 247, 195, 211, 119, 255, 157, 220, 65, 184, 26, 190]))
})

it('should use test uuid', () => {
setUuidDefaultEncoding('test')
expect(uuid()).toEqual('test-0')
expect(uuid()).toEqual('test-1')

setUuidDefaultEncoding('test')
expect(uuid()).toEqual('test-0')
expect(uuid()).toEqual('test-1')
})
})
18 changes: 15 additions & 3 deletions src/common/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomUint8Array } from './crypto'
import { decodeBase32, decodeBase62, encodeBase32, encodeBase62 } from './data/basex'
import { fromHex, toHex } from './data/bin'
import { fromHex, toHex, toUint8Array, Uint8ArrayToString } from './data/bin'
import { getTimestamp } from './time'

// 128 bit UUID
Expand Down Expand Up @@ -139,13 +139,21 @@ const mapModes = {
uuidDecode: uuidDecodeV4,
uuidEncode: uuidEncodeV4,
},
test: {
uuid: () => uname('test'),
uuidDecode: (id: string) => toUint8Array(id),
uuidEncode: (bin: Uint8Array) => Uint8ArrayToString(bin),
},
}

let _mode: keyof typeof mapModes = 'base62'
let _sorted = false

export function setUuidDefaultEncoding(mode: keyof typeof mapModes, sorted = false) {
_mode = mode
export function setUuidDefaultEncoding(mode?: keyof typeof mapModes, sorted = false) {
if (mode === 'test')
unameReset('test')

_mode = mode ?? 'base62'
_sorted = sorted
}

Expand Down Expand Up @@ -183,6 +191,10 @@ export function uname(name = 'id'): string {
return `${name}-${_unameCounters[name]++}`
}

export function unameReset(name = 'id') {
_unameCounters[name] = 0
}

let _qid = 0

export function qid(): string {
Expand Down

0 comments on commit 0022602

Please sign in to comment.