1 | # API Reference
|
2 |
|
3 | ## Table of Contents
|
4 |
|
5 | * [`simpleObject`](#simpleobject)
|
6 | * [`objectWithKeys`](#objectwithkeys)
|
7 | * [`fromList`](#fromlist)
|
8 | * [`subList`](#sublist)
|
9 | * [`listOf`](#listof)
|
10 | * [`string`](#string)
|
11 | * [`word`](#word)
|
12 | * [`sentence`](#sentence)
|
13 | * [`paragraph`](#paragraph)
|
14 | * [`integer`](#integer)
|
15 | * [`float`](#float)
|
16 | * [`boolean`](#boolean)
|
17 | * [`url`](#url)
|
18 | * [`email`](#email)
|
19 | * [`date`](#date)
|
20 |
|
21 | ## `simpleObject`
|
22 |
|
23 | Produces a simple object with a random number of key/value pairs. Each key is a
|
24 | random [word](#word), and each value is a random [string](#string)
|
25 |
|
26 | ### Example
|
27 |
|
28 | ```js
|
29 | any.simpleObject();
|
30 |
|
31 | // { ahe: 'pPwLKrme*OU4@AR1)*^', lel: '6ExqUAyD!', fegcim: 'j03do)OUDhvJ', ucalu: 'L#)zHg5@AV9#vD5O' }
|
32 | ```
|
33 |
|
34 | ## `objectWithKeys`
|
35 |
|
36 | Produces a simple object with the keys defined from the provided list of keys
|
37 |
|
38 | ### `objectWithKeys` parameters
|
39 |
|
40 | * `keys` (required): list of keys to be used for the produced object's keys
|
41 | * `options`: object to provide configuration options
|
42 | * `factory`: factory function to be used to create each of the values for the
|
43 | produced object
|
44 | * receives the current key as the first argument and the index as the second
|
45 | * defaults to [`string`](#string), if not provided
|
46 |
|
47 | ### `objectWithKeys` examples
|
48 |
|
49 | ```js
|
50 | any.objectWithKeys(['foo', 'bar', 'baz']);
|
51 |
|
52 | // { foo: 'eUnRumYAyoS@YVj', bar: 'GvdfJH%]OhNCSDe', baz: 'ozLo2]R' }
|
53 | ```
|
54 |
|
55 | ```js
|
56 | any.objectWithKeys(['foo', 'bar', 'baz'], {factory: any.url});
|
57 |
|
58 | // { foo: 'http://hopap.lr/dupro', bar: 'http://kolore.np/cozmavje', baz: 'http://udegowum.cd/ohobasmo' }
|
59 | ```
|
60 |
|
61 | ```js
|
62 | any.objectWithKeys(['foo', 'bar', 'baz'], {factory: id => ({...any.simpleObject(), id})});
|
63 |
|
64 | /*
|
65 | {
|
66 | foo: {
|
67 | ipsawiw: '$McveM',
|
68 | comgahra: ']TORpXOYqKQz&G]h',
|
69 | ranimedo: 'Q^Y)ga2FG^jPzs](',
|
70 | owabopmom: 'YQz[MoIEVHLbVG',
|
71 | jebaru: 'qd&TpwaMEp8Vz#al8C',
|
72 | zuwbedil: 'LC&c3$gPxI',
|
73 | id: 'foo'
|
74 | },
|
75 | bar: {
|
76 | jahaluz: '#HD0Zqs$QLxAC@Lrb*',
|
77 | ekofesi: 'uhAOs',
|
78 | veuhotuh: 'ob1K2TDc5Q',
|
79 | zuufciw: 'L&hqNtx@',
|
80 | riptutru: 'mYSXLZh#OmWvaJC2',
|
81 | obebetmi: '[E@yC^xTBia*]kdvfC',
|
82 | hugmoug: 'DfV8Qs9f8O433etJ',
|
83 | zusurare: '0I0!xzrxEN$f4',
|
84 | gonefo: 'YkLh9Sg&f7o%K)VDT',
|
85 | watbihli: '3F2]ofS',
|
86 | elaecave: 'iY@uTDWy@J',
|
87 | id: 'bar'
|
88 | },
|
89 | baz: {
|
90 | zehitek: '7$GlW8N',
|
91 | upagpu: '9^UDxLQKp',
|
92 | aniheiju: '9$lJPW6$eXhPf$GFa',
|
93 | ekaradcun: ')nD1)rC&c^',
|
94 | molilji: 'qvr7dW$Ve0uR3fkZ]',
|
95 | jebowibuf: '0G6vTn)71NsXWsr]ypd',
|
96 | wednotso: '2hHmqvDlw&&!^ycJ0',
|
97 | arewibvuf: '*N^&UXo$c&eAiF^',
|
98 | gutuet: '^JHXnzETi*VSuUy(3b',
|
99 | molwadi: 'x!dAw',
|
100 | fuglekpe: 'WqDOaU2[LdAvHm',
|
101 | ireakiuso: '@VG9BF$)F',
|
102 | id: 'baz'
|
103 | }
|
104 | }
|
105 | */
|
106 | ```
|
107 |
|
108 | ```js
|
109 | const qux = ['a', 'b', 'c'];
|
110 |
|
111 | any.objectWithKeys(['foo', 'bar', 'baz'], {factory: (id, index) => ({id, letter: qux[index]})});
|
112 |
|
113 | /*
|
114 | {
|
115 | foo: { id: 'foo', letter: 'a' },
|
116 | bar: { id: 'bar', letter: 'b' },
|
117 | baz: { id: 'baz', letter: 'c' }
|
118 | }
|
119 | */
|
120 | ```
|
121 |
|
122 | ## `fromList`
|
123 |
|
124 | Direct usage of [`pickone()` from chance.js](http://chancejs.com/helpers/pickone.html).
|
125 |
|
126 | ### `fromList` parameters
|
127 |
|
128 | * `list` (required): list of items to choose from
|
129 |
|
130 | ### `fromList` example
|
131 |
|
132 | ```js
|
133 | any.fromList(['foo', 'bar', 'baz']);
|
134 |
|
135 | // bar
|
136 | ```
|
137 |
|
138 | ## `subList`
|
139 |
|
140 | An almost direct usage of [`pickset()` from chance.js](http://chancejs.com/helpers/pickset.html).
|
141 |
|
142 | ### `subList` parameters
|
143 |
|
144 | * `list` (required): list of items to choose from
|
145 | * `options`: (required) object to provide configuration options
|
146 | * `size`: (required) length of sub-list that should be produced by choosing
|
147 | unique items from the provided list
|
148 |
|
149 | ### `subList` example
|
150 |
|
151 | ```js
|
152 | any.subList(['foo', 'bar', 'baz'], {size: 2});
|
153 |
|
154 | // ['bar', 'foo']
|
155 | ```
|
156 |
|
157 | ## `listOf`
|
158 |
|
159 | Produces a list of random items from the provided factory function
|
160 |
|
161 | ### `listOf` parameters
|
162 |
|
163 | * `factory` (required): the factory function for producing the items for the
|
164 | list.
|
165 | * receives the current index as the first argument
|
166 | * `options`: object to provide configuration options
|
167 | * `size`: length of list that should be produced
|
168 | * defaults to a random number between `1` and `20`, if not provided
|
169 | * the default range of potential sizes can be overridden by providing
|
170 | options for [integer](#integer)
|
171 | * `uniqueOn`: property on the produced objects that needs to be unique within
|
172 | the resulting list
|
173 |
|
174 | ### `listOf` examples
|
175 |
|
176 | ```js
|
177 | any.listOf(any.string);
|
178 |
|
179 | // [ 'iQw2[CgBJ1FF8', '58jp*w', 'G]5MoXnKe#F43H3#1', 'oO*a7tY^gbY', 'f$AFjgd$Gg2' ]
|
180 | ```
|
181 |
|
182 | ```js
|
183 | any.listOf(any.string, {size: 2});
|
184 |
|
185 | // [ 'wDPGiMtH]fg6o8QxMS', 'OYElY9vO9k4' ]
|
186 | ```
|
187 |
|
188 | ```js
|
189 | any.listOf(() => ({ id: any.integer(), foo: any.string() }), {uniqueOn: 'id'});
|
190 |
|
191 | // [ { id: 173289733357568, foo: 'T8XCg*!Q((*Z5@BUFO' }, { id: 8233254624690176, foo: 'ziY]4e%cDoR07g)IhA' } ]
|
192 | ```
|
193 |
|
194 | ## `string`
|
195 |
|
196 | Direct usage of [`string()` from chance.js](http://chancejs.com/basics/string.html).
|
197 | Options are passed directly to the chance method so refer to its documentation
|
198 | for what is available.
|
199 |
|
200 | ### Note
|
201 |
|
202 | Be aware that `string()` can include special characters that may not be suitable
|
203 | for some use cases. If they would cause issues, you may prefer [`word()`](#word)
|
204 | instead.
|
205 |
|
206 | ### `string` example
|
207 |
|
208 | ```js
|
209 | any.string();
|
210 |
|
211 | // 1%BU8#64p%Z
|
212 | ```
|
213 |
|
214 | ## `word`
|
215 |
|
216 | Direct usage of [`word()` from chance.js](http://chancejs.com/text/word.html).
|
217 | Options are passed directly to the chance method so refer to its documentation
|
218 | for what is available.
|
219 |
|
220 | ### `word` example
|
221 |
|
222 | ```js
|
223 | any.word();
|
224 |
|
225 | // nitte
|
226 | ```
|
227 |
|
228 | ## `sentence`
|
229 |
|
230 | Direct usage of [`sentence()` from chance.js](http://chancejs.com/text/sentence.html).
|
231 | Options are passed directly to the chance method so refer to its documentation
|
232 | for what is available.
|
233 |
|
234 | ### `sentence` example
|
235 |
|
236 | ```js
|
237 | any.sentence();
|
238 |
|
239 | // 'Witpevze mappos isoletu fo res bi geow pofin mu rupoho revzi utva ne.'
|
240 | ```
|
241 |
|
242 | ## `paragraph`
|
243 |
|
244 | Direct usage of [`paragraph()` from chance.js](http://chancejs.com/text/paragraph.html).
|
245 | Options are passed directly to the chance method so refer to its documentation
|
246 | for what is available.
|
247 |
|
248 | ### `paragraph` example
|
249 |
|
250 | ```js
|
251 | any.paragraph();
|
252 |
|
253 | // 'Lel fi huepe jupu akse zej ire vesik kojvulom zon is biwuwkef pa. Uv hokivej voh ebu
|
254 | // numdogi akolo hik uwlez ta vacev ofdaimi acunetum suvet uhdab ir soglazo ju pafbeb. Pub
|
255 | // cezeh fuc kebamnul he ok luumoabi rawkig me fov pin zup biv risugra. Ralpunad apkomgib
|
256 | // alnirciw akel wa lus wahfum burog buol vecotihe abadahoj ugolo wovki ucojal fec.'
|
257 | ```
|
258 |
|
259 | ## `integer`
|
260 |
|
261 | Produces a random integer >= 0. Direct usage of
|
262 | [`natural()` from chance.js](http://chancejs.com/basics/natural.html).
|
263 | Options are passed directly to the chance method so refer to its
|
264 | documentation for what is available.
|
265 |
|
266 | ### `integer` example
|
267 |
|
268 | ```js
|
269 | any.integer();
|
270 |
|
271 | // 8526341888540672
|
272 | ```
|
273 |
|
274 | ## `float`
|
275 |
|
276 | Direct usage of [`floating()` from chance.js](http://chancejs.com/basics/floating.html).
|
277 | Options are passed directly to the chance method so refer to its documentation
|
278 | for what is available.
|
279 |
|
280 | ### `float` example
|
281 |
|
282 | ```js
|
283 | any.float();
|
284 |
|
285 | // -114981703621.0176
|
286 | ```
|
287 |
|
288 | ## `boolean`
|
289 |
|
290 | Produces a random boolean value (`true` or `false`).
|
291 | Options are passed directly to the chance method so refer to its documentation
|
292 | for what is available.
|
293 |
|
294 | ### `boolean` example
|
295 |
|
296 | ```js
|
297 | any.boolean();
|
298 |
|
299 | // true
|
300 | ```
|
301 |
|
302 | ## `url`
|
303 |
|
304 | Direct usage of [`url()` from chance.js](http://chancejs.com/web/url.html).
|
305 | Options are passed directly to the chance method so refer to its
|
306 | documentation for what is available.
|
307 |
|
308 | ### `url` example
|
309 |
|
310 | ```js
|
311 | any.url();
|
312 |
|
313 | // http://vek.fo/derpu
|
314 | ```
|
315 |
|
316 | ## `email`
|
317 |
|
318 | Direct usage of [`email()` from chance.js](http://chancejs.com/web/email.html).
|
319 | Options are passed directly to the chance method so refer to its documentation
|
320 | for what is available.
|
321 |
|
322 | ### `email` example
|
323 |
|
324 | ```js
|
325 | any.email();
|
326 |
|
327 | // itcobiv@iwo.gq
|
328 | ```
|
329 |
|
330 | ## `date`
|
331 |
|
332 | An almost direct usage of [`date()` from chance.js](http://chancejs.com/time/date.html).
|
333 | No options passed to the `any` method are passed to the `chance` method, but
|
334 | the `{string: true}` option is passed because I have only had a use for the
|
335 | string form of random dates so far.
|
336 |
|
337 | ### `date` example
|
338 |
|
339 | ```js
|
340 | any.date();
|
341 |
|
342 | // 10/24/2028
|
343 | ```
|