octo-deno/lib/utils/common.js

69 lines
1.5 KiB
JavaScript
Raw Normal View History

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 {
return '' + obj
2017-04-22 22:49:49 +00:00
} catch (e) {
return '[' + e + ']'
2017-04-22 22:49:49 +00:00
}
},
getAllProperties: function (obj) {
let list = []
2017-04-22 22:49:49 +00:00
while (obj) {
list = list.concat(Object.getOwnPropertyNames(obj))
obj = Object.getPrototypeOf(obj)
2017-04-22 22:49:49 +00:00
}
return list
2017-04-22 22:49:49 +00:00
},
getKeysFromHash: function (obj) {
let list = []
2017-04-22 22:49:49 +00:00
for (let p in obj) {
list.push(p)
2017-04-22 22:49:49 +00:00
}
return list
2017-04-22 22:49:49 +00:00
},
quote: function (obj) {
return JSON.stringify(obj)
2017-04-22 22:49:49 +00:00
},
shuffle: function (list) {
let newArray = list.slice()
let len = newArray.length
let i = len
2017-04-22 22:49:49 +00:00
while (i--) {
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
}
return newArray
2017-04-22 22:49:49 +00:00
},
uniqueList: function (list) {
let tmp = {}
let r = []
2017-04-22 22:49:49 +00:00
for (let i = 0; i < list.length; i++) {
tmp[list[i]] = list[i]
2017-04-22 22:49:49 +00:00
}
for (let i in tmp) {
r.push(tmp[i])
2017-04-22 22:49:49 +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) {
obj1[p] = utils.common.mergeHash(obj1[p], obj2[p])
2017-04-22 22:49:49 +00:00
} else {
obj1[p] = obj2[p]
2017-04-22 22:49:49 +00:00
}
} catch (e) {
obj1[p] = obj2[p]
2017-04-22 22:49:49 +00:00
}
}
return obj1
2017-04-22 22:49:49 +00:00
}
}