MCPcopy Index your code
hub / github.com/danvk/webdiff / splitIntoWords

Function splitIntoWords

ts/codediff/char-diffs.ts:110–141  ·  view source on GitHub ↗
(line: string)

Source from the content-addressed store, hash-verified

108 * splitIntoWords_(line).join('') == line.
109 */
110export function splitIntoWords(line: string): string[] {
111 const LC = 0,
112 UC = 2,
113 NUM = 3,
114 WS = 4,
115 SYM = 5;
116 const charType = function (c: string) {
117 if (/[a-z]/.exec(c)) return LC;
118 if (/[A-Z]/.exec(c)) return UC;
119 if (/[0-9]/.exec(c)) return NUM;
120 if (/\s/.exec(c)) return WS;
121 return SYM;
122 };
123
124 // Single words can be [A-Z][a-z]+, [A-Z]+, [a-z]+, [0-9]+ or \s+.
125 const words = [];
126 let lastType = -1;
127 for (let i = 0; i < line.length; i++) {
128 const c = line.charAt(i);
129 const ct = charType(c);
130 if (
131 (ct == lastType && ct != SYM && ct != WS) ||
132 (ct == LC && lastType == UC && words[words.length - 1].length == 1)
133 ) {
134 words[words.length - 1] += c;
135 } else {
136 words.push(c);
137 }
138 lastType = ct;
139 }
140 return words;
141}
142
143// codes are (span class, start, end) triples.
144// This merges consecutive runs with the same class, which simplifies the HTML.

Callers 2

computeCharacterDiffsFunction · 0.85

Calls 1

charTypeFunction · 0.85

Tested by

no test coverage detected