aggregation-platform/app/my.js

139 lines
4.2 KiB
JavaScript

function Recursion(node){
var count=0;
for (var key in node) {
count++;
var value = node[key];
delete node[key];
//如果node为叶子节点
if (key.toString() == '$') {
for (var attr in value)
node[attr] = value[attr];
} else {
if (value instanceof Array) {
if (value.length > 0) {
node["children"] = value;
for (var obj in value)
Recursion(value[obj]);
}
}
}
}
if(count==1)
node["children"]=[];
}
function randomString(len) {
len = len || 32;
var $chars = 'abcdefhijkmnprstwxyz'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
var maxPos = $chars.length;
var pwd = '';
for (i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
function compArray(array1,array2){
if((array1&&typeof array1 ==="object"&&array1.constructor===Array)&&(array2&&typeof array2 ==="object"&&array2.constructor===Array)){
if(array1.length==array2.length){
for(var i=0;i<array1.length;i++){
var ggg=compObj(array1[i],array2[i]);
if(!ggg)
return false;
}
}else{
return false;
}
}else{
throw new Error("argunment is error ;");
}
return true;
};
function compObj(obj1,obj2){//比较两个对象是否相等,不包含原形上的属性计较
if((obj1&&typeof obj1==="object")&&((obj2&&typeof obj2==="object"))){
var count1=propertyLength(obj1);
var count2=propertyLength(obj2);
if(count1==count2){
for(var ob in obj1){
if(obj1.hasOwnProperty(ob)&&obj2.hasOwnProperty(ob)){
if(obj1[ob].constructor==Array&&obj2[ob].constructor==Array){//如果属性是数组
if(!compArray(obj1[ob],obj2[ob])){
return false;
};
}else if(typeof obj1[ob]==="string"&&typeof obj2[ob]==="string"){//纯属性
if(obj1[ob]!==obj2[ob]){
return false;
}
}else if(typeof obj1[ob]==="object"&&typeof obj2[ob]==="object"){//属性是对象
if(!compObj(obj1[ob],obj2[ob])){
return false;
};
}else{
return false;
}
}else{
return false;
}
}
}else{
return false;
}
}
return true;
};
function propertyLength(obj){//获得对象上的属性个数,不包含对象原形上的属性
var count=0;
if(obj&&typeof obj==="object") {
for(var ooo in obj) {
if(obj.hasOwnProperty(ooo)) {
count++;
}
}
return count;
}else {
throw new Error("argunment can not be null;");
}
};
function selectorMatches(selector,labels){
if(typeof(selector) === 'object'){
var answer = true;
var count = 0;
for(var key in selector){
count++;
if(answer && labels[key] !== selector[key])
answer = false;
}
return answer && count > 0;
}else{
return false;
}
}
Date.prototype.Format = function(fmt)
{ //author: meizz
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
exports.randomString = randomString;
exports.Recursion = Recursion;
exports.compObj = compObj;
exports.selectorMatches = selectorMatches;