UNPKG

interval-management

Version:

No dependency interval management library, able to work with numbers, string, Dates or special objects

52 lines (51 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.interval = void 0; var numberInterval_1 = require("./numberInterval"); var stringInterval_1 = require("./stringInterval"); var objectInterval_1 = require("./objectInterval"); var dateInterval_1 = require("./dateInterval"); exports.interval = function (start, end, next, compare) { if (!start && !end && !next) { return numberInterval_1.numberInterval(0, Infinity, function (current) { return current + 1; }); } if (Array.isArray(start) && start.length > 0 && !end && !next) { var mappedIndex_1 = {}; start.forEach(function (value, i) { if (mappedIndex_1[JSON.stringify(value)] !== undefined) { throw Error('Cannot define array-like interval with two equal (as JSON.stringify) values!'); } mappedIndex_1[JSON.stringify(value)] = i; }); return exports.interval(start[0], start[start.length - 1], function (value) { var currentIndex = mappedIndex_1[JSON.stringify(value)]; if ((!currentIndex && currentIndex !== 0) || currentIndex === start.length - 1) { return start[start.length - 1]; } return start[currentIndex + 1]; }); } if (start || start === 0) { switch (typeof start) { case 'bigint': case 'function': throw Error('Function or bigint are not correct types for interval'); case 'number': return numberInterval_1.numberInterval(start, end || Infinity, next || (function (current) { return current + 1; })); case 'symbol': case 'string': return stringInterval_1.stringInterval(start.toString(), end || null, next || (function (current) { return "" + current + current; })); case 'object': if (start instanceof Date) { return dateInterval_1.dateInterval(start, end || null, next || (function (current) { return new Date(current.getTime() + 1); })); } if (!next || !compare) { throw Error('Cannot create non-date object interval without knowing how to generate next item'); } return objectInterval_1.objectInterval(compare)(start, end || null, next); case 'boolean': return numberInterval_1.numberInterval(start, end || true, next || (function (_) { return true; })); } } throw Error('Unable to determine what interval to create'); };