-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
126 lines (109 loc) · 3.84 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css" integrity="sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-fork-ribbon-css/0.2.2/gh-fork-ribbon.min.css" crossorigin="anonymous"/>
<style>
.github-fork-ribbon:before {
background-color: #333;
}
</style>
<title>random-data</title>
</head>
<body style="text-align: center; padding: 24px 0">
<textarea style="min-height: 300px; min-width: 400px;" id="input"></textarea>
<br /><button class="pure-button pure-button-primary" id="click" style="margin: 8px 0">随机一个</button><br />
<textarea style="min-height: 300px; min-width: 400px;" id="output"></textarea>
<hr />
<div style="width: 61vw; margin: 0 auto;">
<pre style="text-align: left;">
现在支持两种语句
constraint 约束 限制变量取值
repeat 重复 会输出内容
重要 每一个变量都需要有约束
constraint 的语法
int类型
constraint 变量名 int 最小值 最大值
比如
constraint n int 1 233
变量名 最好都是字母(汉字也行)
最小值和最大值可以是别的变量名
区间是左闭右开
set类型
constraint 变量名 set value1 value2 value3
比如
constraint 数字 set 一 二 三 四
value之间使用空格分隔
repeat 的语法
line类型
repeat 重复次数 重复的内容
重复次数 可以是一个数字,也可以是一个变量
从紧跟 重复次数 的空格后面开始,到这一行结束都会被视为一个模板,我们使用 ${变量名} 来引用一个变量
比如
repeat 1 ${n}
亦或者
repeat 1 prefix string ${n}
都是被允许的
group类型
repeat group 重复次数
repeat line
repeat line
repeat line
end group
重复次数 可以是一个数字,也可以是一个变量
举个例子,假如我们的数据是
第一行输入一个n,代表接下来有n行数据,每行数据有三个数a b c
那么就是
repeat 1 ${n}
repeat n ${a} ${b} ${c}
然后补充一下约束就好了
再举一个例子
假如我们的数据是这个样子的
第一行是n,代表接下来有n组数据
每组数据的第一行有三个数row min max,代表接下来有row个数字,每个数字的取值是 [min, max)
那我们可以这样写
constraint n int 5 10
constraint row int 10 20
constraint min int 150 200
constraint max int 500 1000
constraint num int min max
repeat 1 ${n}
repeat group n
repeat 1 ${row} ${min} ${max}
repeat row ${num}
end group
来一个set的例子
constraint 姓 set 赵 钱 孙 李
constraint 名 set 一 二 三 四
repeat 10 ${姓}${名}
程序没有检查输入是否合法,所以需要你自己注意一下 :)
</pre>
</div>
<a class="github-fork-ribbon" href="https://github.com/muzea-demo/random-data" data-ribbon="F**k me on GitHub" title="F**k me on GitHub">F**k me on GitHub</a>
</body>
<script type="module">
import { parse } from './parse.mjs'
import { generate } from './generate.mjs'
import { stringify } from './stringify.mjs'
let initValue = '';
initValue += 'constraint n int 5 10\n';
initValue += 'constraint a int 1 n\n';
initValue += 'constraint b int 5 1000\n';
initValue += 'constraint c int 5 10000\n';
initValue += 'repeat 1 ${n}\n';
initValue += 'repeat n ${a} ${b} ${c}\n';
document.querySelector('#input').value = initValue;
function handleClick() {
const ret = parse(document.querySelector('#input').value);
try {
document.querySelector('#output').value = stringify(generate(...ret));
} catch (error) {
window.alert(error);
}
}
document.querySelector('#click').addEventListener('click', handleClick);
</script>
</html>