Comparing version
@@ -105,3 +105,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
"use strict"; | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__json__ = __webpack_require__(3); | ||
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__obj__ = __webpack_require__(6); | ||
@@ -116,3 +116,3 @@ | ||
var params = { | ||
type: 'json', | ||
type: 'object', | ||
headers: null, | ||
@@ -124,3 +124,3 @@ exportable: null, | ||
};var args = arguments[0]; | ||
if (args === undefined) throw new Error('csvJS expects at least 1 exportable!'); | ||
if (args === undefined) throw new Error('obj2csv expects at least 1 exportable!'); | ||
@@ -146,4 +146,4 @@ // Process parameters | ||
} | ||
if (params.type !== 'json') { | ||
throw new Error('Invalid exportable type. Available types is "json".'); | ||
if (params.type !== 'object') { | ||
throw new Error('Invalid exportable type. Available types is "object".'); | ||
} | ||
@@ -153,4 +153,4 @@ | ||
switch (params.type) { | ||
case 'json': | ||
__WEBPACK_IMPORTED_MODULE_0__json__["a" /* default */].export(params); | ||
case 'object': | ||
__WEBPACK_IMPORTED_MODULE_0__obj__["a" /* default */].export(params); | ||
break; | ||
@@ -162,69 +162,3 @@ } | ||
/***/ }), | ||
/* 3 */ | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
"use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var Blob = __webpack_require__(4); | ||
/* harmony default export */ __webpack_exports__["a"] = ({ | ||
export: function _export(params) { | ||
object2csv(params.headers, params.exportable, params.fileName); | ||
} | ||
/** | ||
* Function to export the json data as csv. | ||
* @param {json} headers the columns of the csv file. | ||
* @param {json} exportable the records of csv file. | ||
* @param {string} fileName the title of the file which needs to be exported. | ||
*/ | ||
});function object2csv(headers, exportable, fileName) { | ||
if (headers) { | ||
exportable.unshift(headers); | ||
} | ||
// Convert Object to JSON | ||
var jsonObject = JSON.stringify(exportable); | ||
var csv = convert2csv(jsonObject); | ||
var exportFileName = fileName; | ||
var blob = new Blob([csv], { | ||
type: 'text/csv;charset=utf-8;' | ||
}); | ||
if (navigator.msSaveBlob) { | ||
// IE 10+ | ||
navigator.msSaveBlob(blob, exportFileName); | ||
} else { | ||
var link = document.createElement('a'); | ||
if (link.download !== undefined) { | ||
// Browsers that support HTML5 download attribute | ||
var url = URL.createObjectURL(blob); | ||
link.setAttribute('href', url); | ||
link.setAttribute('download', exportFileName); | ||
link.style.visibility = 'hidden'; | ||
document.body.appendChild(link); | ||
link.click(); | ||
document.body.removeChild(link); | ||
} | ||
} | ||
} | ||
/** | ||
* Function to create an object of arrays to csv. | ||
* @param {object} objArray the json data which needs to be converted to an array. | ||
*/ | ||
function convert2csv(objArray) { | ||
var array = (typeof objArray === 'undefined' ? 'undefined' : _typeof(objArray)) !== 'object' ? JSON.parse(objArray) : objArray; | ||
var str = ''; | ||
for (var i = 0; i < array.length; i++) { | ||
var line = ''; | ||
for (var index in array[i]) { | ||
if (line !== '') line += ','; | ||
line += array[i][index]; | ||
} | ||
str += line + '\r\n'; | ||
} | ||
return str; | ||
} | ||
/***/ }), | ||
/* 3 */, | ||
/* 4 */ | ||
@@ -359,4 +293,71 @@ /***/ (function(module, exports, __webpack_require__) { | ||
/***/ }), | ||
/* 6 */ | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
"use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var Blob = __webpack_require__(4); | ||
/* harmony default export */ __webpack_exports__["a"] = ({ | ||
export: function _export(params) { | ||
object2csv(params.headers, params.exportable, params.fileName); | ||
} | ||
/** | ||
* Function to export the json data as csv. | ||
* @param {json} headers the columns of the csv file. | ||
* @param {json} exportable the records of csv file. | ||
* @param {string} fileName the title of the file which needs to be exported. | ||
*/ | ||
});function object2csv(headers, exportable, fileName) { | ||
if (headers) { | ||
exportable.unshift(headers); | ||
} | ||
// Convert Object to JSON | ||
var jsonObject = JSON.stringify(exportable); | ||
var csv = convert2csv(jsonObject); | ||
var exportFileName = fileName; | ||
var blob = new Blob([csv], { | ||
type: 'text/csv;charset=utf-8;' | ||
}); | ||
if (navigator.msSaveBlob) { | ||
// IE 10+ | ||
navigator.msSaveBlob(blob, exportFileName); | ||
} else { | ||
var link = document.createElement('a'); | ||
if (link.download !== undefined) { | ||
// Browsers that support HTML5 download attribute | ||
var url = URL.createObjectURL(blob); | ||
link.setAttribute('href', url); | ||
link.setAttribute('download', exportFileName); | ||
link.style.visibility = 'hidden'; | ||
document.body.appendChild(link); | ||
link.click(); | ||
document.body.removeChild(link); | ||
} | ||
} | ||
} | ||
/** | ||
* Function to create an object of arrays to csv. | ||
* @param {object} objArray the json data which needs to be converted to an array. | ||
*/ | ||
function convert2csv(objArray) { | ||
var array = (typeof objArray === 'undefined' ? 'undefined' : _typeof(objArray)) !== 'object' ? JSON.parse(objArray) : objArray; | ||
var str = ''; | ||
for (var i = 0; i < array.length; i++) { | ||
var line = ''; | ||
for (var index in array[i]) { | ||
if (line !== '') line += ','; | ||
line += array[i][index]; | ||
} | ||
str += line + '\r\n'; | ||
} | ||
return str; | ||
} | ||
/***/ }) | ||
/******/ ]); | ||
}); |
@@ -5,3 +5,3 @@ { | ||
"description": "A javascript based plugin to generate CSV out of an object.", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"main": "dist/obj2csv.js", | ||
@@ -8,0 +8,0 @@ "repository": "https://github.com/gharibi/obj2csv", |
'use strict' | ||
import json from './json' | ||
import obj from './obj' | ||
@@ -8,3 +8,3 @@ export default { | ||
let params = { | ||
type: 'json', | ||
type: 'object', | ||
headers: null, | ||
@@ -17,3 +17,3 @@ exportable: null, | ||
let args = arguments[0] | ||
if (args === undefined) throw new Error('csvJS expects at least 1 exportable!') | ||
if (args === undefined) throw new Error('obj2csv expects at least 1 exportable!') | ||
@@ -39,4 +39,4 @@ // Process parameters | ||
} | ||
if (params.type !== 'json') { | ||
throw new Error('Invalid exportable type. Available types is "json".') | ||
if (params.type !== 'object') { | ||
throw new Error('Invalid exportable type. Available types is "object".') | ||
} | ||
@@ -46,4 +46,4 @@ | ||
switch (params.type) { | ||
case 'json': | ||
json.export(params) | ||
case 'object': | ||
obj.export(params) | ||
break | ||
@@ -50,0 +50,0 @@ } |
@@ -0,5 +1,59 @@ | ||
var Blob = require('blob') | ||
export default { | ||
export: (params) => { | ||
console.log(params) | ||
object2csv(params.headers, params.exportable, params.fileName) | ||
} | ||
} | ||
/** | ||
* Function to export the json data as csv. | ||
* @param {json} headers the columns of the csv file. | ||
* @param {json} exportable the records of csv file. | ||
* @param {string} fileName the title of the file which needs to be exported. | ||
*/ | ||
function object2csv (headers, exportable, fileName) { | ||
if (headers) { | ||
exportable.unshift(headers) | ||
} | ||
// Convert Object to JSON | ||
let jsonObject = JSON.stringify(exportable) | ||
let csv = convert2csv(jsonObject) | ||
let exportFileName = fileName | ||
let blob = new Blob([csv], { | ||
type: 'text/csv;charset=utf-8;' | ||
}) | ||
if (navigator.msSaveBlob) { // IE 10+ | ||
navigator.msSaveBlob(blob, exportFileName) | ||
} else { | ||
let link = document.createElement('a') | ||
if (link.download !== undefined) { | ||
// Browsers that support HTML5 download attribute | ||
var url = URL.createObjectURL(blob) | ||
link.setAttribute('href', url) | ||
link.setAttribute('download', exportFileName) | ||
link.style.visibility = 'hidden' | ||
document.body.appendChild(link) | ||
link.click() | ||
document.body.removeChild(link) | ||
} | ||
} | ||
} | ||
/** | ||
* Function to create an object of arrays to csv. | ||
* @param {object} objArray the json data which needs to be converted to an array. | ||
*/ | ||
function convert2csv (objArray) { | ||
let array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray | ||
let str = '' | ||
for (var i = 0; i < array.length; i++) { | ||
var line = '' | ||
for (var index in array[i]) { | ||
if (line !== '') line += ',' | ||
line += array[i][index] | ||
} | ||
str += line + '\r\n' | ||
} | ||
return str | ||
} |
Sorry, the diff of this file is not supported yet
19661
0.18%12
-7.69%445
-0.89%