forked from danielebarbaro/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-notes.mjs
89 lines (75 loc) · 2.02 KB
/
core-notes.mjs
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
import * as fs from 'fs';
import chalk from 'chalk';
const findNote = function (notes, title) {
return notes.find(note => note.title === title);
}
const displayNote = function (note) {
console.log('Nota \n\t', `${chalk.red(note.title)}: ${chalk.green(note.body)}`, '\n')
}
const errorHelper = function () {
console.log('\n\t', chalk.red('Nota non trovata.'), '\n');
}
const addNote = function (title, body) {
const notes = loadNotes();
const noteExist = findNote(notes, title);
if (!noteExist) {
notes.push({
title: title,
body: body
})
// console.log('posso inserire la nota', '\n')
// console.log('ora le note sono state modificare: ', notes)
saveNotes(notes);
} else {
chalk.red('Non puoi inserire la nota, esiste già.', '\n')
}
}
const removeNote = function (title) {
const notes = [];
const notesToKeep = [];
if (true) {
//
} else {
//
}
}
const listNotes = function () {
const notes = loadNotes();
console.log('Note:\t');
notes.map(note => {
displayNote(note); // o in alternativa le righe seguenti:
// const title = chalk.green(note.title);
// const body = chalk.red(note.body);
// console.log('\t', `${title} - ${body}`, '\n');
})
// console.log(notes, chalk('questo messaggio Lista note'), '\n');
}
const readNote = function (title) {
const notes = [];
const noteExist = findNote(notes, title);
if (noteExist) {
//
} else {
//
}
}
const saveNotes = function (notes) {
const dataJSON = JSON.stringify(notes)
fs.writeFileSync('database/notes.json', dataJSON)
}
const loadNotes = function () {
try {
const data = fs.readFileSync('database/notes.json')
const result = data.toString()
return JSON.parse(result)
} catch (e) {
// console.log('ERRORE file non trovato', e.message)
return []
}
}
export {
addNote,
removeNote,
listNotes,
readNote
}