(text, encoding)
| 59 | } |
| 60 | |
| 61 | function encode(text, encoding) { |
| 62 | switch (normalizeEncoding(encoding)) { |
| 63 | case "base64": |
| 64 | case "base64url": |
| 65 | return base64ToBytes(String(text).replace(/-/g, "+").replace(/_/g, "/")); |
| 66 | case "hex": |
| 67 | return hexToBytes(text); |
| 68 | case "latin1": |
| 69 | return latin1ToBytes(String(text)); |
| 70 | case "utf16le": { |
| 71 | const source = String(text); |
| 72 | const out = new Uint8Array(source.length * 2); |
| 73 | for (let i = 0; i < source.length; i++) { |
| 74 | const code = source.charCodeAt(i); |
| 75 | out[i * 2] = code & 0xff; |
| 76 | out[i * 2 + 1] = code >> 8; |
| 77 | } |
| 78 | return out; |
| 79 | } |
| 80 | default: |
| 81 | return utf8Encode(String(text)); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | function compareBytes(a, b) { |
| 86 | const shared = Math.min(a.length, b.length); |
no test coverage detected