|
| 1 | +const GraphemeSplitter = require('grapheme-splitter'); |
| 2 | +const { encode: encodeUTF8 } = require('isomorphic-textencoder'); |
| 3 | + |
| 4 | +const graphemeSplitter = new GraphemeSplitter(); |
| 5 | + |
| 6 | +/* abstract */ class SubstringTooLargeForLineError extends Error { |
| 7 | + /* substring: string */ |
| 8 | + /* opts: Options */ |
| 9 | + |
| 10 | + constructor(substring/* : string */, opts/* : Options */) { |
| 11 | + super(); |
| 12 | + |
| 13 | + // Maintains proper stack trace for where our error was thrown (only available on V8) |
| 14 | + // @ts-ignore |
| 15 | + if (Error.captureStackTrace) { |
| 16 | + // @ts-ignore |
| 17 | + Error.captureStackTrace(this, this.constructor); |
| 18 | + } |
| 19 | + |
| 20 | + // Custom debugging information |
| 21 | + this.substring = substring; |
| 22 | + this.opts = opts; |
| 23 | + } |
| 24 | + |
| 25 | + get name() { |
| 26 | + return this.constructor.name; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class WordTooLargeForLineError extends SubstringTooLargeForLineError { |
| 31 | + get message() { |
| 32 | + return `${size(this.substring)} byte word can't fit in a ${this.opts.bytes} byte block: ${this.substring}`; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class GraphemeTooLargeForLineError extends SubstringTooLargeForLineError { |
| 37 | + get message() { |
| 38 | + return `${size(this.substring)} byte grapheme can't fit in a ${this.opts.bytes} byte block: ${this.substring}`; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +class CodepointTooLargeForLineError extends SubstringTooLargeForLineError { |
| 43 | + get message() { |
| 44 | + return `${size(this.substring)} byte codepoint can't fit in a ${this.opts.bytes} byte block: ${this.substring}`; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function size(str/* : string */)/* : number */ { |
| 49 | + const byteArray = encodeUTF8(str); |
| 50 | + const bytes = byteArray.byteLength; |
| 51 | + return bytes; |
| 52 | +} |
| 53 | + |
| 54 | +/* export interface Options { |
| 55 | + bytes: number, |
| 56 | + allowBreakingWords?: boolean, |
| 57 | + allowBreakingGraphemes?: boolean, |
| 58 | +} */ |
| 59 | + |
| 60 | +function * lineBreak(str/* : string */, opts/* : Options */)/* : IterableIterator<string> */ { |
| 61 | + let line = ''; |
| 62 | + let previousWhitespace = ''; |
| 63 | + |
| 64 | + for (const [word, trailingWhitespace] of wordBreak(str)) { |
| 65 | + // word fits in current line |
| 66 | + if (size(line) + size(previousWhitespace) + size(word) <= opts.bytes) { |
| 67 | + line += previousWhitespace + word; |
| 68 | + previousWhitespace = trailingWhitespace; |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + // can fit word in a line by itself |
| 73 | + if (size(word) <= opts.bytes) { |
| 74 | + if (line) { |
| 75 | + yield line; // yield previously built up line |
| 76 | + } |
| 77 | + |
| 78 | + // previously buffered whitespace is discarded as it was replaced by a line break |
| 79 | + // store new whitespace for later |
| 80 | + previousWhitespace = trailingWhitespace; |
| 81 | + |
| 82 | + line = word; // next line starts with word |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + // can't fit word into a line by itself |
| 87 | + if (!opts.allowBreakingWords) { |
| 88 | + throw new WordTooLargeForLineError(word, opts); |
| 89 | + } |
| 90 | + |
| 91 | + // try to fit part of word into current line |
| 92 | + const wordPreviousWhitespace = trailingWhitespace; |
| 93 | + for (const grapheme of graphemeSplitter.iterateGraphemes(word)) { |
| 94 | + // can fit next grapheme |
| 95 | + if (size(line) + size(previousWhitespace) + size(grapheme) <= opts.bytes) { |
| 96 | + line += previousWhitespace + grapheme; |
| 97 | + previousWhitespace = ''; |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + // can fit next grapheme into a line by itself |
| 102 | + if (size(grapheme) <= opts.bytes) { |
| 103 | + if (line) { |
| 104 | + yield line; |
| 105 | + } |
| 106 | + previousWhitespace = ''; |
| 107 | + line = grapheme; |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + // grapheme can't fit in a single line |
| 112 | + if (!opts.allowBreakingGraphemes) { |
| 113 | + throw new GraphemeTooLargeForLineError(grapheme, opts); |
| 114 | + } |
| 115 | + |
| 116 | + // break grapheme into codepoints instead |
| 117 | + for (const codepoint of grapheme) { |
| 118 | + // can fit codepoint into current line |
| 119 | + if (size(line) + size(previousWhitespace) + size(codepoint) <= opts.bytes) { |
| 120 | + line += previousWhitespace + codepoint; |
| 121 | + previousWhitespace = ''; |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + // can fit codepoint into its own line |
| 127 | + if (size(codepoint) <= opts.bytes) { |
| 128 | + if (line) { |
| 129 | + yield line; |
| 130 | + } |
| 131 | + previousWhitespace = ''; |
| 132 | + line = codepoint; |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + // can't fit codepoint into its own line |
| 137 | + throw new CodepointTooLargeForLineError(codepoint, opts); |
| 138 | + |
| 139 | + } // end of codepoint loop |
| 140 | + |
| 141 | + } // end of grapheme loop |
| 142 | + previousWhitespace = wordPreviousWhitespace; |
| 143 | + |
| 144 | + } // end of [word, trailingWhitespace] loop |
| 145 | + |
| 146 | + // unyielded leftovers when we're done iterating over the input string |
| 147 | + if (previousWhitespace) { |
| 148 | + if (size(line) + size(previousWhitespace) <= opts.bytes) { |
| 149 | + line += previousWhitespace; // retain trailing whitespace on input line if possible |
| 150 | + } |
| 151 | + } |
| 152 | + if (line) { |
| 153 | + yield line; |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// yields [word, trailingWhitespace] tuples |
| 158 | +function * wordBreak(str/* : string */)/* : IterableIterator<[string, string]> */ { |
| 159 | + let word = ''; |
| 160 | + let trailingWhitespace = ''; |
| 161 | + |
| 162 | + for (const grapheme of graphemeSplitter.iterateGraphemes(str)) { |
| 163 | + // grapheme is whitespace |
| 164 | + if (/^\s+$/.test(grapheme)) { |
| 165 | + // collect whitespace |
| 166 | + trailingWhitespace += grapheme; |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + // grapheme is non-whitespace |
| 171 | + |
| 172 | + // start of new word |
| 173 | + if (trailingWhitespace) { |
| 174 | + yield [word, trailingWhitespace]; |
| 175 | + word = grapheme; |
| 176 | + trailingWhitespace = ''; |
| 177 | + continue; |
| 178 | + } |
| 179 | + |
| 180 | + // continuation of word |
| 181 | + word += grapheme; |
| 182 | + } |
| 183 | + |
| 184 | + // possible leftovers at end of input string |
| 185 | + if (word) { |
| 186 | + yield [word, trailingWhitespace]; |
| 187 | + } |
| 188 | + // trailingWhitespace can't be non-empty unless word is non-empty |
| 189 | +} |
| 190 | + |
| 191 | +module.exports = { |
| 192 | + WordTooLargeForLineError, |
| 193 | + GraphemeTooLargeForLineError, |
| 194 | + CodepointTooLargeForLineError, |
| 195 | + lineBreak, |
| 196 | + wordBreak, |
| 197 | +}; |
0 commit comments