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