What is casual?
The casual npm package is a versatile library for generating random data. It can be used to create fake data for testing, prototyping, and development purposes. The package offers a wide range of functionalities, including generating random strings, numbers, dates, addresses, and more.
What are casual's main functionalities?
Random String Generation
Generates a random string. This can be useful for creating random usernames, passwords, or other text data.
const casual = require('casual');
console.log(casual.string);
Random Number Generation
Generates a random integer between the specified range. This is useful for creating random IDs, quantities, or other numerical data.
const casual = require('casual');
console.log(casual.integer(1, 100));
Random Date Generation
Generates a random date in the specified format. This can be useful for creating random timestamps, birthdates, or other date-related data.
const casual = require('casual');
console.log(casual.date('YYYY-MM-DD'));
Random Address Generation
Generates a random address. This can be useful for creating random location data for testing or development purposes.
const casual = require('casual');
console.log(casual.address);
Random Email Generation
Generates a random email address. This can be useful for creating random user data for testing or development purposes.
const casual = require('casual');
console.log(casual.email);
Other packages similar to casual
faker
Faker is a popular library for generating fake data. It offers a wide range of functionalities similar to casual, including generating random names, addresses, dates, and more. Faker is known for its extensive localization support, allowing users to generate data in various languages and formats.
chance
Chance is another library for generating random data. It provides a comprehensive set of functionalities, including random string, number, date, and address generation. Chance is known for its simplicity and ease of use, making it a good alternative to casual for basic random data generation needs.
random-js
Random-js is a library focused on generating random numbers and other random data. It offers a variety of random number generation methods, including uniform, normal, and exponential distributions. While it may not have as many features as casual, it excels in providing high-quality random number generation.
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
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.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.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.unix_time
casual.moment
casual.date(format = 'YYYY-MM-DD')
casual.time()
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.mime_type
casual.file_extension
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
.
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 });
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;
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
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.