@bookbox/view-html
Version:
Bookbox view for html
63 lines (62 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addTail = addTail;
exports.addHead = addHead;
exports.removeHead = removeHead;
exports.removeTail = removeTail;
exports.findItem = findItem;
function getItem(data) {
return {
data,
next: null,
prev: null,
};
}
function addTail(list, data) {
const item = getItem(data);
const oldTail = list.tail;
if (oldTail) {
oldTail.next = item;
item.prev = oldTail;
}
list.tail = item;
}
function addHead(list, data) {
const item = getItem(data);
const oldHead = list.head;
if (oldHead) {
oldHead.prev = item;
item.next = oldHead;
}
list.head = item;
}
function removeHead(list) {
const oldHead = list.head;
if (oldHead) {
const currentHead = oldHead.next;
if (currentHead) {
currentHead.prev = null;
}
list.head = currentHead;
}
}
function removeTail(list) {
const oldTail = list.tail;
if (oldTail) {
const currentTail = oldTail.prev;
if (currentTail) {
currentTail.next = null;
}
list.tail = currentTail;
}
}
function findItem(list, find) {
let current = list.head;
while (current !== null) {
if (find(current.data)) {
return current;
}
current = current.next;
}
return null;
}