nv-random-kw 1.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 (2) hide show
  1. package/index.js +68 -0
  2. package/package.json +11 -0
package/index.js ADDED
@@ -0,0 +1,68 @@
1
+
2
+ const random_kw = (with_type = false) => {
3
+ // 关键字及其后面应跟的类型(用实际符号表示)
4
+ const keywords = [
5
+ // 声明类
6
+ ['var', ['name']],
7
+ ['let', ['name']],
8
+ ['const', ['name']],
9
+
10
+ // 控制流 - 条件
11
+ ['if', ['()']],
12
+ ['else', ['{}']],
13
+ ['else if', ['()']],
14
+ ['switch', ['()']],
15
+
16
+ // 控制流 - 循环
17
+ ['for', ['()']],
18
+ ['while', ['()']],
19
+ ['do', ['{}']],
20
+
21
+ // 控制流 - 分支
22
+ ['case', ['expr']],
23
+ ['default', [':']],
24
+
25
+ // 跳转
26
+ ['break', ['name']],
27
+ ['continue', ['name']],
28
+ ['return', ['expr', '']],
29
+ ['throw', ['expr']],
30
+ ['try', ['{}']],
31
+ ['catch', ['(name)']],
32
+ ['finally', ['{}']],
33
+
34
+ // 异常处理
35
+ ['yield', ['*', 'expr', '']], // 新增 * 选项
36
+
37
+ // 模块/导入导出
38
+ ['import', ['name']],
39
+ ['export', ['name']],
40
+
41
+ // 其他
42
+ ['class', ['name','{}']],
43
+ ['extends', ['name']],
44
+ ['super', ['()', '.']],
45
+ ['new', ['name']],
46
+ ['this', []],
47
+ ['function', ['*', 'name', '()']],
48
+ ['async', [['()', Symbol.for('function')]]],
49
+ ['static', ['name']],
50
+ ['get', ['name']],
51
+ ['set', ['name']],
52
+ ['from', ['string']],
53
+ ['as', ['name']],
54
+ ['of', ['expr']],
55
+ ];
56
+
57
+ // 过滤掉要排除的关键字
58
+ const excluded = new Set(['typeof', 'instanceof', 'in', 'await', 'delete']);
59
+ const filtered = keywords.filter(([kw]) => !excluded.has(kw));
60
+
61
+ // 随机选择一个关键字
62
+ const idx = Math.floor(Math.random() * filtered.length);
63
+ const [keyword, types] = filtered[idx];
64
+
65
+ return with_type ? [keyword, types] : keyword;
66
+ };
67
+
68
+ module.exports = random_kw;
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "nv-random-kw",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "author": "",
9
+ "license": "ISC",
10
+ "description": ""
11
+ }