daniel-san
Version:
a node-based budget-projection engine that helps your routines and finances find balance. The program features aggregates, terminal and file-based reporting output, multi-currency conversion capability and multi-frequency accounting triggers, including: o
95 lines (84 loc) • 3.16 kB
JavaScript
"use strict";
var _require = require('../utility/validation'),
isUndefinedOrNull = _require.isUndefinedOrNull;
var _require2 = require('../constants'),
DATE_DELIMITER = _require2.DATE_DELIMITER;
var compareByPropertyKey = function compareByPropertyKey(a, b, propertyKey) {
var paramA = typeof a[propertyKey] === 'string' ? a[propertyKey].toLowerCase() : a[propertyKey];
var paramB = typeof b[propertyKey] === 'string' ? b[propertyKey].toLowerCase() : b[propertyKey];
if (!isUndefinedOrNull(paramA) && !isUndefinedOrNull(paramB)) {
if (paramA > paramB) {
return 1;
} else if (paramA < paramB) {
return -1; // eslint-disable-next-line no-else-return
} else {
return 0;
}
} else if (!isUndefinedOrNull(paramA) && isUndefinedOrNull(paramB)) {
return -1;
} else if (isUndefinedOrNull(paramA) && !isUndefinedOrNull(paramB)) {
return 1;
} else {
return 0;
}
};
var compareTime = function compareTime(a, b) {
var propertyKey = 'timeStart';
var paramA = typeof a[propertyKey] === 'string' ? a[propertyKey].toLowerCase() : a[propertyKey];
var paramB = typeof b[propertyKey] === 'string' ? b[propertyKey].toLowerCase() : b[propertyKey];
if (!isUndefinedOrNull(paramA) && !isUndefinedOrNull(paramB)) {
if (paramA.includes('pm') && paramB.includes('am')) {
return 1;
} else if (paramA.includes('am') && paramB.includes('pm')) {
return -1;
} else if (paramA > paramB) {
return 1;
} else if (paramA < paramB) {
return -1; // eslint-disable-next-line no-else-return
} else {
// the times are equal so check if there is a sortPriority to sort against
// eslint-disable-next-line no-lonely-if
if (a.sortPriority || b.sortPriority) {
return compareByPropertyKey(a, b, 'sortPriority'); // eslint-disable-next-line no-else-return
} else {
return 0;
}
}
} else if (!isUndefinedOrNull(paramA) && isUndefinedOrNull(paramB)) {
return -1;
} else if (!isUndefinedOrNull(paramB) && isUndefinedOrNull(paramA)) {
return 1;
} else {
return 0;
}
};
var sortEvents = function sortEvents(events) {
events.sort(function (a, b) {
var thisDateA = a.dateStart.split(DATE_DELIMITER).join('');
var thisDateB = b.dateStart.split(DATE_DELIMITER).join('');
if (thisDateA > thisDateB) {
return 1;
} else if (thisDateA < thisDateB) {
return -1;
} else if (thisDateA === thisDateB) {
if (a.timeStart || b.timeStart) {
return compareTime(a, b); // eslint-disable-next-line no-else-return
} else {
// eslint-disable-next-line no-lonely-if
if (a.sortPriority || b.sortPriority) {
return compareByPropertyKey(a, b, 'sortPriority'); // eslint-disable-next-line no-else-return
} else {
return 0;
}
}
} else {
return 0;
} // eslint-disable-next-line no-unreachable
return 0; // this line is unreachable/dead code, but it satisfies another linting error
});
};
module.exports = {
compareByPropertyKey: compareByPropertyKey,
compareTime: compareTime,
sortEvents: sortEvents
};