random-word-wikipedia 1.1.0 → 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.
Files changed (4) hide show
  1. package/cli.js +30 -29
  2. package/index.js +13 -21
  3. package/package.json +5 -13
  4. package/readme.md +41 -39
package/cli.js CHANGED
@@ -1,41 +1,42 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import meow from "meow";
3
+ import { parseArgs } from "node:util";
4
4
  import randomWordWikipedia from "./index.js";
5
5
 
6
- const cli = meow(
7
- `
8
- Usage
9
- $ random-word-wikipedia [lang=en]
10
-
11
- Options
12
- -n 10 or less [Default: 1]
13
-
14
- Examples
15
- $ random-word-wikipedia
16
- YuruYuri
17
-
18
- $ random-word-wikipedia ja -n 3
19
- バダインジャラン砂漠
20
- 内野 (印西市)
21
- PAC-MAN 256
22
-
23
- `,
24
- {
25
- importMeta: import.meta,
26
- flags: {
27
- n: {
28
- type: "number"
29
- }
30
- }
6
+ const help = `
7
+ Usage
8
+ $ random-word-wikipedia [lang=en]
9
+
10
+ Options
11
+ -n Number of words, 10 or less [Default: 1]
12
+
13
+ Examples
14
+ $ random-word-wikipedia
15
+ $ random-word-wikipedia ja -n 3
16
+ `;
17
+
18
+ const { values, positionals } = parseArgs({
19
+ allowPositionals: true,
20
+ options: {
21
+ n: { type: "string", short: "n" },
22
+ help: { type: "boolean", short: "h" }
31
23
  }
32
- );
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
33
 
34
- randomWordWikipedia(cli.input[0], cli.flags.n || 1)
34
+ randomWordWikipedia(lang, n)
35
35
  .then((res) => {
36
36
  console.log(res.join("\n"));
37
37
  })
38
38
  .catch((err) => {
39
- console.error("Error: err.message");
39
+ console.error(`Error: ${err.message}`);
40
40
  console.error("$ random-word-wikipedia --help");
41
+ process.exit(1);
41
42
  });
package/index.js CHANGED
@@ -1,28 +1,20 @@
1
- import "isomorphic-fetch";
2
- import promise from "es6-promise";
3
- promise.polyfill();
1
+ const randomWordWikipedia = async (lang = "en", n = 1) => {
2
+ if (typeof lang !== "string") {
3
+ throw new TypeError(`Expected a string, got ${typeof lang}`);
4
+ }
4
5
 
5
- const randomWordWikipedia = (lang, n = 1) => {
6
- return new Promise(function (resolve, reject) {
7
- lang = lang || "en";
8
- if (typeof lang !== "string") {
9
- throw new TypeError(`Expected a string, got ${typeof lang}`);
10
- }
6
+ if (n <= 0 || n > 10) {
7
+ throw new TypeError(`Expected a -n (1 - 10), got ${n}`);
8
+ }
11
9
 
12
- if (n <= 0 || n > 10) {
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
- // rnlimit: API limit 10 or less
17
- const url = `https://${lang}.wikipedia.org/w/api.php?format=json&action=query&list=random&rnnamespace=0&rnlimit=${n}`;
13
+ const res = await fetch(url);
14
+ const data = await res.json();
15
+ const words = data.query.random;
18
16
 
19
- const res = fetch(url)
20
- .then((res) => res.json())
21
- .then((data) => {
22
- const words = data.query.random;
23
- resolve(words.slice(-n).map((v) => v.title));
24
- });
25
- });
17
+ return words.slice(-n).map((v) => v.title);
26
18
  };
27
19
 
28
20
  export default randomWordWikipedia;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "random-word-wikipedia",
3
3
  "description": "Get random word from wikipedia random page",
4
- "version": "1.1.0",
4
+ "version": "2.0.0",
5
5
  "author": {
6
6
  "name": "elzup",
7
7
  "email": "guild0105@gmail.com",
@@ -11,17 +11,8 @@
11
11
  "bin": {
12
12
  "random-word-wikipedia": "./cli.js"
13
13
  },
14
- "dependencies": {
15
- "es6-promise": "4.2.8",
16
- "fetch-mock": "9.11.0",
17
- "isomorphic-fetch": "3.0.0",
18
- "meow": "10.1.2"
19
- },
20
- "devDependencies": {
21
- "ava": "3.15.0"
22
- },
23
14
  "engines": {
24
- "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
15
+ "node": ">=22"
25
16
  },
26
17
  "files": [
27
18
  "cli.js",
@@ -37,7 +28,8 @@
37
28
  "license": "MIT",
38
29
  "repository": "elzup/random-word-wikipedia",
39
30
  "scripts": {
40
- "test": "ava",
41
- "cli": "node cli.js"
31
+ "test": "node --test",
32
+ "cli": "node cli.js",
33
+ "prepublishOnly": "node --test"
42
34
  }
43
35
  }
package/readme.md CHANGED
@@ -1,69 +1,71 @@
1
- # random-word-wikipedia [![Node.js CI](https://github.com/elzup/random-word-wikipedia/actions/workflows/node.js.yml/badge.svg)](https://github.com/elzup/random-word-wikipedia/actions/workflows/node.js.yml)
1
+ # random-word-wikipedia
2
2
 
3
- > Get random word from wikipedia random page
3
+ [![npm](https://img.shields.io/npm/v/random-word-wikipedia.svg)](https://www.npmjs.com/package/random-word-wikipedia)
4
+ [![Node.js CI](https://github.com/elzup/random-word-wikipedia/actions/workflows/node.js.yml/badge.svg)](https://github.com/elzup/random-word-wikipedia/actions/workflows/node.js.yml)
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`, …)
4
11
 
5
12
  ## Install
6
13
 
7
- ```
8
- $ npm install random-word-wikipedia
14
+ ```sh
15
+ npm install random-word-wikipedia
9
16
  ```
10
17
 
11
18
  ## Usage
12
19
 
13
20
  ```js
14
- const randomWordWikipedia = require("random-word-wikipedia");
21
+ import randomWordWikipedia from "random-word-wikipedia";
15
22
 
16
- randomWordWikipedia().then(console.log);
17
- //=> [ 'Saxifraga spathularis' ]
23
+ await randomWordWikipedia();
24
+ //=> ['Saxifraga spathularis']
18
25
 
19
- randomWordWikipedia("ja", 2).then(console.log);
20
- //=> [ 'ジョン・イサーク・ブリケ', '月は闇夜に隠るが如く' ]
26
+ await randomWordWikipedia("ja", 2);
27
+ //=> ['ジョン・イサーク・ブリケ', '月は闇夜に隠るが如く']
21
28
  ```
22
29
 
23
30
  ## API
24
31
 
25
- ### randomWordWikipedia([lang, n])
26
-
27
- #### options
28
-
29
- ##### lang
30
-
31
- Type: `string`<br>
32
- Default: `en`
32
+ ### `randomWordWikipedia(lang?, n?)`
33
33
 
34
- wikipedia lang string.
34
+ Returns `Promise<string[]>` — an array of random article titles.
35
35
 
36
- ##### n
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`. |
37
40
 
38
- Type: `number` 1 - 10<br>
39
- Default: 1
40
-
41
- number of word.
41
+ Throws a `TypeError` if `lang` is not a string or `n` is outside `1`–`10`.
42
42
 
43
43
  ## CLI
44
44
 
45
- ```
46
- $ npm install --global random-word-wikipedia
45
+ ```sh
46
+ npm install --global random-word-wikipedia
47
47
  ```
48
48
 
49
- ```
50
- $ random-word-wikipedia --help
49
+ ```console
50
+ $ random-word-wikipedia
51
+ Mapania caudata
52
+
53
+ $ random-word-wikipedia ja -n 3
54
+ バダインジャラン砂漠
55
+ 内野 (印西市)
56
+ PAC-MAN 256
51
57
 
52
- Usage
53
- $ random-word-wikipedia [lang]
58
+ $ random-word-wikipedia --help
54
59
 
55
- Options
56
- -n 10 or less [Default: 1]
60
+ Usage
61
+ $ random-word-wikipedia [lang=en]
57
62
 
58
- Examples
59
- $ random-word-wikipedia
60
- YuruYuri
63
+ Options
64
+ -n Number of words, 10 or less [Default: 1]
61
65
 
62
- $ random-word-wikipedia ja -n 4
63
- バダインジャラン砂漠
64
- ゆるゆり
65
- 内野 (印西市)
66
- PAC-MAN 256
66
+ Examples
67
+ $ random-word-wikipedia
68
+ $ random-word-wikipedia ja -n 3
67
69
  ```
68
70
 
69
71
  ## License