-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
289 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# Node.js CSV demo for CommonJS | ||
|
||
The package exposes a few JavaScript and TypeScript examples to import the CSV parser using the CommonJS module loader. |
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,27 @@ | ||
|
||
// Import the package main module | ||
const csv = require('csv'); | ||
|
||
// Run the pipeline | ||
csv | ||
// Generate 20 records | ||
.generate({ | ||
delimiter: '|', | ||
length: 20 | ||
}) | ||
// Transform CSV data into records | ||
.pipe(csv.parse({ | ||
delimiter: '|' | ||
})) | ||
// Transform each value into uppercase | ||
.pipe(csv.transform((record) => { | ||
return record.map((value) => { | ||
return value.toUpperCase() | ||
}); | ||
})) | ||
// Convert objects into a stream | ||
.pipe(csv.stringify({ | ||
quoted: true | ||
})) | ||
// Print the CSV stream to stdout | ||
.pipe(process.stdout) |
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,35 @@ | ||
|
||
// Import the package main module | ||
import * as csv from 'csv' | ||
|
||
// Generate 20 records | ||
const generator: csv.generator.Generator = csv.generate({ | ||
delimiter: '|', | ||
length: 20 | ||
}) | ||
// Transform CSV data into records | ||
const parser: csv.parser.Parser = csv.parse({ | ||
delimiter: '|' | ||
}) | ||
// Transform each value into uppercase | ||
const transformer: csv.transformer.Transformer = csv.transform((record) => { | ||
return record.map((value: string) => { | ||
return value.toUpperCase() | ||
}); | ||
}) | ||
// Convert objects into a stream | ||
const stringifier: csv.stringifier.Stringifier = csv.stringify({ | ||
cast: { | ||
string: (value: string, context: csv.stringifier.CastingContext) => { | ||
return context.index % 2 ? value.toLowerCase() : value.toUpperCase(); | ||
} | ||
}, | ||
quoted: true, | ||
}) | ||
|
||
// Run the pipeline | ||
generator | ||
.pipe(parser) | ||
.pipe(transformer) | ||
.pipe(stringifier) | ||
.pipe(process.stdout) |
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,25 @@ | ||
|
||
// Import the package sync module | ||
const csv = require('csv/sync'); | ||
|
||
// Generate 20 records | ||
const input = csv.generate({ | ||
delimiter: '|', | ||
length: 20 | ||
}); | ||
// Transform CSV data into records | ||
const records = csv.parse(input, { | ||
delimiter: '|' | ||
}); | ||
// Transform each value into uppercase | ||
const uppercaseRecords = csv.transform(records, (record) => { | ||
return record.map((value) => { | ||
return value.toUpperCase() | ||
}); | ||
}); | ||
// Convert objects into a stream | ||
const output = csv.stringify(uppercaseRecords, { | ||
quoted: true | ||
}); | ||
// Print the CSV stream to stdout | ||
process.stdout.write(output); |
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,30 @@ | ||
|
||
// Import the package sync module | ||
import * as csv from 'csv/sync'; | ||
|
||
// Generate 20 records | ||
const input: string = csv.generate({ | ||
delimiter: '|', | ||
length: 20, | ||
}); | ||
// Transform CSV data into records | ||
const records: any = csv.parse(input, { | ||
delimiter: '|', | ||
}); | ||
// Transform each value into uppercase | ||
const uppercaseRecords: any = csv.transform(records, (record) => { | ||
return record.map((value: string) => { | ||
return value.toUpperCase() | ||
}); | ||
}); | ||
// Convert objects into a stream | ||
const output: any = csv.stringify(uppercaseRecords, { | ||
cast: { | ||
string: (value: string, context: csv.stringifier.CastingContext) => { | ||
return context.index % 2 ? value.toLowerCase() : value.toUpperCase(); | ||
} | ||
}, | ||
quoted: true, | ||
}); | ||
// Print the CSV stream to stdout | ||
process.stdout.write(output); |
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,34 @@ | ||
|
||
const assert = require('assert'); | ||
const { parse } = require('csv-parse'); | ||
|
||
const output = [] | ||
// Create the parser | ||
const parser = parse({ | ||
delimiter: ':' | ||
}) | ||
// Use the readable stream api to consume records | ||
parser.on('readable', function(){ | ||
let record; while ((record = parser.read()) !== null) { | ||
output.push(record) | ||
} | ||
}) | ||
// Catch any error | ||
parser.on('error', function(err){ | ||
console.error(err.message) | ||
}) | ||
// Test that the parsed records matched what's expected | ||
parser.on('end', function(){ | ||
assert.deepStrictEqual( | ||
output, | ||
[ | ||
[ 'a','b','c' ], | ||
[ '1','2','3' ] | ||
] | ||
) | ||
}) | ||
// Write data to the stream | ||
parser.write("a:b:c\n") | ||
parser.write("1:2:3\n") | ||
// Close the readable stream | ||
parser.end() |
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,34 @@ | ||
|
||
import assert from 'assert' | ||
import { parse, Parser } from 'csv-parse' | ||
|
||
const output: any = [] | ||
// Create the parser | ||
const parser: Parser = parse({ | ||
delimiter: ':' | ||
}) | ||
// Use the readable stream api to consume records | ||
parser.on('readable', function(){ | ||
let record; while ((record = parser.read()) !== null) { | ||
output.push(record) | ||
} | ||
}) | ||
// Catch any error | ||
parser.on('error', function(err){ | ||
console.error(err.message) | ||
}) | ||
// Test that the parsed records matched what's expected | ||
parser.on('end', function(){ | ||
assert.deepStrictEqual( | ||
output, | ||
[ | ||
[ 'a','b','c' ], | ||
[ '1','2','3' ] | ||
] | ||
) | ||
}) | ||
// Write data to the stream | ||
parser.write("a:b:c\n") | ||
parser.write("1:2:3\n") | ||
// Close the readable stream | ||
parser.end() |
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,19 @@ | ||
|
||
const assert = require('assert'); | ||
const { parse } = require('csv-parse/sync'); | ||
|
||
// Create the parser | ||
const records = parse([ | ||
"a:b:c\n", | ||
"1:2:3\n" | ||
].join(''), { | ||
delimiter: ':' | ||
}) | ||
// Test that the parsed records matched what's expected | ||
assert.deepStrictEqual( | ||
records, | ||
[ | ||
[ 'a','b','c' ], | ||
[ '1','2','3' ] | ||
] | ||
) |
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,19 @@ | ||
|
||
import assert from 'assert' | ||
import { parse } from 'csv-parse/sync' | ||
|
||
// Create the parser | ||
const records: [] = parse([ | ||
"a:b:c\n", | ||
"1:2:3\n" | ||
].join(''), { | ||
delimiter: ':' | ||
}) | ||
// Test that the parsed records matched what's expected | ||
assert.deepStrictEqual( | ||
records, | ||
[ | ||
[ 'a','b','c' ], | ||
[ '1','2','3' ] | ||
] | ||
) |
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,30 @@ | ||
{ | ||
"name": "csv_demo_cjs", | ||
"version": "0.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"type": "commonjs", | ||
"private": true, | ||
"devDependencies": { | ||
"@types/node": "^16.11.0", | ||
"coffeescript": "^2.6.1", | ||
"mocha": "^9.1.3", | ||
"should": "^13.2.3", | ||
"ts-node": "^10.3.0", | ||
"typescript": "^4.4.4" | ||
}, | ||
"mocha": { | ||
"throw-deprecation": true, | ||
"require": [ | ||
"should", | ||
"coffeescript/register" | ||
], | ||
"inline-diffs": true, | ||
"timeout": 40000, | ||
"reporter": "spec", | ||
"recursive": true | ||
}, | ||
"scripts": { | ||
"test": "mocha 'test/**/*.coffee'" | ||
} | ||
} |
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,23 @@ | ||
|
||
fs = require 'fs' | ||
path = require 'path' | ||
{ exec } = require 'child_process' | ||
|
||
dir = path.resolve __dirname, '../lib' | ||
samples = fs.readdirSync dir | ||
|
||
describe 'Samples', -> | ||
samples | ||
.filter (sample) -> | ||
return false unless /\.(js|ts)?$/.test sample | ||
true | ||
.map (sample) -> | ||
it "Sample #{sample}", (callback) -> | ||
ext = /\.(\w+)?$/.exec(sample)[0] | ||
bin = switch ext | ||
when '.js' | ||
'node' | ||
when '.ts' | ||
'node --loader ts-node/esm' | ||
exec "#{bin} #{path.resolve dir, sample}", (err, stdout, stderr) -> | ||
callback err |
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,9 @@ | ||
{ | ||
"compileOnSave": false, | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"module": "commonjs", | ||
"target": "ES2018", | ||
"strict": true, | ||
} | ||
} |