Skip to content

Commit

Permalink
Merge pull request #31 from 5-gwoap/release/0.4.0
Browse files Browse the repository at this point in the history
Release/0.4.0
  • Loading branch information
rainrivas authored Feb 22, 2018
2 parents db8a479 + 9e911b8 commit d447592
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 48 deletions.
127 changes: 81 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,95 @@ const program = require('commander');
const request = require('request');

const consoleWidth = () => {
return parseInt(process.stdout.columns)
}
return parseInt(process.stdout.columns);
};

program
.version('0.3.0')
.arguments('<phrase>')
.action((slng) => {
var options = {
method: 'GET',
url: 'http://api.urbandictionary.com/v0/define',
qs: {
term: slng
},
headers: {
'Cache-Control': 'no-cache',
Accept: 'application/json'
}
const printResults = (resList) => {
resList.forEach((result) => {
if (typeof (result.definition) !== undefined) {
console.log(chalk.bold.cyan('Word: ') + result.word);
console.log(chalk.bold.cyan('Definition: ') + result.definition);
console.log(chalk.bold.cyan('Score: ') + (result.thumbs_up - result.thumbs_down));
console.log(chalk.bold.green('Ayys: ') + result.thumbs_up + ' | ' + chalk.bold.red('Nayys: ') + result.thumbs_down);
console.log('='.repeat(consoleWidth()));
}
});
};

const getDefinition = (word) => {
const options = {
method: 'GET',
url: 'http://api.urbandictionary.com/v0/define',
qs: {
term: word
},
headers: {
'Cache-Control': 'no-cache',
Accept: 'application/json'
}
};

request(options, function (err, res, body) {
if (err) throw new Error(err);
var trimRes;
var results = JSON.parse(body).list;
var resultsToDisplay = 3;
request(options, function (err, res, body) {
if (err) throw new Error(err);
var trimRes;
const results = JSON.parse(body).list;
const resultsToDisplay = 3;

console.log('='.repeat(consoleWidth()));
console.log('='.repeat(consoleWidth()));

if (results.length === 0) {
console.log(chalk.red('No results were found, please try another phrase'));
if (results.length === 0) {
console.log(chalk.red('No results were found, please try another phrase'));
} else {
if (results.length > resultsToDisplay) {
trimRes = results.slice(0, resultsToDisplay);
} else {
if (results.length > resultsToDisplay) {
trimRes = results.slice(0, resultsToDisplay);
} else {
trimRes = results;
}
trimRes.forEach((result) => {
if (typeof (result.definition) !== undefined) {
console.log(chalk.bold.cyan('Word: ') + result.word);
console.log(chalk.bold.cyan('Definition: ') + result.definition);
console.log(chalk.bold.cyan('Score: ') + (result.thumbs_up - result.thumbs_down));
console.log(chalk.bold.green('Ayys: ') + result.thumbs_up + ' | ' + chalk.bold.red('Nayys: ') + result.thumbs_down);
console.log('='.repeat(consoleWidth()));
}
});
trimRes = results;
}
});

printResults(trimRes);
}
});
};

program
.version('0.4.0')
.option('-R, --random', 'Display top results for a random word (up to 3), cannot be used when passing a phrase')
.arguments('<phrase>')
.action((slng) => {
getDefinition(slng);
});

program.on('--help', function () {
console.log('');
console.log(' Examples:');
console.log(' $ slng gucci');
console.log(' $ slng \'square up\'');
console.log('');
});
program
.on('--help', function () {
console.log('');
console.log(' Examples:');
console.log(' $ slng gucci');
console.log(' $ slng \'square up\'');
console.log('');
});

program.parse(process.argv);

if (!process.argv.slice(2).length) {
program.outputHelp();
}

if(program.random && process.argv.slice(2).length === 1) {
const options = {
method: 'GET',
url: 'http://api.urbandictionary.com/v0/random',
headers: {
'Cache-Control': 'no-cache',
Accept: 'application/json'
}
};

request(options, function (err, res, body) {
const results = JSON.parse(body).list;
const randomWord = results[0].word;

if (err) throw new Error(err);

getDefinition(randomWord);
});
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "slng",
"version": "0.3.0",
"version": "0.4.0",
"description": "Get hip or die trying",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit d447592

Please sign in to comment.