parag-dataforge 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +106 -2
- package/package.json +30 -4
- package/src/core/dataforge.js +67 -0
- package/src/core/random.js +39 -0
- package/src/generators/orders.js +94 -0
- package/src/generators/products.js +67 -0
- package/src/generators/users.js +42 -0
- package/src/index.js +14 -0
- package/src/utils/exporters.js +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Parag Garg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,107 @@
|
|
|
1
|
-
#
|
|
1
|
+
# parag-dataforge
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`parag-dataforge` is a dependency-free CommonJS library for generating sample users, products, and orders, and exporting arrays of records as JSON or CSV. The generated data is intended for development and testing.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install parag-dataforge
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## CommonJS usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const {
|
|
15
|
+
generateUsers,
|
|
16
|
+
generateProducts,
|
|
17
|
+
generateOrders,
|
|
18
|
+
createDataForge,
|
|
19
|
+
exportToJSON,
|
|
20
|
+
exportToCSV
|
|
21
|
+
} = require('parag-dataforge');
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Seeded generation
|
|
25
|
+
|
|
26
|
+
Use `createDataForge({ seed })` to create an independent generator instance. Seeds can be finite numbers or non-empty strings:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const numericForge = createDataForge({ seed: 12345 });
|
|
30
|
+
const stringForge = createDataForge({ seed: 'checkout-fixture' });
|
|
31
|
+
|
|
32
|
+
const users = numericForge.generateUsers(10);
|
|
33
|
+
const products = numericForge.generateProducts(8);
|
|
34
|
+
const orders = numericForge.generateOrders(12, users, products);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
An instance owns its random state. Separate instances initialized with the same seed produce the same data when given the same arguments in the same sequence. Calls on one instance advance its stream and do not affect another instance. Reproducibility applies when the generator implementation and call sequence are unchanged.
|
|
38
|
+
|
|
39
|
+
## Generate a complete relational dataset
|
|
40
|
+
|
|
41
|
+
`generateDataset` creates users first, products second, and orders last, using the same seeded instance for every collection:
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
const forge = createDataForge({ seed: 12345 });
|
|
45
|
+
const dataset = forge.generateDataset({
|
|
46
|
+
users: 10,
|
|
47
|
+
products: 20,
|
|
48
|
+
orders: 30
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const users = dataset.users;
|
|
52
|
+
const products = dataset.products;
|
|
53
|
+
const orders = dataset.orders;
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Every generated order references a user in `dataset.users`; each order item references a product in `dataset.products`. Product references within an order are distinct, and totals are calculated from the referenced products and item quantities. Each collection's IDs are unique within that collection.
|
|
57
|
+
|
|
58
|
+
Two independent instances with the same seed and identical `generateDataset` arguments produce deeply equal datasets. Calling `generateDataset` advances that instance's random stream; it does not reset the seed. Counts default to zero when omitted, so `forge.generateDataset({})` returns `{ users: [], products: [], orders: [] }`. Counts must be non-negative integers, and a positive order count requires at least one user and one product.
|
|
59
|
+
|
|
60
|
+
## Generate data
|
|
61
|
+
|
|
62
|
+
The direct generator functions remain available for convenience. Each takes a non-negative integer count and returns an array. Direct calls use JavaScript's `Math.random()` and are not seeded or reproducible:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
const users = generateUsers(10);
|
|
66
|
+
const products = generateProducts(8);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
For reproducible data, use the corresponding methods on a `createDataForge` instance.
|
|
70
|
+
|
|
71
|
+
Users contain `id`, `firstName`, `lastName`, `fullName`, `email`, and `age`. Products contain `id`, `name`, `category`, `price`, `stock`, and `sku`.
|
|
72
|
+
|
|
73
|
+
Generate orders by passing the users and products they may reference:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
const orders = generateOrders(12, users, products);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Each order has an `id`, a `userId` referencing one of the supplied users, `items` with `productId` references to supplied products and positive integer quantities, and a `total` calculated from those product prices. Each order includes between one and three distinct products, limited by the number of unique product IDs provided. For a positive order count, both input arrays must be non-empty and records must have valid IDs; product prices must be non-negative finite numbers. A zero order count returns an empty array.
|
|
80
|
+
|
|
81
|
+
## Export data
|
|
82
|
+
|
|
83
|
+
`exportToJSON(data)` returns a two-space-indented JSON string for an array:
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
const json = exportToJSON(users);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`exportToCSV(data)` returns CSV text. It uses the union of object keys in first-seen order, leaves missing and null values empty, and serializes nested objects and arrays as JSON strings:
|
|
90
|
+
|
|
91
|
+
```js
|
|
92
|
+
const csv = exportToCSV(orders);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
CSV fields are escaped using standard double-quote escaping for commas, quotes, and line breaks. Text values beginning with optional whitespace followed by `=`, `+`, `-`, or `@` receive a leading apostrophe to reduce spreadsheet formula-injection risk. Numeric values remain numeric, including ordinary negative numbers such as `-12.5`; a string such as `'-12.5'` is treated as text and receives the safety prefix. Empty input arrays export as an empty string in CSV and `[]` in JSON. Both exporters reject non-array input, and CSV also rejects records that are null, primitive values, or arrays.
|
|
96
|
+
|
|
97
|
+
## Development
|
|
98
|
+
|
|
99
|
+
The package uses Node.js built-in modules and has no runtime dependencies. Run the test suite with:
|
|
100
|
+
|
|
101
|
+
```sh
|
|
102
|
+
npm test
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
MIT. See [LICENSE](LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "parag-dataforge",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A lightweight JavaScript package for generating realistic and relationally consistent test data",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node --test"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"data",
|
|
11
|
+
"test-data",
|
|
12
|
+
"dummy-data",
|
|
13
|
+
"data-generator",
|
|
14
|
+
"fixtures"
|
|
15
|
+
],
|
|
16
|
+
"author": "Parag Garg",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"type": "commonjs",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/gargparag712-ship-it/parag-dataforge-js.git"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/gargparag712-ship-it/parag-dataforge-js/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/gargparag712-ship-it/parag-dataforge-js#readme",
|
|
27
|
+
"files": [
|
|
28
|
+
"src",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
]
|
|
32
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const { createRandom } = require('./random');
|
|
2
|
+
const { generateUsers } = require('../generators/users');
|
|
3
|
+
const { generateProducts } = require('../generators/products');
|
|
4
|
+
const { generateOrders } = require('../generators/orders');
|
|
5
|
+
|
|
6
|
+
/** Create an isolated set of generators backed by a seeded random stream. */
|
|
7
|
+
function createDataForge(options) {
|
|
8
|
+
if (options === null || typeof options !== 'object' || Array.isArray(options)) {
|
|
9
|
+
throw new TypeError('options must be an object containing a seed.');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const random = createRandom(options.seed);
|
|
13
|
+
|
|
14
|
+
function forgeUsers(count) {
|
|
15
|
+
return generateUsers(count, random);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function forgeProducts(count) {
|
|
19
|
+
return generateProducts(count, random);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function forgeOrders(count, users, products) {
|
|
23
|
+
return generateOrders(count, users, products, random);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function generateDataset(datasetOptions) {
|
|
27
|
+
if (
|
|
28
|
+
datasetOptions === null
|
|
29
|
+
|| typeof datasetOptions !== 'object'
|
|
30
|
+
|| Array.isArray(datasetOptions)
|
|
31
|
+
) {
|
|
32
|
+
throw new TypeError('dataset options must be a non-null object.');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const counts = {};
|
|
36
|
+
for (const name of ['users', 'products', 'orders']) {
|
|
37
|
+
const isProvided = Object.prototype.hasOwnProperty.call(datasetOptions, name);
|
|
38
|
+
const count = isProvided ? datasetOptions[name] : 0;
|
|
39
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
40
|
+
throw new TypeError(`${name} must be a non-negative integer.`);
|
|
41
|
+
}
|
|
42
|
+
counts[name] = count;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (counts.orders > 0 && counts.users === 0) {
|
|
46
|
+
throw new TypeError('users must be greater than zero when orders is greater than zero.');
|
|
47
|
+
}
|
|
48
|
+
if (counts.orders > 0 && counts.products === 0) {
|
|
49
|
+
throw new TypeError('products must be greater than zero when orders is greater than zero.');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const users = forgeUsers(counts.users);
|
|
53
|
+
const products = forgeProducts(counts.products);
|
|
54
|
+
const orders = forgeOrders(counts.orders, users, products);
|
|
55
|
+
|
|
56
|
+
return { users, products, orders };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
generateUsers: forgeUsers,
|
|
61
|
+
generateProducts: forgeProducts,
|
|
62
|
+
generateOrders: forgeOrders,
|
|
63
|
+
generateDataset
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = { createDataForge };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Convert supported seeds into a stable 32-bit initial state. */
|
|
2
|
+
function seedToUint32(seed) {
|
|
3
|
+
if (typeof seed !== 'number' && typeof seed !== 'string') {
|
|
4
|
+
throw new TypeError('seed must be a finite number or a non-empty string.');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
if (typeof seed === 'number' && !Number.isFinite(seed)) {
|
|
8
|
+
throw new TypeError('seed must be a finite number or a non-empty string.');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (typeof seed === 'string' && seed.length === 0) {
|
|
12
|
+
throw new TypeError('seed must be a finite number or a non-empty string.');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const input = `${typeof seed}:${String(seed)}`;
|
|
16
|
+
let hash = 0x811c9dc5;
|
|
17
|
+
|
|
18
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
19
|
+
hash ^= input.charCodeAt(index);
|
|
20
|
+
hash = Math.imul(hash, 0x01000193);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return hash >>> 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Create an independent Mulberry32 random number generator in [0, 1). */
|
|
27
|
+
function createRandom(seed) {
|
|
28
|
+
let state = seedToUint32(seed);
|
|
29
|
+
|
|
30
|
+
return function random() {
|
|
31
|
+
state = (state + 0x6d2b79f5) >>> 0;
|
|
32
|
+
let value = state;
|
|
33
|
+
value = Math.imul(value ^ (value >>> 15), value | 1);
|
|
34
|
+
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
|
|
35
|
+
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = { createRandom };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate orders using the users and products provided by the caller.
|
|
3
|
+
* @param {number} count Number of orders to generate.
|
|
4
|
+
* @param {Array<object>} users Existing users that orders can reference.
|
|
5
|
+
* @param {Array<object>} products Existing products that orders can contain.
|
|
6
|
+
* @returns {{id: string, userId: string, items: {productId: string, quantity: number}[], total: number}[]}
|
|
7
|
+
*/
|
|
8
|
+
function generateOrders(count, users, products, random = Math.random) {
|
|
9
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
10
|
+
throw new TypeError('count must be a non-negative integer.');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (count === 0) {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (!Array.isArray(users) || users.length === 0) {
|
|
18
|
+
throw new TypeError('users must be a non-empty array when count is greater than zero.');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!Array.isArray(products) || products.length === 0) {
|
|
22
|
+
throw new TypeError('products must be a non-empty array when count is greater than zero.');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
for (const user of users) {
|
|
26
|
+
if (!user || typeof user.id !== 'string' || user.id.trim() === '') {
|
|
27
|
+
throw new TypeError('Each user must have a non-empty string id.');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
for (const product of products) {
|
|
32
|
+
if (!product || typeof product.id !== 'string' || product.id.trim() === '') {
|
|
33
|
+
throw new TypeError('Each product must have a non-empty string id.');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (typeof product.price !== 'number' || !Number.isFinite(product.price) || product.price < 0) {
|
|
37
|
+
throw new TypeError('Each product price must be a non-negative finite number.');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Treat records with the same ID as the same product for order items.
|
|
42
|
+
const uniqueProducts = [];
|
|
43
|
+
const seenProductIds = new Set();
|
|
44
|
+
for (const product of products) {
|
|
45
|
+
if (!seenProductIds.has(product.id)) {
|
|
46
|
+
uniqueProducts.push(product);
|
|
47
|
+
seenProductIds.add(product.id);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const orders = [];
|
|
52
|
+
|
|
53
|
+
for (let index = 0; index < count; index += 1) {
|
|
54
|
+
const user = users[Math.floor(random() * users.length)];
|
|
55
|
+
const shuffledProducts = uniqueProducts.slice();
|
|
56
|
+
|
|
57
|
+
// Shuffle a copy so this order can select distinct products.
|
|
58
|
+
for (let position = shuffledProducts.length - 1; position > 0; position -= 1) {
|
|
59
|
+
const swapIndex = Math.floor(random() * (position + 1));
|
|
60
|
+
[shuffledProducts[position], shuffledProducts[swapIndex]] = [
|
|
61
|
+
shuffledProducts[swapIndex],
|
|
62
|
+
shuffledProducts[position]
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const maxItems = Math.min(3, shuffledProducts.length);
|
|
67
|
+
const itemCount = Math.floor(random() * maxItems) + 1;
|
|
68
|
+
const items = [];
|
|
69
|
+
|
|
70
|
+
for (let itemIndex = 0; itemIndex < itemCount; itemIndex += 1) {
|
|
71
|
+
items.push({
|
|
72
|
+
productId: shuffledProducts[itemIndex].id,
|
|
73
|
+
quantity: Math.floor(random() * 5) + 1
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const unroundedTotal = items.reduce((sum, item) => {
|
|
78
|
+
const product = uniqueProducts.find((entry) => entry.id === item.productId);
|
|
79
|
+
return sum + product.price * item.quantity;
|
|
80
|
+
}, 0);
|
|
81
|
+
const total = Math.round(unroundedTotal * 100) / 100;
|
|
82
|
+
|
|
83
|
+
orders.push({
|
|
84
|
+
id: `order-${index + 1}`,
|
|
85
|
+
userId: user.id,
|
|
86
|
+
items,
|
|
87
|
+
total
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return orders;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = { generateOrders };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const productsByCategory = {
|
|
2
|
+
Electronics: [
|
|
3
|
+
{ name: 'Wireless Mouse', minPrice: 15, maxPrice: 60 },
|
|
4
|
+
{ name: 'Bluetooth Speaker', minPrice: 25, maxPrice: 150 },
|
|
5
|
+
{ name: 'USB-C Charger', minPrice: 12, maxPrice: 45 }
|
|
6
|
+
],
|
|
7
|
+
Clothing: [
|
|
8
|
+
{ name: 'Cotton T-Shirt', minPrice: 10, maxPrice: 40 },
|
|
9
|
+
{ name: 'Denim Jeans', minPrice: 35, maxPrice: 120 },
|
|
10
|
+
{ name: 'Wool Scarf', minPrice: 18, maxPrice: 70 }
|
|
11
|
+
],
|
|
12
|
+
Stationery: [
|
|
13
|
+
{ name: 'Hardcover Notebook', minPrice: 5, maxPrice: 25 },
|
|
14
|
+
{ name: 'Fountain Pen', minPrice: 12, maxPrice: 80 },
|
|
15
|
+
{ name: 'Colored Pencil Set', minPrice: 6, maxPrice: 30 }
|
|
16
|
+
],
|
|
17
|
+
'Home & Kitchen': [
|
|
18
|
+
{ name: 'Ceramic Dinner Plate Set', minPrice: 20, maxPrice: 90 },
|
|
19
|
+
{ name: 'Stainless Steel Kettle', minPrice: 25, maxPrice: 110 },
|
|
20
|
+
{ name: 'Cotton Bath Towel', minPrice: 8, maxPrice: 45 }
|
|
21
|
+
]
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const categoryPrefixes = {
|
|
25
|
+
Electronics: 'ELC',
|
|
26
|
+
Clothing: 'CLT',
|
|
27
|
+
Stationery: 'STN',
|
|
28
|
+
'Home & Kitchen': 'HMK'
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const categories = Object.keys(productsByCategory);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Generate an array of dummy products.
|
|
35
|
+
* @param {number} count Number of products to generate.
|
|
36
|
+
* @returns {{id: string, name: string, category: string, price: number, stock: number, sku: string}[]}
|
|
37
|
+
*/
|
|
38
|
+
function generateProducts(count, random = Math.random) {
|
|
39
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
40
|
+
throw new TypeError('count must be a non-negative integer.');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const products = [];
|
|
44
|
+
|
|
45
|
+
for (let index = 0; index < count; index += 1) {
|
|
46
|
+
// Rotate through categories so larger datasets include each category.
|
|
47
|
+
const category = categories[index % categories.length];
|
|
48
|
+
const categoryProducts = productsByCategory[category];
|
|
49
|
+
const product = categoryProducts[Math.floor(random() * categoryProducts.length)];
|
|
50
|
+
const priceInCents = Math.floor(
|
|
51
|
+
random() * ((product.maxPrice - product.minPrice) * 100 + 1)
|
|
52
|
+
) + product.minPrice * 100;
|
|
53
|
+
|
|
54
|
+
products.push({
|
|
55
|
+
id: `product-${index + 1}`,
|
|
56
|
+
name: product.name,
|
|
57
|
+
category,
|
|
58
|
+
price: priceInCents / 100,
|
|
59
|
+
stock: Math.floor(random() * 501),
|
|
60
|
+
sku: `${categoryPrefixes[category]}-${String(index + 1).padStart(4, '0')}`
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return products;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = { generateProducts };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const firstNames = [
|
|
2
|
+
'Ava', 'Liam', 'Mia', 'Noah', 'Sofia',
|
|
3
|
+
'Ethan', 'Isabella', 'Lucas', 'Amara', 'Oliver'
|
|
4
|
+
];
|
|
5
|
+
|
|
6
|
+
const lastNames = [
|
|
7
|
+
'Patel', 'Johnson', 'Garcia', 'Smith', 'Chen',
|
|
8
|
+
'Brown', 'Khan', 'Wilson', 'Martin', 'Taylor'
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Generate an array of dummy users.
|
|
13
|
+
* @param {number} count Number of users to generate.
|
|
14
|
+
* @returns {{id: string, firstName: string, lastName: string, fullName: string, email: string, age: number}[]}
|
|
15
|
+
*/
|
|
16
|
+
function generateUsers(count, random = Math.random) {
|
|
17
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
18
|
+
throw new TypeError('count must be a non-negative integer.');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const users = [];
|
|
22
|
+
|
|
23
|
+
for (let index = 0; index < count; index += 1) {
|
|
24
|
+
const firstName = firstNames[Math.floor(random() * firstNames.length)];
|
|
25
|
+
const lastName = lastNames[Math.floor(random() * lastNames.length)];
|
|
26
|
+
const fullName = `${firstName} ${lastName}`;
|
|
27
|
+
|
|
28
|
+
users.push({
|
|
29
|
+
id: `user-${index + 1}`,
|
|
30
|
+
firstName,
|
|
31
|
+
lastName,
|
|
32
|
+
fullName,
|
|
33
|
+
// The index suffix keeps addresses unique even when names repeat.
|
|
34
|
+
email: `${firstName}.${lastName}.${index + 1}@example.com`.toLowerCase(),
|
|
35
|
+
age: Math.floor(random() * 63) + 18
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return users;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = { generateUsers };
|
package/src/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { generateUsers } = require('./generators/users');
|
|
2
|
+
const { generateProducts } = require('./generators/products');
|
|
3
|
+
const { generateOrders } = require('./generators/orders');
|
|
4
|
+
const { exportToJSON, exportToCSV } = require('./utils/exporters');
|
|
5
|
+
const { createDataForge } = require('./core/dataforge');
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
generateUsers,
|
|
9
|
+
generateProducts,
|
|
10
|
+
generateOrders,
|
|
11
|
+
createDataForge,
|
|
12
|
+
exportToJSON,
|
|
13
|
+
exportToCSV
|
|
14
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/** Convert an array of objects into a formatted JSON string. */
|
|
2
|
+
function exportToJSON(data) {
|
|
3
|
+
if (!Array.isArray(data)) {
|
|
4
|
+
throw new TypeError('data must be an array.');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
return JSON.stringify(data, null, 2);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function formatCSVValue(value) {
|
|
11
|
+
if (value === null || value === undefined) {
|
|
12
|
+
return '';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Keep numeric values numeric in spreadsheet programs. For stringified data,
|
|
16
|
+
// prefix formula-leading text with an apostrophe before normal CSV escaping.
|
|
17
|
+
let text = typeof value === 'object' ? JSON.stringify(value) : String(value);
|
|
18
|
+
if (typeof value !== 'number' && /^[\s]*[=+\-@]/.test(text)) {
|
|
19
|
+
text = `'${text}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const escapedText = text.replace(/"/g, '""');
|
|
23
|
+
|
|
24
|
+
if (/[",\r\n]/.test(text)) {
|
|
25
|
+
return `"${escapedText}"`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return escapedText;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Convert an array of objects into CSV using the union of keys in first-seen order.
|
|
33
|
+
* Formula-leading text is prefixed with an apostrophe for spreadsheet safety;
|
|
34
|
+
* numeric values, including negative numbers, remain numeric.
|
|
35
|
+
*/
|
|
36
|
+
function exportToCSV(data) {
|
|
37
|
+
if (!Array.isArray(data)) {
|
|
38
|
+
throw new TypeError('data must be an array.');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (data.length === 0) {
|
|
42
|
+
return '';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const item of data) {
|
|
46
|
+
if (item === null || typeof item !== 'object' || Array.isArray(item)) {
|
|
47
|
+
throw new TypeError('Each item in data must be a non-null object.');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const headers = [];
|
|
52
|
+
const seenHeaders = new Set();
|
|
53
|
+
for (const item of data) {
|
|
54
|
+
for (const header of Object.keys(item)) {
|
|
55
|
+
if (!seenHeaders.has(header)) {
|
|
56
|
+
headers.push(header);
|
|
57
|
+
seenHeaders.add(header);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const rows = [headers.map(formatCSVValue).join(',')];
|
|
63
|
+
|
|
64
|
+
for (const item of data) {
|
|
65
|
+
rows.push(headers.map((header) => formatCSVValue(item[header])).join(','));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return rows.join('\n');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = { exportToJSON, exportToCSV };
|