Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
459 lines (431 loc) · 15.9 KB
/
Copy pathutils.js
File metadata and controls
459 lines (431 loc) · 15.9 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict'
/* eslint no-console: "off" */
/* eslint no-undef: "off" */
/* eslint no-unused-vars: "off" */
/* eslint callback-return: "off" */
/* eslint no-process-env: "off" */
/* eslint no-eval: "off" */
// define a general object, and assign functions to it ...
// const utils = {}
// some generic utility functions
module.exports.clearConsole = function () {
if (console.clear) { console.clear() }
}
module.exports.normalizeData = function (data, asArray) {
// console.debug('normalizeData' + ': data is not null: ' + (data != null))
if (data != null) { return data } else {
if (data instanceof Array || asArray) { return [] } else { return {} }
}
}
module.exports.getOrElse = function (obj, def) {
if (typeof obj !== 'undefined' && obj !== null) { return Object.create(obj) } else { return def }
}
module.exports.getType = function (obj) {
return typeof obj
}
module.exports.getTypeFromConstructor = function (obj) {
if (typeof obj !== 'undefined' && obj !== null) { return obj.constructor.name } else { return null }
}
module.exports.getTypeFromPrototype = function (obj) {
return Object.prototype.toString.call(obj).slice(8, -1)
}
module.exports.has = function (obj, key) {
return key in obj
}
module.exports.hasLocalOrInPrototype = function (obj, key, searchInPrototype) {
if (!searchInPrototype) { return Object.prototype.hasOwnProperty.call(obj, key) } else {
return key in obj
}
}
module.exports.isDefined = function (o) {
return (typeof o !== 'undefined')
}
module.exports.isUndefined = function (o) {
return (typeof o === 'undefined')
}
module.exports.isNull = function (o) {
return (o === null)
// return (o !== undefined && o === null)
}
module.exports.isNotNull = function (o) {
return (o !== null)
}
module.exports.isDefinedAndNotNull = function (o) {
return (o !== undefined && o !== null)
}
module.exports.isUndefinedOrNull = function (o) {
return (o === undefined || o === null)
}
module.exports.isUndefinedOrNullArrayItem = function (a) {
if (this.isUndefinedOrNull(a) && !this.isArray(a)) {
return true
} else {
for (const item of a) {
if (item === null) { return true }
}
return false
}
}
module.exports.isFunction = function (f) {
// return (f !== undefined && typeof f === 'function')
return (typeof f === 'function')
}
module.exports.isArray = function (o) {
// return o instanceof Array
return (Array.isArray(o))
}
module.exports.isBoolean = function (o) {
return (typeof o === 'boolean')
}
module.exports.isNumber = function (o) {
return (typeof o === 'number')
}
module.exports.isDate = function (o) {
return (typeof o === 'object' || o instanceof Date)
// return (o instanceof Date)
// return (this.getTypeFromPrototype(o) === 'Date')
// return (this.getTypeFromConstructor(o) === 'Date')
}
module.exports.isValidDate = function (d) {
return (this.isDate(d) && !isNaN(d))
}
module.exports.isValidDateFromString = function (str) {
return (this.createDateFromString(str) != null)
}
module.exports.isString = function (o) {
// return (typeof(o) === 'string' || o instanceof String)
return (typeof (o) === 'string')
}
module.exports.isNullOrEmpty = function (o) {
return (o == null || (this.isString(o) && o.length === 0) || (this.isArray(o) && o.length === 0))
}
module.exports.isRegExp = function (o) {
return (typeof o === 'object' && o instanceof RegExp)
// return (o && typeof o === 'object' && o.constructor === RegExp)
}
module.exports.isMap = function (o) { // ES6 Maps
return (o instanceof Map || o instanceof WeakMap)
}
module.exports.isSet = function (o) { // ES6 Sets
return (o instanceof Set || o instanceof WeakSet)
}
module.exports.isSymbol = function (o) { // ES6 Symbols
return (typeof o === 'symbol')
}
module.exports.isObject = function (o) {
return (typeof o === 'object')
// return (o && typeof o === 'object' && o.constructor === Object)
}
module.isError = function (o) {
return (o instanceof Error && typeof o.message !== 'undefined')
}
module.exports.objectOwnPropertiesNames = function (obj) {
// return all own properties names of the given object, as a list (array)
return Object.keys(obj)
}
module.exports.objectOwnPropertiesList = function (obj) {
// return all own properties of the given object, as a list (array)
const values = []
for (const prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
// console.debug('obj.' + prop + ' = ' + obj[prop])
values.push(prop)
}
}
return values
}
module.exports.isArrayEmpty = function (obj) {
if (this.isUndefinedOrNull(obj)) { return true }
return obj.length === 0
}
module.exports.isStringEmpty = function (obj) {
if (this.isUndefinedOrNull(obj)) { return true }
return obj.length === 0
}
module.exports.isStringTrimmedEmpty = function (obj) {
if (this.isUndefinedOrNull(obj)) { return true }
return obj.trim().length === 0
}
module.exports.isEmpty = function (obj) {
if (this.isUndefinedOrNull(obj)) { return true }
if (this.isArray(obj) || this.isString(obj)) { return obj.length === 0 }
if (this.isMap(obj) || this.isSet(obj)) { return obj.size === 0 }
if (this.isObject(obj)) { return this.isObjectEmpty(obj) }
}
module.exports.isStringFalse = function (obj) {
return (this.isString(obj) && ['false', 'f', 'no', 'n', '0'].indexOf(obj.toLowerCase()) > -1)
}
module.exports.isStringTrue = function (obj) {
return (this.isString(obj) && ['true', 't', 'yes', 'y', '1'].indexOf(obj.toLowerCase()) > -1)
}
module.exports.inherit = function (proto) {
function F () { }
F.prototype = proto
return new F()
}
module.exports.formatObjectToJson = function (o) {
return JSON.stringify(o)
}
module.exports.formatObjectToString = function (o, onlyOwnProperties) {
let dump = ''
let oop = false
if (typeof onlyOwnProperties === 'undefined') { oop = true }
for (const prop in o) {
if (oop === false || Object.prototype.hasOwnProperty.call(o, prop)) {
dump = dump + prop + ': ' + o[prop] + ', '
}
}
return dump
}
module.exports.formatObjectToMap = function (o) {
if (Object.entries && typeof Map !== 'undefined') { return new Map(Object.entries(o)) } else { return null }
}
module.exports.formatDateToTimestampNoCheck = function (d) {
return d.toISOString()
}
module.exports.formatCurrentDateToTimestamp = function () {
return this.formatDateToTimestampNoCheck(new Date())
}
module.exports.parseDateFromISOStringNoCheck = function (d) {
return Date.parse(d)
}
module.exports.parseStringToBoolean = function (str, def) {
if (this.isUndefinedOrNull(str) && this.isDefinedAndNotNull(def)) {
return def
}
switch (str.toLowerCase().trim()) {
case 'false':
case 'f':
case 'no':
case 'n':
case '0':
case null:
return false
case 'true':
case 't':
case 'yes':
case 'y':
case '1':
return true
default:
return def
}
}
module.exports.parseJSON = function (str, callback) {
if (!this.isFunction(callback)) {
throw new TypeError(`Illegal argument: callback must be a function, instead got a '${typeof callback}'`)
}
try {
const parsedJSON = JSON.parse(str)
callback(null, parsedJSON)
} catch (err) {
callback(err, null)
}
}
module.exports.lowercase = function (o) {
return this.isString(o) ? o.toLowerCase() : o.toString().toLowerCase()
}
module.exports.uppercase = function (o) {
return this.isString(o) ? o.toUpperCase() : o.toString().toUpperCase()
}
module.exports.toInt = function (str) {
return parseInt(str, 10)
}
module.exports.executeIfTrue = function (f, c) {
let r = null
if (this.isFunction(f) && c === true) {
const fArgs = Array.prototype.slice.call(arguments, 2)
r = f.apply(this, fArgs) // pass all arguments (if any) after those given to main function ...
}
return r
}
module.exports.evaluate = function (statement) {
const evaluator = eval
try {
evaluator(statement)
return true
} catch (e) {
return false
}
}
module.exports.isErrorAvailable = function () {
return (typeof Error !== 'undefined')
}
module.exports.throwError = function (msg) { // note that msg could be a string or an Error, or another Object ...
// throw new Error(msg)
if (this.isErrorAvailable()) { throw new Error(msg) } else { throw msg } // fallback
}
module.exports.errorNotImplemented = function () {
this.throwError('Not Implemented (implementation missing)')
}
module.exports.errorNotCallable = function () {
this.throwError('Not Callable (abstract, implement it in childs)')
}
module.exports.logDebugMessage = function (message) {
if (!this.isEnvironmentDevelopment) { return }
// else ...
const msg = this.name + ': ' + ((message != null) ? message : '')
console.debug(msg)
}
module.exports.noop = function () {
// do nothing ...
}
module.exports.userBrowser = function () {
// browser specific
if (window && window.navigator) { return window.navigator.userAgent } else { return null }
}
module.exports.userLanguage = function () {
// browser specific
if (window && window.navigator) { return window.navigator.language || window.navigator.userLanguage } else { return null }
}
module.exports.userLanguages = function () {
// browser specific
if (window && window.navigator) { return window.navigator.languages } else { return null }
}
module.exports.userLocale = function () {
// browser specific
if (window && window.navigator && window.navigator.languages) { return window.navigator.languages[0] } else { return null }
}
const NODE_ENV = process.env
// returns the given variable name from the Node.js environment
module.exports.fromEnv = function (varName) {
return NODE_ENV[varName]
}
// returns the current Node.js environment
module.exports.currentEnv = function () {
return process.env.NODE_ENV || 'development'
}
// tell if the current Node.js environment is development
module.exports.isEnvDevelopment = function () {
return (this.currentEnv() === 'development')
}
// tell if the current Node.js environment is production
module.exports.isEnvProduction = function () {
return (this.currentEnv() === 'production')
}
// tell if the current Node.js environment is not production
module.exports.isEnvNotProduction = function () {
return !this.isEnvProduction()
}
// returns the current Node.js version
module.exports.runtimeVersion = function () {
return process.version || 'unknown'
}
// returns the current Platform/OS name
module.exports.platformName = function () {
return process.platform || 'unknown'
}
// log to console
module.exports.logToConsole = function (msg) {
console.log(msg)
}
// log a fastify request, but only the given URL
module.exports.logRoute = function (req) {
req.log.info(`Got request for URL: '${req.req.url}' ...`)
}
// log a fastify request, full
module.exports.logRequest = function (req) {
const details = this.dumpObject(req.req)
// const details = this.dumpObject(req.req, {method:'inspect'})
req.log.info(`Got request for URL: '${req.req.url}', details: '${details}' ...`)
}
// register in the app the given module
module.exports.registerLoadedModule = function (app, loadedModule, opts, uri) {
if (this.isUndefinedOrNullArrayItem([app, loadedModule])) { throw new Error('Missing mandatory argument (undefined or null)') }
app.log.info(`Registering the app module from URI '${uri}' ...`)
app.register(loadedModule, function (err) {
if (err) { throw err }
})
}
// build an Error with the given arguments, and throw or return it, depending on the last flag
module.exports.buildError = function (req, msg, code, description, throwError) {
const error = new Error()
error.message = msg
error.statusCode = (code != null) ? code : 0
error.description = (description != null) ? description : null
req.log.error(`Build a new Error: code:${error.statusCode}, message:'${error.message}', description:'${error.description}', and throw it:${throwError}`)
if (throwError === true) { throw error } else { return error }
}
const util = require('util') // provided by Node.js
// dump the given object using 'JSON.stringify' or Node.js 'util.inspect' (requires its module 'util'), depending on the given options
// opts is optional, but if given
// opts.method could have one of the following values: 'stringify', 'inspect', 'fast-json-stringify', null (for the default behavior)
module.exports.dumpObject = function (obj, opts) {
if (this.isUndefinedOrNull(opts)) { return (obj != null) ? obj.toString() : obj }
let val = null
switch (opts.method) {
case 'inspect':
val = util.inspect(obj)
break
case 'fast-json-stringify':
// TODO: implement later ...
// val = stringify(obj)
break
case 'stringify':
val = JSON.stringify(obj) // attention to circular references, some types not dump, etc ...
break
default:
val = obj.toString() // default in js
}
return val
}
// TODO: add a specific function to log dumpObject using app logger ... wip
// resolve the given value (generic, by default 0), after the number of given seconds (by default 1), returning a Promise
// use it with the async / await syntax (recommended)
module.exports.valueDelayed = function (value = 0, sec = 1) {
return new Promise((resolve) => {
setTimeout(() => { resolve(value) }, sec * 1000)
})
}
// tell if a feature is enabled
module.exports.featureIsEnabled = function (trueIsDisabled = false, booleanStringName = '', defaultBooleanValue = true) {
return (trueIsDisabled === true)
? !this.parseStringToBoolean(booleanStringName, defaultBooleanValue)
: this.parseStringToBoolean(booleanStringName, defaultBooleanValue)
}
// sort string properties of objects,
// given 'key' (string property name), 'order' ('asc' or 'desc')
module.exports.compareProperties = function (key, order = 'asc') {
return function (a, b) {
if (!Object.prototype.hasOwnProperty.call(a, key) || !Object.prototype.hasOwnProperty.call(b, key)) return 0
const comparison = a[key].localeCompare(b[key])
return (order === 'asc') ? comparison : (comparison * -1)
}
}
// const exec = util.promisify(require('child_process').exec) // provided by Node.js
const execFile = util.promisify(require('child_process').execFile) // provided by Node.js
module.exports.gitVersion = async function () {
const { stdout, stderr } = await execFile('git', ['version'])
// const { stdout, stderr } = await execFile('git', ['version']).catch(e => console.error('Error: ', e.message)) // log some error info here
// otherwise, do the same in callers (a catch for each promise, or wrap in a try/catch block) ...
return stdout.replace('\n', '')
}
module.exports.gitBranch = async function () {
const { stdout, stderr } = await execFile('git', ['branch'])
return stdout.substring(2).replace('\n', '')
}
module.exports.gitHashFull = async function () {
const { stdout, stderr } = await execFile('git', ['rev-parse', 'HEAD'])
return stdout.replace('\n', '')
}
module.exports.gitHashShort = async function () {
const { stdout, stderr } = await execFile('git', ['rev-parse', '--short', 'HEAD'])
return stdout.replace('\n', '')
}
// export main object
// module.exports.utils = utils