- Removed RNG interface and related utility classes from logic package. - Added RNG interface and utility classes in common package. - Implemented various RNG classes (ConstantRNG, MidpointRNG, SineRNG, SequenceRNG) in common package. - Updated conversion utilities for BytesLike to ArrayBuffer and Uint8Array in common package. - Adjusted tests to import RNG utilities from the new common package. - Updated vitest configuration to resolve common package imports.
31 lines
960 B
TypeScript
31 lines
960 B
TypeScript
import type { BytesLike } from './BytesLike.js';
|
|
|
|
export function convertBytesLikeToUint8Array(
|
|
data: BytesLike,
|
|
encodeUTF8 = true
|
|
): Uint8Array<ArrayBuffer> {
|
|
if (data instanceof Uint8Array) {
|
|
if (
|
|
data.buffer instanceof ArrayBuffer
|
|
&& data.byteOffset === 0
|
|
&& data.byteLength === data.buffer.byteLength
|
|
) {
|
|
return data;
|
|
}
|
|
return new Uint8Array(data) as Uint8Array<ArrayBuffer>;
|
|
}
|
|
if (data instanceof ArrayBuffer) {
|
|
return new Uint8Array(data);
|
|
}
|
|
if (data instanceof DataView) {
|
|
return new Uint8Array<ArrayBuffer>(data.buffer, data.byteOffset, data.byteLength);
|
|
}
|
|
if (typeof (data) === 'string') {
|
|
if (encodeUTF8) {
|
|
return (new TextEncoder()).encode(data);
|
|
}
|
|
return new Uint8Array(data.split('').map((s) => s.codePointAt(0) as number));
|
|
}
|
|
throw new Error('Unsupported BytesLike');
|
|
}
|