-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscribbblr.js
More file actions
90 lines (75 loc) · 2.32 KB
/
scribbblr.js
File metadata and controls
90 lines (75 loc) · 2.32 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
const fs = require('fs')
const chalk = require('chalk')
const fetchScribbbles = () => {
try {
const scribbbles = fs.readFileSync('./scribbbles.json', 'utf8')
return JSON.parse(scribbbles)
} catch (err) {
return []
}
}
let scribbblesArr = fetchScribbbles()
const listScribbbles = () => {
if (scribbblesArr.length < 1) {
console.log(chalk.inverse.red('There are no scribbbles yet.'))
} else {
console.log(chalk.blue('Here are your scribbbles!\n'))
scribbblesArr.forEach(scribbble => {
console.log(`Title: ${scribbble.title}`)
console.log(`Content: ${scribbble.content}`)
console.log(`Date: ${scribbble.date}`)
console.log(chalk.green('--------------------------------'))
})
}
}
const newScribbble = (title, content, date) => {
if (findDuplicateTitle(title)) {
console.log(
chalk.red('This scribbble title already exist. Please try another.')
)
} else {
scribbblesArr.push({ title: title, content: content, date: date })
storeScribbbles(scribbblesArr)
console.log(chalk.green('New scribbble saved successfully.'))
}
}
const findDuplicateTitle = title => {
return scribbblesArr.find(scribbble => scribbble.title === title)
}
const storeScribbbles = scribbbles => {
const data = JSON.stringify(scribbbles)
fs.writeFileSync('scribbbles.json', data)
}
const findScribbble = title => {
const scribbble = getScribbble(scribbblesArr, title)
if (scribbble) console.log(showScribbbleDetails(scribbble))
else console.log('Scribbble does not exist.')
}
const getScribbble = (scribbbles, title) => {
return scribbbles.find(scribbble => scribbble.title === title)
}
const showScribbbleDetails = scribbble => {
const details = `
Title: ${scribbble.title}
Content: ${scribbble.content}
Date: ${scribbble.date}
`
return details
}
const deleteScribbble = title => {
const scribbblesToKeep = scribbblesArr.filter(
scribbble => scribbble.title !== title
)
if (scribbblesArr.length === scribbblesToKeep.length)
console.log(chalk.red('Scribbble does not exist.'))
else {
storeScribbbles(scribbblesToKeep)
console.log(chalk.inverse.green('Scribbble has been deleted.'))
}
}
module.exports = {
listScribbbles: listScribbbles,
newScribbble: newScribbble,
findScribbble: findScribbble,
deleteScribbble: deleteScribbble,
}