{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"target": "es5",
"lib": ["es2015", "dom", "dom.iterable"],
"noImplicitAny": true,
"strict": true,
"strictFunctionTypes": true,
},
"include": ["./src/**/*"]
}
module.exports = {
root: true,
env: {
browser: true,
node: true,
jest: true,
},
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
plugins: ['prettier', '@typescript-eslint'],
rules: {
'prettier/prettier': [
'error',
{
singleQuote: true,
semi: true,
useTabs: false,
tabWidth: 2,
printWidth: 80,
bracketSpacing: true,
arrowParens: 'avoid',
},
],
'prefer-const': 'off',
},
parserOptions: {
parser: '@typescript-eslint/parser',
},
};
{
"name": "2_address-book",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.5",
"@babel/preset-typescript": "^7.9.0",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"eslint": "^6.8.0",
"eslint-plugin-prettier": "^3.1.2",
"prettier": "^2.0.4",
"typescript": "^3.8.3"
}
}
interface PhoneNumberDictionary {
[phone: string]: {
num: number;
};
}
interface Contact {
name: string;
address: string;
phones: PhoneNumberDictionary;
}
function fetchContacts() {
const contacts = [
{
name: 'Tony',
address: 'Malibu',
phones: {
home: {
num: 11122223333,
},
office: {
num: 44455556666,
},
},
},
{
name: 'Banner',
address: 'New York',
phones: {
home: {
num: 77788889999,
},
},
},
{
name: '마동석',
address: '서울시 강남구',
phones: {
home: {
num: 213423452,
},
studio: {
num: 314882045,
},
},
},
];
return new Promise(resolve => {
setTimeout(() => resolve(contacts), 2000);
});
}
class AddressBook {
contacts = [];
constructor() {
this.fetchData();
}
fetchData() {
fetchContacts().then(response => {
this.contacts = response;
});
}
findContactByName(name) {
return this.contacts.filter(contact => contact.name === name);
}
findContactByAddress(address) {
return this.contacts.filter(contact => contact.address === address);
}
findContactByPhone(phoneNumber, phoneType: string) {
return this.contacts.filter(
contact => contact.phones[phoneType].num === phoneNumber
);
}
addContact(contact) {
this.contacts.push(contact);
}
displayListByName() {
return this.contacts.map(contact => contact.name);
}
displayListByAddress() {
return this.contacts.map(contact => contact.address);
}
}
new AddressBook();
interface PhoneNumberDictionary {
[phone: string]: {
num: string;
};
}
interface Contact {
name: string;
address: string;
phones: PhoneNumberDictionary;
}
enum PhoneType {
Home = 'home',
Office = 'office',
Studio = 'studio'
}
function fetchContacts(): Promise<Contact[]> {
const contacts: Contact[] = [
{
name: 'Tony',
address: 'Malibu',
phones: {
home: {
num: '11122223333',
},
office: {
num: '44455556666',
},
},
},
{
name: 'Banner',
address: 'New York',
phones: {
home: {
num: '77788889999',
},
},
},
{
name: '마동석',
address: '서울시 강남구',
phones: {
home: {
num: '213423452',
},
studio: {
num: '314882045',
},
},
},
];
return new Promise(resolve => {
setTimeout(() => resolve(contacts), 2000);
});
}
class AddressBook {
contacts: Contact[] = [];
constructor() {
this.fetchData();
}
fetchData(): void {
fetchContacts().then(response => {
this.contacts = response;
});
}
findContactByName(name: string): Contact[] {
return this.contacts.filter(contact => contact.name === name);
}
findContactByAddress(address: string): Contact[] {
return this.contacts.filter(contact => contact.address === address);
}
findContactByPhone(phoneNumber: string, phoneType: PhoneType): Contact[] {
return this.contacts.filter(
contact => contact.phones[phoneType].num === phoneNumber
);
}
addContact(contact: Contact): void {
this.contacts.push(contact);
}
displayListByName(): string[] {
return this.contacts.map(contact => contact.name);
}
displayListByAddress(): string[] {
return this.contacts.map(contact => contact.address);
}
}
const addrBook = new AddressBook();