-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvtl-utils.js
More file actions
114 lines (113 loc) · 3.08 KB
/
vtl-utils.js
File metadata and controls
114 lines (113 loc) · 3.08 KB
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
/* eslint-disable @typescript-eslint/no-var-requires */
const lodash_1 = require('lodash')
exports.utils = {
quiet: () => '',
qr: () => '',
urlEncode(value) {
return encodeURIComponent(value).replace(/[!'()]/g, function (c) {
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
})
},
urlDecode(value) {
return decodeURIComponent(value)
},
base64Encode(value) {
return Buffer.from(value).toString('base64')
},
base64Decode(value) {
return Buffer.from(value, 'base64').toString('ascii')
},
parseJson(value) {
return JSON.parse(value)
},
toJson(value) {
return value !== undefined ? JSON.stringify(value) : JSON.stringify(null)
},
getErrors() {
return this.errors
},
isNull(value) {
return value === null || typeof value == 'undefined'
},
isNullOrEmpty(value) {
if (this.isNull(value)) return true
if (value instanceof Object) {
return Object.keys(value.toJSON()).length == 0
}
if (Array.isArray(value)) {
return value.toJSON().length == 0
}
return !!value
},
isNullOrBlank(value) {
return this.isNullOrEmpty(value)
},
defaultIfNull(value, defaultValue = '') {
if (value !== null && value !== undefined) return value
return defaultValue
},
defaultIfNullOrEmpty(value, defaultValue) {
if (value) return value
return defaultValue
},
defaultIfNullOrBlank(value, defaultValue) {
if (value) return value
return defaultValue
},
isString(value) {
return value instanceof String
},
isNumber(value) {
return typeof value === 'number'
},
isBoolean(value) {
return typeof value === 'boolean'
},
isList(value) {
return Array.isArray(value)
},
isMap(value) {
if (value instanceof Map) return value
return value != null && typeof value === 'object'
},
typeOf(value) {
if (value === null) return 'Null'
if (this.isList(value)) return 'List'
if (this.isMap(value)) return 'Map'
switch (typeof value) {
case 'number':
return 'Number'
case 'string':
return 'String'
case 'boolean':
return 'Boolean'
default:
return 'Object'
}
},
matches(pattern, value) {
return new RegExp(pattern).test(value)
},
now: new Date(),
str: {
toUpper(str) {
return str.toUpperCase()
},
toLower(str) {
return str.toLowerCase()
},
toReplace(str, substr, newSubstr) {
return str.replace(new RegExp(substr, 'g'), newSubstr)
},
normalize(str, form) {
return str.normalize(form.toUpperCase())
},
},
math: {
roundNum: Math.round,
minVal: Math.min,
maxVal: Math.max,
randomDouble: Math.random,
randomWithinRange: lodash_1.random,
},
}