Skip to content

Commit 7152b98

Browse files
author
rikkertkoppes
committed
Fixed example, specs and field type checking
1 parent 67e28a4 commit 7152b98

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

example/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var json2xls = require('../lib/json2xls');
2-
var data = require('../spec/data.json');
2+
var data = require('../spec/arrayData.json');
33
var fs = require('fs');
44

55
var xls = json2xls(data,{});

lib/json2xls.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ function getType(obj,type) {
2323
}
2424
}
2525

26-
transform.beforeCellWrite = function(row, cellData, eOpt){
27-
eOpt.cellType = getType(cellData,types[i]);
28-
return cellData;
29-
};
30-
3126
//prepare json to be in the correct format for excel-export
3227
transform.prepareJson = function(json,config) {
3328
var res = {};
@@ -46,7 +41,10 @@ transform.prepareJson = function(json,config) {
4641
return {
4742
caption: key,
4843
type: getType(jsonArr[0][key],types[i]),
49-
beforeCellWrite: transform.beforeCellWrite
44+
beforeCellWrite: function(row, cellData, eOpt){
45+
eOpt.cellType = getType(cellData,types[i]);
46+
return cellData;
47+
}
5048
};
5149
});
5250
//rows

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json2xls",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "canonically transform json to an excel document",
55
"main": "lib/json2xls.js",
66
"scripts": {

spec/prepareSpec.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ var objectData = require('./objectData.json');
55

66
describe('prepare',function() {
77

8+
beforeEach(function() {
9+
this.addMatchers({
10+
toEqualFields: function() {
11+
return {
12+
compare: function(actual,expected) {
13+
var res;
14+
var res = expected && expected.all && expected.all(function(item,i) {
15+
return actual[i] && Object.keys(item).all(function(field) {
16+
return actual[i][field] === item[field];
17+
});
18+
})
19+
return {
20+
pass: res
21+
}
22+
}
23+
}
24+
}
25+
})
26+
})
27+
828
describe('when the data is an array', function() {
929

1030
describe('cols',function() {
@@ -14,33 +34,28 @@ describe('prepare',function() {
1434
});
1535
it('should create the correct cols',function() {
1636
var res = prep(arrayData);
17-
expect(res.cols).toEqual([{
37+
expect(res.cols).toEqualFields([{
1838
caption: 'name',
19-
type: 'string',
20-
beforeCellWrite: json2xls.beforeCellWrite
39+
type: 'string'
2140
},{
2241
caption: 'date',
23-
type: 'string',
24-
beforeCellWrite: json2xls.beforeCellWrite
42+
type: 'string'
2543
},{
2644
caption: 'number',
27-
type: 'number',
28-
beforeCellWrite: json2xls.beforeCellWrite
45+
type: 'number'
2946
}]);
3047
});
3148

3249
it('should create the correct cols when fields are given as array',function() {
3350
var res = prep(arrayData,{
3451
fields: ['date','name']
3552
});
36-
expect(res.cols).toEqual([{
53+
expect(res.cols).toEqualFields([{
3754
caption: 'date',
38-
type: 'string',
39-
beforeCellWrite: json2xls.beforeCellWrite
55+
type: 'string'
4056
},{
4157
caption: 'name',
42-
type: 'string',
43-
beforeCellWrite: json2xls.beforeCellWrite
58+
type: 'string'
4459
}]);
4560
});
4661

@@ -51,14 +66,12 @@ describe('prepare',function() {
5166
name: 'string'
5267
}
5368
});
54-
expect(res.cols).toEqual([{
69+
expect(res.cols).toEqualFields([{
5570
caption: 'number',
56-
type: 'string',
57-
beforeCellWrite: json2xls.beforeCellWrite
71+
type: 'string'
5872
},{
5973
caption: 'name',
60-
type: 'string',
61-
beforeCellWrite: json2xls.beforeCellWrite
74+
type: 'string'
6275
}]);
6376
});
6477

0 commit comments

Comments
 (0)