Skip to content

Commit

Permalink
docs: cjs demo
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Oct 22, 2021
1 parent e26fd26 commit 66006cf
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 0 deletions.
4 changes: 4 additions & 0 deletions demo/cjs/README.md
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.
27 changes: 27 additions & 0 deletions demo/cjs/lib/csv.js
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)
35 changes: 35 additions & 0 deletions demo/cjs/lib/csv.ts
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)
25 changes: 25 additions & 0 deletions demo/cjs/lib/csv_sync.js
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);
30 changes: 30 additions & 0 deletions demo/cjs/lib/csv_sync.ts
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);
34 changes: 34 additions & 0 deletions demo/cjs/lib/parse.js
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()
34 changes: 34 additions & 0 deletions demo/cjs/lib/parse.ts
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()
19 changes: 19 additions & 0 deletions demo/cjs/lib/parse_sync.js
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' ]
]
)
19 changes: 19 additions & 0 deletions demo/cjs/lib/parse_sync.ts
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' ]
]
)
30 changes: 30 additions & 0 deletions demo/cjs/package.json
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'"
}
}
23 changes: 23 additions & 0 deletions demo/cjs/test/samples.coffee
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
9 changes: 9 additions & 0 deletions demo/cjs/tsconfig.json
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,
}
}

0 comments on commit 66006cf

Please sign in to comment.