superfly-timeline
Version:
Resolver for defining objects with temporal boolean logic relationships on a timeline
112 lines • 3.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareStrings = exports.mapToObject = exports.assertNever = exports.isArray = exports.ensureArray = exports.isEmpty = exports.sortBy = exports.omit = exports.uniq = exports.clone = exports.pushToArray = exports.reduceObj = exports.isObject = exports.last = exports.compact = exports.literal = void 0;
function literal(o) {
return o;
}
exports.literal = literal;
function compact(arr) {
const returnValues = [];
for (let i = 0; i < arr.length; i++) {
const v = arr[i];
if (!!v || (v !== undefined && v !== null && v !== ''))
returnValues.push(v);
}
return returnValues;
}
exports.compact = compact;
function last(arr) {
return arr[arr.length - 1];
}
exports.last = last;
/** Returns true if argument is an object (or an array, but NOT null) */
function isObject(o) {
return o !== null && typeof o === 'object';
}
exports.isObject = isObject;
function reduceObj(objs, fcn, initialValue) {
return Object.entries(objs).reduce((memo, [key, value], index) => {
return fcn(memo, value, key, index);
}, initialValue);
}
exports.reduceObj = reduceObj;
/**
* Concatenate two arrays of values.
* This is a convenience function used to ensure that the two arrays are of the same type.
* @param arr0 The array of values to push into
* @param arr1 An array of values to push into arr0
*/
function pushToArray(arr0, arr1) {
for (const item of arr1) {
arr0.push(item);
}
}
exports.pushToArray = pushToArray;
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
exports.clone = clone;
function uniq(arr) {
return Array.from(new Set(arr));
}
exports.uniq = uniq;
function omit(obj, ...keys) {
const result = {};
for (const [key, value] of Object.entries(obj)) {
if (keys.some((k) => (Array.isArray(k) ? k.includes(key) : k === key)))
continue;
result[key] = value;
}
return result;
}
exports.omit = omit;
function sortBy(arr, fcn) {
const sortArray = arr.map((item) => ({ item, value: fcn(item) }));
sortArray.sort((a, b) => {
if (a.value < b.value)
return -1;
if (a.value > b.value)
return 1;
return 0;
});
return sortArray.map((item) => item.item);
}
exports.sortBy = sortBy;
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
exports.isEmpty = isEmpty;
function ensureArray(value) {
return Array.isArray(value) ? value : [value];
}
exports.ensureArray = ensureArray;
/**
* Slightly faster than Array.isArray().
* Note: Ensure that the value provided is not null!
*/
function isArray(arg) {
// Fast-path optimization: checking for .length is faster than Array.isArray()
return arg.length !== undefined && Array.isArray(arg);
}
exports.isArray = isArray;
/**
* Helper function to simply assert that the value is of the type never.
* Usage: at the end of if/else or switch, to ensure that there is no fallthrough.
*/
function assertNever(_value) {
// does nothing
}
exports.assertNever = assertNever;
function mapToObject(map) {
const o = {};
for (const [key, value] of map.entries()) {
o[key] = value;
}
return o;
}
exports.mapToObject = mapToObject;
function compareStrings(a, b) {
return a > b ? 1 : a < b ? -1 : 0;
}
exports.compareStrings = compareStrings;
//# sourceMappingURL=lib.js.map