openchain-sdk-yxl-ts
Version:
OpenChain SDK for browser
26 lines (23 loc) • 635 B
JavaScript
import { Buffer } from 'buffer';
/**
* Convert Buffer or string to Uint8Array
* @param {Buffer|string} data - The data to convert
* @returns {Uint8Array} The converted Uint8Array
*/
export function toUint8Array(data) {
if (!data) return undefined;
if (data instanceof Uint8Array) {
return data;
}
if (Buffer.isBuffer(data)) {
return new Uint8Array(data);
}
if (typeof data === 'string') {
return new Uint8Array(Buffer.from(data));
}
if (Array.isArray(data)) {
return new Uint8Array(data);
}
throw new Error('Unsupported data type for conversion to Uint8Array');
}
export default toUint8Array;