Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
jokosanyang committed Mar 20, 2019
2 parents 0a8027e + 5f3f878 commit 8c7b740
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "agony-yaunt",
"version": "1.0.0",
"description": "",
"main": "dom.js",
"main": "./src/scripts/functions/dom.js",
"scripts": {
"test": "tape ./src/scripts/tests/*.test.js | tap-spec",
"coverage": "istanbul cover ./src/scripts/tests/*.test.js"
Expand All @@ -14,6 +14,7 @@
"istanbul": "^0.4.5",
"nodemon": "^1.18.10",
"tap-spec": "^5.0.0",
"tape": "^4.10.1"
"tape": "^4.10.1",
"xmlhttprequest": "^1.8.0"
}
}
75 changes: 75 additions & 0 deletions src/scripts/functions/get-advice-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

function hasResult(obj) {
const max = parseInt(obj.total_results);
const idNum = max === NaN ? 0 : Math.floor(Math.random() * Math.floor(max));
console.log(obj.slips[idNum].advice);
return obj.slips[idNum].advice;
}

function randomResult(obj) {
console.log(obj.slip.advice);
return obj.slip.advice;
}

function noResult(string) {
let url = "https://api.adviceslip.com/advice";
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
let xhrObj = JSON.parse(xhr.responseText);
const result = xhrObj.slip.advice;

console.log(
`No thoughts on ${string}, I have... but hear someone say once, I did: ${result}`
);
return `No thoughts on ${string}, I have... but hear someone say once, I did: ${result}`;
}
};
xhr.open("GET", url, true);
xhr.send();

// getAdvice;
// let newAdvice = function(cb) {
// return cb("");
// };
// console.log({ newAdvice });
// console.log(
// `No thoughts on ${string}, I have... but hear someone say once, I did: ${newAdvice(
// getAdvice
// )}`
// );
// return `No thoughts on ${string}, I have... but hear someone say once, I did: ${newAdvice(
// getAdvice
// )}`;
}

function getAdvice(string) {
let url =
string.length === 0
? "https://api.adviceslip.com/advice"
: `https://api.adviceslip.com/advice/search/${string}`;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
let xhrObj = JSON.parse(xhr.responseText);
if (xhrObj.hasOwnProperty("message")) {
noResult(string);
} else if (xhrObj.hasOwnProperty("total_results")) {
hasResult(xhrObj);
} else {
randomResult(xhrObj, string);
}
}
};
xhr.open("GET", url, true);
xhr.send();
}

module.exports = getAdvice;
// getAdvice("");
// getAdvice("cloud");
// getAdvice("cats");
// getAdvice("hand");
// getAdvice("love");
getAdvice("heart");
9 changes: 8 additions & 1 deletion src/scripts/tests/function.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const test = require('tape');
const yodariser = require('../functions/yodariser.js');
const getAdviceApi = require("../functions/get-advice-api.js");
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

const dummyAdvice = "Burn the spreadsheets";
Expand All @@ -19,10 +20,16 @@ test("Testing yodariser", function (t) {
// t.end();
// });

test("Function returns a url", function(t) {
test("Yodarise function returns a url", function(t) {
const actual = yodariser(dummyAdvice);
const expected = "http://yoda-api.appspot.com/api/v1/yodish?text=Burn%20the%20spreadsheets";
t.equal(actual,expected, "Quote is formatted");
t.end();
});

test("Test advice function", t => {
const actual = getAdviceApi("");
const expected = "string";
t.equal(actual, expected, "function should fetch a string from the api");
t.end();
});

0 comments on commit 8c7b740

Please sign in to comment.