nv-random-op 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 +53 -0
  2. package/package.json +11 -0
package/index.js ADDED
@@ -0,0 +1,53 @@
1
+
2
+ const random_op = (with_type = false) => {
3
+ // 一元运算符(1元)
4
+ const unary_ops = [
5
+ '+', '-', '~', '!', // 算术/位/逻辑
6
+ '++', '--', // 自增自减
7
+ 'delete', 'void', 'await' ,'typeof' ,'...' // 特殊的一元运算符
8
+ ];
9
+
10
+ // 二元运算符(2元)
11
+ const binary_ops = [
12
+ // 算术
13
+ '+', '-', '*', '/', '%', '**',
14
+ // 位运算
15
+ '&', '|', '^', '<<', '>>', '>>>',
16
+ // 比较
17
+ '==', '!=', '===', '!==', '<', '>', '<=', '>=',
18
+ // 逻辑
19
+ '&&', '||', '??',
20
+ // 赋值
21
+ '=', '+=', '-=', '*=', '/=', '%=', '**=', '&=', '|=', '^=', '<<=', '>>=', '>>>=',
22
+ // 其他
23
+ '.','?.',
24
+ 'in', 'instanceof'
25
+ ];
26
+
27
+ // 三元运算符(3元)
28
+ const ternary_ops = [
29
+ '?:' // 条件运算符
30
+ ];
31
+
32
+ // 按权重随机选择类型(因为一元、二元、三元数量差异大)
33
+ const r = Math.random();
34
+ let op, type;
35
+
36
+ if (r < 0.2) {
37
+ // 20% 概率选一元
38
+ type = 1;
39
+ op = unary_ops[Math.floor(Math.random() * unary_ops.length)];
40
+ } else if (r < 0.95) {
41
+ // 75% 概率选二元
42
+ type = 2;
43
+ op = binary_ops[Math.floor(Math.random() * binary_ops.length)];
44
+ } else {
45
+ // 5% 概率选三元
46
+ type = 3;
47
+ op = ternary_ops[Math.floor(Math.random() * ternary_ops.length)];
48
+ }
49
+
50
+ return with_type ? [op, type] : op;
51
+ };
52
+
53
+ module.exports = random_op;
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "nv-random-op",
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
+ }