Created
November 12, 2018 17:45
-
-
Save alifrizkip/ba62ec9933109aab466baa2d0524cfa5 to your computer and use it in GitHub Desktop.
sortCommentWithTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const readline = require('readline-sync'); | |
class AppClass { | |
constructor(data) { | |
this.data = [...data]; | |
this.pageNumber = 1; | |
this.pageSize = 10; | |
this.tempData = []; | |
this.isSearch = false; | |
} | |
sortBy(key, direction = 'asc') { | |
const callBackSort = (_key, _direction) => { | |
return (a, b) => (_direction === 'asc') ? a[_key].localeCompare(b[_key]) : b[_key].localeCompare(a[_key]); | |
} | |
this.data.sort(callBackSort(key, direction)); | |
console.log(`Sorting data by ${key} - ${direction}`); | |
} | |
formatComment(comment) { | |
const { id, name, email, body } = comment; | |
let formatedStr = ''; | |
formatedStr += `- ID : ${id}\n`; | |
formatedStr += `- Name : ${name}\n`; | |
formatedStr += `- Email : ${email}\n`; | |
formatedStr += `- Body : ${body}\n` | |
formatedStr += '========================================'; | |
return formatedStr; | |
} | |
printWithPaginate(pageNumber = 1, commentData = this.data) { | |
if (commentData.length === 0) return console.log('Data empty'); | |
this.pageNumber = pageNumber - 1; | |
let datas = commentData.slice(this.pageNumber * this.pageSize, (this.pageNumber+1) * this.pageSize); | |
datas.map(this.formatComment).forEach(comment => console.log(comment)); | |
} | |
searchComment(keyword) { | |
let filteredComments = this.data.filter(comment => { | |
return comment.body.toUpperCase().includes(keyword.toUpperCase()); | |
}); | |
return filteredComments; | |
} | |
setTempData(data) { | |
this.isSearch = true; | |
this.tempData = [...data]; | |
} | |
} | |
const init = data => { | |
console.log('...::: Get data comments successfully :::...\n'); | |
return new AppClass(data); | |
} | |
const selectMenu = APP => { | |
console.log('Sort options: '); | |
console.log('1 name (A-Z)'); | |
console.log('2 name (Z-A)'); | |
console.log('3 email (A-Z)'); | |
console.log('4 email (Z-A)'); | |
console.log('5 search'); | |
// let input = readline.question('Select sort options: '); | |
let input = readlineWithProxy.question('Select sort options: '); | |
switch (input) { | |
case '1': | |
APP.sortBy('name', 'asc'); | |
break; | |
case '2': | |
APP.sortBy('name', 'desc'); | |
break; | |
case '3': | |
APP.sortBy('email', 'asc'); | |
break; | |
case '4': | |
APP.sortBy('email', 'desc'); | |
break; | |
case '5': | |
let keyword = readline.question('Input keyword: '); | |
let filteredData = APP.searchComment(keyword); | |
APP.setTempData(filteredData); | |
break; | |
} | |
return APP; | |
} | |
const selectPage = APP => { | |
let isKeepRunning = true; | |
while (isKeepRunning === true) { | |
console.log('\n\n'); | |
// let pageNumber = readline.question('Input page number: '); | |
let pageNumber = Number(readlineWithProxy.question('Input page number: ')); | |
// switch (pageNumber) { | |
// case 'exit': | |
// isKeepRunning = false; | |
// break; | |
// default: | |
// pageNumber = Number(pageNumber); | |
// if (APP.isSearch === true) { | |
// APP.printWithPaginate(pageNumber, APP.tempData); | |
// } else { | |
// APP.printWithPaginate(pageNumber); | |
// } | |
// break; | |
// } | |
if (isNaN(pageNumber) === false) { | |
if (APP.isSearch === true) { | |
APP.printWithPaginate(pageNumber, APP.tempData); | |
} else { | |
APP.printWithPaginate(pageNumber); | |
} | |
} else { | |
isKeepRunning = false | |
} | |
} | |
} | |
const URL = 'http://jsonplaceholder.typicode.com/comments'; | |
axios.get(URL) | |
.then(res => init(res.data)) | |
.then(APP => selectMenu(APP)) | |
.then(APP => selectPage(APP)) | |
.catch(err => { | |
console.log('Error'); | |
console.log(err); | |
}) | |
// ================================================ | |
let isTesting = false; | |
class ReadlineWithProxyClass { | |
constructor() { | |
this.data = {}; | |
} | |
addQuestion(q, a) { | |
this.data[q] = a; | |
} | |
removeQuestion(q) { | |
delete this.data[q]; | |
} | |
question(question) { | |
if (isTesting) { | |
return this.data[question]; | |
} else { | |
return readline.question(question); | |
} | |
} | |
} | |
let readlineWithProxy = new ReadlineWithProxyClass(); | |
const testEverything = () => { | |
isTesting = true | |
const cases = [{ | |
title: "Case 1 is user menu input unavailable", | |
data: { | |
question: 'Select sort options: ', | |
answer: '6' | |
}, | |
testFunction: testSelectMenu, | |
before_test: function(){ | |
readlineWithProxy.addQuestion(this.data.question, this.data.answer); | |
}, | |
after_test: function(){ | |
readlineWithProxy.removeQuestion(this.data.question); | |
} | |
}, { | |
title: "Case 2 is input page number not a number", | |
data: { | |
question: 'Input page number: ', | |
answer: 'bukalapak', | |
}, | |
testFunction: testSelectPage, | |
before_test: function(){ | |
readlineWithProxy.addQuestion(this.data.question, this.data.answer); | |
}, | |
after_test: function(){ | |
readlineWithProxy.removeQuestion(this.data.question); | |
} | |
}, { | |
title: 'Case 3 is sort by name ascending', | |
data: { | |
sortByKey: 'name', | |
sortDirection: 'asc', | |
pageNumber: 1 | |
}, | |
testFunction: testSortBy, | |
before_test: function(){}, | |
after_test: function(){} | |
}]; | |
let APP = new AppClass([ | |
{ | |
"postId": 1, | |
"id": 1, | |
"name": "repellat consequatur praesentium", | |
"email": "Eliseo@gardner.biz", | |
"body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium" | |
}, | |
{ | |
"postId": 2, | |
"id": 7, | |
"name": "repellat consequatur praesentium vel minus molestias voluptatum", | |
"email": "", | |
"body": "maiores sed dolores similique labore et inventore et\nquasi temporibus esse sunt id et\neos voluptatem aliquam\naliquid ratione corporis molestiae mollitia quia et magnam dolor" | |
} | |
]); | |
cases.forEach(( itemCase ) => { | |
itemCase.before_test() | |
itemCase.testFunction(itemCase.data, itemCase.title, {APP}); | |
itemCase.after_test() | |
}) | |
isTesting = false | |
} | |
const testSelectMenu = (caseData, caseTitle, extra) => { | |
console.log(' --- Test selectMenu ---'); | |
console.log(caseTitle); | |
console.log(' ------ '); | |
selectMenu(extra.APP); | |
} | |
const testSelectPage = (caseData, caseTitle, extra) => { | |
console.log(' --- Test selectPage --- '); | |
console.log(caseTitle); | |
console.log(' ------ '); | |
selectPage(extra.APP); | |
} | |
const testSortBy = (caseData, caseTitle, extra) => { | |
console.log(' --- Test sortBy --- '); | |
console.log(caseTitle); | |
console.log(' ------ '); | |
extra.APP.sortBy(caseData.sortByKey, caseData.sortDirection); | |
extra.APP.printWithPaginate(caseData.pageNumber); | |
} | |
// ==== | |
testEverything(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment