Skip to content

Commit

Permalink
Merge branch 'release/v0.24.13'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Aug 29, 2024
2 parents d285a05 + 023bd4e commit 00b23e7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,12 @@
"**/.{idea,git,cache,output,temp}/**"
],
"typescript.tsdk": "node_modules/typescript/lib",
"javascript.preferences.importModuleSpecifier": "relative"
"javascript.preferences.importModuleSpecifier": "relative",

"github.copilot.enable": {
"*": true,
"plaintext": false,
"markdown": false,
"scminput": false
}
}
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.12",
"version": "0.24.13",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
31 changes: 31 additions & 0 deletions src/common/assert.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from 'vitest'
import { assert, assertCondition, fatal } from './assert'

describe('assert.ts', () => {
describe('fatal', () => {
it('should throw an error with the correct message', () => {
expect(() => fatal('test message')).toThrow('test message')
})
})

describe('assert', () => {
it('should throw an error when the condition is falsy', () => {
expect(() => assert(false, 'condition is false')).toThrow('condition is false')
expect(() => assert(null, 'condition is null')).toThrow('condition is null')
expect(() => assert(Number.NaN, 'condition is NaN')).toThrow('condition is NaN')
})

it('should not throw an error when the condition is truthy', () => {
expect(() => assert(true, 'condition is true')).not.toThrow()
expect(() => assert(1, 'condition is 1')).not.toThrow()
expect(() => assert('non-empty string', 'condition is non-empty string')).not.toThrow()
})
})

describe('assertCondition', () => {
it('should behave the same as assert', () => {
expect(() => assertCondition(false, 'condition is false')).toThrow('condition is false')
expect(() => assertCondition(true, 'condition is true')).not.toThrow()
})
})
})
20 changes: 19 additions & 1 deletion src/common/uuid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,25 @@ describe('uuid', () => {
expect(uuidEncode(b62)).toEqual('78MQbFaILcblSYA7WS2OGE')

const uv4 = uuidEncodeV4(b62)
expect(uv4).toEqual('ea6de673-5bf7-c3d3-77ff9ddc41b81abe')
expect(uv4).toEqual('ea6de673-5bf7-c3d3-77ff-9ddc41b81abe')
expect(uuidDecodeV4(uv4)).toEqual(b62)
})

it('should generate a valid UUIDv4', () => {
const uuid = uuidv4()
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
expect(uuid).toMatch(uuidRegex)
})

it('should encode Uint8Array to UUIDv4 format', () => {
const bytes = new Uint8Array([234, 109, 230, 115, 91, 247, 195, 211, 119, 255, 157, 220, 65, 184, 26, 190])
const encoded = uuidEncodeV4(bytes)
expect(encoded).toEqual('ea6de673-5bf7-c3d3-77ff-9ddc41b81abe')
})

it('should decode UUIDv4 to Uint8Array', () => {
const uuid = 'ea6de673-5bf7-c3d3-77ff-9ddc41b81abe'
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]))
})
})
2 changes: 1 addition & 1 deletion src/common/uuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const uuidv4 = function () {

export function uuidEncodeV4(bytes: Uint8Array): string {
const id = toHex(bytes)
return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16)}` // 10000000 - 1000 - 4000 - 8000 - 100000000000
return `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20)}` // 10000000 - 1000 - 4000 - 8000 - 100000000000
}

export function uuidDecodeV4(uuid: string): Uint8Array {
Expand Down

0 comments on commit 00b23e7

Please sign in to comment.