MCPcopy Create free account
hub / github.com/denoland/std / diffStr

Function diffStr

internal/diff_str.ts:158–208  ·  view source on GitHub ↗
(A: string, B: string)

Source from the content-addressed store, hash-verified

156 * ```
157 */
158export function diffStr(A: string, B: string): DiffResult<string>[] {
159 // Compute multi-line diff
160 const diffResult = diff(
161 tokenize(`${unescape(A)}\n`),
162 tokenize(`${unescape(B)}\n`),
163 );
164
165 const added = [];
166 const removed = [];
167 for (const result of diffResult) {
168 if (result.type === "added") {
169 added.push(result);
170 }
171 if (result.type === "removed") {
172 removed.push(result);
173 }
174 }
175
176 // Compute word-diff
177 const hasMoreRemovedLines = added.length < removed.length;
178 const aLines = hasMoreRemovedLines ? added : removed;
179 const bLines = hasMoreRemovedLines ? removed : added;
180 let bIdx = 0;
181 for (const a of aLines) {
182 let tokens = [] as Array<DiffResult<string>>;
183 let b: undefined | ChangedDiffResult<string>;
184 const aTokens = tokenize(a.value, true);
185 // Search another diff line with at least one common token
186 while (bIdx < bLines.length) {
187 b = bLines[bIdx++];
188 const bTokens = tokenize(b!.value, true);
189 tokens = hasMoreRemovedLines
190 ? diff(bTokens, aTokens)
191 : diff(aTokens, bTokens);
192 if (
193 tokens.some(({ type, value }) =>
194 type === "common" && NON_WHITESPACE_REGEXP.test(value)
195 )
196 ) {
197 break;
198 }
199 }
200 // Register word-diff details
201 a.details = createDetails(a, tokens);
202 if (b) {
203 b.details = createDetails(b, tokens);
204 }
205 }
206
207 return diffResult;
208}

Callers 6

fnFunction · 0.90
throwSnapshotMismatchFunction · 0.85
buildEqualErrorMessageFunction · 0.85
assertEqualsFunction · 0.85
assertStrictEqualsFunction · 0.85

Calls 5

diffFunction · 0.90
tokenizeFunction · 0.85
createDetailsFunction · 0.85
unescapeFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected