@coderzz/mocker-data-generator 3.0.5 → 3.0.7
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/CHANGELOG.md +43 -0
- package/LICENSE +0 -0
- package/README.md +0 -0
- package/build/main/array-includes.d.ts +0 -0
- package/build/main/array-includes.js +45 -45
- package/build/main/index.d.ts +7 -7
- package/build/main/index.js +12 -12
- package/build/main/index.spec.d.ts +1 -1
- package/build/main/index.spec.js +16 -16
- package/build/main/lib/Generator.d.ts +61 -61
- package/build/main/lib/Generator.js +134 -134
- package/build/main/lib/Mocker.d.ts +22 -22
- package/build/main/lib/Mocker.js +76 -76
- package/build/main/lib/Schema.d.ts +9 -9
- package/build/main/lib/Schema.js +210 -210
- package/build/main/lib/types.d.ts +8 -8
- package/build/main/lib/types.js +2 -2
- package/build/main/lib/utils.d.ts +10 -10
- package/build/main/lib/utils.js +222 -222
- package/build/main/tests/Generator.db.spec.d.ts +1 -1
- package/build/main/tests/Generator.db.spec.js +33 -33
- package/build/main/tests/Generator.eval.spec.d.ts +1 -1
- package/build/main/tests/Generator.eval.spec.js +21 -21
- package/build/main/tests/Generator.function.spec.d.ts +1 -1
- package/build/main/tests/Generator.function.spec.js +48 -48
- package/build/main/tests/Generator.generator.spec.d.ts +1 -1
- package/build/main/tests/Generator.generator.spec.js +187 -187
- package/build/main/tests/Generator.hasMany.spec.d.ts +1 -1
- package/build/main/tests/Generator.hasMany.spec.js +115 -115
- package/build/main/tests/Generator.hasOne.spec.d.ts +1 -1
- package/build/main/tests/Generator.hasOne.spec.js +146 -146
- package/build/main/tests/Generator.incrementalId.spec.d.ts +1 -1
- package/build/main/tests/Generator.incrementalId.spec.js +26 -26
- package/build/main/tests/Generator.self.spec.d.ts +1 -1
- package/build/main/tests/Generator.self.spec.js +24 -24
- package/build/main/tests/Generator.static.spec.d.ts +1 -1
- package/build/main/tests/Generator.static.spec.js +15 -15
- package/build/main/tests/Generator.values.spec.d.ts +1 -1
- package/build/main/tests/Generator.values.spec.js +16 -16
- package/build/main/tests/Mocker.build.spec.d.ts +1 -1
- package/build/main/tests/Mocker.build.spec.js +196 -196
- package/build/main/tests/Mocker.reset.spec.d.ts +1 -1
- package/build/main/tests/Mocker.reset.spec.js +17 -17
- package/build/main/tests/Mocker.restart.spec.d.ts +1 -1
- package/build/main/tests/Mocker.restart.spec.js +24 -24
- package/build/main/tests/Mocker.schema.spec.d.ts +1 -1
- package/build/main/tests/Mocker.schema.spec.js +18 -18
- package/build/main/tests/Mocker.seed.spec.d.ts +1 -1
- package/build/main/tests/Mocker.seed.spec.js +42 -42
- package/build/main/tests/Schema.Array.spec.d.ts +1 -1
- package/build/main/tests/Schema.Array.spec.js +332 -332
- package/build/main/tests/Schema.BuildSingle.spec.d.ts +1 -1
- package/build/main/tests/Schema.BuildSingle.spec.js +13 -13
- package/build/main/tests/mocker.spec.d.ts +1 -1
- package/build/main/tests/mocker.spec.js +405 -405
- package/build/module/array-includes.js +45 -45
- package/build/module/index.js +7 -7
- package/build/module/lib/Generator.js +131 -131
- package/build/module/lib/Mocker.js +73 -73
- package/build/module/lib/Schema.js +207 -207
- package/build/module/lib/types.js +1 -1
- package/build/module/lib/utils.js +209 -209
- package/package.json +1 -1
@@ -1,207 +1,207 @@
|
|
1
|
-
import { __assign, __extends, __spreadArray } from "tslib";
|
2
|
-
import { Generator } from './Generator';
|
3
|
-
import { evalWithContextData, fieldArrayCalcLength, iamLastParent, isArray, isConditional, isObject } from './utils';
|
4
|
-
var iterate = function (obj, res, currentPath) {
|
5
|
-
var _this = this;
|
6
|
-
if (!currentPath) {
|
7
|
-
currentPath = [];
|
8
|
-
}
|
9
|
-
Object.keys(obj).map(function (k) {
|
10
|
-
var value = obj[k];
|
11
|
-
var path = currentPath.slice(0);
|
12
|
-
path.push(k);
|
13
|
-
if (iamLastParent(value)) {
|
14
|
-
if (path) {
|
15
|
-
if (isArray(value)) {
|
16
|
-
if (value[0] && value[0].virtual) {
|
17
|
-
_this.virtualPaths.push(path.toString());
|
18
|
-
}
|
19
|
-
}
|
20
|
-
else {
|
21
|
-
if (value.virtual) {
|
22
|
-
_this.virtualPaths.push(path.toString());
|
23
|
-
}
|
24
|
-
}
|
25
|
-
}
|
26
|
-
var key = '';
|
27
|
-
if (!isConditional(k)) {
|
28
|
-
key = k;
|
29
|
-
}
|
30
|
-
else {
|
31
|
-
var keykey = k.split(',');
|
32
|
-
if (evalWithContextData(keykey[0], _this.object, _this.DB, _this.generators)) {
|
33
|
-
key = keykey[1];
|
34
|
-
}
|
35
|
-
}
|
36
|
-
if (key !== '') {
|
37
|
-
res[key] = _this.proccessLeaf(value, key);
|
38
|
-
}
|
39
|
-
}
|
40
|
-
else {
|
41
|
-
res[k] = {};
|
42
|
-
iterate.call(_this, value, res[k], path);
|
43
|
-
}
|
44
|
-
});
|
45
|
-
};
|
46
|
-
var Schema = /** @class */ (function (_super) {
|
47
|
-
__extends(Schema, _super);
|
48
|
-
function Schema(name, cfg, options, generators) {
|
49
|
-
if (generators === void 0) { generators = {}; }
|
50
|
-
var _this = _super.call(this) || this;
|
51
|
-
_this.schema = cfg;
|
52
|
-
_this.name = name;
|
53
|
-
_this.options = options;
|
54
|
-
_this.generators = generators;
|
55
|
-
// Temp fields
|
56
|
-
_this.DB = {};
|
57
|
-
_this.object = {};
|
58
|
-
_this.virtualPaths = [];
|
59
|
-
return _this;
|
60
|
-
}
|
61
|
-
Schema.prototype.proccessLeaf = function (field, fieldName) {
|
62
|
-
var _this = this;
|
63
|
-
if (isArray(field)) {
|
64
|
-
var fieldConfig_1 = field[0];
|
65
|
-
if (field.length > 1) {
|
66
|
-
fieldConfig_1 = { values: field };
|
67
|
-
}
|
68
|
-
var na = Array();
|
69
|
-
if (fieldConfig_1.concat) {
|
70
|
-
na = evalWithContextData(fieldConfig_1.concat, this.object, this.DB, this.generators);
|
71
|
-
// Strict Mode
|
72
|
-
na = fieldConfig_1.concatStrict
|
73
|
-
? __spreadArray([], Array.from(new Set(na)), true) : na;
|
74
|
-
}
|
75
|
-
var length_1 = fieldArrayCalcLength(field.length > 1 ? fieldConfig_1.values : fieldConfig_1, na.length, this);
|
76
|
-
var array = Array.from(new Array(length_1)).reduce(function (acc, el, index) {
|
77
|
-
var self = acc.slice(0);
|
78
|
-
acc.push(_this.generateField(fieldName, fieldConfig_1, index, length_1, self));
|
79
|
-
return acc;
|
80
|
-
}, []);
|
81
|
-
return array.concat(na);
|
82
|
-
}
|
83
|
-
else {
|
84
|
-
return this.generateField(fieldName, field);
|
85
|
-
}
|
86
|
-
};
|
87
|
-
Schema.prototype.generateField = function (fieldName, cfg) {
|
88
|
-
var _a, _b;
|
89
|
-
var args = [];
|
90
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
91
|
-
args[_i - 2] = arguments[_i];
|
92
|
-
}
|
93
|
-
var result = {};
|
94
|
-
var customGenerators = Object.keys(this.generators);
|
95
|
-
var ownedByMocker = [
|
96
|
-
'self',
|
97
|
-
'db',
|
98
|
-
'hasOne',
|
99
|
-
'hasMany',
|
100
|
-
'static',
|
101
|
-
'function',
|
102
|
-
'values',
|
103
|
-
'incrementalId'
|
104
|
-
];
|
105
|
-
var generators = __spreadArray(__spreadArray([], ownedByMocker, true), customGenerators, true);
|
106
|
-
var keys = Object.keys(cfg);
|
107
|
-
var key = keys.reduce(function (acc, val) {
|
108
|
-
if (generators.includes(val)) {
|
109
|
-
acc = val;
|
110
|
-
}
|
111
|
-
return acc;
|
112
|
-
}, 'noKey');
|
113
|
-
if (key === 'noKey' && !keys.includes('eval')) {
|
114
|
-
throw "Error: Invalid or missing generator".concat(fieldName !== 'root' ? " on field ".concat(fieldName) : '', ". Please use one of this generators [").concat(generators.join(','), "], note that if your generator doesnt appear in the list maybe you forgot to add it.");
|
115
|
-
}
|
116
|
-
if (keys.includes('eval') && keys.length === 1) {
|
117
|
-
key = 'eval';
|
118
|
-
}
|
119
|
-
try {
|
120
|
-
result = customGenerators.includes(key)
|
121
|
-
? this.custom({
|
122
|
-
generator: (_a = this.generators[key]) === null || _a === void 0 ? void 0 : _a.library,
|
123
|
-
run: (_b = this.generators[key]) === null || _b === void 0 ? void 0 : _b.run,
|
124
|
-
input: cfg[key],
|
125
|
-
eval: cfg.eval
|
126
|
-
})
|
127
|
-
: this[key].apply(this, __spreadArray([__assign(__assign({}, cfg), { generators: this.generators })], args, false));
|
128
|
-
}
|
129
|
-
catch (e) {
|
130
|
-
throw 'Error: "' + key + '" ' + e;
|
131
|
-
}
|
132
|
-
return result;
|
133
|
-
};
|
134
|
-
Schema.prototype.buildSingle = function (schema) {
|
135
|
-
if (iamLastParent(schema)) {
|
136
|
-
this.object = this.proccessLeaf(schema, 'root');
|
137
|
-
}
|
138
|
-
else {
|
139
|
-
iterate.call(this, schema, this.object);
|
140
|
-
}
|
141
|
-
};
|
142
|
-
Schema.prototype.build = function (generators, db) {
|
143
|
-
var _this = this;
|
144
|
-
if (generators === void 0) { generators = {}; }
|
145
|
-
if (db === void 0) { db = {}; }
|
146
|
-
this.generators = generators;
|
147
|
-
this.object = {};
|
148
|
-
this.DB = db ? db : {};
|
149
|
-
this.DB[this.name] = this.DB[this.name] ? this.DB[this.name] : [];
|
150
|
-
if (Number.isInteger(this.options)) {
|
151
|
-
Array.from(new Array(this.options)).map(function () {
|
152
|
-
_this.buildSingle(_this.schema);
|
153
|
-
_this.DB[_this.name].push(_this.object);
|
154
|
-
_this.object = {};
|
155
|
-
});
|
156
|
-
}
|
157
|
-
else if (isObject(this.options) && this.options.max) {
|
158
|
-
var max = this.options.max;
|
159
|
-
var min = this.options.min ? this.options.min : 0;
|
160
|
-
var length = Math.floor(Math.random() * (max - min + 1) + min);
|
161
|
-
Array.from(new Array(length)).map(function () {
|
162
|
-
_this.buildSingle(_this.schema);
|
163
|
-
_this.DB[_this.name].push(_this.object);
|
164
|
-
_this.object = {};
|
165
|
-
});
|
166
|
-
}
|
167
|
-
else if (isObject(this.options) && this.options.uniqueField) {
|
168
|
-
var f_1 = this.options.uniqueField;
|
169
|
-
var entityConfig_1 = this.schema;
|
170
|
-
var possibleValues = void 0;
|
171
|
-
if (f_1 === '.') {
|
172
|
-
possibleValues = this.schema.values;
|
173
|
-
}
|
174
|
-
else {
|
175
|
-
if (this.schema[f_1]) {
|
176
|
-
if (isArray(this.schema[f_1].values)) {
|
177
|
-
possibleValues = this.schema[f_1].values;
|
178
|
-
}
|
179
|
-
else {
|
180
|
-
possibleValues = this.schema[f_1];
|
181
|
-
}
|
182
|
-
}
|
183
|
-
else {
|
184
|
-
throw "The field \"".concat(f_1, "\" not exists.");
|
185
|
-
}
|
186
|
-
}
|
187
|
-
if (!isArray(possibleValues)) {
|
188
|
-
throw "The posible values value is not an Array";
|
189
|
-
}
|
190
|
-
possibleValues.map(function (value) {
|
191
|
-
if (f_1 === '.') {
|
192
|
-
return;
|
193
|
-
}
|
194
|
-
entityConfig_1[f_1] = { static: value };
|
195
|
-
_this.buildSingle(entityConfig_1);
|
196
|
-
_this.DB[_this.name].push(_this.object);
|
197
|
-
_this.object = {};
|
198
|
-
});
|
199
|
-
}
|
200
|
-
else {
|
201
|
-
throw "An string \"".concat(this.options, "\" is not recognized as a parameter.");
|
202
|
-
}
|
203
|
-
return this.DB[this.name];
|
204
|
-
};
|
205
|
-
return Schema;
|
206
|
-
}(Generator));
|
207
|
-
export { Schema };
|
1
|
+
import { __assign, __extends, __spreadArray } from "tslib";
|
2
|
+
import { Generator } from './Generator';
|
3
|
+
import { evalWithContextData, fieldArrayCalcLength, iamLastParent, isArray, isConditional, isObject } from './utils';
|
4
|
+
var iterate = function (obj, res, currentPath) {
|
5
|
+
var _this = this;
|
6
|
+
if (!currentPath) {
|
7
|
+
currentPath = [];
|
8
|
+
}
|
9
|
+
Object.keys(obj).map(function (k) {
|
10
|
+
var value = obj[k];
|
11
|
+
var path = currentPath.slice(0);
|
12
|
+
path.push(k);
|
13
|
+
if (iamLastParent(value)) {
|
14
|
+
if (path) {
|
15
|
+
if (isArray(value)) {
|
16
|
+
if (value[0] && value[0].virtual) {
|
17
|
+
_this.virtualPaths.push(path.toString());
|
18
|
+
}
|
19
|
+
}
|
20
|
+
else {
|
21
|
+
if (value.virtual) {
|
22
|
+
_this.virtualPaths.push(path.toString());
|
23
|
+
}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
var key = '';
|
27
|
+
if (!isConditional(k)) {
|
28
|
+
key = k;
|
29
|
+
}
|
30
|
+
else {
|
31
|
+
var keykey = k.split(',');
|
32
|
+
if (evalWithContextData(keykey[0], _this.object, _this.DB, _this.generators)) {
|
33
|
+
key = keykey[1];
|
34
|
+
}
|
35
|
+
}
|
36
|
+
if (key !== '') {
|
37
|
+
res[key] = _this.proccessLeaf(value, key);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
else {
|
41
|
+
res[k] = {};
|
42
|
+
iterate.call(_this, value, res[k], path);
|
43
|
+
}
|
44
|
+
});
|
45
|
+
};
|
46
|
+
var Schema = /** @class */ (function (_super) {
|
47
|
+
__extends(Schema, _super);
|
48
|
+
function Schema(name, cfg, options, generators) {
|
49
|
+
if (generators === void 0) { generators = {}; }
|
50
|
+
var _this = _super.call(this) || this;
|
51
|
+
_this.schema = cfg;
|
52
|
+
_this.name = name;
|
53
|
+
_this.options = options;
|
54
|
+
_this.generators = generators;
|
55
|
+
// Temp fields
|
56
|
+
_this.DB = {};
|
57
|
+
_this.object = {};
|
58
|
+
_this.virtualPaths = [];
|
59
|
+
return _this;
|
60
|
+
}
|
61
|
+
Schema.prototype.proccessLeaf = function (field, fieldName) {
|
62
|
+
var _this = this;
|
63
|
+
if (isArray(field)) {
|
64
|
+
var fieldConfig_1 = field[0];
|
65
|
+
if (field.length > 1) {
|
66
|
+
fieldConfig_1 = { values: field };
|
67
|
+
}
|
68
|
+
var na = Array();
|
69
|
+
if (fieldConfig_1.concat) {
|
70
|
+
na = evalWithContextData(fieldConfig_1.concat, this.object, this.DB, this.generators);
|
71
|
+
// Strict Mode
|
72
|
+
na = fieldConfig_1.concatStrict
|
73
|
+
? __spreadArray([], Array.from(new Set(na)), true) : na;
|
74
|
+
}
|
75
|
+
var length_1 = fieldArrayCalcLength(field.length > 1 ? fieldConfig_1.values : fieldConfig_1, na.length, this);
|
76
|
+
var array = Array.from(new Array(length_1)).reduce(function (acc, el, index) {
|
77
|
+
var self = acc.slice(0);
|
78
|
+
acc.push(_this.generateField(fieldName, fieldConfig_1, index, length_1, self));
|
79
|
+
return acc;
|
80
|
+
}, []);
|
81
|
+
return array.concat(na);
|
82
|
+
}
|
83
|
+
else {
|
84
|
+
return this.generateField(fieldName, field);
|
85
|
+
}
|
86
|
+
};
|
87
|
+
Schema.prototype.generateField = function (fieldName, cfg) {
|
88
|
+
var _a, _b;
|
89
|
+
var args = [];
|
90
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
91
|
+
args[_i - 2] = arguments[_i];
|
92
|
+
}
|
93
|
+
var result = {};
|
94
|
+
var customGenerators = Object.keys(this.generators);
|
95
|
+
var ownedByMocker = [
|
96
|
+
'self',
|
97
|
+
'db',
|
98
|
+
'hasOne',
|
99
|
+
'hasMany',
|
100
|
+
'static',
|
101
|
+
'function',
|
102
|
+
'values',
|
103
|
+
'incrementalId'
|
104
|
+
];
|
105
|
+
var generators = __spreadArray(__spreadArray([], ownedByMocker, true), customGenerators, true);
|
106
|
+
var keys = Object.keys(cfg);
|
107
|
+
var key = keys.reduce(function (acc, val) {
|
108
|
+
if (generators.includes(val)) {
|
109
|
+
acc = val;
|
110
|
+
}
|
111
|
+
return acc;
|
112
|
+
}, 'noKey');
|
113
|
+
if (key === 'noKey' && !keys.includes('eval')) {
|
114
|
+
throw "Error: Invalid or missing generator".concat(fieldName !== 'root' ? " on field ".concat(fieldName) : '', ". Please use one of this generators [").concat(generators.join(','), "], note that if your generator doesnt appear in the list maybe you forgot to add it.");
|
115
|
+
}
|
116
|
+
if (keys.includes('eval') && keys.length === 1) {
|
117
|
+
key = 'eval';
|
118
|
+
}
|
119
|
+
try {
|
120
|
+
result = customGenerators.includes(key)
|
121
|
+
? this.custom({
|
122
|
+
generator: (_a = this.generators[key]) === null || _a === void 0 ? void 0 : _a.library,
|
123
|
+
run: (_b = this.generators[key]) === null || _b === void 0 ? void 0 : _b.run,
|
124
|
+
input: cfg[key],
|
125
|
+
eval: cfg.eval
|
126
|
+
})
|
127
|
+
: this[key].apply(this, __spreadArray([__assign(__assign({}, cfg), { generators: this.generators })], args, false));
|
128
|
+
}
|
129
|
+
catch (e) {
|
130
|
+
throw 'Error: "' + key + '" ' + e;
|
131
|
+
}
|
132
|
+
return result;
|
133
|
+
};
|
134
|
+
Schema.prototype.buildSingle = function (schema) {
|
135
|
+
if (iamLastParent(schema)) {
|
136
|
+
this.object = this.proccessLeaf(schema, 'root');
|
137
|
+
}
|
138
|
+
else {
|
139
|
+
iterate.call(this, schema, this.object);
|
140
|
+
}
|
141
|
+
};
|
142
|
+
Schema.prototype.build = function (generators, db) {
|
143
|
+
var _this = this;
|
144
|
+
if (generators === void 0) { generators = {}; }
|
145
|
+
if (db === void 0) { db = {}; }
|
146
|
+
this.generators = generators;
|
147
|
+
this.object = {};
|
148
|
+
this.DB = db ? db : {};
|
149
|
+
this.DB[this.name] = this.DB[this.name] ? this.DB[this.name] : [];
|
150
|
+
if (Number.isInteger(this.options)) {
|
151
|
+
Array.from(new Array(this.options)).map(function () {
|
152
|
+
_this.buildSingle(_this.schema);
|
153
|
+
_this.DB[_this.name].push(_this.object);
|
154
|
+
_this.object = {};
|
155
|
+
});
|
156
|
+
}
|
157
|
+
else if (isObject(this.options) && this.options.max) {
|
158
|
+
var max = this.options.max;
|
159
|
+
var min = this.options.min ? this.options.min : 0;
|
160
|
+
var length = Math.floor(Math.random() * (max - min + 1) + min);
|
161
|
+
Array.from(new Array(length)).map(function () {
|
162
|
+
_this.buildSingle(_this.schema);
|
163
|
+
_this.DB[_this.name].push(_this.object);
|
164
|
+
_this.object = {};
|
165
|
+
});
|
166
|
+
}
|
167
|
+
else if (isObject(this.options) && this.options.uniqueField) {
|
168
|
+
var f_1 = this.options.uniqueField;
|
169
|
+
var entityConfig_1 = this.schema;
|
170
|
+
var possibleValues = void 0;
|
171
|
+
if (f_1 === '.') {
|
172
|
+
possibleValues = this.schema.values;
|
173
|
+
}
|
174
|
+
else {
|
175
|
+
if (this.schema[f_1]) {
|
176
|
+
if (isArray(this.schema[f_1].values)) {
|
177
|
+
possibleValues = this.schema[f_1].values;
|
178
|
+
}
|
179
|
+
else {
|
180
|
+
possibleValues = this.schema[f_1];
|
181
|
+
}
|
182
|
+
}
|
183
|
+
else {
|
184
|
+
throw "The field \"".concat(f_1, "\" not exists.");
|
185
|
+
}
|
186
|
+
}
|
187
|
+
if (!isArray(possibleValues)) {
|
188
|
+
throw "The posible values value is not an Array";
|
189
|
+
}
|
190
|
+
possibleValues.map(function (value) {
|
191
|
+
if (f_1 === '.') {
|
192
|
+
return;
|
193
|
+
}
|
194
|
+
entityConfig_1[f_1] = { static: value };
|
195
|
+
_this.buildSingle(entityConfig_1);
|
196
|
+
_this.DB[_this.name].push(_this.object);
|
197
|
+
_this.object = {};
|
198
|
+
});
|
199
|
+
}
|
200
|
+
else {
|
201
|
+
throw "An string \"".concat(this.options, "\" is not recognized as a parameter.");
|
202
|
+
}
|
203
|
+
return this.DB[this.name];
|
204
|
+
};
|
205
|
+
return Schema;
|
206
|
+
}(Generator));
|
207
|
+
export { Schema };
|
@@ -1 +1 @@
|
|
1
|
-
export {};
|
1
|
+
export {};
|