(str: string)
| 46 | export type Byte = number; |
| 47 | |
| 48 | export function utf8Encode(str: string): Byte[] { |
| 49 | let encoded: Byte[] = []; |
| 50 | for (let index = 0; index < str.length; index++) { |
| 51 | let codePoint = str.charCodeAt(index); |
| 52 | |
| 53 | // decode surrogate |
| 54 | // see https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae |
| 55 | if (codePoint >= 0xd800 && codePoint <= 0xdbff && str.length > index + 1) { |
| 56 | const low = str.charCodeAt(index + 1); |
| 57 | if (low >= 0xdc00 && low <= 0xdfff) { |
| 58 | index++; |
| 59 | codePoint = ((codePoint - 0xd800) << 10) + low - 0xdc00 + 0x10000; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (codePoint <= 0x7f) { |
| 64 | encoded.push(codePoint); |
| 65 | } else if (codePoint <= 0x7ff) { |
| 66 | encoded.push(((codePoint >> 6) & 0x1f) | 0xc0, (codePoint & 0x3f) | 0x80); |
| 67 | } else if (codePoint <= 0xffff) { |
| 68 | encoded.push( |
| 69 | (codePoint >> 12) | 0xe0, |
| 70 | ((codePoint >> 6) & 0x3f) | 0x80, |
| 71 | (codePoint & 0x3f) | 0x80, |
| 72 | ); |
| 73 | } else if (codePoint <= 0x1fffff) { |
| 74 | encoded.push( |
| 75 | ((codePoint >> 18) & 0x07) | 0xf0, |
| 76 | ((codePoint >> 12) & 0x3f) | 0x80, |
| 77 | ((codePoint >> 6) & 0x3f) | 0x80, |
| 78 | (codePoint & 0x3f) | 0x80, |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return encoded; |
| 84 | } |
| 85 | |
| 86 | export function stringify(token: any): string { |
| 87 | if (typeof token === 'string') { |
no test coverage detected
searching dependent graphs…