-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.js
61 lines (54 loc) · 1.5 KB
/
notes.js
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
import fs from 'fs'
import {Logger} from './logger'
export function addNote (title, body) {
const notes = loadNotes()
const duplicates = notes.filter((book) => book.title === title)
console.log('kuku')
debugger
if (!duplicates.length) {
notes.push({ title, body })
saveNotes(notes)
Logger.success('New note was added')
} else {
Logger.warrning('Note title was taken')
}
}
export function removeNote(title) {
const notes = loadNotes()
const noteIndex = notes.findIndex((book) => book.title === title)
if (noteIndex !== -1) {
notes.splice(noteIndex, 1)
saveNotes(notes)
Logger.success('Note was removed')
} else {
Logger.warrning('Note with given title doesn`t exists')
}
}
export function listNotes() {
const notes = loadNotes()
notes.forEach(note => {
Logger.success(note.title)
})
}
export function readNote(title) {
const notes = loadNotes()
const note = notes.find((book) => book.title === title)
if (!note) {
Logger.warrning('Note title was taken')
} else {
Logger.success(`Title ${note.title} \nBody ${note.body}`)
}
}
function loadNotes() {
try {
const dataBuffer = fs.readFileSync('notes.json')
const dataJson = dataBuffer.toString()
return JSON.parse(dataJson)
} catch (e) {
return []
}
}
function saveNotes(notes) {
const dataJSON = JSON.stringify(notes)
fs.writeFileSync('notes.json', dataJSON)
}