Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
samples: update syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Sep 24, 2018
1 parent 8c1b0a7 commit ef8c4d1
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 72 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@

## Trunk

* samples: update syntax
package: improve ignore files

## Version 3.0.0
Expand Down
13 changes: 13 additions & 0 deletions samples/api.callback.js
@@ -0,0 +1,13 @@

const parse = require('../lib')
const assert = require('assert')

const input = '#Welcome\n"1","2","3","4"\n"a","b","c","d"'
parse(input, {
comment: '#'
}, function(err, output){
assert.deepEqual(
output,
[ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ]
)
})
19 changes: 19 additions & 0 deletions samples/api.pipe.js
@@ -0,0 +1,19 @@

const parse = require('..')
const generate = require('csv-generate')
const transform = require('stream-transform')

const parser = parse({
delimiter: ':'
})
const input = generate({
length: 20
})
const transformer = transform(function(record, callback){
setTimeout(function(){
callback(null, record.join(' ')+'\n')
}, 500)
}, {
parallel: 5
})
input.pipe(parser).pipe(transformer).pipe(process.stdout)
35 changes: 35 additions & 0 deletions samples/api.stream.js
@@ -0,0 +1,35 @@

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 writable 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' ]
]
)
})
11 changes: 0 additions & 11 deletions samples/callback.js

This file was deleted.

16 changes: 16 additions & 0 deletions samples/module.sync.js
@@ -0,0 +1,16 @@

// The package "should" must be installed:
// `npm install should`

const parse = require('../lib/sync')
const assert = require('assert')

const input = `
"key_1","key_2"
"value 1","value 2"
`
const records = parse(input, {
columns: true,
skip_empty_lines: true
})
assert.deepEqual(records, [{ key_1: 'value 1', key_2: 'value 2' }])
17 changes: 0 additions & 17 deletions samples/pipe.js

This file was deleted.

34 changes: 0 additions & 34 deletions samples/stream.js

This file was deleted.

10 changes: 0 additions & 10 deletions samples/sync.js

This file was deleted.

0 comments on commit ef8c4d1

Please sign in to comment.