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