-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (41 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* If run directly, print a space separated list of all wikipedia misspelled
* words.
*
* If required, return a promise for the words.
*/
var promiseHtml = require('promiseHtml'),
Promise = require('promise'),
_ = require('lodash'),
cheerio = require('cheerio'),
assert = require('assert');
var baseUrl =
'https://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings/';
var letters = _.map(new Array(26), function(value, index) {
return String.fromCharCode('A'.charCodeAt(0) + index);
});
// multiple promises for arrays of words
var wordsPs = _.map(letters, function(letter) {
return promiseHtml(baseUrl + letter)
.then(function(html) {
var $ = cheerio.load(html);
return $('#mw-content-text ul').last().find('li')
.map(function(i, elm) {
var text = $(elm).text();
var idx = text.indexOf(' ');
assert.notStrictEqual(idx, -1);
return text.substring(0, idx);
})
.toArray();
});
});
var wordsP = Promise.all(wordsPs).then(_.flatten)
if (module.parent) {
module.exports = wordsP;
} else {
// console.log(letters.join(' '));
wordsP.then(function(words) {
return words.join(' ');
})
.then(console.log.bind(console));
}