sangja
Version:
JavaScript data structures library
60 lines (54 loc) • 1.27 kB
JavaScript
/**
* Check if given object is iterable
* @function
* @memberof sangja
* @param {*} obj - An object to check if iterable
* @return {boolean} True if obj is iterable else false
*/
function isIterable(obj) {
// checks for null and undefined
if (obj == null) {
return false;
}
return typeof obj[Symbol.iterator] === 'function';
}
/**
* Default key function.<br>
* Data structures that require compare will use this function as a default key function.<br>
* Returns the input argument as it is.
* @function
* @memberof sangja
*/
const defaultKey = x => x;
/**
* Default compare function.<br>
* Data structures that require compare will use this function as a default compare function.<br>
* Compare 2 given operands and return the result. This function can compare number, string.
* @function
* @memberof sangja
*/
const defaultCompare = (x, y) => {
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
return 0;
};
function mergeOptions(given = {}, original = {}) {
if (!given) {
return original;
}
const merged = {};
Object.keys(original).forEach((key) => {
merged[key] = given[key] || original[key];
});
return merged;
}
module.exports = {
isIterable,
defaultKey,
defaultCompare,
mergeOptions,
};