Skip to content

Commit

Permalink
utils, _assert: reduce code duplication with abytes function
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Feb 27, 2024
1 parent cf7e01c commit 0d26f5a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/_assert.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
function number(n: number) {
if (!Number.isSafeInteger(n) || n < 0) throw new Error(`Wrong positive integer: ${n}`);
if (!Number.isSafeInteger(n) || n < 0) throw new Error(`positive integer expected, not ${n}`);
}

function bool(b: boolean) {
if (typeof b !== 'boolean') throw new Error(`Expected boolean, not ${b}`);
if (typeof b !== 'boolean') throw new Error(`boolean expected, not ${b}`);
}

// copied from utils
function isBytes(a: unknown): a is Uint8Array {
export function isBytes(a: unknown): a is Uint8Array {
return (
a instanceof Uint8Array ||
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array')
);
}

function bytes(b: Uint8Array | undefined, ...lengths: number[]) {
if (!isBytes(b)) throw new Error('Expected Uint8Array');
if (!isBytes(b)) throw new Error('Uint8Array expected');
if (lengths.length > 0 && !lengths.includes(b.length))
throw new Error(`Expected Uint8Array of length ${lengths}, not of length=${b.length}`);
throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
}

type Hash = {
Expand Down
16 changes: 6 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// Makes the utils un-importable in browsers without a bundler.
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
import { crypto } from '@noble/hashes/crypto';
import { isBytes, bytes as abytes } from './_assert.js';

// prettier-ignore
export type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array |
Expand All @@ -17,13 +18,6 @@ export const u8 = (arr: TypedArray) => new Uint8Array(arr.buffer, arr.byteOffset
export const u32 = (arr: TypedArray) =>
new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));

function isBytes(a: unknown): a is Uint8Array {
return (
a instanceof Uint8Array ||
(a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array')
);
}

// Cast array to view
export const createView = (arr: TypedArray) =>
new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
Expand All @@ -44,6 +38,8 @@ export const byteSwap = (word: number) =>
// Conditionally byte swap if on a big-endian platform
export const byteSwapIfBE = isLE ? (n: number) => n : (n: number) => byteSwap(n);

export { isBytes };

// In place byte swap for Uint32Array
export function byteSwap32(arr: Uint32Array) {
for (let i = 0; i < arr.length; i++) {
Expand All @@ -59,7 +55,7 @@ const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) =>
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
*/
export function bytesToHex(bytes: Uint8Array): string {
if (!isBytes(bytes)) throw new Error('Uint8Array expected');
abytes(bytes);
// pre-caching improves the speed 6x
let hex = '';
for (let i = 0; i < bytes.length; i++) {
Expand Down Expand Up @@ -136,7 +132,7 @@ export type Input = Uint8Array | string;
*/
export function toBytes(data: Input): Uint8Array {
if (typeof data === 'string') data = utf8ToBytes(data);
if (!isBytes(data)) throw new Error(`expected Uint8Array, got ${typeof data}`);
abytes(data);
return data;
}

Expand All @@ -147,7 +143,7 @@ export function concatBytes(...arrays: Uint8Array[]): Uint8Array {
let sum = 0;
for (let i = 0; i < arrays.length; i++) {
const a = arrays[i];
if (!isBytes(a)) throw new Error('Uint8Array expected');
abytes(a);
sum += a.length;
}
const res = new Uint8Array(sum);
Expand Down
2 changes: 1 addition & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert');
const { should } = require('micro-should');
const { optional, integer, gen } = require('./generator');
const { byteSwap, byteSwapIfBE, byteSwap32, isLE } = require('../utils.js');
const { byteSwap, byteSwapIfBE, byteSwap32, isLE } = require('../utils');

// Here goes test for tests...
should(`Test generator`, () => {
Expand Down

0 comments on commit 0d26f5a

Please sign in to comment.