UNPKG

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