-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathutil.js
81 lines (77 loc) · 1.74 KB
/
util.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
// returns a string "type" of input object
export function toType (obj, bigNumber) {
/* Check if the object is an instance of the custom BigNumber class passed in as a prop
* If it matches, return 'bigNumber' type so it can be displayed appropriately
*/
if (bigNumber && obj?.constructor === bigNumber) {
return 'bigNumber'
}
let type = getType(obj)
// some extra disambiguation for numbers
if (type === 'number') {
if (isNaN(obj)) {
type = 'nan'
} else if ((obj | 0) != obj) {
// bitwise OR produces integers
type = 'float'
} else {
type = 'integer'
}
}
return type
}
// source: http://stackoverflow.com/questions/7390426/better-way-to-get-type-of-a-javascript-variable/7390612#7390612
function getType (obj) {
return {}.toString
.call(obj)
.match(/\s([a-zA-Z]+)/)[1]
.toLowerCase()
}
export function escapeString (value) {
return value
.replace(/\\/g, '\\\\')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
.replace(/\r/g, '\\r')
.replace(/\f/g, '\\f')
}
// validation for base-16 themes
export function isTheme (theme) {
const theme_keys = [
'base00',
'base01',
'base02',
'base03',
'base04',
'base05',
'base06',
'base07',
'base08',
'base09',
'base0A',
'base0B',
'base0C',
'base0D',
'base0E',
'base0F'
]
if (toType(theme) === 'object') {
for (let i = 0; i < theme_keys.length; i++) {
if (!(theme_keys[i] in theme)) {
return false
}
}
return true
}
return false
}
export function getBraceChar (object_type, end = false) {
switch (object_type) {
case 'array':
case 'map':
case 'set':
return end ? ']' : '['
default:
return end ? '}' : '{'
}
}