-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.js
87 lines (69 loc) · 2.56 KB
/
contacts.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
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
const fs = require("fs");
const chalk = require("chalk");
const validator = require("validator");
const dirPath = "./data";
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
const dataPath = "./data/contacts.json";
if (!fs.existsSync(dataPath)) {
fs.writeFileSync(dataPath, "[]", "utf-8");
}
const loadContact = () => {
const fileBuffer = fs.readFileSync("data/contacts.json", "utf-8");
const contacts = JSON.parse(fileBuffer);
return contacts;
};
const saveContact = (name, email, noHP) => {
const contact = { name, email, noHP };
const contacts = loadContact();
const duplicate = contacts.find((contact) => contact.email.toLowerCase() === email.toLowerCase());
if (duplicate) {
console.log(chalk.red.bold("Email address has already taken. Please use another email!"));
return false;
}
if (email) {
if (!validator.isEmail(email)) {
console.log(chalk.red.bold("Invalid email format. Please input a valid email!"));
return false;
}
}
if (!validator.isMobilePhone(noHP, "id-ID")) {
console.log(chalk.red.bold("Invalid phone number format. Please input a valid phone number!"));
return false;
}
contacts.push(contact);
fs.writeFileSync("data/contacts.json", JSON.stringify(contacts, null, 2));
console.log(chalk.green.bold("Thank you for inputting the data!"));
};
const listContact = () => {
const contacts = loadContact();
console.log(chalk.cyan.bold("Contact Lists"));
contacts.forEach((contact, i) => {
console.log(chalk.green.bold`${i + 1}. ${contact.name} - ${contact.email}`);
});
};
const detailContact = (name) => {
const contacts = loadContact();
const contact = contacts.find((contact) => contact.name.toLowerCase() === name.toLowerCase());
if (!contact) {
console.log(chalk.red.bold`Contact with name ${name} not found.`);
return false;
}
console.log(chalk.cyan.bold(contact.name));
console.log(chalk.green.bold`Phone Number: ${contact.noHP}`);
if (contact.email) {
console.log(chalk.green.bold`Email Address: ${contact.email}`);
}
};
const deleteContact = (name) => {
const contacts = loadContact();
const newContact = contacts.filter((contact) => contact.name.toLowerCase() !== name.toLowerCase());
if (contacts.length === newContact.length) {
console.log(chalk.red.bold`Contact with name ${name} not found.`);
return false;
}
fs.writeFileSync("data/contacts.json", JSON.stringify(newContact, null, 2));
console.log(chalk.green.bold`Data Contact ${name} successfully deleted!`);
};
module.exports = { saveContact, listContact, detailContact, deleteContact };