-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
158 lines (141 loc) · 3.03 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Applies async magic to handle almost any shit in your code.
*
* @summary The golden hack.
* @param {Function} f
*/
export default function fix (f) {
return setTimeout(f, 0);
}
/**
* Takes a function following the common promise style,
* and returns function in error-first style
* i.e. taking a (err, value) => ... callback as the last argument.
*
* @param {function} f
* @returns {Function}
*/
export function callbackotify(f) {
return (...args) => {
const callback = args.pop();
f(...args)
.then(res => callback(null, res))
.catch(e => callback(e, null));
};
}
/**
* Evaluates code in the safe way!
*
* @param {String} code
*/
export function safeEval (code) {
'use strict';
try {
'use strict';
eval('\'use strict;\'' + code);
} catch (e) {
console.error('Script evaluation error! \n', e);
}
}
/**
* Returns an human-readable and understandable boolean.
* Brilliant solution for client-server communications.
*
* @param {Boolean} boolean
* @returns {String}
*/
export function makeUnderstandableBoolean (boolean) {
return boolean ? 'yes' : 'no';
}
/**
* Tries to call THE FUNCTION HARDER!1
* @param f
*/
export function tryHard (f) {
while (true) {
try {
return f();
} catch (e) {
console.log('Try harder!');
}
}
}
/**
* Checks is smth enabled or not
* @param {Boolean} isDisabled
* @returns {boolean}
*/
export function isEnabled (isDisabled) {
return isDisabled === false;
}
/**
* Submits your form if you don't know about type="submit"
* @param form
* @param button
*/
export function submitForm (form, button) {
return button.addEventListener('click', (e) => {
e.preventDefault();
form.submit()
});
}
/**
* Parses string as JSON or don't
* @param {String} jsonOrNot
* @credits @subzey
*/
export function parseJSONorNot(jsonOrNot) {
try {
return JSON.parse(jsonOrNot);
} catch (e) {
return jsonOrNot;
}
}
/**
* Chain second argument to promise, if first arg is Promise,
* otherwise pass fisrt argument to second.
* @param {Object} maybePromise
* @param {Function} cb
*/
export function handleIfPromise(maybePromise, cb) {
if (maybePromise instanceof Promise ) {
return maybePromise.then(cb);
}
return cb(maybePromise);
}
/**
* Checks self length to understand is app in prod mode or not
* @returns {boolean}
*/
export function isProduction () {
return isProduction.toString().length < 75;
}
/**
* Helps to debug your code.
* Works only in dev mode.
*/
export function deb () {
if (!isProduction()) {
debugger;
}
}
/**
* Detect availability of ES6 in browser
*/
export function isES6available() {
try {
return eval('(() => true)()');
} catch (e) {
return false;
}
}
/**
* Invoke cb each time when devtools opened
*/
export function onDevtools(cb) {
const test = /./;
test.toString = function() {
cb();
}
console.log('%s', test);
}