| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- /*
- inspired by https://www.npmjs.com/package/react-csv-downloader
- now removed from Github
- inspired by https://github.com/iview/iview
- */
- const newLine = '\r\n'
- const appendLine = (content, row, { separator, quoted }) => {
- const line = row.map(data => {
- if (!quoted) return data
- // quote data
- data = typeof data === 'string' ? data.replace(/"/g, '"') : data
- return `"${data}"`
- })
- content.push(line.join(separator))
- }
- const defaults = {
- separator: ',',
- quoted: false
- }
- export default function csv(columns, datas, options, noHeader = false) {
- options = Object.assign({}, defaults, options)
- let columnOrder
- const content = []
- const column = []
- if (columns) {
- columnOrder = columns.map(v => {
- if (typeof v === 'string') return v
- if (!noHeader) {
- column.push(typeof v.label !== 'undefined' ? v.label : v.prop)
- }
- return v.prop
- })
- if (column.length > 0) appendLine(content, column, options)
- } else {
- columnOrder = []
- datas.forEach(v => {
- if (!Array.isArray(v)) {
- columnOrder = columnOrder.concat(Object.keys(v))
- }
- })
- if (columnOrder.length > 0) {
- columnOrder = columnOrder.filter((value, index, self) => self.indexOf(value) === index)
- if (!noHeader) appendLine(content, columnOrder, options)
- }
- }
- if (Array.isArray(datas)) {
- datas.forEach(row => {
- if (!Array.isArray(row)) {
- row = columnOrder.map(k => (typeof row[k] !== 'undefined' ? row[k] : ''))
- }
- appendLine(content, row, options)
- })
- }
- return content.join(newLine)
- }
|