forked from lykmapipo/js-export
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (60 loc) · 1.63 KB
/
index.js
File metadata and controls
79 lines (60 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
//dependencies
var path = require('path');
var _ = require('lodash');
//load default engines
var excel = require(path.join(__dirname, 'lib', 'excel'));
//defaults options
var defaults = {
sheet: 'Sheet',
flat: true,
joinFieldName: true,
fieldSeparator: '_',
multi: false,
joinSheetName: false,
missing: 'NA'
};
/**
* @constructor
* @description export js data into different acceptable format
* @param {Object} options export options
* @return {Object} export instance
* @public
*/
function Export(data, options) {
//normalize options
options = options || {};
//merge defaults
options = _.merge({}, defaults, options);
//bind options
this.options = options;
//bind and normalize data
this.data = data;
if (this.data && !_.isArray(this.data)) {
this.data = [data];
}
//set default engines
this.use('excel', excel);
}
/**
* @function
* @description plugin new export engine
* @param {String} name name of the export engine
* @param {Object} exporter valid export engine definition
* @public
*/
Export.prototype.use = function(name, engine) {
//extend export with additional exporter engine
var methods = ['write', 'download'];
_.forEach(methods, function(method) {
//prepare method name
var _method = method + _.capitalize(name);
//bind export engine functions into export
this[_method] = function() {
//invoke export engine method
return engine[method].apply(this, arguments);
};
}.bind(this));
};
//export xlsform template
module.exports = exports = Export;