csv-faker-generator 1.0.0 → 2.0.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 CHANGED
@@ -4,11 +4,13 @@ Generate CSV files with realistic fake data using customizable headers and data
4
4
 
5
5
  ## Features
6
6
 
7
- - Generate CSV files with custom column headers
8
- - Support for 19+ data types (UUID, names, emails, addresses, etc.)
9
- - Simple CLI interface
10
- - Programmatic API for Node.js projects
11
- - TypeScript support with full type definitions
7
+ - **27+ data types**: UUID, names, emails, addresses, banking info, and more
8
+ - **Advanced data generation**: Alphanumeric strings, pick from custom values, formula-based calculated fields
9
+ - **Flexible number formatting**: Control decimal precision and ranges
10
+ - **Custom delimiters**: Support for comma, semicolon, tab, pipe
11
+ - **Simple CLI interface**: Generate CSV files from command line
12
+ - **Programmatic API**: Use in Node.js/TypeScript projects
13
+ - **Full TypeScript support**: Complete type definitions included
12
14
 
13
15
  ## Installation
14
16
 
@@ -16,34 +18,20 @@ Generate CSV files with realistic fake data using customizable headers and data
16
18
  npm install csv-faker-generator
17
19
  ```
18
20
 
19
- ## Usage
21
+ ## Quick Start
20
22
 
21
23
  ### CLI Usage
22
24
 
23
- Generate a CSV file from the command line:
24
-
25
25
  ```bash
26
26
  # Basic usage
27
27
  npx csv-faker -c id:uuid,name:name,email:email -r 100 -o users.csv
28
28
 
29
- # Generate user data
30
- npx csv-faker --columns "userId:uuid,firstName:firstName,lastName:lastName,email:email,phone:phone" --rows 50 --output customers.csv
31
-
32
- # Generate company data
33
- npx csv-faker -c id:uuid,company:company,city:city,country:country -r 20
29
+ # With custom delimiter
30
+ npx csv-faker -c id:uuid,name:name,email:email --delimiter ";" -r 50 -o contacts.csv
34
31
  ```
35
32
 
36
- #### CLI Options
37
-
38
- - `--columns, -c` - Column definitions (format: `header:type,header:type,...`)
39
- - `--rows, -r` - Number of rows to generate (default: 10)
40
- - `--output, -o` - Output file path (default: output.csv)
41
- - `--help, -h` - Show help message
42
-
43
33
  ### Programmatic Usage
44
34
 
45
- Use in your Node.js/TypeScript projects:
46
-
47
35
  ```typescript
48
36
  import { generateCSV } from 'csv-faker-generator';
49
37
 
@@ -52,109 +40,313 @@ await generateCSV({
52
40
  { header: 'id', type: 'uuid' },
53
41
  { header: 'name', type: 'name' },
54
42
  { header: 'email', type: 'email' },
55
- { header: 'address', type: 'address' },
56
43
  ],
57
44
  rows: 100,
58
45
  output: 'users.csv',
59
46
  });
60
47
  ```
61
48
 
62
- #### With Custom Number Ranges
49
+ ## Advanced Features
50
+
51
+ ### 1. Alphanumeric Type
52
+
53
+ Generate alphanumeric strings with custom length and case:
63
54
 
64
55
  ```typescript
65
56
  await generateCSV({
66
57
  columns: [
67
- { header: 'id', type: 'uuid' },
68
- { header: 'age', type: 'number', min: 18, max: 65 },
69
- { header: 'salary', type: 'number', min: 30000, max: 150000 },
58
+ { header: 'product_code', type: 'alphanumeric', length: 10, uppercase: true },
59
+ { header: 'sku', type: 'alphanumeric', length: 15 },
70
60
  ],
71
- rows: 50,
72
- output: 'employees.csv',
61
+ rows: 100,
62
+ output: 'products.csv',
73
63
  });
74
64
  ```
75
65
 
76
- ## Supported Data Types
66
+ Output:
67
+ ```csv
68
+ product_code,sku
69
+ PMA094QITG,nbdAIBY9Og7gDWd
70
+ SXYBL4LJQO,gakXOrBZYw1tYFP
71
+ ```
77
72
 
78
- | Type | Description | Example |
79
- |------|-------------|---------|
80
- | `uuid` | UUID v4 | `550e8400-e29b-41d4-a716-446655440000` |
81
- | `name` | Full name | `John Doe` |
82
- | `firstName` | First name only | `John` |
83
- | `lastName` | Last name only | `Doe` |
84
- | `email` | Email address | `john.doe@example.com` |
85
- | `phone` | Phone number | `+1-234-567-8900` |
86
- | `address` | Street address | `123 Main St` |
87
- | `city` | City name | `New York` |
88
- | `country` | Country name | `United States` |
89
- | `zipCode` | Postal code | `10001` |
90
- | `company` | Company name | `Tech Corp` |
91
- | `jobTitle` | Job title | `Software Engineer` |
92
- | `number` | Random number | `42` |
93
- | `date` | ISO date string | `2024-01-15T10:30:00.000Z` |
94
- | `boolean` | Boolean value | `true` or `false` |
95
- | `url` | Website URL | `https://example.com` |
96
- | `ip` | IP address | `192.168.1.1` |
97
- | `username` | Username | `john_doe123` |
98
- | `password` | Password | `aB3$xYz9!qW` |
99
-
100
- ## Examples
73
+ ### 2. Pick Type (Enum Values)
101
74
 
102
- ### Example 1: User Database
75
+ Select values from predefined arrays:
103
76
 
104
- ```bash
105
- npx csv-faker -c userId:uuid,username:username,email:email,password:password,createdAt:date -r 1000 -o users.csv
77
+ ```typescript
78
+ await generateCSV({
79
+ columns: [
80
+ { header: 'product', type: 'name' },
81
+ { header: 'tax_rate', type: 'pick', values: [0, 7, 19] },
82
+ { header: 'status', type: 'pick', values: ['active', 'pending', 'inactive'] },
83
+ { header: 'unit', type: 'pick', values: ['g', 'kg', 'Stück', 'Meter'] },
84
+ ],
85
+ rows: 100,
86
+ output: 'products.csv',
87
+ });
88
+ ```
89
+
90
+ ### 3. Formula Type (Calculated Fields)
91
+
92
+ Create calculated fields based on other columns:
93
+
94
+ ```typescript
95
+ await generateCSV({
96
+ columns: [
97
+ { header: 'product', type: 'company' },
98
+ { header: 'netto', type: 'number', min: 10, max: 200, decimals: 2 },
99
+ { header: 'tax', type: 'pick', values: [0, 7, 19] },
100
+ {
101
+ header: 'brutto',
102
+ type: 'formula',
103
+ formula: (row) => {
104
+ const netto = row.netto;
105
+ const tax = row.tax;
106
+ return parseFloat((netto * (1 + tax / 100)).toFixed(2));
107
+ },
108
+ },
109
+ ],
110
+ rows: 100,
111
+ output: 'invoices.csv',
112
+ });
113
+ ```
114
+
115
+ Output:
116
+ ```csv
117
+ product,netto,tax,brutto
118
+ Reichert LLC,120.39,19,143.26
119
+ Becker LLC,132.14,7,141.39
120
+ ```
121
+
122
+ ### 4. Decimal Precision
123
+
124
+ Control decimal places for number types:
125
+
126
+ ```typescript
127
+ await generateCSV({
128
+ columns: [
129
+ { header: 'price', type: 'number', min: 10, max: 200, decimals: 2 },
130
+ { header: 'weight', type: 'number', min: 0.1, max: 99.9, decimals: 1 },
131
+ { header: 'quantity', type: 'number', min: 1, max: 1000, decimals: 0 }, // integers
132
+ ],
133
+ rows: 100,
134
+ output: 'products.csv',
135
+ });
136
+ ```
137
+
138
+ ### 5. Custom Delimiters
139
+
140
+ Generate CSV files with different delimiters:
141
+
142
+ ```typescript
143
+ // Semicolon-separated
144
+ await generateCSV({
145
+ columns: [{ header: 'id', type: 'uuid' }, { header: 'name', type: 'name' }],
146
+ rows: 100,
147
+ delimiter: ';',
148
+ output: 'contacts.csv',
149
+ });
150
+
151
+ // Tab-separated (TSV)
152
+ await generateCSV({
153
+ columns: [{ header: 'id', type: 'uuid' }, { header: 'name', type: 'name' }],
154
+ rows: 100,
155
+ delimiter: '\t',
156
+ output: 'data.tsv',
157
+ });
106
158
  ```
107
159
 
108
- ### Example 2: E-commerce Orders
160
+ ## Complete Example: E-commerce Articles
161
+
162
+ This example demonstrates all advanced features together:
109
163
 
110
164
  ```typescript
111
165
  import { generateCSV } from 'csv-faker-generator';
112
166
 
113
167
  await generateCSV({
114
168
  columns: [
115
- { header: 'orderId', type: 'uuid' },
116
- { header: 'customerName', type: 'name' },
117
- { header: 'email', type: 'email' },
118
- { header: 'amount', type: 'number', min: 10, max: 500 },
119
- { header: 'orderDate', type: 'date' },
120
- { header: 'city', type: 'city' },
121
- { header: 'country', type: 'country' },
169
+ { header: 'Artikelnummer*', type: 'alphanumeric', length: 15 },
170
+ { header: 'Bezeichnung*', type: 'company' },
171
+ { header: 'Beschreibung', type: 'company' },
172
+ { header: 'Verkaufspreis netto in €', type: 'number', min: 10, max: 200, decimals: 2 },
173
+ { header: 'Steuersatz', type: 'pick', values: [0, 7, 19] },
174
+ {
175
+ header: 'Verkaufspreis brutto in €',
176
+ type: 'formula',
177
+ formula: (row) => {
178
+ const netto = row['Verkaufspreis netto in €'];
179
+ const tax = row['Steuersatz'];
180
+ return parseFloat((netto * (1 + tax / 100)).toFixed(2));
181
+ },
182
+ },
183
+ { header: 'Einkaufspreis netto in €', type: 'number', min: 5, max: 100, decimals: 2 },
184
+ { header: 'Einheit', type: 'pick', values: ['Stück', 'kg', 'g', 'Meter', 'Liter'] },
185
+ { header: 'Bestand', type: 'number', min: 0, max: 1000, decimals: 0 },
186
+ { header: 'Kategorie', type: 'pick', values: ['Electronics', 'Food', 'Clothing', 'Books'] },
122
187
  ],
123
- rows: 500,
124
- output: 'orders.csv',
188
+ rows: 100,
189
+ output: 'articles.csv',
125
190
  });
126
191
  ```
127
192
 
128
- ### Example 3: Employee Directory
193
+ ## CLI Reference
194
+
195
+ ### Options
196
+
197
+ - `--columns, -c` - Column definitions (format: `header:type,header:type,...`)
198
+ - `--rows, -r` - Number of rows to generate (default: 10)
199
+ - `--output, -o` - Output file path (default: output.csv)
200
+ - `--delimiter, -d` - Field delimiter (default: `,`) [options: `,` `;` `|` `\t`]
201
+ - `--help, -h` - Show help message
202
+
203
+ ### Examples
129
204
 
130
205
  ```bash
131
- npx csv-faker -c id:uuid,firstName:firstName,lastName:lastName,email:email,phone:phone,jobTitle:jobTitle,company:company -r 200 -o employees.csv
132
- ```
206
+ # Generate users
207
+ npx csv-faker -c id:uuid,name:name,email:email -r 100 -o users.csv
133
208
 
134
- ## API Reference
209
+ # Generate with semicolon delimiter
210
+ npx csv-faker -c id:uuid,name:name,email:email --delimiter ";" -r 50 -o contacts.csv
135
211
 
136
- ### `generateCSV(options: GenerateOptions): Promise<void>`
212
+ # Generate customer data
213
+ npx csv-faker -c customer_number:number,title:title,firstname:firstName,lastname:lastName,email:email,iban:iban,bic:bic -r 200 -o customers.csv
214
+ ```
137
215
 
138
- Generates a CSV file with fake data.
216
+ **Note**: Advanced features like `alphanumeric` with length, `pick` with values, and `formula` require programmatic usage. CLI only supports basic data types.
139
217
 
140
- **Parameters:**
141
- - `options.columns`: Array of column definitions
142
- - `options.rows`: Number of rows to generate
143
- - `options.output`: Output file path
218
+ ## Supported Data Types
219
+
220
+ | Type | Description | Example | CLI | Programmatic |
221
+ |------|-------------|---------|-----|--------------|
222
+ | `uuid` | UUID v4 | `550e8400-e29b-...` | ✓ | ✓ |
223
+ | `name` | Full name | `John Doe` | ✓ | ✓ |
224
+ | `firstName` | First name | `John` | ✓ | ✓ |
225
+ | `lastName` | Last name | `Doe` | ✓ | ✓ |
226
+ | `title` | Person prefix | `Mr.`, `Mrs.`, `Dr.` | ✓ | ✓ |
227
+ | `email` | Email address | `john.doe@example.com` | ✓ | ✓ |
228
+ | `phone` | Phone number | `+1-234-567-8900` | ✓ | ✓ |
229
+ | `address` | Street address | `123 Main St` | ✓ | ✓ |
230
+ | `secondaryAddress` | Apt/Suite | `Apt. 4B` | ✓ | ✓ |
231
+ | `city` | City name | `New York` | ✓ | ✓ |
232
+ | `country` | Country name | `United States` | ✓ | ✓ |
233
+ | `zipCode` | Postal code | `10001` | ✓ | ✓ |
234
+ | `company` | Company name | `Tech Corp` | ✓ | ✓ |
235
+ | `jobTitle` | Job title | `Software Engineer` | ✓ | ✓ |
236
+ | `number` | Random number | `42` | ✓ | ✓ |
237
+ | `date` | ISO date | `2024-01-15T10:30:00.000Z` | ✓ | ✓ |
238
+ | `boolean` | Boolean | `true` / `false` | ✓ | ✓ |
239
+ | `url` | Website URL | `https://example.com` | ✓ | ✓ |
240
+ | `ip` | IP address | `192.168.1.1` | ✓ | ✓ |
241
+ | `username` | Username | `john_doe123` | ✓ | ✓ |
242
+ | `password` | Password | `aB3$xYz9!qW` | ✓ | ✓ |
243
+ | `iban` | IBAN | `DE89370400440532013000` | ✓ | ✓ |
244
+ | `bic` | BIC/SWIFT | `COBADEFFXXX` | ✓ | ✓ |
245
+ | `bankName` | Bank name | `Deutsche Bank` | ✓ | ✓ |
246
+ | `alphanumeric` | Alphanumeric string | `aB3xYz9qW` | ✓* | ✓ |
247
+ | `pick` | Pick from array | Custom values | ✓* | ✓ |
248
+ | `formula` | Calculated value | Based on other fields | ✗ | ✓ |
249
+
250
+ *CLI supports the type but not advanced options (length, uppercase, values, etc.)
251
+
252
+ ## TypeScript API
253
+
254
+ ### Types
144
255
 
145
- **ColumnDefinition:**
146
256
  ```typescript
257
+ type DataType =
258
+ | 'uuid' | 'name' | 'firstName' | 'lastName' | 'title'
259
+ | 'email' | 'phone' | 'address' | 'secondaryAddress'
260
+ | 'city' | 'country' | 'zipCode' | 'company' | 'jobTitle'
261
+ | 'number' | 'date' | 'boolean' | 'url' | 'ip'
262
+ | 'username' | 'password' | 'iban' | 'bic' | 'bankName'
263
+ | 'alphanumeric' | 'pick' | 'formula';
264
+
147
265
  interface ColumnDefinition {
148
266
  header: string;
149
267
  type: DataType;
150
- min?: number; // For 'number' type
151
- max?: number; // For 'number' type
268
+ min?: number; // For 'number' type
269
+ max?: number; // For 'number' type
270
+ length?: number; // For 'alphanumeric' type
271
+ uppercase?: boolean; // For 'alphanumeric' type
272
+ values?: any[]; // For 'pick' type
273
+ formula?: (row: Record<string, any>) => any; // For 'formula' type
274
+ decimals?: number; // For 'number' type
152
275
  }
276
+
277
+ interface GenerateOptions {
278
+ columns: ColumnDefinition[];
279
+ rows: number;
280
+ output: string;
281
+ delimiter?: string; // Default: ','
282
+ }
283
+
284
+ function generateCSV(options: GenerateOptions): Promise<void>;
285
+ ```
286
+
287
+ ## More Examples
288
+
289
+ ### Example 1: User Database
290
+
291
+ ```typescript
292
+ await generateCSV({
293
+ columns: [
294
+ { header: 'userId', type: 'uuid' },
295
+ { header: 'username', type: 'username' },
296
+ { header: 'email', type: 'email' },
297
+ { header: 'password', type: 'password' },
298
+ { header: 'createdAt', type: 'date' },
299
+ ],
300
+ rows: 1000,
301
+ output: 'users.csv',
302
+ });
153
303
  ```
154
304
 
155
- ### `parseColumnString(columnStr: string): ColumnDefinition`
305
+ ### Example 2: Banking Data
306
+
307
+ ```typescript
308
+ await generateCSV({
309
+ columns: [
310
+ { header: 'accountId', type: 'uuid' },
311
+ { header: 'accountOwner', type: 'name' },
312
+ { header: 'iban', type: 'iban' },
313
+ { header: 'bic', type: 'bic' },
314
+ { header: 'bankName', type: 'bankName' },
315
+ { header: 'balance', type: 'number', min: 0, max: 100000, decimals: 2 },
316
+ ],
317
+ rows: 500,
318
+ output: 'accounts.csv',
319
+ });
320
+ ```
321
+
322
+ ### Example 3: Inventory System
323
+
324
+ ```typescript
325
+ await generateCSV({
326
+ columns: [
327
+ { header: 'SKU', type: 'alphanumeric', length: 12, uppercase: true },
328
+ { header: 'Product', type: 'company' },
329
+ { header: 'Category', type: 'pick', values: ['Electronics', 'Furniture', 'Clothing', 'Food'] },
330
+ { header: 'Price', type: 'number', min: 5, max: 500, decimals: 2 },
331
+ { header: 'Stock', type: 'number', min: 0, max: 1000, decimals: 0 },
332
+ { header: 'Status', type: 'pick', values: ['In Stock', 'Low Stock', 'Out of Stock'] },
333
+ ],
334
+ rows: 200,
335
+ output: 'inventory.csv',
336
+ });
337
+ ```
338
+
339
+ ## Future Features (Roadmap)
340
+
341
+ Based on community feedback, we're considering:
342
+
343
+ - Locale support for region-specific data
344
+ - Templates/presets for common use cases
345
+ - Row dependencies and conditional values
346
+ - Related dataset generation (parent-child relationships)
347
+ - More business data types (VAT numbers, tax IDs, etc.)
156
348
 
157
- Parses a column string (e.g., "id:uuid") into a ColumnDefinition object.
349
+ See [issues](https://github.com/yourusername/csv-faker-generator/issues) for discussions and requests.
158
350
 
159
351
  ## License
160
352
 
@@ -164,6 +356,6 @@ MIT
164
356
 
165
357
  Contributions are welcome! Please feel free to submit a Pull Request.
166
358
 
167
- ## Issues
359
+ ## Issues & Support
168
360
 
169
361
  Found a bug or have a feature request? Please open an issue on [GitHub](https://github.com/yourusername/csv-faker-generator/issues).
package/dist/cli.js CHANGED
@@ -14,23 +14,30 @@ Options:
14
14
  --columns, -c Column definitions (format: header:type,header:type,...)
15
15
  --rows, -r Number of rows to generate (default: 10)
16
16
  --output, -o Output file path (default: output.csv)
17
+ --delimiter, -d Field delimiter (default: ,) [options: , ; | \\t]
17
18
  --help, -h Show this help message
18
19
 
19
20
  Data Types:
20
- uuid, name, firstName, lastName, email, phone, address, city,
21
- country, zipCode, company, jobTitle, number, date, boolean,
22
- url, ip, username, password
21
+ uuid, name, firstName, lastName, title, email, phone, address,
22
+ secondaryAddress, city, country, zipCode, company, jobTitle,
23
+ number, date, boolean, url, ip, username, password, iban, bic,
24
+ bankName, alphanumeric, pick
25
+
26
+ Note: Advanced features (alphanumeric length, pick values, formula)
27
+ require programmatic usage. See README for examples.
23
28
 
24
29
  Examples:
25
30
  csv-faker -c id:uuid,name:name,email:email -r 100 -o users.csv
26
31
  csv-faker -c userId:uuid,firstName:firstName,lastName:lastName,email:email,address:address -r 50
27
32
  csv-faker --columns "id:uuid,company:company,city:city" --rows 20 --output companies.csv
33
+ csv-faker -c id:uuid,name:name,email:email --delimiter ";" -r 50 -o contacts.csv
28
34
  `);
29
35
  }
30
36
  function parseArgs() {
31
37
  let columnsStr = '';
32
38
  let rows = 10;
33
39
  let output = 'output.csv';
40
+ let delimiter = ',';
34
41
  for (let i = 0; i < args.length; i++) {
35
42
  const arg = args[i];
36
43
  if (arg === '--help' || arg === '-h') {
@@ -50,6 +57,13 @@ function parseArgs() {
50
57
  else if (arg === '--output' || arg === '-o') {
51
58
  output = args[++i];
52
59
  }
60
+ else if (arg === '--delimiter' || arg === '-d') {
61
+ delimiter = args[++i];
62
+ // Handle common escape sequences
63
+ if (delimiter === '\\t') {
64
+ delimiter = '\t';
65
+ }
66
+ }
53
67
  }
54
68
  if (!columnsStr) {
55
69
  console.error('Error: --columns is required\n');
@@ -57,7 +71,7 @@ function parseArgs() {
57
71
  process.exit(1);
58
72
  }
59
73
  const columns = columnsStr.split(',').map(index_1.parseColumnString);
60
- return { columns, rows, output };
74
+ return { columns, rows, output, delimiter };
61
75
  }
62
76
  async function main() {
63
77
  try {
package/dist/index.d.ts CHANGED
@@ -1,14 +1,20 @@
1
- export type DataType = 'uuid' | 'name' | 'firstName' | 'lastName' | 'title' | 'email' | 'phone' | 'address' | 'secondaryAddress' | 'city' | 'country' | 'zipCode' | 'company' | 'jobTitle' | 'number' | 'date' | 'boolean' | 'url' | 'ip' | 'username' | 'password' | 'iban' | 'bic' | 'bankName';
1
+ export type DataType = 'uuid' | 'name' | 'firstName' | 'lastName' | 'title' | 'email' | 'phone' | 'address' | 'secondaryAddress' | 'city' | 'country' | 'zipCode' | 'company' | 'jobTitle' | 'number' | 'date' | 'boolean' | 'url' | 'ip' | 'username' | 'password' | 'iban' | 'bic' | 'bankName' | 'alphanumeric' | 'pick' | 'formula';
2
2
  export interface ColumnDefinition {
3
3
  header: string;
4
4
  type: DataType;
5
5
  min?: number;
6
6
  max?: number;
7
+ length?: number;
8
+ uppercase?: boolean;
9
+ values?: any[];
10
+ formula?: (row: Record<string, any>) => any;
11
+ decimals?: number;
7
12
  }
8
13
  export interface GenerateOptions {
9
14
  columns: ColumnDefinition[];
10
15
  rows: number;
11
16
  output: string;
17
+ delimiter?: string;
12
18
  }
13
19
  export declare function generateCSV(options: GenerateOptions): Promise<void>;
14
20
  export declare function parseColumnString(columnStr: string): ColumnDefinition;
package/dist/index.js CHANGED
@@ -4,7 +4,8 @@ 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
- function generateValue(type, min, max) {
7
+ function generateValue(column, row) {
8
+ const { type, min, max, length, uppercase, values, decimals } = column;
8
9
  switch (type) {
9
10
  case 'uuid':
10
11
  return faker_1.faker.string.uuid();
@@ -34,8 +35,14 @@ function generateValue(type, min, max) {
34
35
  return faker_1.faker.company.name();
35
36
  case 'jobTitle':
36
37
  return faker_1.faker.person.jobTitle();
37
- case 'number':
38
- return faker_1.faker.number.int({ min: min || 1, max: max || 1000 });
38
+ case 'number': {
39
+ const num = faker_1.faker.number.int({ min: min || 1, max: max || 1000 });
40
+ if (decimals !== undefined) {
41
+ const float = faker_1.faker.number.float({ min: min || 1, max: max || 1000, fractionDigits: decimals });
42
+ return parseFloat(float.toFixed(decimals));
43
+ }
44
+ return num;
45
+ }
39
46
  case 'date':
40
47
  return faker_1.faker.date.recent().toISOString();
41
48
  case 'boolean':
@@ -54,24 +61,54 @@ function generateValue(type, min, max) {
54
61
  return faker_1.faker.finance.bic();
55
62
  case 'bankName':
56
63
  return faker_1.faker.company.name() + ' Bank';
64
+ case 'alphanumeric': {
65
+ const len = length || 10;
66
+ let result = faker_1.faker.string.alphanumeric(len);
67
+ if (uppercase) {
68
+ result = result.toUpperCase();
69
+ }
70
+ return result;
71
+ }
72
+ case 'pick': {
73
+ if (!values || values.length === 0) {
74
+ throw new Error('pick type requires values array');
75
+ }
76
+ return faker_1.faker.helpers.arrayElement(values);
77
+ }
78
+ case 'formula': {
79
+ if (!column.formula || !row) {
80
+ throw new Error('formula type requires formula function and row context');
81
+ }
82
+ return column.formula(row);
83
+ }
57
84
  default:
58
85
  return faker_1.faker.lorem.word();
59
86
  }
60
87
  }
61
88
  async function generateCSV(options) {
62
- const { columns, rows, output } = options;
89
+ const { columns, rows, output, delimiter = ',' } = options;
63
90
  const csvWriter = (0, csv_writer_1.createObjectCsvWriter)({
64
91
  path: output,
65
92
  header: columns.map((col) => ({
66
93
  id: col.header,
67
94
  title: col.header,
68
95
  })),
96
+ fieldDelimiter: delimiter,
69
97
  });
70
98
  const records = [];
71
99
  for (let i = 0; i < rows; i++) {
72
100
  const record = {};
101
+ // First pass: generate non-formula columns
102
+ for (const column of columns) {
103
+ if (column.type !== 'formula') {
104
+ record[column.header] = generateValue(column, record);
105
+ }
106
+ }
107
+ // Second pass: generate formula columns (they can reference other columns)
73
108
  for (const column of columns) {
74
- record[column.header] = generateValue(column.type, column.min, column.max);
109
+ if (column.type === 'formula') {
110
+ record[column.header] = generateValue(column, record);
111
+ }
75
112
  }
76
113
  records.push(record);
77
114
  }
@@ -89,7 +126,7 @@ function parseColumnString(columnStr) {
89
126
  'uuid', 'name', 'firstName', 'lastName', 'title', 'email', 'phone',
90
127
  'address', 'secondaryAddress', 'city', 'country', 'zipCode', 'company', 'jobTitle',
91
128
  'number', 'date', 'boolean', 'url', 'ip', 'username', 'password',
92
- 'iban', 'bic', 'bankName'
129
+ 'iban', 'bic', 'bankName', 'alphanumeric', 'pick'
93
130
  ];
94
131
  if (!validTypes.includes(type)) {
95
132
  throw new Error(`Invalid data type: ${type}. Valid types: ${validTypes.join(', ')}`);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const index_1 = require("./index");
4
+ async function testAdvancedFeatures() {
5
+ console.log('Testing Advanced Features...\n');
6
+ // Test 1: Alphanumeric with length and uppercase
7
+ console.log('Test 1: Alphanumeric type with custom length and uppercase...');
8
+ await (0, index_1.generateCSV)({
9
+ columns: [
10
+ { header: 'product_code', type: 'alphanumeric', length: 10, uppercase: true },
11
+ { header: 'sku', type: 'alphanumeric', length: 15 },
12
+ { header: 'name', type: 'name' },
13
+ ],
14
+ rows: 5,
15
+ output: 'test-alphanumeric.csv',
16
+ });
17
+ // Test 2: Pick type for enum values
18
+ console.log('\nTest 2: Pick type for predefined values...');
19
+ await (0, index_1.generateCSV)({
20
+ columns: [
21
+ { header: 'product', type: 'name' },
22
+ { header: 'tax_rate', type: 'pick', values: [0, 7, 19] },
23
+ { header: 'status', type: 'pick', values: ['active', 'pending', 'inactive'] },
24
+ { header: 'unit', type: 'pick', values: ['g', 'kg', 'Stück', 'Meter'] },
25
+ ],
26
+ rows: 5,
27
+ output: 'test-pick.csv',
28
+ });
29
+ // Test 3: Formula fields (calculated values)
30
+ console.log('\nTest 3: Formula type for calculated fields...');
31
+ await (0, index_1.generateCSV)({
32
+ columns: [
33
+ { header: 'product', type: 'company' },
34
+ { header: 'netto', type: 'number', min: 10, max: 200, decimals: 2 },
35
+ { header: 'tax', type: 'pick', values: [0, 7, 19] },
36
+ {
37
+ header: 'brutto',
38
+ type: 'formula',
39
+ formula: (row) => {
40
+ const netto = row.netto;
41
+ const tax = row.tax;
42
+ return parseFloat((netto * (1 + tax / 100)).toFixed(2));
43
+ },
44
+ },
45
+ ],
46
+ rows: 5,
47
+ output: 'test-formula.csv',
48
+ });
49
+ // Test 4: Custom delimiter (semicolon)
50
+ console.log('\nTest 4: Custom delimiter (semicolon)...');
51
+ await (0, index_1.generateCSV)({
52
+ columns: [
53
+ { header: 'id', type: 'uuid' },
54
+ { header: 'name', type: 'name' },
55
+ { header: 'email', type: 'email' },
56
+ ],
57
+ rows: 5,
58
+ delimiter: ';',
59
+ output: 'test-semicolon.csv',
60
+ });
61
+ // Test 5: Number with decimal precision
62
+ console.log('\nTest 5: Number type with decimal precision...');
63
+ await (0, index_1.generateCSV)({
64
+ columns: [
65
+ { header: 'price', type: 'number', min: 10, max: 200, decimals: 2 },
66
+ { header: 'weight', type: 'number', min: 0.1, max: 99.9, decimals: 1 },
67
+ { header: 'quantity', type: 'number', min: 1, max: 1000, decimals: 0 },
68
+ ],
69
+ rows: 5,
70
+ output: 'test-decimals.csv',
71
+ });
72
+ // Test 6: Complete E-commerce Article Example
73
+ console.log('\nTest 6: Complete e-commerce article example (the dogfooding use case)...');
74
+ await (0, index_1.generateCSV)({
75
+ columns: [
76
+ { header: 'Artikelnummer*', type: 'alphanumeric', length: 15 },
77
+ { header: 'Bezeichnung*', type: 'company' },
78
+ { header: 'Beschreibung', type: 'company' },
79
+ { header: 'Verkaufspreis netto in €', type: 'number', min: 10, max: 200, decimals: 2 },
80
+ { header: 'Steuersatz', type: 'pick', values: [0, 7, 19] },
81
+ {
82
+ header: 'Verkaufspreis brutto in €',
83
+ type: 'formula',
84
+ formula: (row) => {
85
+ const netto = row['Verkaufspreis netto in €'];
86
+ const tax = row['Steuersatz'];
87
+ return parseFloat((netto * (1 + tax / 100)).toFixed(2));
88
+ },
89
+ },
90
+ { header: 'Einkaufspreis netto in €', type: 'number', min: 5, max: 100, decimals: 2 },
91
+ { header: 'Einheit', type: 'pick', values: ['Stück', 'kg', 'g', 'Meter', 'Liter'] },
92
+ { header: 'Bestand', type: 'number', min: 0, max: 1000, decimals: 0 },
93
+ { header: 'Kategorie', type: 'pick', values: ['Electronics', 'Food', 'Clothing', 'Books'] },
94
+ ],
95
+ rows: 10,
96
+ output: 'test-articles-complete.csv',
97
+ });
98
+ console.log('\n✓ All advanced feature tests completed successfully!');
99
+ console.log('\nGenerated files:');
100
+ console.log(' - test-alphanumeric.csv (alphanumeric with length/uppercase)');
101
+ console.log(' - test-pick.csv (pick from predefined values)');
102
+ console.log(' - test-formula.csv (calculated fields)');
103
+ console.log(' - test-semicolon.csv (custom delimiter)');
104
+ console.log(' - test-decimals.csv (decimal precision)');
105
+ console.log(' - test-articles-complete.csv (complete e-commerce example)');
106
+ }
107
+ testAdvancedFeatures().catch((error) => {
108
+ console.error('Error running advanced tests:', error);
109
+ process.exit(1);
110
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "csv-faker-generator",
3
- "version": "1.0.0",
4
- "description": "Generate CSV files with fake data using custom headers (UUID, names, emails, addresses, etc.)",
3
+ "version": "2.0.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.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "bin": {
@@ -27,7 +27,17 @@
27
27
  "generator",
28
28
  "uuid",
29
29
  "csv-generator",
30
- "mock-data"
30
+ "mock-data",
31
+ "alphanumeric",
32
+ "formula",
33
+ "calculated-fields",
34
+ "enum",
35
+ "pick",
36
+ "delimiter",
37
+ "test-data",
38
+ "prototyping",
39
+ "iban",
40
+ "banking"
31
41
  ],
32
42
  "author": "Manoj Kengudelu",
33
43
  "license": "MIT",