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
Next Next commit
add BigNumber Support for function diffString()
  • Loading branch information
SebastianG committed Feb 8, 2019
commit eba3d700ad5db1fdbde83a45303aaca195b7485f
34 changes: 17 additions & 17 deletions lib/colorize.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
color = require 'cli-color'
JSONbig = require 'true-json-bigint'

{ extendedTypeOf } = require './util'

Expand All @@ -8,24 +9,23 @@ Theme =
'-': color.red


subcolorizeToCallback = (key, diff, output, color, indent) ->
subcolorizeToCallback = (key, diff, output, color, indent, options) ->
prefix = if key then "#{key}: " else ''
subindent = indent + ' '

switch extendedTypeOf(diff)
switch extendedTypeOf(diff, options.bigNumberSupport)
when 'object'
if ('__old' of diff) and ('__new' of diff) and (Object.keys(diff).length is 2)
subcolorizeToCallback(key, diff.__old, output, '-', indent)
subcolorizeToCallback(key, diff.__new, output, '+', indent)
subcolorizeToCallback(key, diff.__old, output, '-', indent, options)
subcolorizeToCallback(key, diff.__new, output, '+', indent, options)
else
output color, "#{indent}#{prefix}{"
for own subkey, subvalue of diff
if m = subkey.match /^(.*)__deleted$/
subcolorizeToCallback(m[1], subvalue, output, '-', subindent)
subcolorizeToCallback(m[1], subvalue, output, '-', subindent, options)
else if m = subkey.match /^(.*)__added$/
subcolorizeToCallback(m[1], subvalue, output, '+', subindent)
subcolorizeToCallback(m[1], subvalue, output, '+', subindent, options)
else
subcolorizeToCallback(subkey, subvalue, output, color, subindent)
subcolorizeToCallback(subkey, subvalue, output, color, subindent, options)
output color, "#{indent}}"

when 'array'
Expand All @@ -42,34 +42,34 @@ subcolorizeToCallback = (key, diff, output, color, indent) ->
output(' ', subindent + '...')
else
unless op in [' ', '~', '+', '-']
throw new Error("Unexpected op '#{op}' in #{JSON.stringify(diff, null, 2)}")
throw new Error("Unexpected op '#{op}' in #{JSONbig.stringify(diff, null, 2)}")
op = ' ' if op is '~'
subcolorizeToCallback('', subvalue, output, op, subindent)
subcolorizeToCallback('', subvalue, output, op, subindent, options)
else
for subvalue in diff
subcolorizeToCallback('', subvalue, output, color, subindent)
subcolorizeToCallback('', subvalue, output, color, subindent, options)

output color, "#{indent}]"

else
if diff == 0 or diff
output(color, indent + prefix + JSON.stringify(diff))
output(color, indent + prefix + JSONbig.stringify(diff))



colorizeToCallback = (diff, output) ->
subcolorizeToCallback('', diff, output, ' ', '')
colorizeToCallback = (diff, options, output) ->
subcolorizeToCallback('', diff, output, ' ', '', options)


colorizeToArray = (diff) ->
colorizeToArray = (diff, options = {}) ->
output = []
colorizeToCallback(diff, (color, line) -> output.push "#{color}#{line}")
colorizeToCallback(diff, options, (color, line) -> output.push "#{color}#{line}")
return output


colorize = (diff, options={}) ->
output = []
colorizeToCallback diff, (color, line) ->
colorizeToCallback diff, options, (color, line) ->
if options.color ? yes
output.push (options.theme?[color] ? Theme[color])("#{color}#{line}") + "\n"
else
Expand Down
6 changes: 4 additions & 2 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ arrayDiff = (obj1, obj2, options = {}) ->


diffWithScore = (obj1, obj2, options = {}) ->
type1 = extendedTypeOf obj1
type2 = extendedTypeOf obj2
type1 = extendedTypeOf(obj1, options.bigNumberSupport)
type2 = extendedTypeOf(obj2, options.bigNumberSupport)

if type1 == type2
switch type1
Expand All @@ -173,6 +173,8 @@ diffScore = (obj1, obj2, options = {}) ->
return score

diffString = (obj1, obj2, colorizeOptions, diffOptions = {}) ->
if colorizeOptions != undefined and colorizeOptions.bigNumberSupport
diffOptions.bigNumberSupport = true
return colorize(diff(obj1, obj2, diffOptions), colorizeOptions)


Expand Down
5 changes: 4 additions & 1 deletion lib/util.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
BigNumber = require 'bignumber.js'

extendedTypeOf = (obj) ->
extendedTypeOf = (obj, bigNumberSupport = false) ->
result = typeof obj
if !obj?
'null'
else if result is 'object' and obj.constructor is Array
'array'
else if bigNumberSupport and BigNumber.isBigNumber(obj)
'number'
else
result

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"dependencies": {
"cli-color": "~0.1.6",
"difflib": "~0.2.1",
"dreamopt": "~0.6.0"
"dreamopt": "~0.6.0",
"true-json-bigint": "^0.4.4"
},
"devDependencies": {
"coffee-script": "^1.12.7",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
# yarn lockfile v1


bignumber.js@^7.0.0:
version "7.2.1"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-7.2.1.tgz#80c048759d826800807c4bfd521e50edbba57a5f"
integrity sha512-S4XzBk5sMB+Rcb/LNcpzXr57VRTxgAvaAEDAl1AwRx27j00hT84O6OkteE7u8UB3NuaaygCRrEpqox4uDOrbdQ==

cli-color@~0.1.6:
version "0.1.7"
resolved "https://registry.yarnpkg.com/cli-color/-/cli-color-0.1.7.tgz#adc3200fa471cc211b0da7f566b71e98b9d67347"
Expand Down Expand Up @@ -85,6 +90,13 @@ ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

"true-json-bigint@^0.4.4":
version "0.4.4"
resolved "https://registry.yarnpkg.com/true-json-bigint/-/true-json-bigint-0.4.4.tgz#82a2a04a959958ae0eeb819e62481db4717bdf60"
integrity sha512-RE5QJtnrn50WicRYER45GNfnd/CTULiaW3EdtalvAFuMqt/e7XB5mZArEvBRItr8+/KxCpYMMDwcuGoXId/4KA==
dependencies:
bignumber.js "^7.0.0"

wordwrap@>=0.0.2:
version "1.0.0"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"