pig-dam-core
Version:
Library that should be included in every Pig DAM project we build
51 lines (50 loc) • 1.22 kB
JavaScript
;
/**
* Date: 7/9/2019
* Time: 9:10 PM
* @license MIT (see project's LICENSE file)
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareString = exports.compareAny = void 0;
const _ = require("lodash");
/**
* Compares compareAny object types with handling for undefined objects.
*/
function compareAny(value1, value2, { ignoreCase = false } = {}) {
if (value1 === value2) {
return 0;
}
else if (value1 == null) {
return 1;
}
else if (value2 == null) {
return -1;
}
else if (_.isString(value1) && _.isString(value2)) {
return compareString(value1, value2, { ignoreCase });
}
return _.clamp(value1 - value2, -1, 1);
}
exports.compareAny = compareAny;
/**
* Compares two strings
*/
function compareString(s1, s2, { ignoreCase = true } = {}) {
if (s1 == null) {
return (s2 == null)
? 0
: 1;
}
else if (s2 == null) {
return -1;
}
else {
if (ignoreCase) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
}
return _.clamp(s1.localeCompare(s2), -1, 1);
}
}
exports.compareString = compareString;