Skip to content

Commit 05920ea

Browse files
committed
An advanced object property extraction function. Allows you to defined property callbacks, arrays to pick from, random ints and random floats.
1 parent 9ee3e75 commit 05920ea

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var MATH = require('../../math');
2+
var GetObjectValue = require('./GetObjectValue');
3+
4+
// Allowed types:
5+
6+
// Implicit
7+
// {
8+
// x: 4
9+
// }
10+
//
11+
// From function
12+
// {
13+
// x: function ()
14+
// }
15+
//
16+
// Randomly pick one element from the array
17+
// {
18+
// x: [a, b, c, d, e, f]
19+
// }
20+
//
21+
// Random integer between min and max:
22+
// {
23+
// x: { randInt: [min, max] }
24+
// }
25+
//
26+
// Random float between min and max:
27+
// {
28+
// x: { randFloat: [min, max] }
29+
// }
30+
31+
var GetAdvancedValue = function (source, key, defaultValue)
32+
{
33+
var value = GetObjectValue(source, key, null);
34+
35+
if (value === null)
36+
{
37+
return defaultValue;
38+
}
39+
else if (Array.isArray(value))
40+
{
41+
return MATH.RND.pick(value);
42+
}
43+
else if (typeof value === 'object')
44+
{
45+
if (value.hasOwnProperty('randInt'))
46+
{
47+
return MATH.RND.integerInRange(value.randInt[0], value.randInt[1]);
48+
}
49+
else if (value.hasOwnProperty('randFloat'))
50+
{
51+
return MATH.RND.realInRange(value.randFloat[0], value.randFloat[1]);
52+
}
53+
}
54+
else if (typeof value === 'function')
55+
{
56+
return value(key);
57+
}
58+
59+
return value;
60+
};
61+
62+
module.exports = GetAdvancedValue;

0 commit comments

Comments
 (0)