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
|
|
|
},
|
|
|
|
quote: function (obj) {
|
2017-04-25 22:21:31 +00:00
|
|
|
return JSON.stringify(obj)
|
2017-04-22 22:49:49 +00:00
|
|
|
},
|
|
|
|
shuffle: function (list) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let newArray = list.slice()
|
|
|
|
let len = newArray.length
|
|
|
|
let i = len
|
2017-04-22 22:49:49 +00:00
|
|
|
while (i--) {
|
2017-04-25 22:21:31 +00:00
|
|
|
let p = parseInt(Math.random() * len)
|
|
|
|
let t = newArray[i]
|
|
|
|
newArray[i] = newArray[p]
|
|
|
|
newArray[p] = t
|
2017-04-22 22:49:49 +00:00
|
|
|
}
|
2017-04-25 22:21:31 +00:00
|
|
|
return newArray
|
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
|
|
|
}
|