Skip to content
Merged

u8 #14

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://raw.githubusercontent.com/devcontainers/spec/refs/heads/main/schemas/devContainer.schema.json",
"name": "node",
"image": "mcr.microsoft.com/devcontainers/javascript-node:4.0.5-trixie",
"customizations": {
"vscode": {
"extensions": [
"editorconfig.editorconfig"
]
}
},
"postCreateCommand": "sudo npm install -g npm-check-updates"
}
4 changes: 2 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ jobs:

strategy:
matrix:
node-version: [20, 22, 24]
node-version: [20, 22, 24, 25]
platform: [ubuntu-latest]

runs-on: ${{ matrix.platform }}

steps:
- uses: actions/checkout@v5
- uses: actions/checkout@v6
with:
submodules: true
- name: Install pnpm
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.pnpm-store/
coverage/
docs/
lib/
Expand Down
5 changes: 5 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import jts from '@cto.af/eslint-config/jsdoc_ts.js';
import markdown from '@cto.af/eslint-config/markdown.js';
import ts from '@cto.af/eslint-config/ts.js';

ts[1].settings.n.typescriptExtensionMap = [
['.js', '.js'],
['.ts', '.ts'],
];

export default [
{
ignores: [
Expand Down
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
"files": [
"lib/*"
],
"exports": {
".": "./lib/index.mjs",
"u8": "./lib/u8.mjs"
},
"type": "module",
"keywords": [],
"author": "Joe Hildebrand <joe-github@cursive.net>",
"license": "MIT",
"repository": {
Expand All @@ -29,18 +32,18 @@
"build": "npm run test && npm run lint && npm run docs && npm pack --dry-run"
},
"devDependencies": {
"@cto.af/eslint-config": "6.1.0",
"@cto.af/eslint-config": "6.2.1",
"@eslint/markdown": "7.5.1",
"@types/node": "24.10.1",
"@types/node": "25.0.2",
"c8": "10.1.3",
"eslint": "9.39.1",
"eslint-plugin-jsdoc": "61.2.0",
"tsdown": "^0.16.3",
"typedoc": "0.28.14",
"eslint": "9.39.2",
"eslint-plugin-jsdoc": "61.5.0",
"tsdown": "^0.17.4",
"typedoc": "0.28.15",
"typescript": "5.9.3",
"typescript-eslint": "8.46.4"
"typescript-eslint": "8.49.0"
},
"packageManager": "pnpm@10.22.0",
"packageManager": "pnpm@10.25.0",
"engines": {
"node": ">=20"
}
Expand Down
753 changes: 290 additions & 463 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions src/u8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as impl from './u8Impl.ts';

/**
* Convert hex string to bytes.
*
* @param str String.
* @returns Buffer.
*/
const hexToU8: (s: string) => Uint8Array =
// @ts-expect-error fromHex not in types yet
Uint8Array.fromHex as typeof impl.hexToU8 ??
impl.hexToU8;

/**
* Convert bytes to hex string.
* @param u8 Bytes.
* @returns Hex.
*/
const u8toHex: (u8: Uint8Array) => string =
// @ts-expect-error toHex not in types yet
Uint8Array.prototype.toHex ? impl.u8toHexModern : impl.u8toHex;

export {
hexToU8,
u8toHex,
};
39 changes: 39 additions & 0 deletions src/u8Impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Convert hex string to bytes.
*
* @param str String.
* @returns Buffer.
*/
export function hexToU8(str: string): Uint8Array {
str = str.replace(/\s/g, '');
let len = Math.ceil(str.length / 2);
const res = new Uint8Array(len);
len--;
for (let end = str.length, start = end - 2;
end >= 0;
end = start, start -= 2, len--
) {
res[len] = parseInt(str.substring(start, end), 16);
}

return res;
}

/**
* Convert bytes to hex string.
* @param u8 Bytes.
* @returns Hex.
*/
export function u8toHex(u8: Uint8Array): string {
return u8.reduce((t, v) => t + v.toString(16).padStart(2, '0'), '');
}

/**
* Convert bytes to hex string.
* @param u8 Bytes.
* @returns Hex.
*/
export function u8toHexModern(u8: Uint8Array): string {
// @ts-expect-error toHex not in types yet
return u8.toHex();
}
26 changes: 26 additions & 0 deletions test/u8.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as impl from '../lib/u8Impl.mjs';
import {hexToU8, u8toHex} from '../lib/u8.mjs';
import assert from 'node:assert';
import test from 'node:test';

test('u8', async () => {
await test('u8toHex', () => {
const res = u8toHex(new Uint8Array([0, 1, 2, 0xff]));
assert.equal(res, '000102ff');
});

await test('u8toHex impl', () => {
const res = impl.u8toHex(new Uint8Array([0, 1, 2, 0xff]));
assert.equal(res, '000102ff');
});

await test('hexToU8', () => {
const res = hexToU8('000102ff');
assert.deepEqual(res, new Uint8Array([0, 1, 2, 0xff]));
});

await test('hexToU8 impl', () => {
const res = impl.hexToU8('000102ff');
assert.deepEqual(res, new Uint8Array([0, 1, 2, 0xff]));
});
});
7 changes: 5 additions & 2 deletions tsdown.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ export default defineConfig({
clean: true,
dts: true,
entry: [
'src/index.ts',
'src/*.ts',
],
format: 'esm',
minify: true,
minify: {
mangle: false,
},
outDir: 'lib',
sourcemap: false,
splitting: false,
target: 'es2022',
unbundle: true,
});