qm-bus
Version:
千米公有云业务组件库
213 lines (203 loc) • 5.73 kB
JavaScript
;
/**
* @name QM.Array
* @class
* @description 数组的一些方法
*/
var assign = require('object-assign');
var _Array = module.exports = {}; //下面有一个Array的引用 , 不这样不行呀
_Array.mixin = function (arrays, v, propertyName, howMerge) {
var _this = arrays;
if (_Array.isArray(v)) {
var res = [];
v.forEach(function (item) {
var _arr = _Array.mixin(_this, item, propertyName, howMerge);
res = res.concat(_arr);
});
return res;
}
if (typeof propertyName == 'function') {
var res = [];
_this.forEach(function (t, k) {
if (propertyName(v, t)) {
if (typeof howMerge == 'function') {
_this[k] = howMerge(v, _this[k]);
} else {
_this[k] = assign(_this[k], v);
}
res = res.concat([assign({}, _this[k])]);
}
});
return res;
}
var res = [];
var i = _Array.indexOf(_this, v, propertyName);
if (i > -1) {
if (typeof howMerge == 'function') {
_this[i] = howMerge(v, _this[i]);
} else {
_this[i] = assign(_this[i], v);
}
return res = res.concat([assign({}, _this[i])]);
}
return res;
};
_Array.upush = function (arrays, v, propertyName, isMerge) {
if (typeof v == 'function') {
arrays.push(v);
return arrays;
} else if (typeof propertyName == 'function') {
var _this = arrays;
_this.forEach(function (t, k) {
if (propertyName(v, t)) {
if (isMerge) {
_this[k] = assign(_this[k], v);
}
return _this;
}
});
_this.push(v);
return _this;
}
var i = _Array.indexOf(_this, v, propertyName);
if (i == -1) {
_this.push(v);
} else if (isMerge) {
_this[i] = assign(_this[i], v);
}
return _this;
};
_Array.umerge = function (arrays, v, propertyName, isMerge) {
var _this = arrays;
if (v instanceof Array) {
v.forEach(function (item) {
_Array.upush(_this, item, propertyName, isMerge);
});
} else {
_Array.upush(_this, v, propertyName, isMerge);
}
return _this;
};
/**
*
* @param value
* @param propertyName 如何设置, 将会判断这个对象的属性值在这个数组里面是否有重复
* @returns
*/
_Array.indexOf = function (arrays, value, propertyName) {
var _this = arrays;
var i;
for (i = 0; i < _this.length; i++) {
if (!propertyName) {
var l = typeof _this[i] == 'string' ? _this[i].trim() : _this[i];
var r = typeof value == 'string' ? value.trim() : value;
if (l == r) {
return i;
}
} else if (propertyName) {
if (typeof propertyName == 'function') {
if (propertyName(value, _this[i])) {
return i;
}
} else if (this.isArray(propertyName)) {
var allEq = true;
for (var j = 0; j < propertyName.length; j++) {
var l = typeof _this[i][propertyName[j]] == 'string' ? _this[i][propertyName[j]].trim() : _this[i][propertyName[j]];
var r = typeof value[propertyName[j]] == 'string' ? value[propertyName[j]].trim() : value[propertyName[j]];
if (l != r) {
allEq = false;
break;
}
}
if (allEq) {
return i;
}
} else {
var l = typeof _this[i][propertyName] == 'string' ? _this[i][propertyName].trim() : _this[i][propertyName];
var r = typeof value[propertyName] == 'string' ? value[propertyName].trim() : value[propertyName];
if (l == r) {
return i;
}
}
}
}
return -1;
};_Array.contain = function (arrays, obj, propertyName) {
if (typeof obj == 'function') return null;
return _Array.indexOf(arrays, obj, propertyName) != -1;
}, _Array.remove = function (arrays, val, propertyName) {
if (typeof val == 'function') return null;
var i = _Array.indexOf(arrays, val, propertyName);
return i != -1 ? arrays.splice(i, 1) : [];
};
_Array.treeSort = function (arrays, idProp, pidProp, startWith) {
var arrs = [];
var _this = arrays;
var children = function children(arr, pid, deep) {
deep = deep || 0;
var childList = [];
arr.forEach(function (v, k) {
if (v[pidProp] == pid) {
arrs.push(v);
childList.push(v);
var child = children(arr, v[idProp], deep + 1);
if (!child.length) {
v.NODE_LEAF = true;
}
v.NODE_DEEP = deep + 1;
}
});
return childList;
};
children(_this, startWith || 0);
return arrs;
};
_Array.tree = function (arrays, idProp, pidProp, startWith, process) {
var arrs = [];
var _this = arrays;
var children = function children(_arr, pid, deep, pos) {
deep = deep || 0;
pos = pos || [0];
var childList = [];
if (_arr && _arr instanceof Array) {
_arr.forEach(function (v) {
if (v[pidProp] == pid) {
var _pos = [].concat(pos);
_pos.push(childList.length);
v.NODE_POSITION = _pos;
var child = children(_arr, v[idProp], deep + 1, [].concat(_pos));
v.childNodes = child;
if (!child.length) {
v.NODE_LEAF = true;
}
v.NODE_DEEP = deep + 1;
process && process(v);
childList.push(assign({}, v));
}
});
}
return childList;
};
return children(_this, startWith || 0);
};
/**
* 判断是不是数组
*
* @param arr
* @returns {boolean}
*/
_Array.isArray = function (arrays) {
return Object.prototype.toString.call(arrays) === '[object Array]';
};
/**
* 获取数组中的元素
*/
_Array.get = function (arr, propertyValue, propertyName) {
var val = null;
arr.map(function (v, k) {
if (v[propertyName] == propertyValue) {
val = v;
}
});
return val;
};