binary.js 711 B

1234567891011121314151617181920212223242526
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.binary = void 0;
  4. exports.encode = encode;
  5. exports.decode = decode;
  6. exports.is = is;
  7. const index_js_1 = require("../bytes/index.js");
  8. function encode(data) {
  9. const bytes = (0, index_js_1.toUint8Array)(data);
  10. let result = "";
  11. for (const byte of bytes) {
  12. result += String.fromCharCode(byte);
  13. }
  14. return result;
  15. }
  16. function decode(text) {
  17. const result = new Uint8Array(text.length);
  18. for (let i = 0; i < text.length; i++) {
  19. result[i] = text.charCodeAt(i) & 0xff;
  20. }
  21. return result;
  22. }
  23. function is(text) {
  24. return typeof text === "string";
  25. }
  26. exports.binary = { encode, decode, is };