Comparing version 1.1.1 to 1.1.2
40
diff.js
@@ -85,2 +85,3 @@ /** | ||
} | ||
diffs = fix_emoji(diffs); | ||
return diffs; | ||
@@ -675,3 +676,42 @@ }; | ||
} | ||
} | ||
/* | ||
* Check diff did not split surrogate pairs. | ||
* Ex. [0, '\uD83D'], [-1, '\uDC36'], [1, '\uDC2F'] -> [-1, '\uD83D\uDC36'], [1, '\uD83D\uDC2F'] | ||
* '\uD83D\uDC36' === '🐶', '\uD83D\uDC2F' === '🐯' | ||
* | ||
* @param {Array} diffs Array of diff tuples | ||
* @return {Array} Array of diff tuples | ||
*/ | ||
function fix_emoji (diffs) { | ||
var compact = false; | ||
var starts_with_pair_end = function(str) { | ||
return str.charCodeAt(0) >= 0xDC00 && str.charCodeAt(0) <= 0xDFFF; | ||
} | ||
var ends_with_pair_start = function(str) { | ||
return str.charCodeAt(str.length-1) >= 0xD800 && str.charCodeAt(str.length-1) <= 0xDBFF; | ||
} | ||
for (var i = 2; i < diffs.length; i += 1) { | ||
if (diffs[i-2][0] === DIFF_EQUAL && ends_with_pair_start(diffs[i-2][1]) && | ||
diffs[i-1][0] === DIFF_DELETE && starts_with_pair_end(diffs[i-1][1]) && | ||
diffs[i][0] === DIFF_INSERT && starts_with_pair_end(diffs[i][1])) { | ||
compact = true; | ||
diffs[i-1][1] = diffs[i-2][1].slice(-1) + diffs[i-1][1]; | ||
diffs[i][1] = diffs[i-2][1].slice(-1) + diffs[i][1]; | ||
diffs[i-2][1] = diffs[i-2][1].slice(0, -1); | ||
} | ||
} | ||
if (!compact) { | ||
return diffs; | ||
} | ||
var fixed_diffs = []; | ||
for (var i = 0; i < diffs.length; i += 1) { | ||
if (diffs[i][1].length > 0) { | ||
fixed_diffs.push(diffs[i]); | ||
} | ||
} | ||
return fixed_diffs; | ||
} | ||
@@ -678,0 +718,0 @@ |
{ | ||
"name": "fast-diff", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Fast Javascript text diff", | ||
@@ -5,0 +5,0 @@ "author": "Jason Chen <jhchen7@gmail.com>", |
39
test.js
@@ -52,2 +52,41 @@ var _ = require('lodash'); | ||
console.log('Running emoji tests'); | ||
(function() { | ||
var result = diff('🐶', '🐯'); | ||
var expected = [ | ||
[diff.DELETE, '🐶'], | ||
[diff.INSERT, '🐯'], | ||
]; | ||
if (!_.isEqual(result, expected)) { | ||
console.log(result, '!==', expected); | ||
throw new Error('Emoji simple case test failed'); | ||
} | ||
})(); | ||
(function() { | ||
var result = diff('👨🏽', '👩🏽'); | ||
var expected = [ | ||
[diff.DELETE, '👨'], | ||
[diff.INSERT, '👩'], | ||
[diff.EQUAL, '🏽'] | ||
]; | ||
if (!_.isEqual(result, expected)) { | ||
console.log(result, '!==', expected); | ||
throw new Error('Emoji before case test failed'); | ||
} | ||
})(); | ||
(function() { | ||
var result = diff('👩🏼', '👩🏽'); | ||
var expected = [ | ||
[diff.EQUAL, '👩'], | ||
[diff.DELETE, '🏼'], | ||
[diff.INSERT, '🏽'], | ||
]; | ||
if (!_.isEqual(result, expected)) { | ||
console.log(result, '!==', expected); | ||
throw new Error('Emoji after case test failed'); | ||
} | ||
})(); | ||
console.log("Success!"); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29926
772
5