1 | ## Random Fixture Data
|
2 |
|
3 | Adapted from casual's fake data generator. This version creates mixed-case methods that pass jshint tests...
|
4 |
|
5 | [![NPM version](https://badge.fury.io/js/random-fixture-data.svg)](http://badge.fury.io/js/random-fixture-data)
|
6 | [![Build Status](https://travis-ci.org/darrylwest/random-fixture-data.png?branch=master)](https://travis-ci.org/darrylwest/random-fixture-data)
|
7 | [![Dependency Status](https://david-dm.org/darrylwest/random-fixture-data.svg)](https://david-dm.org/darrylwest/random-fixture-data)
|
8 |
|
9 | ## Installation
|
10 |
|
11 | > npm install random-fixture-data --save-dev
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```javascript
|
16 | var data = require('random-fixture-data');
|
17 |
|
18 | // Generate random sentence
|
19 | // You don't need function call operator here
|
20 | // because most of generators use properties mechanism
|
21 | var sentence = data.sentence;
|
22 |
|
23 | // Generate random city name
|
24 | var city = data.city;
|
25 |
|
26 | // Define custom generator
|
27 | data.define('point', function() {
|
28 | return {
|
29 | x: Math.random(),
|
30 | y: Math.random()
|
31 | };
|
32 | });
|
33 |
|
34 | // Generate random point
|
35 | var point = data.point;
|
36 |
|
37 | // And so on..
|
38 | ```
|
39 |
|
40 | Random Fixture Data uses javascript properties for common generators so you don't need to use function call operator
|
41 |
|
42 | ## Embedded generators
|
43 |
|
44 | ```javascript
|
45 |
|
46 | // Address
|
47 |
|
48 | casual.country // 'United Kingdom'
|
49 | casual.city // 'New Ortiz chester'
|
50 | casual.zip // '26995'
|
51 | casual.street // 'Jadyn Islands'
|
52 | casual.address // '6390 Tremblay Pines Suite 784'
|
53 | casual.address1 // '8417 Veda Circles'
|
54 | casual.address2 // 'Suite 648'
|
55 | casual.state // 'Michigan'
|
56 | casual.state_abbr // 'CO'
|
57 | casual.stateAbbr // 'CO'
|
58 | casual.latitude // 90.0610
|
59 | casual.longitude // 180.0778
|
60 | casual.building_number // 2413
|
61 | casual.buildingNumber // 2413
|
62 |
|
63 | // Text
|
64 |
|
65 | casual.sentence // 'Laborum eius porro consequatur.'
|
66 | casual.sentences(n = 3) // 'Dolorum fuga nobis sit natus consequatur. Laboriosam sapiente. Natus quos ut.'
|
67 | casual.title // 'Systematic nobis'
|
68 | casual.text // 'Nemo tempore natus non accusamus eos placeat nesciunt. et fugit ut odio nisi dolore non ... (long text)'
|
69 | casual.description // 'Vel et rerum nostrum quia. Dolorum fuga nobis sit natus consequatur.'
|
70 | casual.short_description // 'Qui iste similique iusto.'
|
71 | casual.shortDescription // 'Qui iste similique iusto.'
|
72 | casual.string // 'saepe quia molestias voluptates et'
|
73 | casual.word // 'voluptatem'
|
74 | casual.words(n = 7) // 'sed quis ut beatae id adipisci aut'
|
75 | casual.array_of_words(n = 7) // [ 'voluptas', 'atque', 'vitae', 'vel', 'dolor', 'saepe', 'ut' ]
|
76 | casual.arrayOfWords(n = 7) // [ 'voluptas', 'atque', 'vitae', 'vel', 'dolor', 'saepe', 'ut' ]
|
77 | casual.letter // 'k'
|
78 |
|
79 | // Internet
|
80 |
|
81 | casual.ip // '21.44.122.149'
|
82 | casual.domain // 'darrion.us'
|
83 | casual.url // 'germaine.net'
|
84 | casual.email // 'Josue.Hessel@claire.us'
|
85 |
|
86 | // Person
|
87 |
|
88 | casual.name // 'Alberto'
|
89 | casual.username // 'Darryl'
|
90 | casual.first_name // 'Derek'
|
91 | casual.firstName // 'Derek'
|
92 | casual.last_name // 'Considine'
|
93 | casual.lastName // 'Considine'
|
94 | casual.full_name // 'Kadin Torphy'
|
95 | casual.password // '(205)580-1350Schumm'
|
96 | casual.name_prefix // 'Miss'
|
97 | casual.namePrefix // 'Miss'
|
98 | casual.name_suffix // 'Jr.'
|
99 | casual.nameSuffix // 'Jr.'
|
100 | casual.company_name // 'Cole, Wuckert and Strosin'
|
101 | casual.companyName // 'Cole, Wuckert and Strosin'
|
102 | casual.company_suffix // 'Inc'
|
103 | casual.companySuffix // 'Inc'
|
104 | casual.catch_phrase // 'Synchronised optimal concept'
|
105 | casual.catchPhrase // 'Synchronised optimal concept'
|
106 | casual.phone // '380 82 790 25 92'
|
107 |
|
108 | // Numbers
|
109 |
|
110 | casual.random // 0.7171590146608651 (core generator)
|
111 | casual.integer(from = -1000, to = 1000) // 632
|
112 | casual.double(from = -1000, to = 1000) // -234.12987444
|
113 | casual.array_of_digits(n = 7) // [ 4, 8, 3, 1, 7, 6, 6 ]
|
114 | casual.arrayOfDigits(n = 7) // [ 4, 8, 3, 1, 7, 6, 6 ]
|
115 | casual.array_of_integers(n = 7) // [ -105, -7, -532, -596, -430, -957, -234 ]
|
116 | casual.arrayOfIntegers(n = 7) // [ -105, -7, -532, -596, -430, -957, -234 ]
|
117 | casual.arrayOfDoubles(n = 7) // [ -866.3755785673857, -166.62194719538093, ...]
|
118 |
|
119 | // Date
|
120 |
|
121 | casual.unix_time // 659897901
|
122 | casual.moment // moment.js object see http://momentjs.com/docs/
|
123 | casual.date(format = 'YYYY-MM-DD') // '2001-07-06' (see available formatters http://momentjs.com/docs/#/parsing/string-format/)
|
124 | casual.time(format = 'HH:mm:ss') // '03:08:02' (see available formatters http://momentjs.com/docs/#/parsing/string-format/)
|
125 | casual.century // 'IV'
|
126 | casual.am_pm // 'am'
|
127 | casual.day_of_year // 323
|
128 | casual.dayOfYear // 323
|
129 | casual.day_of_month // 9
|
130 | casual.dayOfMonth // 9
|
131 | casual.day_of_week // 4
|
132 | casual.dayOfWeek // 4
|
133 | casual.month_number // 9
|
134 | casual.monthNumber // 9
|
135 | casual.month_name // 'March'
|
136 | casual.monthName // 'March'
|
137 | casual.year // 1990
|
138 | casual.timezone // 'America/Miquelon'
|
139 |
|
140 | // Payments
|
141 |
|
142 | casual.card_type // 'American Express'
|
143 | casual.cardType // 'American Express'
|
144 | casual.card_number(vendor) // '4716506247152101' (if no vendor specified then random)
|
145 | casual.cardNumber(vendor) // '4716506247152101' (if no vendor specified then random)
|
146 | casual.card_exp // '03/04'
|
147 | casual.cardExp // '03/04'
|
148 | casual.card_data // { type: 'MasterCard', number: '5307558778577046', exp: '04/88', holder_name: 'Jaron Gibson' }
|
149 | casual.cardData // { type: 'MasterCard', number: '5307558778577046', exp: '04/88', holder_name: 'Jaron Gibson' }
|
150 |
|
151 | // Misc
|
152 |
|
153 | casual.country_code // 'ES'
|
154 | casual.countryCode // 'ES'
|
155 | casual.language_code // 'ru'
|
156 | casual.languageCode // 'ru'
|
157 | casual.locale // 'hi_IN'
|
158 | casual.mime_type // 'audio/mpeg'
|
159 | casual.mimeType // 'audio/mpeg'
|
160 | casual.file_extension // 'rtf'
|
161 | casual.fileExtension // 'rtf'
|
162 |
|
163 | // Colors
|
164 |
|
165 | casual.color_name // 'DarkOliveGreen'
|
166 | casual.colorName // 'DarkOliveGreen'
|
167 | casual.safe_color_name // 'maroon'
|
168 | casual.safeColorName // 'maroon'
|
169 | casual.rgb_hex // '#2e4e1f'
|
170 | casual.rgbHex // '#2e4e1f'
|
171 | casual.rgb_array // [ 194, 193, 166 ]
|
172 | casual.rgbArray // [ 194, 193, 166 ]
|
173 | ```
|
174 |
|
175 | ## Define custom generators
|
176 |
|
177 | ```javascript
|
178 | casual.define('user', function() {
|
179 | return {
|
180 | email: casual.email,
|
181 | firstname: casual.first_name,
|
182 | lastname: casual.last_name,
|
183 | password: casual.password
|
184 | };
|
185 | });
|
186 |
|
187 | // Generate object with randomly generated fields
|
188 | var user = casual.user;
|
189 | ```
|
190 |
|
191 | If you want to pass some params to your generator:
|
192 |
|
193 | ```javascript
|
194 | casual.define('profile', function(type) {
|
195 | return {
|
196 | title: casual.title,
|
197 | description: casual.description,
|
198 | type: type || 'private'
|
199 | };
|
200 | });
|
201 |
|
202 | // Generate object with random data
|
203 | var profile = casual.profile('public');
|
204 | ```
|
205 |
|
206 | NOTE: if getter function has non-empty arguments list then generator should be called as function `casual.profile('public')`,
|
207 | otherwise it should be accessed as property `casual.profile`.
|
208 |
|
209 | ## Localization
|
210 |
|
211 | You can get localized version of casual generator:
|
212 |
|
213 | ```javascript
|
214 | var casual = require('casual').ru_RU;
|
215 | casual.street; // 'Бухарестская'
|
216 | ```
|
217 |
|
218 | Default locale is `en_US`.
|
219 |
|
220 | See [src/providers/{{locale}}](https://github.com/boo1ean/casual/blob/master/locales.md) for more details about available locales and locale specific generators.
|
221 |
|
222 | If you don't find necessary locale, please create an issue or just [add it](#contributing) :)
|
223 |
|
224 | ## Helpers
|
225 |
|
226 | #### random_element
|
227 |
|
228 | Get random array element
|
229 |
|
230 | ```javascript
|
231 | var item = casual.random_element(['ball', 'clock', 'table']);
|
232 | ```
|
233 |
|
234 | #### random_value
|
235 |
|
236 | Extract random object value
|
237 |
|
238 | ```javascript
|
239 | var val = casual.random_value({ a: 1, b: 3, c: 42 });
|
240 | // val will be equal 1 or 3 or 42
|
241 | ```
|
242 |
|
243 | #### random_key
|
244 |
|
245 | Extract random object key
|
246 |
|
247 | ```javascript
|
248 | var val = casual.random_key({ a: 1, b: 3, c: 42 });
|
249 | // val will be equal 'a' or 'b' or 'c'
|
250 | ```
|
251 |
|
252 | #### populate
|
253 |
|
254 | Replace placeholders with generators results
|
255 |
|
256 | ```javascript
|
257 | casual.populate('{{email}} {{first_name}}');
|
258 | // 'Dallin.Konopelski@yahoo.com Lyla'
|
259 | ```
|
260 |
|
261 | #### populate_one_of
|
262 |
|
263 | Pick random element from given array and populate it
|
264 |
|
265 | ```javascript
|
266 | var formats = ['{{first_name}}', '{{last_name}} {{city}}'];
|
267 | casual.populate_one_of(formats);
|
268 |
|
269 | // Same as
|
270 |
|
271 | casual.populate(casual.random_element(formats));
|
272 | ```
|
273 |
|
274 | #### numerify
|
275 |
|
276 | Replace all `#` in string with digits
|
277 |
|
278 | ```javascript
|
279 | var format = '(##)-00-###-##';
|
280 | casual.numerify(format); // '(10)-00-843-32'
|
281 | ```
|
282 |
|
283 | #### define
|
284 |
|
285 | [See custom generators](#define-custom-generators)
|
286 |
|
287 | #### register_provider
|
288 |
|
289 | Register generators provider
|
290 |
|
291 | ```javascript
|
292 | var words = ['flexible', 'great', 'ok', 'good'];
|
293 | var doge_provider = {
|
294 | such: function() {
|
295 | return 'such ' + casual.random_element(words);
|
296 | },
|
297 |
|
298 | doge_phrase: function() {
|
299 | return 'wow ' + provider.such();
|
300 | }
|
301 | };
|
302 |
|
303 | casual.register_provider(doge_provider);
|
304 |
|
305 | casual.such; // 'such good'
|
306 | casual.doge_phrase; // 'wow such flexible'
|
307 | ```
|
308 |
|
309 | ## Seeding
|
310 |
|
311 | If you want to use a specific seed in order to get a repeatable random sequence:
|
312 |
|
313 | ```javascript
|
314 | casual.seed(123);
|
315 | ```
|
316 |
|
317 | It uses [Mersenne Twister](https://github.com/boo1ean/mersenne-twister) pseudorandom number generator in core.
|
318 |
|
319 | ## Generators functions
|
320 |
|
321 | If you want to pass generator as a callback somewhere or just hate properties you always can access generator **function** at `casual._{generator}`
|
322 |
|
323 | ```javascript
|
324 | // Generate value using function
|
325 | var title = casual._title();
|
326 | // Same as
|
327 | var title = casual.title;
|
328 |
|
329 | // Pass generator as callback
|
330 | var array_of = function(times, generator) {
|
331 | var result = [];
|
332 |
|
333 | for (var i = 0; i < times; ++i) {
|
334 | result.push(generator());
|
335 | }
|
336 |
|
337 | return result;
|
338 | };
|
339 |
|
340 | // Will generate array of five random timestamps
|
341 | var array_of_timestamps = array_of(5, casual._unix_time);
|
342 | ```
|
343 |
|
344 | Or you can get functional version of casual generator:
|
345 |
|
346 | ```javascript
|
347 | var casual = require('casual').functions();
|
348 |
|
349 | // Generate title
|
350 | casual.title();
|
351 |
|
352 | // Generate timestamp
|
353 | casual.unix_time();
|
354 | ```
|
355 |
|
356 | ## View providers output cli
|
357 |
|
358 | There is a simple cli util which could be used to view/debug providers output:
|
359 |
|
360 | # Will render table with columns [generator_name, result] for all providers
|
361 | node utils/show.js
|
362 |
|
363 | # Will render table with columns [generator_name, result] only for person provider
|
364 | node utils/show.js person
|
365 |
|
366 | ## Contributing
|
367 |
|
368 | - [Adding new locale](https://github.com/boo1ean/casual/blob/master/locales.md)
|
369 |
|
370 | # License
|
371 |
|
372 | Heavily inspired by https://github.com/fzaninotto/Faker
|
373 |
|
374 | The MIT License (MIT)
|
375 | Copyright (c) 2014 Egor Gumenyuk <boo1ean0807@gmail.com>
|
376 |
|
377 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
378 | of this software and associated documentation files (the "Software"), to deal
|
379 | in the Software without restriction, including without limitation the rights
|
380 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
381 | copies of the Software, and to permit persons to whom the Software is
|
382 | furnished to do so, subject to the following conditions:
|
383 |
|
384 | The above copyright notice and this permission notice shall be included in all
|
385 | copies or substantial portions of the Software.
|
386 |
|
387 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
388 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
389 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
390 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
391 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
392 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
393 | OR OTHER DEALINGS IN THE SOFTWARE.
|