jora
Version:
JavaScript object query engine
80 lines (64 loc) • 1.9 kB
JavaScript
;
const hasOwn = Object.hasOwn || ((subject, key) => Object.hasOwnProperty.call(subject, key));
const toString = Object.prototype.toString;
function addToSet(set, value) {
if (value !== undefined) {
if (isArray(value)) {
value.forEach(item => set.add(item));
} else {
set.add(value);
}
}
return set;
}
function addToMapSet(map, key, value) {
if (map.has(key)) {
map.get(key).add(value);
} else {
map.set(key, new Set([value]));
}
}
function getPropertyValue(value, property) {
return value && hasOwn(value, property) ? value[property] : undefined;
}
function isPlainObject(value) {
return value !== null && typeof value === 'object' && value.constructor === Object;
}
function isRegExp(value) {
return toString.call(value) === '[object RegExp]';
}
function isArrayLike(value) {
return value && (isArray(value) || (hasOwn(value, 'length') && isFinite(value.length)));
}
function isArray(value) {
return Array.isArray(value) || ArrayBuffer.isView(value);
}
function parseIntDefault(value, defaultValue = 0) {
const int = parseInt(value, 10);
return !isNaN(int) ? int : defaultValue;
}
function isTruthy(value) {
if (isArray(value)) {
return value.length > 0;
}
if (isPlainObject(value)) {
for (const key in value) {
if (hasOwn(value, key)) {
return true;
}
}
return false;
}
return Boolean(value);
}
exports.addToMapSet = addToMapSet;
exports.addToSet = addToSet;
exports.getPropertyValue = getPropertyValue;
exports.hasOwn = hasOwn;
exports.isArray = isArray;
exports.isArrayLike = isArrayLike;
exports.isPlainObject = isPlainObject;
exports.isRegExp = isRegExp;
exports.isTruthy = isTruthy;
exports.parseIntDefault = parseIntDefault;
exports.toString = toString;