The getJoke
function imports required modules and makes a GET request to a web page to retrieve a list of jokes, extracting the questions and answers using regular expressions. It then returns a random joke from the list, or resolves with the existing joke data if it has already been loaded.
npm run import -- "tell joke"
var util = require('bluebird');
var request = util.promisify(require('request'));
var importer = require('../Core')
var jokes;
function getJoke() {
// TODO: collect jokes instead
return (typeof jokes === 'undefined'
? request('http://www.ducksters.com/jokes/silly.php')
.then(res => importer.regexToArray(/^.*?Q:.*$|^.*?A:.*$/igm, res.body))
.then(r => {
r = r.reduce((arr, j, i) => {
if(i % 2 === 1) {
arr.push([
r[i-1].replace(/<.*?\s*\/?>/ig, '').trim().replace(/^\s*|\s*$/igm, ''),
j.replace(/<.*?\s*\/?>/ig, '').trim().replace(/^\s*|\s*$/igm, '')
]);
}
return arr;
}, []);
console.log(r);
jokes = r;
return r;
})
: Promise.resolve(jokes))
.then(arr => {
const i = Math.round(Math.random() * arr.length);
return arr[i];
})
}
module.exports = getJoke;
```javascript
const request = require('request'). defaults({ jar: true });
const { regexToArray } = require('../Core');
/**
* Returns a random joke from a collection of silly jokes.
* @returns {Promise<Array<string>>} A promise resolving to an array of jokes.
*/
function getJoke() {
// Cache jokes to avoid repeated requests
const cacheKey ='silly-jokes';
const cachedJokes = globalThis.localStorage.getItem(cacheKey);
if (cachedJokes) {
return Promise.resolve(JSON.parse(cachedJokes));
}
return new Promise((resolve, reject) => {
request('http://www.ducksters.com/jokes/silly.php', (error, response, body) => {
if (error) {
reject(error);
} else if (response.statusCode === 200) {
const regex = /^.*?Q:.*$|^.*?A:.*$/igm;
const jokes = regexToArray(regex, body);
// Pair up question and answer
const pairedJokes = jokes.reduce((arr, j, i) => {
if (i % 2 === 1) {
arr.push([j.replace(/<.*?\s*\/?>/ig, '').trim().replace(/^\s*|\s*$/igm, ''), jokes[i - 1].replace(/<.*?\s*\/?>/ig, '').trim().replace(/^\s*|\s*$/igm, '')]);
}
return arr;
}, []);
// Store jokes in cache
globalThis.localStorage.setItem(cacheKey, JSON.stringify(pairedJokes));
resolve(pairedJokes);
} else {
reject(new Error(`Request failed with status code ${response.statusCode}`));
}
});
})
.then((jokes) => {
const randomJokeIndex = Math.floor(Math.random() * jokes.length);
return jokes[randomJokeIndex];
});
}
module.exports = getJoke;
```
var util = require('bluebird');
var request = util.promisify(require('request'));
var importer = require('../Core')
bluebird
library is imported and assigned to the util
variable.request
module is imported and its promisify
method is used to convert it into a Promise-based function.importer
module is imported from a relative path.getJoke
Functionvar jokes;
function getJoke() {
//...
}
getJoke
function is defined.jokes
variable is declared.return (typeof jokes === 'undefined'
? request('http://www.ducksters.com/jokes/silly.php')
.then(res => importer.regexToArray(/^.*?Q:.*$|^.*?A:.*$/igm, res.body))
.then(r => {
//...
})
: Promise.resolve(jokes))
.then(arr => {
//...
})
jokes
is undefined, the function makes a GET request to http://www.ducksters.com/jokes/silly.php
using the request
function.importer.regexToArray
function, which extracts questions and answers from the response body.jokes
variable.jokes
is defined, the function resolves with the existing jokes
data..then(arr => {
const i = Math.round(Math.random() * arr.length);
return arr[i];
})
i
is generated based on the length of the arr
array.i
is returned.module.exports = getJoke;
getJoke
function is exported as a module.