Chance - Your All-Purpose JavaScript Data Generator
Written by Ian Elliot   
Wednesday, 24 July 2013

So you need to test your program, but where do you get some test data from? Chance is a small JavaScript library that will generate random data of various types - like telephone numbers - for you. 

The temptation is to headline this piece with "Chance would be a fine thing", but it would really have to be "Chance is a fine thing" to avoid misleading the reader. Chance is a relatively new library of JavaScript functions which generate test data. 

 

chancebanner

 

For example if you need a random zip code you simply use:

chance.zip()

and you get a random number with the characteristics of a zip code. The same for

chance.phone()

but notice that these aren't necessarily "real" in the sense that the zip code or the phone number might not actually exist, they simply fit the pattern for the type of data so that you can feed them to a function that is supposed to work with them. 

To use it all you have to do is include the library, about 5Kbytes minified and zipped, and start using it. It creates a global chance object but you can also create additional chance objects and initialize the seed for the random number generator so to produce a repeatable pseudo-random sequence.  It works in the browser or with Node.js. 

As well as generating text and formatted data types, it also has some random generators for primitive types - Boolean, character, integer and so on and, yes there is a dice! 

Although the library is quite young, it has a good collection of advanced generators. For example, it will generate random sentences, paragraphs, names, domains, email addresses, postal addresses, time stamps, credit card numbers and, my own favorite, don't ask why, random GUIDs.  For a full list of generators, see the Chance website.

The library is available under an Open Source MIT licence and there is lots of scope for adding additional generators. Given that each generator is small and fairly standalone it would make a good starting point for anyone wanting to get involved in a first open source project. 

 

chanceicon

More Information

Chance website

Chance GitHub

Related Articles

Inside Random Numbers       

The Monte Carlo Method

 

Task.js Asynchronous Tasks In JavaScript

 

JavaScript Physics Playground

 

Tearable Cloth In JavaScript

 

JavaScript Assembly Language

 

Tetris In 140 Bytes

 

Ray Tracer in JavaScript 

      

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus

 

Banner


GCC Gets An Award From ACM And A Blast From Linus
28/07/2014

The venerable GCC compiler got itself into the news by winning an award from the ACM and by attracting a typical Linus Torvalds blast of rhetoric for an "interesting" bug.



1000 Kilobots Make Patterns
17/08/2014

Self organizing swarms are fascinating. If you have grown bored with watching ants or termites do their thing, you can now watch a swarm of 1000 tiny robots making shapes without anyone having overall [ ... ]


More News

 

 

 

 

Last Updated ( Wednesday, 24 July 2013 )
 
 

   
RSS feed of news items only
I Programmer News
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.