Skip to content

Commit

Permalink
WIP strip bom
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Aug 20, 2019
1 parent 32b5b57 commit 8bb2259
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/loaders/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import fs from 'fs'
import discernFormat from '../helpers/discernFormat'
import {formatsIndex} from '../config/equivalentFormats'
import stripBom from '../utils/stripBom.js'

export default function file (filePath, parser, parserOptions, cb) {
fs.readFile(filePath, 'utf8', function (err, data) {
Expand All @@ -26,6 +27,6 @@ export default function file (filePath, parser, parserOptions, cb) {
cb(err)
return
}
cb(null, parsed)
cb(null, stripBom(parsed))
})
}
6 changes: 2 additions & 4 deletions src/loaders/fileSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import fs from 'fs'
import discernFormat from '../helpers/discernFormat'
import {formatsIndex} from '../config/equivalentFormats'
import stripBom from '../utils/stripBom.js'

export default function file (filePath, parser, parserOptions, cb) {
var data = fs.readFileSync(filePath, 'utf8')
Expand All @@ -19,8 +20,5 @@ export default function file (filePath, parser, parserOptions, cb) {
return new Error('Your specified parser is not properly formatted. It must either be a function or have a `parse` method.')
}

// if (opts_ && opts_.flatten) {
// parsed = _.map(parsed, flatten)
// }
return parsed
return stripBom(parsed)
}
2 changes: 1 addition & 1 deletion src/utils/identity.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default (d => d)
export default d => d
11 changes: 11 additions & 0 deletions src/utils/stripBom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// from https://github.com/sindresorhus/strip-bom/blob/d5696fdc9eeb6cc8d97e390cf1de7558f74debd5/index.js#L3

export default function stripBom (string) {
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM)
if (string.charCodeAt(0) === 0xFEFF) {
return string.slice(1)
}

return string
};
3 changes: 3 additions & 0 deletions test/data/bom/bom.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name,occupation,height
jim,land surveyor,70
francis,conductor,63
38 changes: 38 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,44 @@ function assertBasicValidObject (obj, strings, row) {
}
}

describe('csv with bom characters', function () {
describe('readDataSync()', function () {
it('should be read proper keys', function () {
var file = io.readDataSync(testDataPath('bom/bom.csv'))
// file.forEach(d => {
// Object.keys(d).forEach(k => {
// d[k.trim()]= d[k]
// })
// })
assert.equal(file[0].name, 'jim')
assert.equal(file[0].occupation, 'land surveyor')
assert.equal(file[0].height, 70)
assert.equal(file[1].name, 'francis')
assert.equal(file[1].occupation, 'conductor')
assert.equal(file[1].height, 63)
})
})

describe('readData()', function () {
it('should be read proper keys', function (done) {
io.readData(testDataPath('bom/bom.csv'), (err, file) => {
// file.forEach(d => {
// Object.keys(d).forEach(k => {
// d[k.trim()]= d[k]
// })
// })
assert.equal(file[0].name, 'jim')
assert.equal(file[0].occupation, 'land surveyor')
assert.equal(file[0].height, 70)
assert.equal(file[1].name, 'francis')
assert.equal(file[1].occupation, 'conductor')
assert.equal(file[1].height, 63)
done()
})
})
})

})
describe('discernFormat()', function () {
describe('no extension', function () {
it('should be false', function () {
Expand Down

0 comments on commit 8bb2259

Please sign in to comment.