Fake data generator
Installation
npm install casual
Usage
var casual = require('casual');
var sentence = casual.sentence;
var city = casual.city;
casual.define('point', function() {
return {
x: Math.random(),
y: Math.random()
};
});
var point = casual.point;
Casual uses javascript properties for common generators so you don't need to use function call operator
Embedded generators
casual.country
casual.city
casual.zip(digits = {5, 9})
casual.street
casual.address
casual.address1
casual.address2
casual.state
casual.state_abbr
casual.latitude
casual.longitude
casual.building_number
casual.sentence
casual.sentences(n = 3)
casual.title
casual.text
casual.description
casual.short_description
casual.string
casual.word
casual.words(n = 7)
casual.array_of_words(n = 7)
casual.letter
casual.ip
casual.domain
casual.url
casual.email
casual.user_agent
casual.name
casual.username
casual.first_name
casual.last_name
casual.full_name
casual.password
casual.name_prefix
casual.name_suffix
casual.company_name
casual.company_suffix
casual.catch_phrase
casual.phone
casual.random
casual.integer(from = -1000, to = 1000)
casual.double(from = -1000, to = 1000)
casual.array_of_digits(n = 7)
casual.array_of_integers(n = 7)
casual.array_of_doubles(n = 7)
casual.coin_flip
casual.unix_time
casual.moment
casual.date(format = 'YYYY-MM-DD')
casual.time(format = 'HH:mm:ss')
casual.century
casual.am_pm
casual.day_of_year
casual.day_of_month
casual.day_of_week
casual.month_number
casual.month_name
casual.year
casual.timezone
casual.card_type
casual.card_number(vendor)
casual.card_exp
casual.card_data
casual.country_code
casual.language_code
casual.locale
casual.currency
casual.currency_code
casual.currency_symbol
casual.currency_name
casual.mime_type
casual.file_extension
casual.boolean
casual.color_name
casual.safe_color_name
casual.rgb_hex
casual.rgb_array
Define custom generators
casual.define('user', function() {
return {
email: casual.email,
firstname: casual.first_name,
lastname: casual.last_name,
password: casual.password
};
});
var user = casual.user;
If you want to pass some params to your generator:
casual.define('profile', function(type) {
return {
title: casual.title,
description: casual.description,
type: type || 'private'
};
});
var profile = casual.profile('public');
NOTE: if getter function has non-empty arguments list then generator should be called as function casual.profile('public')
,
otherwise it should be accessed as property casual.profile
.
Localization
You can get localized version of casual generator:
var casual = require('casual').ru_RU;
casual.street;
Default locale is en_US
.
See src/providers/{{locale}} for more details about available locales and locale specific generators.
If you don't find necessary locale, please create an issue or just add it :)
Helpers
random_element
Get random array element
var item = casual.random_element(['ball', 'clock', 'table']);
random_value
Extract random object value
var val = casual.random_value({ a: 1, b: 3, c: 42 });
random_key
Extract random object key
var val = casual.random_key({ a: 1, b: 3, c: 42 });
populate
Replace placeholders with generators results
casual.populate('{{email}} {{first_name}}');
populate_one_of
Pick random element from given array and populate it
var formats = ['{{first_name}}', '{{last_name}} {{city}}'];
casual.populate_one_of(formats);
casual.populate(casual.random_element(formats));
numerify
Replace all #
in string with digits
var format = '(##)-00-###-##';
casual.numerify(format);
define
See custom generators
register_provider
Register generators provider
var words = ['flexible', 'great', 'ok', 'good'];
var doge_provider = {
such: function() {
return 'such ' + casual.random_element(words);
},
doge_phrase: function() {
return 'wow ' + provider.such();
}
};
casual.register_provider(doge_provider);
casual.such;
casual.doge_phrase;
Seeding
If you want to use a specific seed in order to get a repeatable random sequence:
casual.seed(123);
It uses Mersenne Twister pseudorandom number generator in core.
Generators functions
If you want to pass generator as a callback somewhere or just hate properties you always can access generator function at casual._{generator}
var title = casual._title();
var title = casual.title;
var array_of = function(times, generator) {
var result = [];
for (var i = 0; i < times; ++i) {
result.push(generator());
}
return result;
};
var array_of_timestamps = array_of(5, casual._unix_time);
Or you can get functional version of casual generator:
var casual = require('casual').functions();
casual.title();
casual.unix_time();
View providers output cli
There is a simple cli util which could be used to view/debug providers output:
# Will render table with columns [generator_name, result] for all providers
node utils/show.js
# Will render table with columns [generator_name, result] only for person provider
node utils/show.js person
Browserify support
Currently you can't use casual with browserify. Please check out this browserify-friendly fork Klowner/casual-browserify
Contributing
License
Heavily inspired by https://github.com/fzaninotto/Faker
The MIT License (MIT)
Copyright (c) 2014 Egor Gumenyuk boo1ean0807@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.