@bukuzend/simple-mock-gen 0.0.1

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.
Files changed (2) hide show
  1. package/index.js +57 -0
  2. package/package.json +9 -0
package/index.js ADDED
@@ -0,0 +1,57 @@
1
+ class MockSimple {
2
+ mocks = {};
3
+ generateMockObject(mockName, template, count = 1) {
4
+ for (let i = 0; i < count; i++) {
5
+ this.addInMock(mockName, this.generateObject(template));
6
+ }
7
+ }
8
+ getMock(mockName = "") {
9
+ if (!mockName) {
10
+ return this.mocks;
11
+ }
12
+ return mockName.length && this.mocks[mockName] ? this.mocks[mockName] : undefined;
13
+ }
14
+ generateObject(template) {
15
+ const obj = {};
16
+ for (let [key, value] of Object.entries(template)) {
17
+ if (typeof value === "string") {
18
+ obj[key] = this.getRandomFromArray(value);
19
+ }
20
+ else if (Array.isArray(value)) {
21
+ obj[key] = this.generateRandomArray(value);
22
+ }
23
+ else if (typeof value === "object") {
24
+ obj[key] = this.generateObject(value);
25
+ }
26
+ else {
27
+ throw new Error("Wrong Type in generate");
28
+ }
29
+ }
30
+ return obj;
31
+ }
32
+ generateRandomArray(arr) {
33
+ const finArr = [];
34
+ const randomCount = Math.floor(Math.random() * 10);
35
+ for (let i = 0; i < randomCount; i++) {
36
+ finArr.push(arr[Math.floor(Math.random() * arr.length)]);
37
+ }
38
+ return finArr;
39
+ }
40
+ getRandomFromArray(strItems) {
41
+ const items = JSON.parse(strItems);
42
+ return items[Math.floor(Math.random() * items.length)];
43
+ }
44
+ addInMock(mockName, value) {
45
+ if (this.checkExistGroup(mockName)) {
46
+ this.mocks[mockName]?.push(value);
47
+ }
48
+ else {
49
+ this.mocks[mockName] = [];
50
+ this.mocks[mockName]?.push(value);
51
+ }
52
+ }
53
+ checkExistGroup(mockName) {
54
+ return this.mocks[mockName] ? mockName : false;
55
+ }
56
+ }
57
+ module.export = MockSimple;
package/package.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "@bukuzend/simple-mock-gen",
3
+ "version": "0.0.1",
4
+ "description": "A simple library for generating mock data.",
5
+ "main": "index.js",
6
+ "scripts": {},
7
+ "author": "bukuzend",
8
+ "license": "ISC"
9
+ }