@blazedpath/commons 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/blz-base/index.js +895 -893
- package/blz-cache/index.js +14 -25
- package/blz-core/index.js +349 -347
- package/blz-cryptography/index.js +49 -47
- package/blz-datetimes/index.js +350 -348
- package/blz-file/index.js +88 -83
- package/blz-hazelcast/index.js +103 -100
- package/blz-iterable/index.js +406 -404
- package/blz-json-schema/index.js +8 -6
- package/blz-jwt/index.js +92 -89
- package/blz-kafka/index.js +107 -105
- package/blz-math/index.js +129 -127
- package/blz-mongodb/index.js +35 -33
- package/blz-rds/index.js +46 -44
- package/blz-rds-mysql/index.js +23 -21
- package/blz-rds-mysqlx/index.js +22 -20
- package/blz-rds-oracle/index.js +205 -200
- package/blz-rds-postgres/index.js +22 -20
- package/blz-redis/index.js +125 -123
- package/blz-regex/index.js +24 -22
- package/blz-strings/index.js +167 -165
- package/blz-uuid/index.js +4 -2
- package/blz-yaml/index.js +19 -16
- package/package.json +1 -1
package/blz-core/index.js
CHANGED
|
@@ -1,364 +1,366 @@
|
|
|
1
1
|
module.exports = {
|
|
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
|
-
(value1Type === '[object
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return result;
|
|
46
|
-
for (let i = 0; i < value1.length; i++) {
|
|
47
|
-
let item1 = value1[i];
|
|
48
|
-
let item2 = value2[i];
|
|
49
|
-
result = this.compare(item1, item2);
|
|
2
|
+
Dual: {
|
|
3
|
+
errorUndefinedArgument: function (argName) {
|
|
4
|
+
let err = new Error();
|
|
5
|
+
err.code = 'UndefinedArgument';
|
|
6
|
+
err.data = { argName: argName };
|
|
7
|
+
return err;
|
|
8
|
+
},
|
|
9
|
+
errorInvalidConversion: function (value, targetType) {
|
|
10
|
+
let err = new Error();
|
|
11
|
+
err.code = 'InvalidConversion';
|
|
12
|
+
err.data = { value: value, targetType: targetType };
|
|
13
|
+
return err;
|
|
14
|
+
},
|
|
15
|
+
compare: function (value1, value2) { // TODO: Sort require for objects
|
|
16
|
+
if (value1 === null && value2 === null)
|
|
17
|
+
return 0;
|
|
18
|
+
if (value1 === null && value2 !== null)
|
|
19
|
+
return -1;
|
|
20
|
+
if (value1 !== null && value2 === null)
|
|
21
|
+
return 1;
|
|
22
|
+
let value1Type = toString.call(value1);
|
|
23
|
+
let value2Type = toString.call(value2);
|
|
24
|
+
let value1ToCompare = null;
|
|
25
|
+
let value2ToCompare = null;
|
|
26
|
+
if ((value1Type === '[object String]' && value2Type === '[object String]') ||
|
|
27
|
+
(value1Type === '[object Number]' && value2Type === '[object Number]')) {
|
|
28
|
+
value1ToCompare = value1;
|
|
29
|
+
value2ToCompare = value2;
|
|
30
|
+
}
|
|
31
|
+
else if (value1Type === '[object Boolean]' && value2Type === '[object Boolean]') {
|
|
32
|
+
value1ToCompare = value1 ? 1 : 0;
|
|
33
|
+
value2ToCompare = value2 ? 1 : 0;
|
|
34
|
+
}
|
|
35
|
+
else if (value1Type === '[object Date]' && value2Type === '[object Date]') {
|
|
36
|
+
value1ToCompare = value1.getTime();
|
|
37
|
+
value2ToCompare = value2.getTime();
|
|
38
|
+
}
|
|
39
|
+
else if (value1Type === '[object Object]' && value2Type === '[object Object]') {
|
|
40
|
+
value1ToCompare = JSON.stringify(value1);
|
|
41
|
+
value2ToCompare = JSON.stringify(value2);
|
|
42
|
+
}
|
|
43
|
+
else if (value1Type === '[object Array]' && value2Type === '[object Array]') {
|
|
44
|
+
let result = this.compare(value1.length, value2.length);
|
|
50
45
|
if (result !== 0)
|
|
51
46
|
return result;
|
|
47
|
+
for (let i = 0; i < value1.length; i++) {
|
|
48
|
+
let item1 = value1[i];
|
|
49
|
+
let item2 = value2[i];
|
|
50
|
+
result = this.compare(item1, item2);
|
|
51
|
+
if (result !== 0)
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
return 0;
|
|
52
55
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
56
|
+
else {
|
|
57
|
+
if (value1Type === '[object String]')
|
|
58
|
+
value1ToCompare = value1;
|
|
59
|
+
else if (value1Type === '[object Number]')
|
|
60
|
+
value1ToCompare = value1.toString();
|
|
61
|
+
else if (value1Type === '[object Boolean]')
|
|
62
|
+
value1ToCompare = value1 ? '1' : '0';
|
|
63
|
+
else if (value1Type === '[object Date]')
|
|
64
|
+
value1ToCompare = value1.toJSON();
|
|
65
|
+
else
|
|
66
|
+
value1ToCompare = value1.toString();
|
|
67
|
+
if (value2Type === '[object String]')
|
|
68
|
+
value2ToCompare = value2;
|
|
69
|
+
else if (value2Type === '[object Number]')
|
|
70
|
+
value2ToCompare = value2.toString();
|
|
71
|
+
else if (value2Type === '[object Boolean]')
|
|
72
|
+
value2ToCompare = value2 ? '1' : '0';
|
|
73
|
+
else if (value2Type === '[object Date]')
|
|
74
|
+
value2ToCompare = value2.toJSON();
|
|
75
|
+
else
|
|
76
|
+
value2ToCompare = value2.toString();
|
|
77
|
+
}
|
|
78
|
+
if (value1ToCompare === value2ToCompare)
|
|
79
|
+
return 0;
|
|
80
|
+
else if (value1ToCompare < value2ToCompare)
|
|
81
|
+
return -1;
|
|
82
|
+
else if (value1ToCompare > value2ToCompare)
|
|
83
|
+
return 1;
|
|
74
84
|
else
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return null;
|
|
85
|
-
},
|
|
86
|
-
convertMilliseconds: function (strMilliseconds) {
|
|
87
|
-
if (strMilliseconds) {
|
|
88
|
-
if (strMilliseconds.length < 3)
|
|
89
|
-
strMilliseconds = strMilliseconds.padEnd(3, '0');
|
|
90
|
-
if (strMilliseconds.length > 3)
|
|
91
|
-
strMilliseconds = strMilliseconds.substr(0, 3);
|
|
92
|
-
return Number(strMilliseconds);
|
|
93
|
-
}
|
|
94
|
-
else
|
|
95
|
-
return 0;
|
|
96
|
-
},
|
|
97
|
-
apply: function (obj1, obj2) {
|
|
98
|
-
let result = null;
|
|
99
|
-
if (obj1) {
|
|
100
|
-
result = {};
|
|
101
|
-
for (let key in obj1) {
|
|
102
|
-
result[key] = obj1[key];
|
|
85
|
+
return null;
|
|
86
|
+
},
|
|
87
|
+
convertMilliseconds: function (strMilliseconds) {
|
|
88
|
+
if (strMilliseconds) {
|
|
89
|
+
if (strMilliseconds.length < 3)
|
|
90
|
+
strMilliseconds = strMilliseconds.padEnd(3, '0');
|
|
91
|
+
if (strMilliseconds.length > 3)
|
|
92
|
+
strMilliseconds = strMilliseconds.substr(0, 3);
|
|
93
|
+
return Number(strMilliseconds);
|
|
103
94
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
95
|
+
else
|
|
96
|
+
return 0;
|
|
97
|
+
},
|
|
98
|
+
apply: function (obj1, obj2) {
|
|
99
|
+
let result = null;
|
|
100
|
+
if (obj1) {
|
|
101
|
+
result = {};
|
|
102
|
+
for (let key in obj1) {
|
|
103
|
+
result[key] = obj1[key];
|
|
104
|
+
}
|
|
105
|
+
if (obj2) {
|
|
106
|
+
for (let key in obj2) {
|
|
107
|
+
result[key] = obj2[key];
|
|
108
|
+
}
|
|
107
109
|
}
|
|
108
110
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return
|
|
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
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return new Date(Date.UTC(Number(matchDatetimeZone[1]), Number(matchDatetimeZone[2]) - 1, Number(matchDatetimeZone[3]), 0, 0, 0, 0));
|
|
171
|
-
}
|
|
172
|
-
if (valueType === '[object Date]') {
|
|
173
|
-
return new Date(Date.UTC(value.getUTCFullYear(), value.getUTCMonth(), value.getUTCDate(), 0, 0, 0, 0));
|
|
174
|
-
}
|
|
175
|
-
throw this.errorInvalidConversion(value, 'date');
|
|
176
|
-
},
|
|
177
|
-
convertToDatetime: function (value) {
|
|
178
|
-
if (value === null)
|
|
179
|
-
return null;
|
|
180
|
-
let valueType = toString.call(value);
|
|
181
|
-
if (valueType === '[object String]') {
|
|
182
|
-
let matchDatetimeUtc = /^(\d{4})-(\d{1,2})-(\d{1,2})[T,\s](\d{1,2})\:(\d{1,2})\:(\d{1,2})\.?(\d+)?Z?$/.exec(value);
|
|
183
|
-
if (matchDatetimeUtc && matchDatetimeUtc[7] !== undefined) {
|
|
184
|
-
return new Date(Date.UTC(Number(matchDatetimeUtc[1]), Number(matchDatetimeUtc[2]) - 1, Number(matchDatetimeUtc[3]), Number(matchDatetimeUtc[4]), Number(matchDatetimeUtc[5]), Number(matchDatetimeUtc[6] || 0), this.convertMilliseconds(matchDatetimeUtc[7])));
|
|
111
|
+
return result;
|
|
112
|
+
},
|
|
113
|
+
base64Decode: function (value) {
|
|
114
|
+
if (value === undefined)
|
|
115
|
+
throw this.errorUndefinedArgument('value');
|
|
116
|
+
if (value === null)
|
|
117
|
+
return null;
|
|
118
|
+
return Buffer.from(value, 'base64');
|
|
119
|
+
},
|
|
120
|
+
base64Encode: function (value) {
|
|
121
|
+
if (value === undefined)
|
|
122
|
+
throw this.errorUndefinedArgument('value');
|
|
123
|
+
if (value === null)
|
|
124
|
+
return null;
|
|
125
|
+
return Buffer.from(value).toString('base64');
|
|
126
|
+
},
|
|
127
|
+
convertToBinary: function (value) {
|
|
128
|
+
if (value == null)
|
|
129
|
+
return null;
|
|
130
|
+
if (value && value.type === 'Buffer')
|
|
131
|
+
return Buffer.from(value);
|
|
132
|
+
if (Buffer.isBuffer(value))
|
|
133
|
+
return value;
|
|
134
|
+
if (Array.isArray(value) || typeof value === 'string')
|
|
135
|
+
return Buffer.from(value);
|
|
136
|
+
throw this.errorInvalidConversion(value, 'binary');
|
|
137
|
+
},
|
|
138
|
+
convertToBoolean: function (value) {
|
|
139
|
+
if (value === null)
|
|
140
|
+
return null;
|
|
141
|
+
let valueType = toString.call(value);
|
|
142
|
+
if (valueType === '[object String]') {
|
|
143
|
+
if (value === '1' || value.toUpperCase() === 'T' || value.toUpperCase() === 'TRUE' || value.toUpperCase() === 'Y' || value.toUpperCase() === 'YES')
|
|
144
|
+
return true;
|
|
145
|
+
if (value === '0' || value.toUpperCase() === 'F' || value.toUpperCase() === 'FALSE' || value.toUpperCase() === 'N' || value.toUpperCase() === 'NO')
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
if (valueType === '[object Number]') {
|
|
149
|
+
if (value === 1)
|
|
150
|
+
return true;
|
|
151
|
+
if (value === 0)
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
if (valueType === '[object Boolean]')
|
|
155
|
+
return value;
|
|
156
|
+
throw this.errorInvalidConversion(value, 'boolean');
|
|
157
|
+
},
|
|
158
|
+
convertToDate: function (value) {
|
|
159
|
+
if (value === null)
|
|
160
|
+
return null;
|
|
161
|
+
let valueType = toString.call(value);
|
|
162
|
+
if (valueType === '[object String]') {
|
|
163
|
+
let matchDate = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(value);
|
|
164
|
+
if (matchDate)
|
|
165
|
+
return new Date(Date.UTC(Number(matchDate[1]), Number(matchDate[2]) - 1, Number(matchDate[3]), 0, 0, 0, 0));
|
|
166
|
+
let matchDatetimeUtc = /^(\d{4})-(\d{1,2})-(\d{1,2})[T,\s](\d{1,2})\:(\d{1,2})\:(\d{1,2})\.?(\d+)?Z?$/.exec(value);
|
|
167
|
+
if (matchDatetimeUtc)
|
|
168
|
+
return new Date(Date.UTC(Number(matchDatetimeUtc[1]), Number(matchDatetimeUtc[2]) - 1, Number(matchDatetimeUtc[3]), 0, 0, 0, 0));
|
|
169
|
+
let matchDatetimeZone = /^(\d{4})-(\d{1,2})-(\d{1,2})[T\s](\d{1,2}):(\d{1,2}):(\d{1,2})(?:\.(\d+))?([+-]\d{2}:\d{2})?$/.exec(value);
|
|
170
|
+
if (matchDatetimeZone) // timezone
|
|
171
|
+
return new Date(Date.UTC(Number(matchDatetimeZone[1]), Number(matchDatetimeZone[2]) - 1, Number(matchDatetimeZone[3]), 0, 0, 0, 0));
|
|
185
172
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
173
|
+
if (valueType === '[object Date]') {
|
|
174
|
+
return new Date(Date.UTC(value.getUTCFullYear(), value.getUTCMonth(), value.getUTCDate(), 0, 0, 0, 0));
|
|
175
|
+
}
|
|
176
|
+
throw this.errorInvalidConversion(value, 'date');
|
|
177
|
+
},
|
|
178
|
+
convertToDatetime: function (value) {
|
|
179
|
+
if (value === null)
|
|
180
|
+
return null;
|
|
181
|
+
let valueType = toString.call(value);
|
|
182
|
+
if (valueType === '[object String]') {
|
|
183
|
+
let matchDatetimeUtc = /^(\d{4})-(\d{1,2})-(\d{1,2})[T,\s](\d{1,2})\:(\d{1,2})\:(\d{1,2})\.?(\d+)?Z?$/.exec(value);
|
|
184
|
+
if (matchDatetimeUtc && matchDatetimeUtc[7] !== undefined) {
|
|
185
|
+
return new Date(Date.UTC(Number(matchDatetimeUtc[1]), Number(matchDatetimeUtc[2]) - 1, Number(matchDatetimeUtc[3]), Number(matchDatetimeUtc[4]), Number(matchDatetimeUtc[5]), Number(matchDatetimeUtc[6] || 0), this.convertMilliseconds(matchDatetimeUtc[7])));
|
|
186
|
+
}
|
|
187
|
+
let matchDatetimeZone = /^(\d{4})-(\d{1,2})-(\d{1,2})[T\s](\d{1,2}):(\d{1,2}):(\d{1,2})(?:\.(\d+))?([+-]\d{2}:\d{2})?$/.exec(value);
|
|
188
|
+
if (matchDatetimeZone) { // timezone
|
|
189
|
+
let year = Number(matchDatetimeZone[1]);
|
|
190
|
+
let month = Number(matchDatetimeZone[2]) - 1; // Month is zero-based in JavaScript Date
|
|
191
|
+
let day = Number(matchDatetimeZone[3]);
|
|
192
|
+
let hour = Number(matchDatetimeZone[4]);
|
|
193
|
+
let minute = Number(matchDatetimeZone[5]);
|
|
194
|
+
let second = Number(matchDatetimeZone[6] || 0);
|
|
195
|
+
let millisecond = this.convertMilliseconds(matchDatetimeZone[7]);
|
|
196
|
+
let timezoneOffset = matchDatetimeZone[8];
|
|
197
|
+
if (timezoneOffset) {
|
|
198
|
+
let offsetSign = timezoneOffset[0] === "+" ? 1 : -1;
|
|
199
|
+
let [offsetHour, offsetMinute] = timezoneOffset.slice(1).split(":").map(Number);
|
|
200
|
+
hour -= offsetSign * offsetHour;
|
|
201
|
+
minute -= offsetSign * offsetMinute;
|
|
202
|
+
}
|
|
203
|
+
return new Date(Date.UTC(year, month, day, hour, minute, second, millisecond));
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
const probableDate = new Date(value)
|
|
207
|
+
if (probableDate.toString() !== 'Invalid Date') {
|
|
208
|
+
return probableDate
|
|
209
|
+
}
|
|
201
210
|
}
|
|
202
|
-
return new Date(Date.UTC(year, month, day, hour, minute, second, millisecond));
|
|
203
211
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
212
|
+
if (valueType === '[object Date]') {
|
|
213
|
+
return value;
|
|
214
|
+
}
|
|
215
|
+
throw this.errorInvalidConversion(value, 'datetime');
|
|
216
|
+
},
|
|
217
|
+
convertToDecimal: function (value) {
|
|
218
|
+
if (value === null)
|
|
219
|
+
return null;
|
|
220
|
+
let valueType = toString.call(value);
|
|
221
|
+
if (valueType === '[object String]' && value !== '' && !isNaN(value))
|
|
222
|
+
return Number(value);
|
|
223
|
+
if (valueType === '[object Number]')
|
|
224
|
+
return value;
|
|
225
|
+
if (valueType === '[object Boolean]')
|
|
226
|
+
return value ? 1 : 0;
|
|
227
|
+
throw this.errorInvalidConversion(value, 'decimal');
|
|
228
|
+
},
|
|
229
|
+
convertToInteger: function (value) {
|
|
230
|
+
if (value === null)
|
|
231
|
+
return null;
|
|
232
|
+
let valueType = toString.call(value);
|
|
233
|
+
if (valueType === '[object String]' && value !== '' && !isNaN(value))
|
|
234
|
+
return Math.round(Number(value));
|
|
235
|
+
if (valueType === '[object Number]')
|
|
236
|
+
return Math.round(value);
|
|
237
|
+
if (valueType === '[object Boolean]')
|
|
238
|
+
return value ? 1 : 0;
|
|
239
|
+
throw this.errorInvalidConversion(value, 'integer');
|
|
240
|
+
},
|
|
241
|
+
convertToString: function (value) {
|
|
242
|
+
if (value === null)
|
|
243
|
+
return null;
|
|
244
|
+
let valueType = toString.call(value);
|
|
245
|
+
if (valueType === '[object String]')
|
|
246
|
+
return value;
|
|
247
|
+
if (valueType === '[object Number]')
|
|
248
|
+
return value.toString();
|
|
249
|
+
if (valueType === '[object Uint8Array]')
|
|
250
|
+
return value.toString();
|
|
251
|
+
if (valueType === '[object Boolean]')
|
|
252
|
+
return value ? 'true' : 'false';
|
|
253
|
+
if (valueType === '[object Date]')
|
|
254
|
+
return value.toJSON();
|
|
255
|
+
if (valueType === '[object Object]' && value.type === 'Buffer' && value.data)
|
|
256
|
+
return Buffer.from(value.data).toString();
|
|
257
|
+
throw this.errorInvalidConversion(value, 'string');
|
|
258
|
+
},
|
|
259
|
+
convertToTime: function (value) {
|
|
260
|
+
if (value === null)
|
|
261
|
+
return null;
|
|
262
|
+
let valueType = toString.call(value);
|
|
263
|
+
if (valueType === '[object String]') {
|
|
264
|
+
let matchTime = /^(\d{1,2})\:(\d{1,2})\:(\d{1,2})\.?(\d+)?$/.exec(value);
|
|
265
|
+
if (matchTime)
|
|
266
|
+
return new Date(Date.UTC(1970, 0, 1, Number(matchTime[1]), Number(matchTime[2]), Number(matchTime[3]), this.convertMilliseconds(matchTime[4])));
|
|
267
|
+
let matchDatetime = /^(\d{4})-(\d{1,2})-(\d{1,2})[T,\s](\d{1,2})\:(\d{1,2})\:(\d{1,2})\.?(\d+)?Z?$/.exec(value);
|
|
268
|
+
if (matchDatetime)
|
|
269
|
+
return new Date(Date.UTC(1970, 0, 1, Number(matchDatetime[4]), Number(matchDatetime[5]), Number(matchDatetime[6] || 0), this.convertMilliseconds(matchDatetime[7])));
|
|
270
|
+
}
|
|
271
|
+
if (valueType === '[object Date]') {
|
|
272
|
+
return new Date(Date.UTC(1970, 0, 1, value.getUTCHours(), value.getUTCMinutes(), value.getUTCSeconds(), value.getUTCMilliseconds()));
|
|
273
|
+
}
|
|
274
|
+
throw this.errorInvalidConversion(value, 'time');
|
|
275
|
+
},
|
|
276
|
+
entries: function (obj) {
|
|
277
|
+
if (obj === undefined) throw this.errorUndefinedArgument('obj');
|
|
278
|
+
if (obj === null) return null
|
|
279
|
+
return Object.entries(obj)
|
|
280
|
+
},
|
|
281
|
+
format: function (value, format) {
|
|
282
|
+
if (typeof Blz !== 'undefined' && Blz.format) {
|
|
283
|
+
return Blz.format(value, format);
|
|
284
|
+
}
|
|
285
|
+
if (value != null && value.toString) {
|
|
286
|
+
return value.toString();
|
|
287
|
+
}
|
|
288
|
+
return '';
|
|
289
|
+
},
|
|
290
|
+
htmlEncode: function (value) {
|
|
291
|
+
if (value)
|
|
292
|
+
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/'/g, ''').replace(/"/g, '"');
|
|
293
|
+
else
|
|
294
|
+
return value;
|
|
295
|
+
},
|
|
296
|
+
htmlDecode: function (value) {
|
|
297
|
+
if (value)
|
|
298
|
+
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/'/g, '\'').replace(/"/g, '"');
|
|
299
|
+
else
|
|
300
|
+
return value;
|
|
301
|
+
},
|
|
302
|
+
if: function (condition, valueIfConditionIsTrue, valueIfConditionIsFalse) {
|
|
303
|
+
return condition ? valueIfConditionIsTrue : valueIfConditionIsFalse
|
|
304
|
+
},
|
|
305
|
+
isNotNull: function (value) {
|
|
306
|
+
return value !== null && value !== undefined;
|
|
307
|
+
},
|
|
308
|
+
isNull: function (value) {
|
|
309
|
+
return value === null || value === undefined;
|
|
310
|
+
},
|
|
311
|
+
isNullOrValue: function (value, valueToCompare) {
|
|
312
|
+
return value === null || value === undefined || this.compare(value, valueToCompare) === 0;
|
|
313
|
+
},
|
|
314
|
+
jsonParse: function (value) {
|
|
315
|
+
if (value === undefined)
|
|
316
|
+
throw this.errorUndefinedArgument('value');
|
|
317
|
+
if (value === null)
|
|
318
|
+
return null;
|
|
319
|
+
let obj = JSON.parse(value);
|
|
320
|
+
return obj;
|
|
321
|
+
},
|
|
322
|
+
jsonStringify: function (value, spaces) {
|
|
323
|
+
if (value === undefined)
|
|
324
|
+
throw this.errorUndefinedArgument('value');
|
|
325
|
+
if (value === null)
|
|
326
|
+
return null;
|
|
327
|
+
if (spaces === undefined || spaces === null)
|
|
328
|
+
return JSON.stringify(value);
|
|
329
|
+
else
|
|
330
|
+
return JSON.stringify(value, null, spaces);
|
|
331
|
+
},
|
|
332
|
+
keys: function (obj) {
|
|
333
|
+
if (obj === undefined) throw this.errorUndefinedArgument('obj');
|
|
334
|
+
if (obj === null) return null
|
|
335
|
+
return Object.keys(obj)
|
|
336
|
+
},
|
|
337
|
+
map: function (source, fnSelection) {
|
|
338
|
+
if (Array.isArray(source)) { // list
|
|
339
|
+
const resultArray = new Array(source.length);
|
|
340
|
+
for (let i = 0; i < source.length; ++i) {
|
|
341
|
+
const item = source[i]
|
|
342
|
+
resultArray[i] = fnSelection(item)
|
|
208
343
|
}
|
|
344
|
+
return resultArray
|
|
209
345
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
return value;
|
|
213
|
-
}
|
|
214
|
-
throw this.errorInvalidConversion(value, 'datetime');
|
|
215
|
-
},
|
|
216
|
-
convertToDecimal: function (value) {
|
|
217
|
-
if (value === null)
|
|
218
|
-
return null;
|
|
219
|
-
let valueType = toString.call(value);
|
|
220
|
-
if (valueType === '[object String]' && value !== '' && !isNaN(value))
|
|
221
|
-
return Number(value);
|
|
222
|
-
if (valueType === '[object Number]')
|
|
223
|
-
return value;
|
|
224
|
-
if (valueType === '[object Boolean]')
|
|
225
|
-
return value ? 1 : 0;
|
|
226
|
-
throw this.errorInvalidConversion(value, 'decimal');
|
|
227
|
-
},
|
|
228
|
-
convertToInteger: function (value) {
|
|
229
|
-
if (value === null)
|
|
230
|
-
return null;
|
|
231
|
-
let valueType = toString.call(value);
|
|
232
|
-
if (valueType === '[object String]' && value !== '' && !isNaN(value))
|
|
233
|
-
return Math.round(Number(value));
|
|
234
|
-
if (valueType === '[object Number]')
|
|
235
|
-
return Math.round(value);
|
|
236
|
-
if (valueType === '[object Boolean]')
|
|
237
|
-
return value ? 1 : 0;
|
|
238
|
-
throw this.errorInvalidConversion(value, 'integer');
|
|
239
|
-
},
|
|
240
|
-
convertToString: function (value) {
|
|
241
|
-
if (value === null)
|
|
242
|
-
return null;
|
|
243
|
-
let valueType = toString.call(value);
|
|
244
|
-
if (valueType === '[object String]')
|
|
245
|
-
return value;
|
|
246
|
-
if (valueType === '[object Number]')
|
|
247
|
-
return value.toString();
|
|
248
|
-
if (valueType === '[object Uint8Array]')
|
|
249
|
-
return value.toString();
|
|
250
|
-
if (valueType === '[object Boolean]')
|
|
251
|
-
return value ? 'true' : 'false';
|
|
252
|
-
if (valueType === '[object Date]')
|
|
253
|
-
return value.toJSON();
|
|
254
|
-
if (valueType === '[object Object]' && value.type === 'Buffer' && value.data)
|
|
255
|
-
return Buffer.from(value.data).toString();
|
|
256
|
-
throw this.errorInvalidConversion(value, 'string');
|
|
257
|
-
},
|
|
258
|
-
convertToTime: function (value) {
|
|
259
|
-
if (value === null)
|
|
260
|
-
return null;
|
|
261
|
-
let valueType = toString.call(value);
|
|
262
|
-
if (valueType === '[object String]') {
|
|
263
|
-
let matchTime = /^(\d{1,2})\:(\d{1,2})\:(\d{1,2})\.?(\d+)?$/.exec(value);
|
|
264
|
-
if (matchTime)
|
|
265
|
-
return new Date(Date.UTC(1970, 0, 1, Number(matchTime[1]), Number(matchTime[2]), Number(matchTime[3]), this.convertMilliseconds(matchTime[4])));
|
|
266
|
-
let matchDatetime = /^(\d{4})-(\d{1,2})-(\d{1,2})[T,\s](\d{1,2})\:(\d{1,2})\:(\d{1,2})\.?(\d+)?Z?$/.exec(value);
|
|
267
|
-
if (matchDatetime)
|
|
268
|
-
return new Date(Date.UTC(1970, 0, 1, Number(matchDatetime[4]), Number(matchDatetime[5]), Number(matchDatetime[6] || 0), this.convertMilliseconds(matchDatetime[7])));
|
|
269
|
-
}
|
|
270
|
-
if (valueType === '[object Date]') {
|
|
271
|
-
return new Date(Date.UTC(1970, 0, 1, value.getUTCHours(), value.getUTCMinutes(), value.getUTCSeconds(), value.getUTCMilliseconds()));
|
|
272
|
-
}
|
|
273
|
-
throw this.errorInvalidConversion(value, 'time');
|
|
274
|
-
},
|
|
275
|
-
entries: function( obj ) {
|
|
276
|
-
if( obj === undefined ) throw this.errorUndefinedArgument( 'obj' );
|
|
277
|
-
if( obj === null ) return null
|
|
278
|
-
return Object.entries( obj )
|
|
279
|
-
},
|
|
280
|
-
format: function (value, format) {
|
|
281
|
-
if (typeof Blz !== 'undefined' && Blz.format) {
|
|
282
|
-
return Blz.format(value, format);
|
|
283
|
-
}
|
|
284
|
-
if (value != null && value.toString) {
|
|
285
|
-
return value.toString();
|
|
286
|
-
}
|
|
287
|
-
return '';
|
|
288
|
-
},
|
|
289
|
-
htmlEncode: function (value) {
|
|
290
|
-
if (value)
|
|
291
|
-
return value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/'/g,''').replace(/"/g,'"');
|
|
292
|
-
else
|
|
293
|
-
return value;
|
|
294
|
-
},
|
|
295
|
-
htmlDecode: function (value) {
|
|
296
|
-
if (value)
|
|
297
|
-
return value.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/'/g,'\'').replace(/"/g,'"');
|
|
298
|
-
else
|
|
299
|
-
return value;
|
|
300
|
-
},
|
|
301
|
-
if: function( condition, valueIfConditionIsTrue, valueIfConditionIsFalse ) {
|
|
302
|
-
return condition ? valueIfConditionIsTrue : valueIfConditionIsFalse
|
|
303
|
-
},
|
|
304
|
-
isNotNull: function (value) {
|
|
305
|
-
return value !== null && value !== undefined;
|
|
306
|
-
},
|
|
307
|
-
isNull: function (value) {
|
|
308
|
-
return value === null || value === undefined;
|
|
309
|
-
},
|
|
310
|
-
isNullOrValue: function (value, valueToCompare) {
|
|
311
|
-
return value === null || value === undefined || this.compare(value, valueToCompare) === 0;
|
|
312
|
-
},
|
|
313
|
-
jsonParse: function (value) {
|
|
314
|
-
if (value === undefined)
|
|
315
|
-
throw this.errorUndefinedArgument('value');
|
|
316
|
-
if (value === null)
|
|
317
|
-
return null;
|
|
318
|
-
let obj = JSON.parse(value);
|
|
319
|
-
return obj;
|
|
320
|
-
},
|
|
321
|
-
jsonStringify: function (value, spaces) {
|
|
322
|
-
if (value === undefined)
|
|
323
|
-
throw this.errorUndefinedArgument('value');
|
|
324
|
-
if (value === null)
|
|
325
|
-
return null;
|
|
326
|
-
if (spaces === undefined || spaces === null)
|
|
327
|
-
return JSON.stringify(value);
|
|
328
|
-
else
|
|
329
|
-
return JSON.stringify(value, null, spaces);
|
|
330
|
-
},
|
|
331
|
-
keys: function( obj ) {
|
|
332
|
-
if( obj === undefined ) throw this.errorUndefinedArgument( 'obj' );
|
|
333
|
-
if( obj === null ) return null
|
|
334
|
-
return Object.keys( obj )
|
|
335
|
-
},
|
|
336
|
-
map: function( source, fnSelection ) {
|
|
337
|
-
if( Array.isArray( source ) ) { // list
|
|
338
|
-
const resultArray = new Array( source.length );
|
|
339
|
-
for( let i = 0; i < source.length; ++i ) {
|
|
340
|
-
const item = source[ i ]
|
|
341
|
-
resultArray[ i ] = fnSelection( item )
|
|
346
|
+
else if (source) { // obj
|
|
347
|
+
return fnSelection(source);
|
|
342
348
|
}
|
|
343
|
-
return
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
return
|
|
349
|
+
return null;
|
|
350
|
+
},
|
|
351
|
+
nvl: function (value, replacementValue) {
|
|
352
|
+
return value !== null && value !== undefined ? value : replacementValue;
|
|
353
|
+
},
|
|
354
|
+
urlEncode: function (value) {
|
|
355
|
+
return encodeURIComponent(value);
|
|
356
|
+
},
|
|
357
|
+
urlDecode: function (value) {
|
|
358
|
+
return decodeURIComponent(value);
|
|
359
|
+
},
|
|
360
|
+
values: function (obj) {
|
|
361
|
+
if (obj === undefined) throw this.errorUndefinedArgument('obj');
|
|
362
|
+
if (obj === null) return null
|
|
363
|
+
return Object.values(obj)
|
|
347
364
|
}
|
|
348
|
-
return null;
|
|
349
|
-
},
|
|
350
|
-
nvl: function (value, replacementValue) {
|
|
351
|
-
return value !== null && value !== undefined ? value : replacementValue;
|
|
352
|
-
},
|
|
353
|
-
urlEncode: function (value) {
|
|
354
|
-
return encodeURIComponent(value);
|
|
355
|
-
},
|
|
356
|
-
urlDecode: function (value) {
|
|
357
|
-
return decodeURIComponent(value);
|
|
358
|
-
},
|
|
359
|
-
values: function( obj ) {
|
|
360
|
-
if( obj === undefined ) throw this.errorUndefinedArgument( 'obj' );
|
|
361
|
-
if( obj === null ) return null
|
|
362
|
-
return Object.values( obj )
|
|
363
365
|
}
|
|
364
366
|
};
|