@jacks/jutils
Version:
A common set of JavaScript tool Libraries
63 lines (42 loc) • 2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findCollectionObj = exports.cleanEmpty = exports.getPageCount = exports.differenceObj = exports.getDateByID = exports.getTimestampByID = void 0;
var _moment = _interopRequireDefault(require("moment"));
var _lodash = require("lodash");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Get Timestamp based on mongoId
var getTimestampByID = function getTimestampByID(_id) {
return parseInt(_id.toString().substring(0, 8), 16) * 1000;
}; // Get Date based on mongoId
exports.getTimestampByID = getTimestampByID;
var getDateByID = function getDateByID(_id) {
return (0, _moment.default)(getTimestampByID(_id)).format('YYYY-MM-DD HH:mm:ss');
}; // 对比对象的差异
exports.getDateByID = getDateByID;
var differenceObj = function differenceObj(object, base) {
function changes(object, base) {
return (0, _lodash.transform)(object, function (result, value, key) {
if (!(0, _lodash.isEqual)(value, base[key])) {
result[key] = (0, _lodash.isObject)(value) && (0, _lodash.isObject)(base[key]) ? changes(value, base[key]) : value;
}
});
}
return changes(object, base);
};
exports.differenceObj = differenceObj;
var getPageCount = function getPageCount(count, limit) {
return Math.ceil((count | 0) / limit);
}; // 删除没填数据的字段
exports.getPageCount = getPageCount;
var cleanEmpty = function cleanEmpty(object) {
return Object.keys(object).forEach(function (key) {
return object[key] === '' || (0, _lodash.isNil)(object[key]) || (0, _lodash.isNaN)(object[key]) ? delete object[key] : object[key];
});
}; // 根据value匹配集合中对应的对象
exports.cleanEmpty = cleanEmpty;
var findCollectionObj = function findCollectionObj(collection, predicate, fromIndex) {
return (0, _lodash.find)(collection, predicate, fromIndex) || {};
};
exports.findCollectionObj = findCollectionObj;