Skip to content

Commit 66006cf

Browse files
committed
docs: cjs demo
1 parent e26fd26 commit 66006cf

12 files changed

+289
-0
lines changed

demo/cjs/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# Node.js CSV demo for CommonJS
3+
4+
The package exposes a few JavaScript and TypeScript examples to import the CSV parser using the CommonJS module loader.

demo/cjs/lib/csv.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// Import the package main module
3+
const csv = require('csv');
4+
5+
// Run the pipeline
6+
csv
7+
// Generate 20 records
8+
.generate({
9+
delimiter: '|',
10+
length: 20
11+
})
12+
// Transform CSV data into records
13+
.pipe(csv.parse({
14+
delimiter: '|'
15+
}))
16+
// Transform each value into uppercase
17+
.pipe(csv.transform((record) => {
18+
return record.map((value) => {
19+
return value.toUpperCase()
20+
});
21+
}))
22+
// Convert objects into a stream
23+
.pipe(csv.stringify({
24+
quoted: true
25+
}))
26+
// Print the CSV stream to stdout
27+
.pipe(process.stdout)

demo/cjs/lib/csv.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
// Import the package main module
3+
import * as csv from 'csv'
4+
5+
// Generate 20 records
6+
const generator: csv.generator.Generator = csv.generate({
7+
delimiter: '|',
8+
length: 20
9+
})
10+
// Transform CSV data into records
11+
const parser: csv.parser.Parser = csv.parse({
12+
delimiter: '|'
13+
})
14+
// Transform each value into uppercase
15+
const transformer: csv.transformer.Transformer = csv.transform((record) => {
16+
return record.map((value: string) => {
17+
return value.toUpperCase()
18+
});
19+
})
20+
// Convert objects into a stream
21+
const stringifier: csv.stringifier.Stringifier = csv.stringify({
22+
cast: {
23+
string: (value: string, context: csv.stringifier.CastingContext) => {
24+
return context.index % 2 ? value.toLowerCase() : value.toUpperCase();
25+
}
26+
},
27+
quoted: true,
28+
})
29+
30+
// Run the pipeline
31+
generator
32+
.pipe(parser)
33+
.pipe(transformer)
34+
.pipe(stringifier)
35+
.pipe(process.stdout)

demo/cjs/lib/csv_sync.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
// Import the package sync module
3+
const csv = require('csv/sync');
4+
5+
// Generate 20 records
6+
const input = csv.generate({
7+
delimiter: '|',
8+
length: 20
9+
});
10+
// Transform CSV data into records
11+
const records = csv.parse(input, {
12+
delimiter: '|'
13+
});
14+
// Transform each value into uppercase
15+
const uppercaseRecords = csv.transform(records, (record) => {
16+
return record.map((value) => {
17+
return value.toUpperCase()
18+
});
19+
});
20+
// Convert objects into a stream
21+
const output = csv.stringify(uppercaseRecords, {
22+
quoted: true
23+
});
24+
// Print the CSV stream to stdout
25+
process.stdout.write(output);

demo/cjs/lib/csv_sync.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
// Import the package sync module
3+
import * as csv from 'csv/sync';
4+
5+
// Generate 20 records
6+
const input: string = csv.generate({
7+
delimiter: '|',
8+
length: 20,
9+
});
10+
// Transform CSV data into records
11+
const records: any = csv.parse(input, {
12+
delimiter: '|',
13+
});
14+
// Transform each value into uppercase
15+
const uppercaseRecords: any = csv.transform(records, (record) => {
16+
return record.map((value: string) => {
17+
return value.toUpperCase()
18+
});
19+
});
20+
// Convert objects into a stream
21+
const output: any = csv.stringify(uppercaseRecords, {
22+
cast: {
23+
string: (value: string, context: csv.stringifier.CastingContext) => {
24+
return context.index % 2 ? value.toLowerCase() : value.toUpperCase();
25+
}
26+
},
27+
quoted: true,
28+
});
29+
// Print the CSV stream to stdout
30+
process.stdout.write(output);

demo/cjs/lib/parse.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
const assert = require('assert');
3+
const { parse } = require('csv-parse');
4+
5+
const output = []
6+
// Create the parser
7+
const parser = parse({
8+
delimiter: ':'
9+
})
10+
// Use the readable stream api to consume records
11+
parser.on('readable', function(){
12+
let record; while ((record = parser.read()) !== null) {
13+
output.push(record)
14+
}
15+
})
16+
// Catch any error
17+
parser.on('error', function(err){
18+
console.error(err.message)
19+
})
20+
// Test that the parsed records matched what's expected
21+
parser.on('end', function(){
22+
assert.deepStrictEqual(
23+
output,
24+
[
25+
[ 'a','b','c' ],
26+
[ '1','2','3' ]
27+
]
28+
)
29+
})
30+
// Write data to the stream
31+
parser.write("a:b:c\n")
32+
parser.write("1:2:3\n")
33+
// Close the readable stream
34+
parser.end()

demo/cjs/lib/parse.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import assert from 'assert'
3+
import { parse, Parser } from 'csv-parse'
4+
5+
const output: any = []
6+
// Create the parser
7+
const parser: Parser = parse({
8+
delimiter: ':'
9+
})
10+
// Use the readable stream api to consume records
11+
parser.on('readable', function(){
12+
let record; while ((record = parser.read()) !== null) {
13+
output.push(record)
14+
}
15+
})
16+
// Catch any error
17+
parser.on('error', function(err){
18+
console.error(err.message)
19+
})
20+
// Test that the parsed records matched what's expected
21+
parser.on('end', function(){
22+
assert.deepStrictEqual(
23+
output,
24+
[
25+
[ 'a','b','c' ],
26+
[ '1','2','3' ]
27+
]
28+
)
29+
})
30+
// Write data to the stream
31+
parser.write("a:b:c\n")
32+
parser.write("1:2:3\n")
33+
// Close the readable stream
34+
parser.end()

demo/cjs/lib/parse_sync.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
const assert = require('assert');
3+
const { parse } = require('csv-parse/sync');
4+
5+
// Create the parser
6+
const records = parse([
7+
"a:b:c\n",
8+
"1:2:3\n"
9+
].join(''), {
10+
delimiter: ':'
11+
})
12+
// Test that the parsed records matched what's expected
13+
assert.deepStrictEqual(
14+
records,
15+
[
16+
[ 'a','b','c' ],
17+
[ '1','2','3' ]
18+
]
19+
)

demo/cjs/lib/parse_sync.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import assert from 'assert'
3+
import { parse } from 'csv-parse/sync'
4+
5+
// Create the parser
6+
const records: [] = parse([
7+
"a:b:c\n",
8+
"1:2:3\n"
9+
].join(''), {
10+
delimiter: ':'
11+
})
12+
// Test that the parsed records matched what's expected
13+
assert.deepStrictEqual(
14+
records,
15+
[
16+
[ 'a','b','c' ],
17+
[ '1','2','3' ]
18+
]
19+
)

demo/cjs/package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "csv_demo_cjs",
3+
"version": "0.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"type": "commonjs",
7+
"private": true,
8+
"devDependencies": {
9+
"@types/node": "^16.11.0",
10+
"coffeescript": "^2.6.1",
11+
"mocha": "^9.1.3",
12+
"should": "^13.2.3",
13+
"ts-node": "^10.3.0",
14+
"typescript": "^4.4.4"
15+
},
16+
"mocha": {
17+
"throw-deprecation": true,
18+
"require": [
19+
"should",
20+
"coffeescript/register"
21+
],
22+
"inline-diffs": true,
23+
"timeout": 40000,
24+
"reporter": "spec",
25+
"recursive": true
26+
},
27+
"scripts": {
28+
"test": "mocha 'test/**/*.coffee'"
29+
}
30+
}

demo/cjs/test/samples.coffee

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
fs = require 'fs'
3+
path = require 'path'
4+
{ exec } = require 'child_process'
5+
6+
dir = path.resolve __dirname, '../lib'
7+
samples = fs.readdirSync dir
8+
9+
describe 'Samples', ->
10+
samples
11+
.filter (sample) ->
12+
return false unless /\.(js|ts)?$/.test sample
13+
true
14+
.map (sample) ->
15+
it "Sample #{sample}", (callback) ->
16+
ext = /\.(\w+)?$/.exec(sample)[0]
17+
bin = switch ext
18+
when '.js'
19+
'node'
20+
when '.ts'
21+
'node --loader ts-node/esm'
22+
exec "#{bin} #{path.resolve dir, sample}", (err, stdout, stderr) ->
23+
callback err

demo/cjs/tsconfig.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compileOnSave": false,
3+
"compilerOptions": {
4+
"esModuleInterop": true,
5+
"module": "commonjs",
6+
"target": "ES2018",
7+
"strict": true,
8+
}
9+
}

0 commit comments

Comments
 (0)