-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomUser.js
66 lines (53 loc) · 1.58 KB
/
RandomUser.js
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
58
59
60
61
62
63
64
65
66
const json = require('./data.js');
const users = json.results;
const MAX = users.length;
const RandomUser = function () {
this.evaluate = function () {
return getObject(users[getRandomInt()], this.fieldType);
}
this.title = function() {
return this.fieldType ? `Random ${this.fieldType}` : 'Choose field';
}
}
function getRandomInt() {
return Math.floor(Math.random() * Math.floor(MAX));
}
function getObject(obj, pathString) {
if (!pathString) {
return '';
}
const path = pathString.split('.');
for (let key of path) {
obj = obj[key];
}
return obj;
}
function buildSelectOptions() {
function buildOptions(obj, path, options) {
if (!obj) {
return;
}
Object.keys(obj).forEach(key => {
const type = typeof obj[key];
if (['string', 'number', 'boolean'].indexOf(type) >= 0) {
const pathString = path.concat(key).join('.');
options[pathString] = pathString;
} else if (type === 'object') {
buildOptions(obj[key], path.concat(key), options);
}
});
}
const options = {};
buildOptions(users[getRandomInt()], [], options);
return options;
}
RandomUser.identifier = "dev.anuragsaini.RandomUser";
RandomUser.title = "Random User";
RandomUser.help = "https://randomuser.me/documentation";
RandomUser.inputs = [
InputField("fieldType", "Field Type", "Select", {
persisted: true,
choices: buildSelectOptions()
})
];
registerDynamicValueClass(RandomUser);