random-word-wikipedia 1.0.5 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.js +31 -20
- package/index.js +15 -22
- package/package.json +9 -13
- package/readme.md +40 -42
- package/lib/cli.js +0 -19
- package/lib/index.js +0 -37
package/cli.js
CHANGED
|
@@ -1,31 +1,42 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
const meow = require("meow");
|
|
4
|
-
const randomWordWikipedia = require(".");
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
$ random-word-wikipedia [lang]
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import randomWordWikipedia from "./index.js";
|
|
9
5
|
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
const help = `
|
|
7
|
+
Usage
|
|
8
|
+
$ random-word-wikipedia [lang=en]
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
YuruYuri
|
|
10
|
+
Options
|
|
11
|
+
-n Number of words, 10 or less [Default: 1]
|
|
16
12
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
Examples
|
|
14
|
+
$ random-word-wikipedia
|
|
15
|
+
$ random-word-wikipedia ja -n 3
|
|
16
|
+
`;
|
|
21
17
|
|
|
22
|
-
|
|
18
|
+
const { values, positionals } = parseArgs({
|
|
19
|
+
allowPositionals: true,
|
|
20
|
+
options: {
|
|
21
|
+
n: { type: "string", short: "n" },
|
|
22
|
+
help: { type: "boolean", short: "h" }
|
|
23
|
+
}
|
|
24
|
+
});
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
.
|
|
26
|
+
if (values.help) {
|
|
27
|
+
console.log(help);
|
|
28
|
+
process.exit(0);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const lang = positionals[0];
|
|
32
|
+
const n = values.n === undefined ? 1 : Number(values.n);
|
|
33
|
+
|
|
34
|
+
randomWordWikipedia(lang, n)
|
|
35
|
+
.then((res) => {
|
|
26
36
|
console.log(res.join("\n"));
|
|
27
37
|
})
|
|
28
|
-
.catch(err => {
|
|
29
|
-
console.error(
|
|
38
|
+
.catch((err) => {
|
|
39
|
+
console.error(`Error: ${err.message}`);
|
|
30
40
|
console.error("$ random-word-wikipedia --help");
|
|
41
|
+
process.exit(1);
|
|
31
42
|
});
|
package/index.js
CHANGED
|
@@ -1,27 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const randomWordWikipedia = async (lang = "en", n = 1) => {
|
|
2
|
+
if (typeof lang !== "string") {
|
|
3
|
+
throw new TypeError(`Expected a string, got ${typeof lang}`);
|
|
4
|
+
}
|
|
3
5
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if (typeof lang !== "string") {
|
|
8
|
-
throw new TypeError(`Expected a string, got ${typeof lang}`);
|
|
9
|
-
}
|
|
6
|
+
if (n <= 0 || n > 10) {
|
|
7
|
+
throw new TypeError(`Expected a -n (1 - 10), got ${n}`);
|
|
8
|
+
}
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
throw new TypeError(`Expected a -n (1 - 10), got ${n}`);
|
|
14
|
-
}
|
|
10
|
+
// rnlimit: API limit 10 or less
|
|
11
|
+
const url = `https://${lang}.wikipedia.org/w/api.php?format=json&action=query&list=random&rnnamespace=0&rnlimit=${n}`;
|
|
15
12
|
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
const res = await fetch(url);
|
|
14
|
+
const data = await res.json();
|
|
15
|
+
const words = data.query.random;
|
|
18
16
|
|
|
19
|
-
|
|
20
|
-
.then(res => res.json())
|
|
21
|
-
.then(data => {
|
|
22
|
-
const words = data.query.random;
|
|
23
|
-
words.length = n;
|
|
24
|
-
resolve(words.map(v => v.title));
|
|
25
|
-
});
|
|
26
|
-
});
|
|
17
|
+
return words.slice(-n).map((v) => v.title);
|
|
27
18
|
};
|
|
19
|
+
|
|
20
|
+
export default randomWordWikipedia;
|
package/package.json
CHANGED
|
@@ -1,30 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "random-word-wikipedia",
|
|
3
3
|
"description": "Get random word from wikipedia random page",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "elzup",
|
|
7
7
|
"email": "guild0105@gmail.com",
|
|
8
|
-
"url": "
|
|
8
|
+
"url": "https://anozon.me"
|
|
9
9
|
},
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"isomorphic-fetch": "^2.2.1",
|
|
14
|
-
"meow": "^3.7.0"
|
|
15
|
-
},
|
|
16
|
-
"devDependencies": {
|
|
17
|
-
"ava": "^0.20.0"
|
|
10
|
+
"type": "module",
|
|
11
|
+
"bin": {
|
|
12
|
+
"random-word-wikipedia": "./cli.js"
|
|
18
13
|
},
|
|
19
14
|
"engines": {
|
|
20
|
-
"node": ">=
|
|
15
|
+
"node": ">=22"
|
|
21
16
|
},
|
|
22
17
|
"files": [
|
|
23
18
|
"cli.js",
|
|
24
19
|
"index.js"
|
|
25
20
|
],
|
|
26
21
|
"keywords": [
|
|
27
|
-
"",
|
|
28
22
|
"cli",
|
|
29
23
|
"cli-app",
|
|
30
24
|
"wikipedia",
|
|
@@ -34,6 +28,8 @@
|
|
|
34
28
|
"license": "MIT",
|
|
35
29
|
"repository": "elzup/random-word-wikipedia",
|
|
36
30
|
"scripts": {
|
|
37
|
-
"test": "
|
|
31
|
+
"test": "node --test",
|
|
32
|
+
"cli": "node cli.js",
|
|
33
|
+
"prepublishOnly": "node --test"
|
|
38
34
|
}
|
|
39
35
|
}
|
package/readme.md
CHANGED
|
@@ -1,74 +1,72 @@
|
|
|
1
|
-
# random-word-wikipedia
|
|
1
|
+
# random-word-wikipedia
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/random-word-wikipedia)
|
|
4
|
+
[](https://github.com/elzup/random-word-wikipedia/actions/workflows/node.js.yml)
|
|
4
5
|
|
|
6
|
+
> Get a random word (article title) from Wikipedia's random page.
|
|
7
|
+
|
|
8
|
+
- 🪶 **Zero dependencies** — uses the platform `fetch`, nothing else
|
|
9
|
+
- 📦 **ESM-only** and typed for modern Node.js (`>= 22`)
|
|
10
|
+
- 🌍 **Any language** — pass a Wikipedia language code (`en`, `ja`, …)
|
|
5
11
|
|
|
6
12
|
## Install
|
|
7
13
|
|
|
8
|
-
```
|
|
9
|
-
|
|
14
|
+
```sh
|
|
15
|
+
npm install random-word-wikipedia
|
|
10
16
|
```
|
|
11
17
|
|
|
12
|
-
|
|
13
18
|
## Usage
|
|
14
19
|
|
|
15
20
|
```js
|
|
16
|
-
|
|
21
|
+
import randomWordWikipedia from "random-word-wikipedia";
|
|
17
22
|
|
|
18
|
-
randomWordWikipedia()
|
|
19
|
-
//=> [
|
|
23
|
+
await randomWordWikipedia();
|
|
24
|
+
//=> ['Saxifraga spathularis']
|
|
20
25
|
|
|
21
|
-
randomWordWikipedia(
|
|
22
|
-
//=> [
|
|
26
|
+
await randomWordWikipedia("ja", 2);
|
|
27
|
+
//=> ['ジョン・イサーク・ブリケ', '月は闇夜に隠るが如く']
|
|
23
28
|
```
|
|
24
29
|
|
|
25
|
-
|
|
26
30
|
## API
|
|
27
31
|
|
|
28
|
-
### randomWordWikipedia(
|
|
29
|
-
|
|
30
|
-
#### options
|
|
31
|
-
|
|
32
|
-
##### lang
|
|
33
|
-
|
|
34
|
-
Type: `string`<br>
|
|
35
|
-
Default: `en`
|
|
32
|
+
### `randomWordWikipedia(lang?, n?)`
|
|
36
33
|
|
|
37
|
-
|
|
34
|
+
Returns `Promise<string[]>` — an array of random article titles.
|
|
38
35
|
|
|
39
|
-
|
|
36
|
+
| Param | Type | Default | Description |
|
|
37
|
+
| ------ | -------- | ------- | -------------------------------------------- |
|
|
38
|
+
| `lang` | `string` | `"en"` | Wikipedia language code (e.g. `en`, `ja`). |
|
|
39
|
+
| `n` | `number` | `1` | How many words to return. Must be `1`–`10`. |
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
Default: 1
|
|
43
|
-
|
|
44
|
-
number of word.
|
|
41
|
+
Throws a `TypeError` if `lang` is not a string or `n` is outside `1`–`10`.
|
|
45
42
|
|
|
46
43
|
## CLI
|
|
47
44
|
|
|
48
|
-
```
|
|
49
|
-
|
|
45
|
+
```sh
|
|
46
|
+
npm install --global random-word-wikipedia
|
|
50
47
|
```
|
|
51
48
|
|
|
52
|
-
```
|
|
53
|
-
$ random-word-wikipedia
|
|
49
|
+
```console
|
|
50
|
+
$ random-word-wikipedia
|
|
51
|
+
Mapania caudata
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
$ random-word-wikipedia ja -n 3
|
|
54
|
+
バダインジャラン砂漠
|
|
55
|
+
内野 (印西市)
|
|
56
|
+
PAC-MAN 256
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
-n 10 or less [Default: 1]
|
|
58
|
+
$ random-word-wikipedia --help
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
YuruYuri
|
|
60
|
+
Usage
|
|
61
|
+
$ random-word-wikipedia [lang=en]
|
|
64
62
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
ゆるゆり
|
|
68
|
-
内野 (印西市)
|
|
69
|
-
PAC-MAN 256
|
|
70
|
-
```
|
|
63
|
+
Options
|
|
64
|
+
-n Number of words, 10 or less [Default: 1]
|
|
71
65
|
|
|
66
|
+
Examples
|
|
67
|
+
$ random-word-wikipedia
|
|
68
|
+
$ random-word-wikipedia ja -n 3
|
|
69
|
+
```
|
|
72
70
|
|
|
73
71
|
## License
|
|
74
72
|
|
package/lib/cli.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
"use strict";
|
|
4
|
-
|
|
5
|
-
var _ = require(".");
|
|
6
|
-
|
|
7
|
-
var _2 = _interopRequireDefault(_);
|
|
8
|
-
|
|
9
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
|
|
11
|
-
var meow = require("meow");
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
var cli = meow("\n\tUsage\n\t $ random-word-wikipedia [lang]\n\n\tOptions\n\t -n 10 or less [Default: 1]\n\n\tExamples\n\t $ random-word-wikipedia\n\t\tYuruYuri\n\n\t $ random-word-wikipedia ja -n 3\n\t\t\u30D0\u30C0\u30A4\u30F3\u30B8\u30E3\u30E9\u30F3\u7802\u6F20\n\t\t\u5185\u91CE (\u5370\u897F\u5E02)\n\t\tPAC-MAN 256\n\n");
|
|
15
|
-
|
|
16
|
-
(0, _2.default)(cli.input[0], cli.flags.n).then(function (res) {
|
|
17
|
-
console.log(res.join("\n"));
|
|
18
|
-
});
|
|
19
|
-
//# sourceMappingURL=cli.js.map
|
package/lib/index.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
|
8
|
-
|
|
9
|
-
require("isomorphic-fetch");
|
|
10
|
-
|
|
11
|
-
require("es6-promise").polyfill();
|
|
12
|
-
|
|
13
|
-
exports.default = async function main(lang, n) {
|
|
14
|
-
lang = lang || "en";
|
|
15
|
-
if (typeof lang !== "string") {
|
|
16
|
-
throw new TypeError("Expected a string, got " + (typeof lang === "undefined" ? "undefined" : _typeof(lang)));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
n = n || 1;
|
|
20
|
-
if (n <= 0 || n > 10) {
|
|
21
|
-
throw new TypeError("Expected a -n (1 - 10), got " + n);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// rnlimit: API limit 10 or less
|
|
25
|
-
var url = "http://" + lang + ".wikipedia.org/w/api.php?format=json&action=query&list=random&rnnamespace=0&rnlimit=10";
|
|
26
|
-
|
|
27
|
-
var res = await fetch(url);
|
|
28
|
-
var data = await res.json();
|
|
29
|
-
var words = data.query.random;
|
|
30
|
-
words.length = n;
|
|
31
|
-
return words.map(function (v) {
|
|
32
|
-
return v.title;
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
module.exports = exports["default"];
|
|
37
|
-
//# sourceMappingURL=index.js.map
|