-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
4 changed files
with
128 additions
and
0 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
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
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,64 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { hex } from './hex'; | ||
|
||
describe('hex', () => { | ||
describe('encode', () => { | ||
it('should encode a string to hexadecimal', () => { | ||
const input = "Hello, World!"; | ||
const expected = "48656c6c6f2c20576f726c6421"; | ||
expect(hex.encode(input)).toBe(expected); | ||
}); | ||
|
||
it('should encode an ArrayBuffer to hexadecimal', () => { | ||
const input = new TextEncoder().encode("Hello").buffer; | ||
const expected = "48656c6c6f"; | ||
expect(hex.encode(input)).toBe(expected); | ||
}); | ||
|
||
it('should encode a TypedArray to hexadecimal', () => { | ||
const input = new Uint8Array([72, 101, 108, 108, 111]); | ||
const expected = "48656c6c6f"; | ||
expect(hex.encode(input)).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe('decode', () => { | ||
it('should decode a hexadecimal string to its original value', () => { | ||
const input = "48656c6c6f2c20576f726c6421"; | ||
const expected = "Hello, World!"; | ||
expect(hex.decode(input)).toBe(expected); | ||
}); | ||
|
||
it('should handle decoding of a hexadecimal string to binary data', () => { | ||
const input = "48656c6c6f"; | ||
const expected = "Hello"; | ||
expect(hex.decode(input)).toBe(expected); | ||
}); | ||
|
||
it('should throw an error for an odd-length string', () => { | ||
const input = "123"; | ||
expect(() => hex.decode(input)).toThrow(Error); | ||
}); | ||
|
||
it('should throw an error for a non-hexadecimal string', () => { | ||
const input = "zzzz"; | ||
expect(() => hex.decode(input)).toThrow(Error); | ||
}); | ||
}); | ||
|
||
describe('round-trip tests', () => { | ||
it('should return the original string after encoding and decoding', () => { | ||
const input = "Hello, Hex!"; | ||
const encoded = hex.encode(input); | ||
const decoded = hex.decode(encoded); | ||
expect(decoded).toBe(input); | ||
}); | ||
|
||
it('should handle empty strings', () => { | ||
const input = ""; | ||
const encoded = hex.encode(input); | ||
const decoded = hex.decode(encoded); | ||
expect(decoded).toBe(input); | ||
}); | ||
}); | ||
}); |
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,37 @@ | ||
import type { TypedArray } from "./type"; | ||
|
||
|
||
const hexadecimal = "0123456789abcdef"; | ||
export const hex = { | ||
encode: (data: | ||
string | ArrayBuffer | TypedArray) => { | ||
if (typeof data === "string") { | ||
data = new TextEncoder().encode(data); | ||
} | ||
if (data.byteLength === 0) { | ||
return ""; | ||
} | ||
const buffer = new Uint8Array(data); | ||
let result = ""; | ||
for (const byte of buffer) { | ||
result += byte.toString(16).padStart(2, "0"); | ||
} | ||
return result; | ||
}, | ||
decode: (data: string) => { | ||
if (!data) { | ||
return ""; | ||
} | ||
if (data.length % 2 !== 0) { | ||
throw new Error("Invalid hexadecimal string"); | ||
} | ||
if (!new RegExp(`^[${hexadecimal}]+$`).test(data)) { | ||
throw new Error("Invalid hexadecimal string"); | ||
} | ||
const result = new Uint8Array(data.length / 2); | ||
for (let i = 0; i < data.length; i += 2) { | ||
result[i / 2] = parseInt(data.slice(i, i + 2), 16); | ||
} | ||
return new TextDecoder().decode(result); | ||
} | ||
} |