Login    Sites MenuBlueStep

BlueStep Platform Support

RelateScript
Outline full outline 
Overview 
Data Types 
Operators 
Statements 
Functions 
Data Conversion 
String Functions 
HTML/CSS/JavaScript 
decodeURI(...) 
diff(...) 
encodeURI(...) 
getButtonHTML(...) 
getButtonURL(...) 
getPopInScript(...) 
newJSONArray(...)
B.xmlToJsonString
 
newJSONObject(...)
B.xmlToJsonString
 
putContent(...)
B.pageContent
 
scriptIncl(...) 
scriptTag(...) 
toHTML(...) 
toPlainText(...) 
Array Functions 
Date/Time Functions 
Mathematical Functions 
Advanced Functions 
Restricted Functions 
Working with Relate Data 
How Formulas Work 
Related Topics 

The diff(...) function has five variations, not counting syntax variations. Diff is short for "difference."  The function receives two String values, finds the differences between them, and highlights those differences. The highlighting is done with HTML markup. Four different algorithms can be used to identify and mark differences by comparing a line at a time, a phrase at a time, a word at a time or character sequences.  The result of the diff(...) function is a custom object with properties to explore and display the differences between the two input Strings.  The properties of this diff object are documented following the documention of the function parameters.

The diff function creates an HTML representation of the original String values with differences highlighted.  The highlighting is done with HTML markup and CSS classes.  These CSS classes can be redefined to alter the way the highlighting appears.  The line diff uses the following classes: diffLnAdd, diffLnDel, diffLnChange, diffLnNone and diffChange.  It also uses a "pre" class which replicates the effect of the "pre" tag.  All of the other diff varients use diffAdd, diffDel and diffNone for highlighting.  These classes represent added content, deleted content, changed content and unchanged content.  By default the diffLn[...] classes change background colors to make new lines green, deleted lines red, changed lines blue and unchanged lines white.  The default diff[...] classes make added text bold and black, deleted text red with strikethrough and unchanged text gray.  The diffChange class makes text black and is used by the line diff function to highlight lines that are changed, but are sufficiently different that highlighting add/remove changes within a line would be impractical.

Syntax:
diff( original-string, current-string )
diff(
original-string, current-string, sample )
diff(
original-string, current-string, line-diff )
diff(
original-string, current-string, line-diff, sample )
diff(
original-string, current-string, line-diff, sample, sensitivity )
original-string.diff( current-string )
original-string.diff( current-string, sample )
original-string.diff( current-string, line-diff )
original-string.diff( current-string, line-diff, sample )
original-string.diff( current-string, line-diff, sample, sensitivity )

Parameter Description
original-string The original String to be compared.
current-string The current, or later, String to be compared.
line-diff

A boolean value which, if true, requests a line-by-line comparison.  Line-by-line comparison is very different from other types of comparison for several reasons:  

  1. The highlighting of differences uses background colors to show lines added (green), lines removed (red) and lines changed (blue).  The other algorithms indicate things removed with strikeout and things added with bold.
  2. The result is forced into a fixed width font with no word wrapping.
  3. The differences within changed lines can be found and highlighted using one of the other diff algorithms.
  4. Since lines added are highlighted differently from lines removed, the order of original versus current/later is actually important. The highlighting style of the other diff algorithms is symmetrical for adds and removes, so the order of the inputs doesn't reallly matter.
  5. Line-base diff is designed to be viewed side-by-side showing original on the left and current on the right.  With the other diff algorithms only one side of the diff result is normally displayed.

The non-line-diff algorithms show changes with strikeout and bold.  Text that is present in one String but not the other is displayed in both results, but shown with strikeout in the String where is wasn't present and bold in the String where it was present.  If a bit of text has been replaced with another piece of text, the text from the opposite string is shown with strikeout, followed by the replacement text in bold.  Using this type of highlighting it is not necessary to display both the original and current versions.  Normally just the current version is shown since the highlighting shows all text from both versions.

sample This is a numeric, code value.  If the sample value is -1, then a phrase-based diff will be used.  If the sample value is 0 then a word-based diff will be used.  If the sample is 1 or greater, then a character based diff will be used where the "sample" value is the minimum number of consecutive character that must match following a non-matching sequence and thus mark the end of the changed region.  The default value is 0.

If using the line-based diff, then in modified lines a secondary diff algorithm may run to find changes within each line.  This parameter determines what diff algorithm will be used during the secondary phase.
sensitivity This is a floating-point value used exclusively by the line diff algorithm.  It is a percentage between 0.0 and 100.0.  When a line is found to be changed, then a secondary diff algorithm is used to find changes within the line.  Sometimes the line is completely different, and sometimes only a small part of the line is changed.  Most times when there is a complete change of the line to something different, there are still characters here and there or even whole words that match between the two separate lines.  However, it looks ridiculous to mark the entire original line with strikeout (except for a single word or a few characters) and then show the entire current line in bold. The sensitivity parameter indicates what percent of the line must match the original before differences are highlighted.  If sensitivity is 100.0 then a secondary diff is not even attempted because only a 100% match would cause the differences to be highlighted, and it's already been proven that the lines do not match 100%.

Notes:  In general, line-based and character-based diff algorithms work best for machine readable text such as software source code, XML, JSON or other formats that are readable by humans but mostly intended for computers.  Word-based and phrase-based diff algorithms work best for human language text such as sentences and paragraphs.  The longer the pieces that are being compared, the easier it is to read the diff result (line and phrase based diffs).  However, comparing shorter pieces show changes more precisely (word and character based diffs). There is a balance that must be adapted to the type of Strings being compared and the audience that will be viewing the result.

Diff Object
Property Description
original The 'original' String that was compared.
current The 'current' String that was compared.
originalDiff The 'original' String that was compared with differences highlighted.
currentDiff The 'current' String that was compared with differences highlighted.
matchedChars An integer value indicating the number of characters that matched between the two input strings.
origMatchedPercent A floating point value indicating what percent of the characters in the 'original' string are also found in the 'current' string.
curMatchedPercent A floating point value indicating what percent of the characters in the 'current' string are also found in the 'original' string.

bluestep.js: B.diff()