Skip to content

Commit 19a17ac

Browse files
committed
Fix export json to turn bolt ints into js integers
1 parent 3eaca14 commit 19a17ac

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

app/scripts/directives/exportable.coffee

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ angular.module('neo.exportable', ['neo.csv'])
6868
'$scope',
6969
'CSV',
7070
'exportService'
71-
($scope, CSV, exportService) ->
71+
'BoltIntHelpers'
72+
($scope, CSV, exportService, BoltIntHelpers) ->
7273

7374
$scope.exportGraphSVG = ->
7475
$scope.$emit('frame.export.graph.svg')
@@ -88,6 +89,7 @@ angular.module('neo.exportable', ['neo.csv'])
8889

8990
$scope.exportJSON = (data) ->
9091
return unless data
92+
data = BoltIntHelpers.mapBoltIntsToInts data
9193
exportService.download('result.json', 'application/json', JSON.stringify(data))
9294

9395
$scope.exportCSV = (data) ->

lib/boltIntHelpers.coffee

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,22 @@ class neo.boltIntHelpers
3737

3838

3939
mapBoltIntsToStrings = (val) ->
40-
return val.toString() if bolt.isInt val
41-
return val.map(mapBoltIntsToStrings) if Array.isArray(val)
42-
return val if val is null
43-
if typeof val == 'object'
40+
return mapBoltInts(val, (boltInt) -> boltInt.toString())
41+
42+
mapBoltIntsToInts = (val) ->
43+
return mapBoltInts(val, (boltInt) -> parseInt(boltInt.toString()))
44+
45+
mapBoltInts = (val, mappingFunc) ->
46+
return mappingFunc val if bolt.isInt val
47+
return val.map((item) -> mapBoltInts(item, mappingFunc)) if Array.isArray(val)
48+
if typeof val == 'object' && val != null
4449
out = {}
4550
Object.keys(val).forEach((key) ->
46-
out[key] = mapBoltIntsToStrings(val[key]))
51+
out[key] = mapBoltInts(val[key], mappingFunc))
4752
return out
4853
return val
4954

5055
constructor: ->
5156
@stringify = stringify
5257
@mapBoltIntsToStrings = mapBoltIntsToStrings
58+
@mapBoltIntsToInts = mapBoltIntsToInts

test/spec/services/boltIntHelpers.coffee

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ describe 'BoltIntHelpers', () ->
5656
obj = {a:bolt.int("1"), arrayKey: arr, anotherKey: null}
5757
expect(BoltIntHelpers.stringify(obj)).toBe '{"a":1,"arrayKey":[9999,{"b":43243242423434324}],"anotherKey":null}'
5858

59-
describe 'mapBigIntegersToStrings', ->
59+
describe 'mapBoltIntegersToStrings', ->
6060

61-
it 'should map big integers to strings', ->
61+
it 'should map bolt integers to strings', ->
6262
expect(BoltIntHelpers.mapBoltIntsToStrings(bolt.int("922337203685477580"))).toBe '922337203685477580'
6363

64-
it 'should map all big integers to strings in nested obejcts', ->
64+
it 'should map all bolt integers to strings in nested obejcts', ->
6565
arr = [bolt.int("9999"), {b: bolt.int("43243242423434324")}]
6666
obj = {a:bolt.int("1"), arrayKey: arr}
6767

@@ -73,6 +73,24 @@ describe 'BoltIntHelpers', () ->
7373
it 'should return null if obejct is null', ->
7474
expect(BoltIntHelpers.mapBoltIntsToStrings(null)).toBe null
7575

76+
describe 'mapBoltIntegersToInts', ->
77+
78+
it 'should map bolt integers to plain integers', ->
79+
expect(BoltIntHelpers.mapBoltIntsToInts(bolt.int("922337203685477580"))).toBe 922337203685477600
80+
81+
it 'should map all bolt integers to integers in nested obejcts', ->
82+
arr = [bolt.int("9999"), {b: bolt.int("43243242423434324")}]
83+
obj = {a:bolt.int("1"), arrayKey: arr}
84+
85+
mappedObj = BoltIntHelpers.mapBoltIntsToInts(obj)
86+
expect(mappedObj.a).toBe(1)
87+
expect(mappedObj.arrayKey[0]).toBe(9999)
88+
expect(mappedObj.arrayKey[1].b).toBe(43243242423434324)
89+
90+
it 'should return null if obejct is null', ->
91+
expect(BoltIntHelpers.mapBoltIntsToInts(null)).toBe null
92+
93+
7694

7795

7896

0 commit comments

Comments
 (0)