12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- const fs = require('fs');
- const Table = require('cli-table3');
- const chalk = require('chalk');
- const justify = require('justified');
- const fileName = 'notes-data.json';
- const printNote = (note) => {
- const title = chalk.inverse.bold.red;
- const body = chalk.bold.inverse.white;
- console.log(title(justify(note.title)));
- console.log(body(justify(note.body)));
- };
- const printNoteList = (notelist) => {
- var table = new Table({
- head: ['Title', 'Body'],
- colWidths: [25, 100]
- });
- notelist.forEach((note) => table.push([note.title, note.body]))
- console.log(table.toString());
- };
- const fetchNotes = () => {
- var notesString;
- try {
- notesString = fs.readFileSync(fileName);
- return JSON.parse(notesString);
- } catch (error) {
- return [];
- }
- };
- const saveNotes = (notes) => {
- fs.writeFileSync(fileName, JSON.stringify(notes));
- };
- const addNote = (title, body) => {
- var notes = fetchNotes(),
- note = { title, body };
-
- var duplicateNotes = notes.filter(note => note.title === title);
- if (duplicateNotes.length === 0) {
- notes.push(note);
- saveNotes(notes);
- return note;
- }
- };
- const removeNote = (title) => {
- var notes = fetchNotes();
- var filteredNotes = notes.filter(note => note.title !== title);
- saveNotes(filteredNotes);
- return notes.length !== filteredNotes.length;
- }
- const getNote = (title) => {
- let notes = fetchNotes();
- var note = notes.find(n => n.title === title);
- return note;
- };
- const getAll = () => {
- return fetchNotes();
- };
- module.exports = {
- addNote,
- getNote,
- getAll,
- removeNote,
- printNote,
- printNoteList
- };
|