Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pass options.bigNumberSupport to all extendedTypeOf() function calls …
…and remove default property value for parameter bigNumberSupport in function extendedTypeOf() to ensure no unexpected parameter value will be processed
  • Loading branch information
SebastianG committed Feb 8, 2019
commit 955c1c360f7a9bc26e79d1fa4595631e5b5dd587
2 changes: 1 addition & 1 deletion lib/colorize.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ subcolorizeToCallback = (key, diff, output, color, indent, options) ->

looksLikeDiff = yes
for item in diff
if (extendedTypeOf(item) isnt 'array') or !((item.length is 2) or ((item.length is 1) and (item[0] is ' '))) or !(typeof(item[0]) is 'string') or item[0].length != 1 or !(item[0] in [' ', '-', '+', '~'])
if (extendedTypeOf(item, options.bigNumberSupport) isnt 'array') or !((item.length is 2) or ((item.length is 1) and (item[0] is ' '))) or !(typeof(item[0]) is 'string') or item[0].length != 1 or !(item[0] in [' ', '-', '+', '~'])
looksLikeDiff = no

if looksLikeDiff
Expand Down
14 changes: 8 additions & 6 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ objectDiff = (obj1, obj2, options = {}) ->
return [score, result]


findMatchingObject = (item, index, fuzzyOriginals) ->
findMatchingObject = (item, index, fuzzyOriginals, options) ->
# console.log "findMatchingObject: " + JSON.stringify({item, fuzzyOriginals}, null, 2)
bestMatch = null

matchIndex = 0
for own key, candidate of fuzzyOriginals when key isnt '__next'
indexDistance = Math.abs(matchIndex - index)
if extendedTypeOf(item) == extendedTypeOf(candidate)
if extendedTypeOf(item, options.bigNumberSupport) == extendedTypeOf(candidate, options.bigNumberSupport)
score = diffScore(item, candidate)
if !bestMatch || score > bestMatch.score || (score == bestMatch.score && indexDistance < bestMatch.indexDistance)
bestMatch = { score, key, indexDistance }
Expand All @@ -53,11 +53,11 @@ findMatchingObject = (item, index, fuzzyOriginals) ->
bestMatch


scalarize = (array, originals, fuzzyOriginals) ->
scalarize = (array, originals, fuzzyOriginals, options) ->
for item, index in array
if isScalar item
item
else if fuzzyOriginals && (bestMatch = findMatchingObject(item, index, fuzzyOriginals)) && bestMatch.score > 40 && !originals[bestMatch.key]?
else if fuzzyOriginals && (bestMatch = findMatchingObject(item, index, fuzzyOriginals, options)) && bestMatch.score > 40 && !originals[bestMatch.key]?
originals[bestMatch.key] = item
bestMatch.key
else
Expand All @@ -77,9 +77,9 @@ descalarize = (item, originals) ->

arrayDiff = (obj1, obj2, options = {}) ->
originals1 = { __next: 1 }
seq1 = scalarize(obj1, originals1)
seq1 = scalarize(obj1, originals1, undefined, options)
originals2 = { __next: originals1.__next }
seq2 = scalarize(obj2, originals2, originals1)
seq2 = scalarize(obj2, originals2, originals1, options)

opcodes = new SequenceMatcher(null, seq1, seq2).getOpcodes()

Expand Down Expand Up @@ -175,6 +175,8 @@ diffScore = (obj1, obj2, options = {}) ->
diffString = (obj1, obj2, colorizeOptions, diffOptions = {}) ->
if colorizeOptions != undefined and colorizeOptions.bigNumberSupport
diffOptions.bigNumberSupport = true
if diffOptions != undefined and diffOptions.bigNumberSupport
colorizeOptions.bigNumberSupport = true
return colorize(diff(obj1, obj2, diffOptions), colorizeOptions)


Expand Down
2 changes: 1 addition & 1 deletion lib/util.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BigNumber = require 'bignumber.js'

extendedTypeOf = (obj, bigNumberSupport = false) ->
extendedTypeOf = (obj, bigNumberSupport) ->
result = typeof obj
if !obj?
'null'
Expand Down