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

Commit

Permalink
samples: new mixed api samples
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Sep 28, 2018
1 parent 35e2f14 commit 0a935dc
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@

## Trunk

* samples: new mixed api samples
* travis: support Node.js 10
* samples: new column script
* samples: update syntax
Expand Down
2 changes: 1 addition & 1 deletion lib/es5/index.js
Expand Up @@ -221,7 +221,7 @@ Parser = function Parser() {
this.is_float = function (value) {
return value - parseFloat(value) + 1 >= 0; // Borrowed from jquery
};
// Internal state
// Internal private state
this._ = {
decoder: new StringDecoder(),
quoting: false,
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions samples/api.pipe.js
Expand Up @@ -3,12 +3,12 @@ const parse = require('..')
const generate = require('csv-generate')
const transform = require('stream-transform')

const generator = generate({
length: 20
})
const parser = parse({
delimiter: ':'
})
const input = generate({
length: 20
})
const transformer = transform(function(record, callback){
setTimeout(function(){
callback(null, record.join(' ')+'\n')
Expand Down
2 changes: 1 addition & 1 deletion samples/api.stream.js
Expand Up @@ -12,7 +12,7 @@ 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
// Use the readable stream api
parser.on('readable', function(){
let record
while (record = parser.read()) {
Expand Down
21 changes: 21 additions & 0 deletions samples/mixed.input_stream.js
@@ -0,0 +1,21 @@

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

// Create the parser
const parser = parse({
delimiter: ':'
}, function(err, records){
assert.deepEqual(
records,
[
[ 'root','x','0','0','root','/root','/bin/bash' ],
[ 'someone','x','1022','1022','','/home/someone','/bin/bash' ]
]
)
})
// 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()
29 changes: 29 additions & 0 deletions samples/mixed.output_stream.js
@@ -0,0 +1,29 @@

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

const output = []
parse(`
"1","2","3"
"a","b","c"
`, {
trim: true,
skip_empty_lines: true
})
// Use the readable stream api
.on('readable', function(){
let record
while (record = this.read()) {
output.push(record)
}
})
// When we are done, test that the parsed output matched what expected
.on('end', function(){
assert.deepEqual(
output,
[
[ '1','2','3' ],
[ 'a','b','c' ]
]
)
})
2 changes: 1 addition & 1 deletion src/index.coffee.md
Expand Up @@ -125,7 +125,7 @@ Options are documented [here](http://csv.adaltas.com/parse/).
# @is_float = /^(\-|\+)?([0-9]+(\.[0-9]+)([eE][0-9]+)?|Infinity)$/
# @is_float = /^(\-|\+)?((([0-9])|([1-9]+[0-9]*))(\.[0-9]+)([eE][0-9]+)?|Infinity)$/
@is_float = (value) -> (value - parseFloat( value ) + 1) >= 0 # Borrowed from jquery
# Internal state
# Internal private state
@_ =
decoder: new StringDecoder()
quoting: false
Expand Down

0 comments on commit 0a935dc

Please sign in to comment.