-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (76 loc) · 2.51 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const _ = require("lodash");
const validUrl = require("valid-url");
const uuid = require("uuid");
/**
* Converts setTimeout to a promise
* @param {number} [time = 5000] - The amount of time to pause for before it resolves. Default is 5000 (5 secs).
* @returns {Promise <void>} - Returns a resolved Promise
*/
const sleep = async(time) => {
const pause = time || 5 * 1000;
return new Promise((resolve) => {
setTimeout(resolve, pause);
});
};
/**
* Selects a random number between then converts it to seconds
* @param {object} [options = {min:5, max:7}] - The options passed for random selection
* @param {number} [options.min = 5] - The min value that should be selected defaults to 5
* @param {number} [options.max = 7] - The max value the should be selected defaults to 7
* @returns {number} - A random number converted to seconds
*/
const randomTime = (options) => {
if (!_.isNil(options) && _.gt(options.min, options.max)) {
throw new RangeError("Min value can't be greater than max");
}
if (!_.isNil(options) && _.eq(options.min, options.max)) {
throw new RangeError("Min time can't equal Max time");
}
const defaults = _.merge({}, { min: 5, max: 7 }, options);
return _.random(defaults.min, defaults.max) * 1000;
};
/**
* Selects a random number from the min and max provided
* @param {object} [options = {min:5, max:7}] - The options passed for random selection
* @param {number} [options.min = 1] - The min value that should be selected defaults to 1
* @param {number} [options.max = 10] - The max value the should be selected defaults to 10
* @returns {number} -Returns a random number
*/
const randomNumber = (options) => {
const defaults = _.merge({}, { min: 1, max: 10 }, options);
return _.random(defaults.min, defaults.max);
};
/**
* Validates if the url given is valid
* @param url {string} - The url you want validated
* @returns {boolean} - Returns true or false on whether the url is valid
*/
const isValidUrl = (url) => {
return validUrl.isUri(url);
};
/**
* Generates UUID
* @returns {string} - Returns a random
*/
const generateUUID = () => {
return uuid();
};
/**
* Returns a date object in the format of an epoch date
* @returns {number} Date
*/
const epochDate = () => {
return new Date().valueOf();
};
const prettyPrintJSON = (json) => {
console.log(JSON.stringify(json, null, 2));
};
module.exports = {
epochDate,
generateUUID,
isValidUrl,
prettyPrintJSON,
sleep,
randomNumber,
randomTime
};