-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
46 lines (38 loc) · 1.12 KB
/
utils.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
// Get HTML element by id
export function getElement(selector, scope = document) {
return scope.getElementById(selector);
}
// Select HTML element
export function select(selector, scope = document) {
return scope.querySelector(selector);
}
// Get a list of HTML elements as an array
export function selectAll(selector, scope = document) {
return [...scope.querySelectorAll(selector)];
}
// Add event listener
export function listen(event, selector, callback) {
return selector.addEventListener(event, callback);
}
// Create HTML element
export function create(element) {
return document.createElement(element);
}
// Initialize array with n values from 0 to n - 1
export function initArray(n) {
return Array.from(Array(n).keys());
}
// Generate random number between - and including - 'min' and 'max'
export function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Sleep
export function sleep(duration) {
return new Promise(resolve => {
setTimeout(resolve, duration)
});
}
// Print multiple arguments
export function print(...args) {
args.forEach(arg => console.log(arg));
}