-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.js
41 lines (38 loc) · 1.17 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
'use strict';
/* eslint-disable complexity, max-statements */
/**
* Returns value info in form 'value (type)'
*
* @param {*} v value
* @param {String} tv type of value
* @returns {*}
*/
exports.valueInfo = function (v, tv) {
if (tv === 'null' || tv === 'undefined' || tv === 'NaN' || tv === 'Infinity') return tv;
var s = '';
try { s = JSON.stringify(v); } catch(e) { } // eslint-disable-line no-empty
try { s = s || String(v); } catch(e) { } // eslint-disable-line no-empty
s = s.length > 20 ? s.substr(0, 20) + '...' : s;
return s + ' (' + tv + ')';
};
/**
* Returns type of value
*
* @param {*} v
* @param {Number} ct - customTypes: 1 (allow), 0 (deny)
* @returns {String}
*/
exports.typeInfo = function (v, ct) {
if (v === null || v === undefined) return String(v);
if (v !== v) return 'NaN';
var tv = typeof v;
if (tv === 'number' && !isFinite(v)) return 'Infinity';
if (tv !== 'object') return tv;
var c = v.constructor && v.constructor.name;
if (!c) return tv;
if (c === 'Object') c = 'object';
if (ct) return c;
var g = global || window;
var isNative = g && /\{\s*\[native code\]\s*\}/.test(String(g[c]));
return isNative ? c : tv;
};