random-word-wikipedia 1.1.0 → 2.1.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 CHANGED
@@ -1,41 +1,42 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import meow from "meow";
4
- import randomWordWikipedia from "./index.js";
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
- }
3
+ import { parseArgs } from "node:util";
4
+ import randomWordWikipedia from "./lib/esm/index.js";
5
+
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
  });
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const randomWordWikipedia = async (lang = "en", n = 1) => {
4
+ if (typeof lang !== "string") {
5
+ throw new TypeError(`Expected a string, got ${typeof lang}`);
6
+ }
7
+ if (n <= 0 || n > 10) {
8
+ throw new TypeError(`Expected a -n (1 - 10), got ${n}`);
9
+ }
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}`;
12
+ const res = await fetch(url);
13
+ const data = (await res.json());
14
+ const words = data.query.random;
15
+ return words.slice(-n).map((v) => v.title);
16
+ };
17
+ exports.default = randomWordWikipedia;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,15 @@
1
+ const randomWordWikipedia = async (lang = "en", n = 1) => {
2
+ if (typeof lang !== "string") {
3
+ throw new TypeError(`Expected a string, got ${typeof lang}`);
4
+ }
5
+ if (n <= 0 || n > 10) {
6
+ throw new TypeError(`Expected a -n (1 - 10), got ${n}`);
7
+ }
8
+ // rnlimit: API limit 10 or less
9
+ const url = `https://${lang}.wikipedia.org/w/api.php?format=json&action=query&list=random&rnnamespace=0&rnlimit=${n}`;
10
+ const res = await fetch(url);
11
+ const data = (await res.json());
12
+ const words = data.query.random;
13
+ return words.slice(-n).map((v) => v.title);
14
+ };
15
+ export default randomWordWikipedia;
@@ -0,0 +1,2 @@
1
+ declare const randomWordWikipedia: (lang?: string, n?: number) => Promise<string[]>;
2
+ export default randomWordWikipedia;
package/package.json CHANGED
@@ -1,31 +1,33 @@
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.1.0",
5
5
  "author": {
6
6
  "name": "elzup",
7
7
  "email": "guild0105@gmail.com",
8
8
  "url": "https://anozon.me"
9
9
  },
10
10
  "type": "module",
11
+ "main": "lib/cjs/index.js",
12
+ "module": "lib/esm/index.js",
13
+ "types": "lib/types/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./lib/types/index.d.ts",
17
+ "import": "./lib/esm/index.js",
18
+ "require": "./lib/cjs/index.js"
19
+ }
20
+ },
11
21
  "bin": {
12
22
  "random-word-wikipedia": "./cli.js"
13
23
  },
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
- },
24
+ "sideEffects": false,
23
25
  "engines": {
24
- "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
26
+ "node": ">=22"
25
27
  },
26
28
  "files": [
27
- "cli.js",
28
- "index.js"
29
+ "lib/",
30
+ "cli.js"
29
31
  ],
30
32
  "keywords": [
31
33
  "cli",
@@ -37,7 +39,19 @@
37
39
  "license": "MIT",
38
40
  "repository": "elzup/random-word-wikipedia",
39
41
  "scripts": {
40
- "test": "ava",
41
- "cli": "node cli.js"
42
+ "clean": "rimraf lib",
43
+ "build": "npm run clean && npm run build:cjs && npm run build:esm && npm run build:types",
44
+ "build:cjs": "tsc --module commonjs --outDir lib/cjs && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
45
+ "build:esm": "tsc --module esnext --outDir lib/esm",
46
+ "build:types": "tsc --declaration --emitDeclarationOnly --outDir lib/types",
47
+ "pretest": "npm run build",
48
+ "test": "node --test",
49
+ "cli": "node cli.js",
50
+ "prepublishOnly": "npm test"
51
+ },
52
+ "devDependencies": {
53
+ "@types/node": "^24.0.0",
54
+ "rimraf": "^6.0.0",
55
+ "typescript": "^5.6.0"
42
56
  }
43
57
  }
package/readme.md CHANGED
@@ -1,69 +1,79 @@
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 runtime dependencies** — uses the platform `fetch`, nothing else
9
+ - 📦 **ESM + CommonJS** with bundled **TypeScript types**, for 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
- ## API
24
-
25
- ### randomWordWikipedia([lang, n])
30
+ CommonJS (note the `.default`):
26
31
 
27
- #### options
32
+ ```js
33
+ const randomWordWikipedia = require("random-word-wikipedia").default;
28
34
 
29
- ##### lang
35
+ randomWordWikipedia().then(console.log);
36
+ ```
30
37
 
31
- Type: `string`<br>
32
- Default: `en`
38
+ ## API
33
39
 
34
- wikipedia lang string.
40
+ ### `randomWordWikipedia(lang?, n?)`
35
41
 
36
- ##### n
42
+ Returns `Promise<string[]>` — an array of random article titles.
37
43
 
38
- Type: `number` 1 - 10<br>
39
- Default: 1
44
+ | Param | Type | Default | Description |
45
+ | ------ | -------- | ------- | -------------------------------------------- |
46
+ | `lang` | `string` | `"en"` | Wikipedia language code (e.g. `en`, `ja`). |
47
+ | `n` | `number` | `1` | How many words to return. Must be `1`–`10`. |
40
48
 
41
- number of word.
49
+ Throws a `TypeError` if `lang` is not a string or `n` is outside `1`–`10`.
42
50
 
43
51
  ## CLI
44
52
 
45
- ```
46
- $ npm install --global random-word-wikipedia
53
+ ```sh
54
+ npm install --global random-word-wikipedia
47
55
  ```
48
56
 
49
- ```
50
- $ random-word-wikipedia --help
57
+ ```console
58
+ $ random-word-wikipedia
59
+ Mapania caudata
60
+
61
+ $ random-word-wikipedia ja -n 3
62
+ バダインジャラン砂漠
63
+ 内野 (印西市)
64
+ PAC-MAN 256
51
65
 
52
- Usage
53
- $ random-word-wikipedia [lang]
66
+ $ random-word-wikipedia --help
54
67
 
55
- Options
56
- -n 10 or less [Default: 1]
68
+ Usage
69
+ $ random-word-wikipedia [lang=en]
57
70
 
58
- Examples
59
- $ random-word-wikipedia
60
- YuruYuri
71
+ Options
72
+ -n Number of words, 10 or less [Default: 1]
61
73
 
62
- $ random-word-wikipedia ja -n 4
63
- バダインジャラン砂漠
64
- ゆるゆり
65
- 内野 (印西市)
66
- PAC-MAN 256
74
+ Examples
75
+ $ random-word-wikipedia
76
+ $ random-word-wikipedia ja -n 3
67
77
  ```
68
78
 
69
79
  ## License
package/index.js DELETED
@@ -1,28 +0,0 @@
1
- import "isomorphic-fetch";
2
- import promise from "es6-promise";
3
- promise.polyfill();
4
-
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
- }
11
-
12
- if (n <= 0 || n > 10) {
13
- throw new TypeError(`Expected a -n (1 - 10), got ${n}`);
14
- }
15
-
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}`;
18
-
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
- });
26
- };
27
-
28
- export default randomWordWikipedia;