-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.js
More file actions
72 lines (63 loc) · 1.74 KB
/
notes.js
File metadata and controls
72 lines (63 loc) · 1.74 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
const fs = require('fs')
const chalk = require('chalk')
// Add a note
const addNotes = (title, body) => {
const notes = loadNotes()
const dualNote = notes.find(note => note.title === title)
if (!dualNote) {
notes.push({title, body})
saveNotes(notes)
console.log(chalk.greenBright('Note Added Successfully!'))
} else {
console.log(chalk.redBright(`Title "${title}" already exists`))
}
}
// List All Notes
const listNotes = () => {
const notes = loadNotes();
console.log(chalk.italic.magentaBright('Listing All Notes . . .'))
notes.forEach( note => {
console.log(`${chalk.yellowBright(note.title)} ${chalk.cyanBright(note.body)}`)
})
}
// Remove a note
const removeNote = (title) => {
const notes = loadNotes();
const filteredNotes = notes.filter(notes => notes.title !== title)
if (notes.length === filteredNotes.length) {
console.log(chalk.redBright(`Note "${title}" not found!`))
} else {
saveNotes(filteredNotes)
console.log(chalk.whiteBright.inverse('Note removed'))
}
}
// Read one Note
const readNote = (title) => {
const notes = loadNotes()
const note = notes.find(note => note.title === title)
if (note) {
console.log(`${chalk.yellowBright(note.title)} ${chalk.cyanBright(note.body)}`)
} else {
console.log(chalk.redBright(`Note "${title}" not found!`))
}
}
// Save notes
const saveNotes = (notes) => {
fs.writeFileSync('notes.json', JSON.stringify(notes))
}
// Load a notes
const loadNotes = () => {
try {
const dataBuffer = fs.readFileSync('notes.json')
const data = dataBuffer.toString()
return JSON.parse(data)
} catch (error) {
return []
}
}
module.exports = {
addNotes: addNotes,
listNotes: listNotes,
removeNote: removeNote,
readNote: readNote
}