2017-04-22 22:49:49 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2018-03-21 21:04:57 +00:00
|
|
|
const jsesc = require('jsesc')
|
2018-03-19 04:54:40 +00:00
|
|
|
const utils = require('../utils')
|
2018-03-19 03:23:05 +00:00
|
|
|
|
|
|
|
class common extends utils {
|
2018-08-13 14:59:05 +00:00
|
|
|
/**
|
|
|
|
* Return stringified object
|
|
|
|
* @param obj
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static objToString (obj) {
|
2017-04-22 22:49:49 +00:00
|
|
|
try {
|
2018-05-21 15:50:44 +00:00
|
|
|
return `${obj}`
|
2017-04-22 22:49:49 +00:00
|
|
|
} catch (e) {
|
2018-05-21 15:50:44 +00:00
|
|
|
return `[${e}]`
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 14:59:05 +00:00
|
|
|
/**
|
|
|
|
* Return enumerable properties recursively
|
|
|
|
* @param obj
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static getAllProperties (obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let list = []
|
2017-04-22 22:49:49 +00:00
|
|
|
while (obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
list = list.concat(Object.getOwnPropertyNames(obj))
|
|
|
|
obj = Object.getPrototypeOf(obj)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return list
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 14:59:05 +00:00
|
|
|
/**
|
|
|
|
* Return all properties (non-recursive)
|
|
|
|
* @param obj
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static getKeysFromHash (obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let list = []
|
2017-04-22 22:49:49 +00:00
|
|
|
for (let p in obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
list.push(p)
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return list
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 18:26:17 +00:00
|
|
|
/**
|
|
|
|
* Escape and quote a string
|
2018-08-13 14:59:05 +00:00
|
|
|
* @param s - String to be quoted
|
|
|
|
* @param {boolean} html - Identifies whether the string must be HTML safe
|
2018-08-02 18:26:17 +00:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
static quote (s, html = false) {
|
|
|
|
const options = {
|
|
|
|
minimal: true,
|
|
|
|
isScriptContext: html
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:14:40 +00:00
|
|
|
if (typeof s === 'string') {
|
2018-08-02 18:26:17 +00:00
|
|
|
return `'${jsesc(s, options)}'`
|
2018-04-07 15:14:40 +00:00
|
|
|
} else {
|
2018-08-02 18:26:17 +00:00
|
|
|
return jsesc(s, options)
|
2018-04-07 15:14:40 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 14:59:05 +00:00
|
|
|
/**
|
|
|
|
* Unicode safe b64 encoding
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
|
|
|
* @param {string} str
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static b64encode (str) {
|
2018-05-14 17:49:08 +00:00
|
|
|
if (process.browser) {
|
|
|
|
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
|
|
|
|
function toSolidBytes (match, p1) {
|
|
|
|
// noinspection JSCheckFunctionSignatures
|
|
|
|
return String.fromCharCode('0x' + p1)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return Buffer.from(str).toString('base64')
|
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 14:59:05 +00:00
|
|
|
/**
|
|
|
|
* Unicode safe b64 decoding
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
|
|
|
* @param {string} str
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static b64decode (str) {
|
2018-05-14 17:49:08 +00:00
|
|
|
if (process.browser) {
|
|
|
|
return decodeURIComponent(atob(str).split('').map(function (c) {
|
2018-05-21 15:50:44 +00:00
|
|
|
return `%${('00' + c.charCodeAt(0).toString(16)).slice(-2)}`
|
2018-05-14 17:49:08 +00:00
|
|
|
}).join(''))
|
|
|
|
} else {
|
|
|
|
return Buffer.from(str, 'base64').toString('ascii')
|
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 14:59:05 +00:00
|
|
|
/**
|
|
|
|
* Remove duplicate items from a list
|
|
|
|
* @param {Array} list
|
|
|
|
* @returns {Array}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static uniqueList (list) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let tmp = {}
|
|
|
|
let r = []
|
2017-04-22 22:49:49 +00:00
|
|
|
for (let i = 0; i < list.length; i++) {
|
2017-04-25 22:21:31 +00:00
|
|
|
tmp[list[i]] = list[i]
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
for (let i in tmp) {
|
2017-04-25 22:21:31 +00:00
|
|
|
r.push(tmp[i])
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return r
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 14:59:05 +00:00
|
|
|
/**
|
|
|
|
* Merge two objects recursively
|
|
|
|
* @param {Object} obj1
|
|
|
|
* @param {Object} obj2
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static mergeHash (obj1, obj2) {
|
2017-04-22 22:49:49 +00:00
|
|
|
for (let p in obj2) {
|
|
|
|
try {
|
2017-04-25 15:22:15 +00:00
|
|
|
if (obj2[p].constructor === Object) {
|
2017-04-25 22:21:31 +00:00
|
|
|
obj1[p] = utils.common.mergeHash(obj1[p], obj2[p])
|
2017-04-22 22:49:49 +00:00
|
|
|
} else {
|
2017-04-25 22:21:31 +00:00
|
|
|
obj1[p] = obj2[p]
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2017-04-25 22:21:31 +00:00
|
|
|
obj1[p] = obj2[p]
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return obj1
|
2018-03-19 03:23:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 14:59:05 +00:00
|
|
|
/**
|
|
|
|
* Template string beautifier
|
|
|
|
* @param {Object} obj
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-03-19 03:23:05 +00:00
|
|
|
static mockup (obj) {
|
|
|
|
return obj.split('\n').map((ln) => ln.trim()).join('')
|
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
}
|
2018-03-19 03:23:05 +00:00
|
|
|
|
|
|
|
module.exports = common
|