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/. */
|
|
|
|
|
|
|
|
utils.common = {
|
|
|
|
objToString: function (obj) {
|
|
|
|
try {
|
2017-04-25 22:21:31 +00:00
|
|
|
return '' + obj
|
2017-04-22 22:49:49 +00:00
|
|
|
} catch (e) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return '[' + e + ']'
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
getAllProperties: function (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
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
getKeysFromHash: function (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
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
2017-06-15 21:24:32 +00:00
|
|
|
quote: function (s) {
|
|
|
|
// Taken from DOMfuzz
|
2017-06-14 18:04:53 +00:00
|
|
|
function escapeString (s) {
|
2017-06-15 21:24:32 +00:00
|
|
|
return ('\"' + // eslint-disable-line no-useless-escape
|
|
|
|
s.replace(/\\/g, '\\\\')
|
|
|
|
.replace(/\"/g, '\\\"') // eslint-disable-line no-useless-escape
|
|
|
|
.replace(/\0/g, '\\0')
|
|
|
|
.replace(/\n/g, '\\n') +
|
|
|
|
'\"') // eslint-disable-line no-useless-escape
|
2017-06-14 18:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof s === 'string') {
|
2017-06-15 21:24:32 +00:00
|
|
|
if (/^[\n\x20-\x7f]*$/.exec(s) || !self.uneval) { // eslint-disable-line no-undef
|
|
|
|
// Printable ASCII characters and line breaks: try to make it pretty.
|
|
|
|
return escapeString(s)
|
|
|
|
} else {
|
|
|
|
// Non-ASCII: use uneval to get \u escapes.
|
|
|
|
return uneval(s) // eslint-disable-line no-undef
|
|
|
|
}
|
2017-06-14 18:04:53 +00:00
|
|
|
} else {
|
2017-06-15 21:24:32 +00:00
|
|
|
// For other things (such as numbers, |null|, and |undefined|), just coerce to string.
|
|
|
|
return JSON.stringify(s)
|
2017-06-14 18:04:53 +00:00
|
|
|
}
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
2017-06-08 16:05:06 +00:00
|
|
|
b64encode: function (str) {
|
|
|
|
// Unicode safe b64 encoding
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
|
|
|
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
|
|
|
|
function toSolidBytes (match, p1) {
|
|
|
|
// noinspection JSCheckFunctionSignatures
|
|
|
|
return String.fromCharCode('0x' + p1)
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
b64decode: function (str) {
|
|
|
|
// Unicode safe b64 decoding
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
|
|
|
return decodeURIComponent(atob(str).split('').map(function (c) {
|
|
|
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
|
|
|
|
}).join(''))
|
|
|
|
},
|
2017-04-22 22:49:49 +00:00
|
|
|
uniqueList: function (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
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
mergeHash: function (obj1, obj2) {
|
|
|
|
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
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
}
|