UNPKG

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