Skip to content

Commit e26fd26

Browse files
committed
docs: esm demo
1 parent 44a3b37 commit e26fd26

14 files changed

+374
-0
lines changed

demo/esm/lib/csv.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// Import the package main module
3+
import * as csv from '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/esm/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/esm/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+
import * as csv from '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/esm/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/esm/lib/parse.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import assert from 'assert';
3+
import { parse } from '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/esm/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/esm/lib/parse_sync.js

+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/esm/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/esm/package.json

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

demo/esm/test.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
node --loader ts-node/esm lib/parse.ts

demo/esm/test/loaders/all.mjs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
import * as coffee from './coffee.mjs'
3+
import * as ts from 'ts-node/esm'
4+
5+
const coffeeRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;
6+
const tsRegex = /\.ts$/;
7+
8+
export function resolve(specifier, context, defaultResolve) {
9+
if (coffeeRegex.test(specifier)) {
10+
return coffee.resolve.apply(this, arguments)
11+
}
12+
if (tsRegex.test(specifier)) {
13+
return ts.resolve.apply(this, arguments)
14+
}
15+
return ts.resolve.apply(this, arguments);
16+
}
17+
18+
export function getFormat(url, context, defaultGetFormat) {
19+
if (coffeeRegex.test(url)) {
20+
return coffee.getFormat.apply(this, arguments)
21+
}
22+
if (tsRegex.test(url)) {
23+
return ts.getFormat.apply(this, arguments)
24+
}
25+
return ts.getFormat.apply(this, arguments);
26+
}
27+
28+
export function transformSource(source, context, defaultTransformSource) {
29+
const { url } = context;
30+
if (coffeeRegex.test(url)) {
31+
return coffee.transformSource.apply(this, arguments)
32+
}
33+
if (tsRegex.test(url)) {
34+
return ts.transformSource.apply(this, arguments)
35+
}
36+
return ts.transformSource.apply(this, arguments);
37+
}

demo/esm/test/loaders/coffee.mjs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// coffeescript-loader.mjs
2+
import { URL, pathToFileURL } from 'url';
3+
import CoffeeScript from 'coffeescript';
4+
import { cwd } from 'process';
5+
6+
const baseURL = pathToFileURL(`${cwd()}/`).href;
7+
8+
// CoffeeScript files end in .coffee, .litcoffee or .coffee.md.
9+
const extensionsRegex = /\.coffee$|\.litcoffee$|\.coffee\.md$/;
10+
11+
export function resolve(specifier, context, defaultResolve) {
12+
const { parentURL = baseURL } = context;
13+
// Node.js normally errors on unknown file extensions, so return a URL for
14+
// specifiers ending in the CoffeeScript file extensions.
15+
if (extensionsRegex.test(specifier)) {
16+
return {
17+
url: new URL(specifier, parentURL).href,
18+
stop: true
19+
};
20+
}
21+
// Let Node.js handle all other specifiers.
22+
return defaultResolve(specifier, context, defaultResolve);
23+
}
24+
25+
export function getFormat(url, context, defaultGetFormat) {
26+
// Now that we patched resolve to let CoffeeScript URLs through, we need to
27+
// tell Node.js what format such URLs should be interpreted as. For the
28+
// purposes of this loader, all CoffeeScript URLs are ES modules.
29+
if (extensionsRegex.test(url)) {
30+
return {
31+
format: 'module',
32+
stop: true
33+
};
34+
}
35+
// Let Node.js handle all other URLs.
36+
return defaultGetFormat(url, context, defaultGetFormat);
37+
}
38+
39+
export function transformSource(source, context, defaultTransformSource) {
40+
const { url, format } = context;
41+
42+
if (extensionsRegex.test(url)) {
43+
return {
44+
source: CoffeeScript.compile(String(source), { bare: true })
45+
};
46+
}
47+
48+
// Let Node.js handle all other sources.
49+
return defaultTransformSource(source, context, defaultTransformSource);
50+
}

demo/esm/test/samples.coffee

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

demo/esm/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": "ES2020",
6+
"moduleResolution": "node",
7+
"strict": true,
8+
}
9+
}

0 commit comments

Comments
 (0)