-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
97 lines (83 loc) · 2.58 KB
/
notes.js
File metadata and controls
97 lines (83 loc) · 2.58 KB
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
94
95
96
97
//imports
const fs = require('fs')
const chalk = require('chalk')
//chalk colors
const errorColor = chalk.red.bold;
const successColor = chalk.green.bold;
const systemColor = chalk.blue.bold;
const inverse = chalk.inverse.bold
//reusables
const loadnotes = () => {
try {
const dataBuffer = fs.readFileSync('notes.json');
const dataJSON = dataBuffer.toString();
return JSON.parse(dataJSON)
} catch (e) {
return []
}
};
const saveNotes = (notes) => {
const dataJSON = JSON.stringify(notes);
fs.writeFileSync('notes.json', dataJSON)
};
const findNote = (notes, testTitle) => notes.find(note => note.title.toUpperCase() === testTitle.toUpperCase())
//Command Handler functions
//add
const addNote = (newTitle, newBody) => {
const notes = loadnotes()
const duplicateFound = findNote(notes, newTitle)
if (duplicateFound) {
console.log(`${errorColor('\nERROR: Title already taken')}: '${newTitle}'\n`);
} else {
notes.push({
title: newTitle,
body: newBody
})
saveNotes(notes);
console.log(`${successColor('\nNew note added')}: '${newTitle}'\n`);
}
};
//remove
const removeNote = (title) => {
let notes = loadnotes();
const duplicateFound = findNote(notes, title)
if (duplicateFound) {
notes.forEach((note, i) => {
if (note.title.toUpperCase() === title.toUpperCase()) {
notes.splice(i, 1)
console.log(`\n${successColor('Note removed')}: '${note.title}'\n`);
}
})
} else {
console.log(`${errorColor(`\nERROR: Note not found`)}: '${title}'\n`);
}
saveNotes(notes)
}
//list
const listNotes = () => {
const notes = loadnotes();
if (notes.length) {
console.log('\nYour Notes:\n');
notes.forEach((note, i) => console.log(`${i + 1}: '${systemColor(note.title)}'\n`));
} else {
console.log(`${errorColor('\nNo notes found.\n')}${systemColor('\nAdd notes by typing:')} node app.js add --title="your title" --body="your text"`);
}
}
//read
const readNote = (title) => {
const notes = loadnotes()
const note = findNote(notes, title)
if (note) {
console.log(successColor('\nNote found!'));
console.log(`\n${inverse(note.title)}\n\n${note.body}\n`);
} else {
console.log(errorColor(`\nNote '${title}' not found!\n`));
console.log(`${systemColor('List all your notes with:')} node app.js list\n`);
}
}
module.exports = {
addNote: addNote,
removeNote: removeNote,
listNotes: listNotes,
readNote: readNote
};