diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..1c0202d --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "semi": true, + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "es5", + "printWidth": 80 +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..5707c1a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,105 @@ +{ + "name": "c55-core-week-6", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "c55-core-week-6", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "chalk": "^4.1.2" + }, + "devDependencies": { + "prettier": "^3.8.1" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "license": "MIT", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "license": "MIT", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "license": "MIT" + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/prettier": { + "version": "3.8.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.8.1.tgz", + "integrity": "sha512-UOnG6LftzbdaHZcKoPFtOcCKztrQ57WkHDeRD9t/PTQtmT0NHSeWWepj6pS0z/N7+08BHFDQVUrfmfMRcZwbMg==", + "dev": true, + "license": "MIT", + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "license": "MIT", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..e71300c --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "c55-core-week-6", + "version": "1.0.0", + "description": "The week 6 assignment for the HackYourFuture Core program can be found at the following link: https://hub.hackyourfuture.nl/core-program-week-6-assignment", + "main": "index.js", + "scripts": { + "format": "prettier --write .", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/noneeeed/c55-core-week-6.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "module", + "bugs": { + "url": "https://github.com/noneeeed/c55-core-week-6/issues" + }, + "homepage": "https://github.com/noneeeed/c55-core-week-6#readme", + "dependencies": { + "chalk": "^4.1.2" + }, + "devDependencies": { + "prettier": "^3.8.1" + } +} diff --git a/reading-list-manager/app.js b/reading-list-manager/app.js index b0365ef..3a22bc4 100644 --- a/reading-list-manager/app.js +++ b/reading-list-manager/app.js @@ -11,3 +11,33 @@ console.log('šŸ“š MY READING LIST šŸ“š\n'); // Your implementation here +import { + printAllBooks, + printSummary, + getUnreadBooks, + addBook, + books, + getBooksByGenre, + markAsRead, +} from './readingList.js'; +import chalk from 'chalk'; + +console.log('All Books:'); +printAllBooks(); +printSummary(); + +console.log(chalk.bold('\nUnread books:')); +console.log(getUnreadBooks()); +console.log(chalk.bold('\nBooks by genre (fiction):')); +console.log(getBooksByGenre('fiction')); +const newBook = addBook({ + title: 'The Catcher in the Rye', + author: 'J.D. Salinger', + genre: 'fiction', + read: false, +}); +console.log('New Book Added!)'); +console.log('Current Library:', books); +console.log(chalk.bold('\nAfter marking new book as read:')); +markAsRead(6); +printAllBooks(); diff --git a/reading-list-manager/books.json b/reading-list-manager/books.json index 0637a08..a3eaec7 100644 --- a/reading-list-manager/books.json +++ b/reading-list-manager/books.json @@ -1 +1,37 @@ -[] \ No newline at end of file +[ + { + "id": 1, + "title": "The Great Gatsby", + "author": "F. Scott Fitzgerald", + "genre": "Novel", + "read": true + }, + { + "id": 2, + "title": "To Kill a Mockingbird", + "author": "Harper Lee", + "genre": "Novel", + "read": false + }, + { + "id": 3, + "title": "1984", + "author": "George Orwell", + "genre": "fiction", + "read": true + }, + { + "id": 4, + "title": "Moby Dick", + "author": "Herman Melville", + "genre": "fiction", + "read": false + }, + { + "id": 5, + "title": "Pride and Prejudice", + "author": "Jane Austen", + "genre": "Romance", + "read": true + } +] diff --git a/reading-list-manager/readingList.js b/reading-list-manager/readingList.js index 84febab..a201a4d 100644 --- a/reading-list-manager/readingList.js +++ b/reading-list-manager/readingList.js @@ -1,53 +1,106 @@ -// Place here the file operation functions for loading and saving books +import fs from 'node:fs'; +import chalk from 'chalk'; function loadBooks() { - // TODO: Implement this function - // Read from books.json - // Handle missing file (create empty array) - // Handle invalid JSON (notify user, use empty array) - // Use try-catch for error handling + try { + const books = fs.readFileSync('books.json', 'utf8'); + return JSON.parse(books); + } catch (error) { + if (error.code === 'ENOENT') { + console.log('books.json not found. Creating an empty array.'); + return []; + } else { + console.log('Error reading books.json:', error.message); + return []; + } + } } +const books = loadBooks(); function saveBooks(books) { - // TODO: Implement this function - // Write books array to books.json - // Use try-catch for error handling + try { + const booksArray = JSON.stringify(books, null, 2); + fs.writeFileSync('books.json', booksArray, 'utf8'); + } catch (error) { + console.log('Error saving books.json:', error.message); + } } function addBook(book) { - // TODO: Implement this function + const maxId = books.reduce((max, b) => (b.id > max ? b.id : max), 0); + const newBook = { ...book, id: maxId + 1 }; + books.push(newBook); + saveBooks(books); } function getUnreadBooks() { - // TODO: Implement this function using filter() + const unreadBooks = books.filter((book) => !book.read); + return unreadBooks.map((book) => book.title); } function getBooksByGenre(genre) { - // TODO: Implement this function using filter() + const booksByGenre = books.filter( + (book) => book.genre.toLowerCase() === genre.toLowerCase() + ); + return booksByGenre.map((book) => book.title); } function markAsRead(id) { - // TODO: Implement this function using map() + const markRead = books.map((book) => { + if (book.id === id) { + return { ...book, read: true }; + } + return book; + }); + for (let i = 0; i < markRead.length; i++) { + books[i] = markRead[i]; + } + saveBooks(books); + return books; } function getTotalBooks() { - // TODO: Implement this function using length + return books.length; } function hasUnreadBooks() { - // TODO: Implement this function using some() + return books.some((book) => !book.read); } function printAllBooks() { - // TODO: Implement this function - // Loop through and display with chalk - // Use green for read books, yellow for unread - // Use cyan for titles + books.forEach((book) => { + const title = chalk.cyan( + `${book.id}. ${book.title} by ${book.author} (${book.genre})` + ); + if (book.read) { + console.log(chalk.green(`${title} āœ“ Read`)); + } else { + console.log(chalk.yellow(`${title} ⚠ Unread`)); + } + }); } function printSummary() { - // TODO: Implement this function - // Show statistics with chalk - // Display total books, read count, unread count - // Use bold for stats -} \ No newline at end of file + const total = getTotalBooks(); + const readCount = books.filter((book) => book.read).length; + const unreadCount = getUnreadBooks().length; + + console.log(chalk.bold('\nšŸ“Š SUMMARY šŸ“Š')); + console.log(chalk.bold(`Total Books: ${total}`)); + console.log(chalk.bold.green(`Read: ${readCount}`)); + console.log(chalk.bold.yellow(`Unread: ${unreadCount}`)); +} + +export { + loadBooks, + saveBooks, + addBook, + books, + getUnreadBooks, + getBooksByGenre, + markAsRead, + getTotalBooks, + hasUnreadBooks, + printAllBooks, + printSummary, +};