Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | 3x 11x 11x 1x 1x 10x 10x 1x 1x 9x 3x 3x 1x 2x 2x 5x 2x 1x 1x 3x 5x 5x 3x 1x 1x 1x 1x 1x 1x 3x 2x 2x 4x 2x 1x 1x 1x 1x 3x 3x 1x 2x 2x 5x 2x 1x 1x 1x 1x 3x | // get books from local storage
const get = () => {
const bookValue = localStorage.getItem("books");
if (bookValue === undefined) {
const bookCollection = { nextId: 1, books: [] }
return set(bookCollection);
}
const bookCollection = JSON.parse(bookValue);
if (bookCollection === null) {
const bookCollection = { nextId: 1, books: [] }
return set(bookCollection);
}
return bookCollection;
};
const getById = (id) => {
if (id === undefined) {
return { "error": "id is a required parameter" };
}
const bookCollection = get();
const books = bookCollection.books;
/* eslint-disable-next-line eqeqeq */ // we really do want == here, not ===
const index = books.findIndex((r) => r.id == id);
if (index === -1) {
return { "error": `book with id ${id} not found` };
}
return { book: books[index] };
}
// set books in local storage
const set = (bookCollection) => {
localStorage.setItem("books", JSON.stringify(bookCollection));
return bookCollection;
};
// add a book to local storage
const add = (book) => {
const bookCollection = get();
book = { ...book, id: bookCollection.nextId };
bookCollection.nextId++;
bookCollection.books.push(book);
set(bookCollection);
return book;
};
// update a book in local storage
const update = (book) => {
const bookCollection = get();
const books = bookCollection.books;
/* eslint-disable-next-line eqeqeq */ // we really do want == here, not ===
const index = books.findIndex((r) => r.id == book.id);
if (index === -1) {
return { "error": `book with id ${book.id} not found` };
}
books[index] = book;
set(bookCollection);
return { bookCollection: bookCollection };
};
// delete a book from local storage
const del = (id) => {
if (id === undefined) {
return { "error": "id is a required parameter" };
}
const bookCollection = get();
const books = bookCollection.books;
/* eslint-disable-next-line eqeqeq */ // we really do want == here, not ===
const index = books.findIndex((r) => r.id == id);
if (index === -1) {
return { "error": `book with id ${id} not found` };
}
books.splice(index, 1);
set(bookCollection);
return { bookCollection: bookCollection };
};
const bookUtils = {
get,
getById,
add,
update,
del
};
export { bookUtils };
|