-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-companies-csv.js
More file actions
59 lines (48 loc) · 2.13 KB
/
generate-companies-csv.js
File metadata and controls
59 lines (48 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const fs = require('fs');
const path = require('path');
const NUM_ROWS = 100;
const OUTPUT_FILE = path.join(__dirname, 'test-data', `companies_${NUM_ROWS}.csv`);
// 20 predefined (company, address) pairs
const PREDEFINED_ROWS = [
['Acme Corp', '123 Main St, Springfield, IL 62701'],
['Globex Inc', '456 Oak Ave, Shelbyville, IN 46176'],
['Initech LLC', '789 Pine Rd, Columbus, OH 43085'],
['Umbrella Co', '101 Elm St, Raccoon City, CO 80014'],
['Stark Industries', '200 Park Ave, New York, NY 10166'],
['Wayne Enterprises', '1007 Mountain Dr, Gotham, NJ 07030'],
['Hooli', '1 Silicon Way, Palo Alto, CA 94301'],
['Massive Dynamic', '77 Water St, New York, NY 10005'],
['Wonka Industries', '5 Candy Ln, Scranton, PA 18503'],
['Prestige Worldwide', '42 Yacht Club Rd, Miami, FL 33101'],
['Duff Brewing', '321 Beer Blvd, Springfield, OR 97477'],
['Pied Piper', '88 Compression Dr, San Francisco, CA 94105'],
['Cyberdyne Systems', '909 Skynet Blvd, San Diego, CA 92101'],
['Tyrell Corporation', '22 Nexus Pl, Los Angeles, CA 90012'],
['Blue Sun Corp', '19 Serenity Way, Houston, TX 77002'],
['Black Mesa', '3 Research Pkwy, Mesa, AZ 85201'],
['Aperture Science', '12 Test Lab Rd, Portland, OR 97201'],
['Gringotts Bank', '17 Diagon Alley, Boston, MA 02108'],
['Dunder Mifflin', '1725 Slough Ave, Scranton, PA 18503'],
['Monsters Inc', '29 Door St, Monstropolis, MI 48201'],
];
function toCsvField(value) {
const str = String(value);
if (/[",\n]/.test(str)) {
return `"${str.replace(/"/g, '""')}"`;
}
return str;
}
function generateCsv(numRows = NUM_ROWS, outputFile = OUTPUT_FILE) {
const header = ['company name', 'address'];
const lines = [header.map(toCsvField).join(',')];
for (let i = 0; i < numRows; i++) {
const [company, address] = PREDEFINED_ROWS[Math.floor(Math.random() * PREDEFINED_ROWS.length)];
lines.push([company, address].map(toCsvField).join(','));
}
fs.writeFileSync(outputFile, lines.join('\n'), 'utf8');
console.log(`CSV file with ${numRows} rows written to ${outputFile}`);
}
if (require.main === module) {
generateCsv();
}
module.exports = { generateCsv, PREDEFINED_ROWS };