-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'import-xlsx-encoding' into test
- Loading branch information
Showing
6 changed files
with
315 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
_ = require 'underscore' | ||
Csv = require 'csv' | ||
path = require 'path' | ||
Promise = require 'bluebird' | ||
iconv = require 'iconv-lite' | ||
fs = Promise.promisifyAll require('fs') | ||
Excel = require 'exceljs' | ||
|
||
debugLog = console.log | ||
debugLog = _.noop | ||
|
||
|
||
class Reader | ||
|
||
constructor: (@options = {}) -> | ||
debugLog "READER::options:", JSON.stringify(@options) | ||
@options.encoding = @options.encoding || 'utf-8' | ||
@options.csvDelimiter = @options.csvDelimiter || ',' | ||
@header = null | ||
@rows = [] | ||
|
||
if not iconv.encodingExists @options.encoding | ||
throw new Error 'Encoding does not exist: ' + @options.encoding | ||
|
||
read: (file) => | ||
debugLog "READER::stream file %s", file | ||
@inputStream = fs.createReadStream file | ||
|
||
if @options.importFormat == 'xlsx' | ||
@_readXlsx(@inputStream) | ||
else | ||
@_readCsv(@inputStream) | ||
|
||
@mapRow: (header, row) -> | ||
res = {} | ||
header.forEach (item, index) -> | ||
res[item] = row[index] | ||
res | ||
|
||
@parseCsv: (csv, delimiter, encoding) -> | ||
header = null | ||
rows = [] | ||
options = | ||
delimiter: delimiter | ||
skip_empty_lines: true | ||
|
||
# only buffer can be decoded from another encoding | ||
if csv instanceof Buffer | ||
csv = @decode(csv, encoding) | ||
|
||
new Promise (resolve, reject) -> | ||
Csv() | ||
.from.string(csv, options) | ||
.on 'record', (row) -> | ||
if not header | ||
header = row | ||
else | ||
rows.push Reader.mapRow(header, row) | ||
.on 'error', (err) -> | ||
reject(err) | ||
.on 'end', -> | ||
resolve(rows) | ||
|
||
_readCsv: (stream) => | ||
new Promise (resolve, reject) => | ||
buffers = [] | ||
|
||
# stream whole file to buffer because we need to decode it first from buffer | ||
# - iconv-lite does not support string to string decoding | ||
stream.on 'data', (buffer) -> | ||
buffers.push buffer | ||
stream.on 'error', (err) -> reject(err) | ||
stream.on 'end', => | ||
buffer = Buffer.concat(buffers) | ||
Reader.parseCsv(buffer, @options.csvDelimiter, @options.encoding) | ||
.then (parsed) -> resolve(parsed) | ||
.catch (err) -> reject(err) | ||
|
||
_readXlsx: (stream) -> | ||
workbook = new Excel.Workbook() | ||
workbook.xlsx.read(stream) | ||
.then (workbook) -> | ||
header = null | ||
rows = [] | ||
worksheet = workbook.getWorksheet(1) | ||
worksheet.eachRow (row) -> | ||
rowValues = row.values | ||
rowValues.shift() | ||
rowVaues = _.map rowValues, (item) -> | ||
if not item? or _.isObject(item) | ||
item = "" | ||
String(item) | ||
|
||
if not header | ||
header = rowVaues | ||
else | ||
rows.push Reader.mapRow(header, rowVaues) | ||
rows | ||
|
||
@decode: (buffer, encoding) -> | ||
debugLog "READER:decode from %s",encoding | ||
if encoding == 'utf-8' | ||
return buffer.toString() | ||
|
||
iconv.decode buffer, encoding | ||
|
||
module.exports = Reader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.