binary.js 553 B

1234567891011121314151617181920
  1. import { toUint8Array } from "../bytes/index.js";
  2. export function encode(data) {
  3. const bytes = toUint8Array(data);
  4. let result = "";
  5. for (const byte of bytes) {
  6. result += String.fromCharCode(byte);
  7. }
  8. return result;
  9. }
  10. export function decode(text) {
  11. const result = new Uint8Array(text.length);
  12. for (let i = 0; i < text.length; i++) {
  13. result[i] = text.charCodeAt(i) & 0xff;
  14. }
  15. return result;
  16. }
  17. export function is(text) {
  18. return typeof text === "string";
  19. }
  20. export const binary = { encode, decode, is };