@postman/csv-parse
Version:
CSV parsing implementing the Node.js `stream.Transform` API
36 lines (33 loc) • 838 B
JavaScript
const parse = require('../lib')
const assert = require('assert')
const output = []
// Create the parser
const parser = parse({
delimiter: ':'
})
// Write data to the stream
parser.write("root:x:0:0:root:/root:/bin/bash\n")
parser.write("someone:x:1022:1022::/home/someone:/bin/bash\n")
// Close the readable stream
parser.end()
// Use the readable stream api
parser.on('readable', function(){
let record
while (record = parser.read()) {
output.push(record)
}
})
// Catch any error
parser.on('error', function(err){
console.error(err.message)
})
// When we are done, test that the parsed output matched what expected
parser.on('end', function(){
assert.deepEqual(
output,
[
[ 'root','x','0','0','root','/root','/bin/bash' ],
[ 'someone','x','1022','1022','','/home/someone','/bin/bash' ]
]
)
})