UNPKG

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