csv-faker-generator 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -0
- package/dist/cli.js +52 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +20 -10
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@ Generate CSV files with realistic fake data using customizable headers and data
|
|
|
13
13
|
- **Advanced data generation**: Alphanumeric strings, pick from custom values, formula-based calculated fields
|
|
14
14
|
- **Flexible number formatting**: Control decimal precision and ranges
|
|
15
15
|
- **Custom delimiters**: Support for comma, semicolon, tab, pipe
|
|
16
|
+
- **Multiple output formats**: CSV, JSON arrays, and newline-delimited JSON
|
|
16
17
|
- **Simple CLI interface**: Generate CSV files from command line
|
|
17
18
|
- **Programmatic API**: Use in Node.js/TypeScript projects
|
|
18
19
|
- **Full TypeScript support**: Complete type definitions included
|
|
@@ -33,6 +34,10 @@ npx csv-faker -c id:uuid,name:name,email:email -r 100 -o users.csv
|
|
|
33
34
|
|
|
34
35
|
# With custom delimiter
|
|
35
36
|
npx csv-faker -c id:uuid,name:name,email:email --delimiter ";" -r 50 -o contacts.csv
|
|
37
|
+
|
|
38
|
+
# JSON and NDJSON (also inferred from .json and .ndjson output extensions)
|
|
39
|
+
npx csv-faker -c id:uuid,name:name,email:email -r 100 -o users.json
|
|
40
|
+
npx csv-faker -c id:uuid,name:name,email:email -r 100 -f ndjson -o users.data
|
|
36
41
|
```
|
|
37
42
|
|
|
38
43
|
### Programmatic Usage
|
|
@@ -51,6 +56,20 @@ await generateCSV({
|
|
|
51
56
|
});
|
|
52
57
|
```
|
|
53
58
|
|
|
59
|
+
Set `format` to `'json'` for an array of row objects or `'ndjson'` for one row object per line. It defaults to `'csv'` in programmatic usage.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
await generateCSV({
|
|
63
|
+
columns: [
|
|
64
|
+
{ header: 'id', type: 'uuid' },
|
|
65
|
+
{ header: 'active', type: 'boolean' },
|
|
66
|
+
],
|
|
67
|
+
rows: 100,
|
|
68
|
+
output: 'users.json',
|
|
69
|
+
format: 'json',
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
54
73
|
## Advanced Features
|
|
55
74
|
|
|
56
75
|
### 1. Alphanumeric Type
|
|
@@ -202,6 +221,7 @@ await generateCSV({
|
|
|
202
221
|
- `--columns, -c` - Column definitions (format: `header:type,header:type,...`)
|
|
203
222
|
- `--rows, -r` - Number of rows to generate (default: 10)
|
|
204
223
|
- `--output, -o` - Output file path (default: output.csv)
|
|
224
|
+
- `--format, -f` - Output format: `csv`, `json`, or `ndjson` (default: inferred from `.json` / `.ndjson` extension, otherwise `csv`)
|
|
205
225
|
- `--delimiter, -d` - Field delimiter (default: `,`) [options: `,` `;` `|` `\t`]
|
|
206
226
|
- `--help, -h` - Show help message
|
|
207
227
|
|
|
@@ -283,6 +303,7 @@ interface GenerateOptions {
|
|
|
283
303
|
columns: ColumnDefinition[];
|
|
284
304
|
rows: number;
|
|
285
305
|
output: string;
|
|
306
|
+
format?: 'csv' | 'json' | 'ndjson'; // Default: 'csv'
|
|
286
307
|
delimiter?: string; // Default: ','
|
|
287
308
|
}
|
|
288
309
|
|
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,41 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
3
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
37
|
const index_1 = require("./index");
|
|
38
|
+
const path = __importStar(require("node:path"));
|
|
5
39
|
const args = process.argv.slice(2);
|
|
6
40
|
function showHelp() {
|
|
7
41
|
console.log(`
|
|
@@ -14,6 +48,7 @@ Options:
|
|
|
14
48
|
--columns, -c Column definitions (format: header:type,header:type,...)
|
|
15
49
|
--rows, -r Number of rows to generate (default: 10)
|
|
16
50
|
--output, -o Output file path (default: output.csv)
|
|
51
|
+
--format, -f Output format: csv, json, or ndjson (default: inferred from output extension)
|
|
17
52
|
--delimiter, -d Field delimiter (default: ,) [options: , ; | \\t]
|
|
18
53
|
--help, -h Show this help message
|
|
19
54
|
|
|
@@ -31,12 +66,15 @@ Examples:
|
|
|
31
66
|
csv-faker -c userId:uuid,firstName:firstName,lastName:lastName,email:email,address:address -r 50
|
|
32
67
|
csv-faker --columns "id:uuid,company:company,city:city" --rows 20 --output companies.csv
|
|
33
68
|
csv-faker -c id:uuid,name:name,email:email --delimiter ";" -r 50 -o contacts.csv
|
|
69
|
+
csv-faker -c id:uuid,name:name -r 50 -o users.json
|
|
70
|
+
csv-faker -c id:uuid,name:name -r 50 -f ndjson -o users.data
|
|
34
71
|
`);
|
|
35
72
|
}
|
|
36
73
|
function parseArgs() {
|
|
37
74
|
let columnsStr = '';
|
|
38
75
|
let rows = 10;
|
|
39
76
|
let output = 'output.csv';
|
|
77
|
+
let format;
|
|
40
78
|
let delimiter = ',';
|
|
41
79
|
for (let i = 0; i < args.length; i++) {
|
|
42
80
|
const arg = args[i];
|
|
@@ -57,6 +95,13 @@ function parseArgs() {
|
|
|
57
95
|
else if (arg === '--output' || arg === '-o') {
|
|
58
96
|
output = args[++i];
|
|
59
97
|
}
|
|
98
|
+
else if (arg === '--format' || arg === '-f') {
|
|
99
|
+
const value = args[++i];
|
|
100
|
+
if (value !== 'csv' && value !== 'json' && value !== 'ndjson') {
|
|
101
|
+
throw new Error(`Invalid format: ${value}. Expected csv, json, or ndjson`);
|
|
102
|
+
}
|
|
103
|
+
format = value;
|
|
104
|
+
}
|
|
60
105
|
else if (arg === '--delimiter' || arg === '-d') {
|
|
61
106
|
delimiter = args[++i];
|
|
62
107
|
// Handle common escape sequences
|
|
@@ -71,7 +116,13 @@ function parseArgs() {
|
|
|
71
116
|
process.exit(1);
|
|
72
117
|
}
|
|
73
118
|
const columns = columnsStr.split(',').map(index_1.parseColumnString);
|
|
74
|
-
|
|
119
|
+
if (!format) {
|
|
120
|
+
const extension = path.extname(output).slice(1).toLowerCase();
|
|
121
|
+
if (extension === 'json' || extension === 'ndjson') {
|
|
122
|
+
format = extension;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return { columns, rows, output, format, delimiter };
|
|
75
126
|
}
|
|
76
127
|
async function main() {
|
|
77
128
|
try {
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.generateCSV = generateCSV;
|
|
|
4
4
|
exports.parseColumnString = parseColumnString;
|
|
5
5
|
const faker_1 = require("@faker-js/faker");
|
|
6
6
|
const csv_writer_1 = require("csv-writer");
|
|
7
|
+
const promises_1 = require("node:fs/promises");
|
|
7
8
|
function generateValue(column, row) {
|
|
8
9
|
const { type, min, max, length, uppercase, values, decimals } = column;
|
|
9
10
|
switch (type) {
|
|
@@ -86,15 +87,7 @@ function generateValue(column, row) {
|
|
|
86
87
|
}
|
|
87
88
|
}
|
|
88
89
|
async function generateCSV(options) {
|
|
89
|
-
const { columns, rows, output, delimiter = ',' } = options;
|
|
90
|
-
const csvWriter = (0, csv_writer_1.createObjectCsvWriter)({
|
|
91
|
-
path: output,
|
|
92
|
-
header: columns.map((col) => ({
|
|
93
|
-
id: col.header,
|
|
94
|
-
title: col.header,
|
|
95
|
-
})),
|
|
96
|
-
fieldDelimiter: delimiter,
|
|
97
|
-
});
|
|
90
|
+
const { columns, rows, output, format = 'csv', delimiter = ',' } = options;
|
|
98
91
|
const records = [];
|
|
99
92
|
for (let i = 0; i < rows; i++) {
|
|
100
93
|
const record = {};
|
|
@@ -112,7 +105,24 @@ async function generateCSV(options) {
|
|
|
112
105
|
}
|
|
113
106
|
records.push(record);
|
|
114
107
|
}
|
|
115
|
-
|
|
108
|
+
if (format === 'json') {
|
|
109
|
+
await (0, promises_1.writeFile)(output, `${JSON.stringify(records, null, 2)}\n`);
|
|
110
|
+
}
|
|
111
|
+
else if (format === 'ndjson') {
|
|
112
|
+
const content = records.map((record) => JSON.stringify(record)).join('\n');
|
|
113
|
+
await (0, promises_1.writeFile)(output, content ? `${content}\n` : '');
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const csvWriter = (0, csv_writer_1.createObjectCsvWriter)({
|
|
117
|
+
path: output,
|
|
118
|
+
header: columns.map((col) => ({
|
|
119
|
+
id: col.header,
|
|
120
|
+
title: col.header,
|
|
121
|
+
})),
|
|
122
|
+
fieldDelimiter: delimiter,
|
|
123
|
+
});
|
|
124
|
+
await csvWriter.writeRecords(records);
|
|
125
|
+
}
|
|
116
126
|
console.log(`✓ Generated ${rows} rows in ${output}`);
|
|
117
127
|
}
|
|
118
128
|
function parseColumnString(columnStr) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "csv-faker-generator",
|
|
3
|
-
"version": "2.0
|
|
4
|
-
"description": "Generate CSV files with fake data: 27+ data types, alphanumeric strings, pick from custom values, formula fields, decimal precision, custom delimiters. Perfect for testing and prototyping.",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Generate CSV, JSON, or NDJSON files with fake data: 27+ data types, alphanumeric strings, pick from custom values, formula fields, decimal precision, custom delimiters. Perfect for testing and prototyping.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"bin": {
|
|
@@ -37,7 +37,10 @@
|
|
|
37
37
|
"test-data",
|
|
38
38
|
"prototyping",
|
|
39
39
|
"iban",
|
|
40
|
-
"banking"
|
|
40
|
+
"banking",
|
|
41
|
+
"json",
|
|
42
|
+
"ndjson",
|
|
43
|
+
"fixtures"
|
|
41
44
|
],
|
|
42
45
|
"author": "Manoj Kengudelu",
|
|
43
46
|
"license": "MIT",
|