-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
28 lines (23 loc) 路 769 Bytes
/
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
import "isomorphic-fetch";
import promise from "es6-promise";
promise.polyfill();
const randomWordWikipedia = (lang, n = 1) => {
return new Promise(function (resolve, reject) {
lang = lang || "en";
if (typeof lang !== "string") {
throw new TypeError(`Expected a string, got ${typeof lang}`);
}
if (n <= 0 || n > 10) {
throw new TypeError(`Expected a -n (1 - 10), got ${n}`);
}
// rnlimit: API limit 10 or less
const url = `https://${lang}.wikipedia.org/w/api.php?format=json&action=query&list=random&rnnamespace=0&rnlimit=${n}`;
const res = fetch(url)
.then((res) => res.json())
.then((data) => {
const words = data.query.random;
resolve(words.slice(-n).map((v) => v.title));
});
});
};
export default randomWordWikipedia;