@travetto/schema-faker 7.1.4 → 8.0.0-alpha.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/package.json +3 -3
- package/src/faker.ts +30 -25
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/schema-faker",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Data generation for schema-registered objects.",
|
|
6
6
|
"keywords": [
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"directory": "module/schema-faker"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@faker-js/faker": "^10.
|
|
30
|
-
"@travetto/schema": "^
|
|
29
|
+
"@faker-js/faker": "^10.3.0",
|
|
30
|
+
"@travetto/schema": "^8.0.0-alpha.0"
|
|
31
31
|
},
|
|
32
32
|
"travetto": {
|
|
33
33
|
"displayName": "Schema Faker"
|
package/src/faker.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { faker } from '@faker-js/faker';
|
|
2
2
|
|
|
3
|
-
import { type Class, TimeUtil } from '@travetto/runtime';
|
|
3
|
+
import { castTo, type Class, TimeUtil } from '@travetto/runtime';
|
|
4
4
|
import { BindUtil, type SchemaFieldConfig, CommonRegex, SchemaRegistryIndex } from '@travetto/schema';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -53,8 +53,8 @@ export class SchemaFaker {
|
|
|
53
53
|
|
|
54
54
|
static #between(fromDays: number, toDays: number): Date {
|
|
55
55
|
return faker.date.between({
|
|
56
|
-
from: TimeUtil.fromNow(fromDays
|
|
57
|
-
to: TimeUtil.fromNow(toDays
|
|
56
|
+
from: TimeUtil.fromNow(`${fromDays}d`),
|
|
57
|
+
to: TimeUtil.fromNow(`${toDays}d`)
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
60
|
|
|
@@ -89,34 +89,43 @@ export class SchemaFaker {
|
|
|
89
89
|
let offset = 1;
|
|
90
90
|
|
|
91
91
|
if (precision !== undefined) {
|
|
92
|
-
min
|
|
93
|
-
max
|
|
92
|
+
min ??= -((10 ** precision[0]) - 1);
|
|
93
|
+
max ??= ((10 ** precision[0]) - 1);
|
|
94
94
|
if (precision[1] !== undefined) {
|
|
95
95
|
offset = (10 ** (precision[1] || 0));
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
max
|
|
100
|
-
min
|
|
99
|
+
max ??= 1000;
|
|
100
|
+
min ??= 0;
|
|
101
101
|
|
|
102
102
|
const range = (max - min) * offset;
|
|
103
|
-
|
|
104
103
|
const result = Math.trunc(Math.random() * range);
|
|
105
|
-
|
|
106
104
|
return (result / offset) + min;
|
|
107
105
|
}
|
|
108
106
|
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Get a new bigint value
|
|
110
|
+
* @param config BigInt config
|
|
111
|
+
*/
|
|
112
|
+
static #bigInt(config: SchemaFieldConfig): bigint {
|
|
113
|
+
const min = config.min && typeof config.min.limit === 'bigint' ? config.min.limit : 0n;
|
|
114
|
+
const max = config.max && typeof config.max.limit === 'bigint' ? config.max.limit : 1000n;
|
|
115
|
+
return faker.number.bigInt({ min, max, });
|
|
116
|
+
}
|
|
117
|
+
|
|
109
118
|
/**
|
|
110
119
|
* Get a date value
|
|
111
120
|
* @param config Field config
|
|
112
121
|
*/
|
|
113
122
|
static #date(config: SchemaFieldConfig): Date {
|
|
114
123
|
const name = config.name.toLowerCase();
|
|
115
|
-
const min = config.min &&
|
|
116
|
-
const max = config.max &&
|
|
124
|
+
const min = config.min && config.min.limit instanceof Date ? config.min.limit : undefined;
|
|
125
|
+
const max = config.max && config.max.limit instanceof Date ? config.max.limit : undefined;
|
|
117
126
|
|
|
118
127
|
if (min !== undefined || max !== undefined) {
|
|
119
|
-
return faker.date.between({ from: min || TimeUtil.fromNow(-
|
|
128
|
+
return faker.date.between({ from: min || TimeUtil.fromNow('-50d'), to: max || new Date() });
|
|
120
129
|
} else {
|
|
121
130
|
for (const [regex, fn] of this.#namesToType.date) {
|
|
122
131
|
if (regex.test(name)) {
|
|
@@ -162,19 +171,15 @@ export class SchemaFaker {
|
|
|
162
171
|
} else if (config.enum) {
|
|
163
172
|
return faker.helpers.arrayElement(config.enum.values);
|
|
164
173
|
} else {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
return this.#
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return this
|
|
174
|
-
} else if (typ === Boolean) {
|
|
175
|
-
return faker.datatype.boolean();
|
|
176
|
-
} else if (SchemaRegistryIndex.has(typ)) {
|
|
177
|
-
return this.generate(typ);
|
|
174
|
+
switch (config.type) {
|
|
175
|
+
case Number: return this.#number(config);
|
|
176
|
+
case castTo(BigInt): return this.#bigInt(config);
|
|
177
|
+
case String: return this.#string(config);
|
|
178
|
+
case Date: return this.#date(config);
|
|
179
|
+
case Boolean: return faker.datatype.boolean();
|
|
180
|
+
}
|
|
181
|
+
if (SchemaRegistryIndex.has(config.type)) {
|
|
182
|
+
return this.generate(config.type);
|
|
178
183
|
}
|
|
179
184
|
}
|
|
180
185
|
}
|