MCPcopy Create free account
hub / github.com/garris/BackstopJS / buildValues

Function buildValues

compare/output/diff.js:352–403  ·  view source on GitHub ↗
(diff, components, newString, oldString, useLongestToken)

Source from the content-addressed store, hash-verified

350 };
351
352 function buildValues(diff, components, newString, oldString, useLongestToken) {
353 var componentPos = 0,
354 componentLen = components.length,
355 newPos = 0,
356 oldPos = 0;
357
358 for (; componentPos < componentLen; componentPos++) {
359 var component = components[componentPos];
360 if (!component.removed) {
361 if (!component.added && useLongestToken) {
362 var value = newString.slice(newPos, newPos + component.count);
363 value = value.map(function (value, i) {
364 var oldValue = oldString[oldPos + i];
365 return oldValue.length > value.length ? oldValue : value;
366 });
367
368 component.value = diff.join(value);
369 } else {
370 component.value = diff.join(newString.slice(newPos, newPos + component.count));
371 }
372 newPos += component.count;
373
374 // Common case
375 if (!component.added) {
376 oldPos += component.count;
377 }
378 } else {
379 component.value = diff.join(oldString.slice(oldPos, oldPos + component.count));
380 oldPos += component.count;
381
382 // Reverse add and remove so removes are output first to match common convention
383 // The diffing algorithm is tied to add then remove output and this is the simplest
384 // route to get the desired output with minimal overhead.
385 if (componentPos && components[componentPos - 1].added) {
386 var tmp = components[componentPos - 1];
387 components[componentPos - 1] = components[componentPos];
388 components[componentPos] = tmp;
389 }
390 }
391 }
392
393 // Special case handle for when one terminal is ignored (i.e. whitespace).
394 // For this case we merge the terminal into the prior string and drop the change.
395 // This is only available for string mode.
396 var lastComponent = components[componentLen - 1];
397 if (componentLen > 1 && typeof lastComponent.value === 'string' && (lastComponent.added || lastComponent.removed) && diff.equals('', lastComponent.value)) {
398 components[componentLen - 2].value += lastComponent.value;
399 components.pop();
400 }
401
402 return components;
403 }
404
405 function clonePath(path) {
406 return { newPos: path.newPos, components: path.components.slice(0) };

Callers 1

execEditLengthFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected