-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.html
247 lines (207 loc) · 7.24 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!doctype HTML>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/jquery.mockjson.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<style type="text/css">
body, html {
padding:0;
margin:0;
color:#333;
color:#000;
background:#EEE;
font-family:'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;
font-family: Georgia, 'Hiragino Mincho Pro', serif;
font-size:14px;
line-height:1.5;
}
h1, h2, h3, h4, h5, p, pre {
margin:0;
padding:8px 0 0;
font-weight:normal;
}
h1 {
text-align:center;
padding:16px;
margin:0 0 8px;
border-bottom:1px dotted #CCC;
font-size:40px;
}
h2 {
font-size:20px;
}
pre {
margin:8px 0;
padding:8px;
background:#EEE;
font-family: 'Courier New', monospace;
overflow:auto;
}
code {
background:#EEE;
font-family: 'Courier New', monospace;
}
p {
padding:8px 0 0;
}
p#intro {
font-size:16px;
}
p#download {
font-size:20px;
font-weight:bold;
text-align:center;
}
#all {
width:944px;
margin:0 auto;
padding:8px;
border-right:1px dotted #CCC;
border-left:1px dotted #CCC;
background:#FFF;
overflow:auto;
}
#template,
#result {
}
#output {
width:391px;
float:right;
margin:0 0 0 8px;
padding:0 0 0 8px;
border-left:1px dotted #CCC;
}
#output h2 {
text-align:center;
margin:0 0 8px;
border-bottom:1px dotted #CCC;
}
</style>
<title>mockJSON - a jQuery plugin for mocking JSON and JSONP requests</title>
</head>
<body>
<a href="https://github.com/mennovanslooten/mockJSON" id="forkme"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://assets1.github.com/img/e6bef7a091f5f3138b8cd40bc3e114258dd68ddf?repo=&url=http%3A%2F%2Fs3.amazonaws.com%2Fgithub%2Fribbons%2Fforkme_right_red_aa0000.png&path=" alt="Fork me on GitHub"></a>
<div id="all">
<h1>mockJSON</h1>
<div id="output">
<h2>Example</h2>
<h3>Template JSON</h3>
<pre id="template"></pre>
<h3>Random generated JSON</h3>
<p>Reload to see newly generated data</p>
<pre id="result"></pre>
</div>
<p id="intro">MockJSON is a plugin for jQuery that can hijack
JSON and JSONP requests and respond with randomly generated JSON
data. Completely random JSON would be pretty useless of course, so
you can specify a JSON <em>template</em> for each request. On the right you
see an example of such a template and some generated
output.</p>
<p id="download"><a href="https://github.com/mennovanslooten/mockJSON">GitHub</a> | <a href="tryit.html">try it</a></p>
<h2>Usage</h2>
<pre>
// create template and regular expression for request that
// should be mocked
$.mockJSON(/foo\.json/, {
"fathers|5-10":[ ...etc... ]
});
// request JSON as usual
$.getJSON('foo/foo.json', function(json) {
// this will be mocked
});
$.getJSON('foo/bar.json', function(json) {
// this will not be mocked
});
</pre>
<p>A template is valid JSON in itself and as such is an object with
labels and values. Values can be strings, numbers, booleans, arrays and other
objects.</p>
<h2>Numbers</h2>
<p>Adding <code>|x-y</code> to a number's label will replace the number
with <code>r</code>, where <code>r</code> is a random number
between <code>x</code> and <code>y</code>.</p>
<pre>
// Example template
{ "age|0-99" : 0 }
// Example output
{ "age" : 42 }
</pre>
<p>Adding <code>|+x</code> to a number's label, will increment that number
with <code>x</code> for each new item. This can be useful for sequential
ID's, etc.
<h2>Boolean</h2>
<p>Adding <code>|x-y</code> to a boolean's label will randomly replace the boolean
with <code>true</code> or <code>false</code>. The values of
<code>x</code> and <code>y</code> are irrelevant.</p>
<pre>
// Example template
{ "married|0-1" : true }
// Example output
{ "married" : false }
</pre>
<h2>Strings</h2>
<p>MockJSON comes with a number of keywords in the form
<code>@KEY_WORD</code> that are automatically replaced with random
data inside a string. These keywords can be combined with other
characters in a string.</p>
<pre>
// Example template
{ "name" : "@LAST_NAME, @MALE_FIRST_NAME" }
// Example output
{ "name" : "Hall, Kevin" }
</pre>
<p>You can easily add your own keywords.</p>
<pre>
// Adding the @US_STATE keyword
$.mockJSON.data.US_STATE = [
'Alabama', 'Alaska', ... , 'Wisconsin', 'Wyoming'
];
// Example template
{ "state" : "@US_STATE" }
// Example output
{ "state" : "Alaska" }
</pre>
<p>Adding <code>|x-y</code> to a string's label will repeat the
string <code>r</code> times, where <code>r</code> is a random number
between <code>x</code> and <code>y</code>.</p>
<pre>
// Example template
{ "rating|1-5" : "*" }
// Example output
{ "rating" : "***" }
// This can also be combined with keywords.
{ "initials|1-5" : "@LETTER_UPPER." }
// Example output
{ "initials" : "V.M.J." }
</pre>
<h2>Arrays</h2>
<p>Adding <code>|x-y</code> to an array's label will fill the array
with <code>r</code> instances of the first element, where
<code>r</code> is a random number between <code>x</code> and
<code>y</code>. Each of these elements will be treated as a new
template.</p>
<pre>
// Example template
{
"daughters|0-3" : [
{ "Name" : "@FEMALE_FIRST_NAME" }
],
"sons|0-3" : ["@MALE_FIRST_NAME"]
}
// Example output
{
"daughters" : [
{ "Name" : "Linda" },
{ "Name" : "Barbara" }
],
"sons" : ["William", "David", "Jeffrey"]
}
</pre>
<h2>Extra's</h2>
<p>Sometimes random is a bit <em>too</em> random. When you want to
have predictable, reproducible JSON generated: set the
<code>$.mockJSON.random = <strong>false</strong>.</code>
</div>
</body>
</html>