simple-in-memory-queue
Version:
A simple in-memory queue, for nodejs and the browser, with consumers for common usecases.
37 lines • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.split = void 0;
const constants_1 = require("../../domain/constants");
/**
* split the queued data into the selected and remainder data
*
* note
* - this is the base function called by peek and pop
* - this should not be called by users directly
*/
const split = ({ data, order, slice, }) => {
// handle fifo
if (order === constants_1.QueueOrder.FIRST_IN_FIRST_OUT) {
const selection = data.slice(slice.start, slice.end);
const remainder = [
...data.slice(0, slice.start),
...data.slice(slice.end), // all after the slice
];
return { selection, remainder };
}
// handle lifo
if (order === constants_1.QueueOrder.LAST_IN_FIRST_OUT) {
const selection = data
.slice(slice.end * -1, slice.start * -1 === 0 ? undefined : slice.start * -1)
.reverse();
const remainder = [
...data.slice(0, slice.end * -1),
...(slice.start > 0 ? data.slice(slice.start * -1) : []), // all after the slice
];
return { selection, remainder };
}
// if the above didn't handle it, probably an error
throw new Error('unexpected queue type');
};
exports.split = split;
//# sourceMappingURL=split.js.map