Fake number generators
A javascript library to generate infinite streams of numbers.
Install
Three options:
- A prebuilt and minified file at
dist/serial-liar.js
bower install serial-liar
npm install serial-liar
Example
var serialLiar = ; // not nescessary in browsers var data = serialLiar ; var oneNumberFromSerial = ; // or data.next();var arrayOfNumbersFromSerial = ; // or data.next(100);
API
// CREATING SERIAL NUMBER GENERATOR// A sequence which will always return 42serial = serialLiar;; // [42, 42, 42, 42, 42] // A sequence which adds 5 each timeserial = serialLiar;; // [1, 6, 11, 16, 21] // A sequence which multiplies by 2 each timeserial = serialLiar;; // [1, 2, 4, 8, 16] // A sequence which cycles on a given patternserial = serialLiar;; // [4, 8, 15, 16, 23] // A random walk incrementing or decrementing between -1 and 1 randomly// An optional start parameter can be providedserial = serialLiar;; // [0, 0.12, 0.2234, 1.20034, 0.50403] // RETRIEVING NUMBERS// Get the next number; // or serial.next(); // Get the next 1000 numbers as an array; // or serial.next(1000); // MANIPULATING SERIAL NUMBER GENERATORS// Add multiple serials togetherserial; // Add 5 to all numbers coming out of this generator. This is exactly the// same as serial.add(serialLiar.constant(5))serial; // Multiply multiple serials together (in order, with the target serial being first).// If the next numbers from these serials would have been 1 (serial), 2 (otherSerial)// and 3 (andAnotherSerial), then the next call to the serial would result in 9.serial; // Scale a serial, multiplying each result by 10. This is exactly the same as// serial.multiply(serialLiar.multiply(10));serial; // or serial.mul(10) // Subtract, divide, and shorthands behave similar to .add and .multiplyserial; // or serial.sub(anotherSerial)serial; // or serial.div(5) // Transform the results coming out of a generatorserial;
Developing
You'll need Node.js and Grunt (npm install -g grunt-cli
after installing node) to develop this package.
git clone git@github.com:oztu/serial-liar.git
npm install
grunt dev
This will build the project, watch for changes to do rebuilds, and open a server listening on localhost:8000 where you can view the example as you develop.
Credits
This was inspired by Alan Kang's project, dgen (https://github.com/akngs/dgen), which has a very similar feature set to this pacakge. I decided to rewrite the functionality because dgen required RequireJS, was written in TypeScript, and I found the interface clunky.