UNPKG

superfly-timeline

Version:

Resolver for defining objects with temporal boolean logic relationships on a timeline

83 lines 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInstanceHash = exports.getInstancesHash = exports.baseInstance = exports.baseInstances = exports.spliceInstances = exports.getInstanceIntersection = exports.instanceIsActive = exports.isInstanceId = void 0; const lib_1 = require("./lib"); function isInstanceId(str) { return str.startsWith('@'); } exports.isInstanceId = isInstanceId; function instanceIsActive(instance, time) { return instance.start <= time && (instance.end ?? Infinity) > time; } exports.instanceIsActive = instanceIsActive; /** * Returns the intersection of two instances. * Example: for (10-20) and (15-30), the intersection is (15-20). */ function getInstanceIntersection(a, b) { if (a.start < (b.end ?? Infinity) && (a.end ?? Infinity) > b.start) { const start = Math.max(a.start, b.start); const end = Math.min(a.end ?? Infinity, b.end ?? Infinity); return { start, end: end === Infinity ? null : end, }; } return null; } exports.getInstanceIntersection = getInstanceIntersection; /** * Convenience function to splice an array of instances * @param instances The array of instances to splice * @param fcn Operator function. * Is called for each instance in the array, * and should return an instance (or an array of instances) to insert in place of the original instance, * or undefined to remove the instance. * (To leave the instance unchanged, return the original instance) */ function spliceInstances(instances, fcn) { for (let i = 0; i < instances.length; i++) { const fcnResult = fcn(instances[i]); const insertInstances = fcnResult === undefined ? [] : (0, lib_1.ensureArray)(fcnResult); if (insertInstances.length === 0) { instances.splice(i, 1); i--; } else { if (insertInstances[0] === instances[i]) continue; // replace: instances.splice(i, 1, ...insertInstances); i += insertInstances.length - 1; } } } exports.spliceInstances = spliceInstances; function baseInstances(instances) { return instances.map((instance) => baseInstance(instance)); } exports.baseInstances = baseInstances; function baseInstance(instance) { return { start: instance.start, end: instance.end, }; } exports.baseInstance = baseInstance; /** Returns a string hash that changes whenever any instance has changed in a significant way */ function getInstancesHash(instances) { const strs = []; for (const instance of instances) { strs.push(getInstanceHash(instance)); } return strs.join(','); } exports.getInstancesHash = getInstancesHash; /** Returns a string hash that changes whenever an instance has changed in a significant way */ function getInstanceHash(instance) { const orgStart = instance.originalStart ?? instance.start; const orgEnd = instance.originalEnd ?? instance.end; return `${instance.start}_${instance.end ?? 'null'}(${orgStart}_${orgEnd ?? 'null'})`; } exports.getInstanceHash = getInstanceHash; //# sourceMappingURL=instance.js.map