Lightweight JavaScript object generator for your node
REPL (or elsewhere, I suppose).
prototoy is not intended to be a full-fledged object generation library rather, it's intended for use in your Node.js REPL when you need a quick dummy object to toy around with.
It's recommended that you load prototoy when you run node
interactively.
I recommend setting up a .noderc
file
with the following:
const t = require('prototoy');
If you don't want to setup a .noderc
file how to do this depends on whether you want it available locally or globally.
npm install prototoy
or yarn install prototoy
Load prototoy when you run node
as a REPL in your project:
node -e 'const t = require("prototoy")' -i
npm install prototoy -g
or yarn install prototoy -g
Load prototoy when you run node
as a REPL. Here's one way to do it:
# In your shell's config (Bash here):
inode() {
NODE_PATH=$(npm config get prefix)/lib/node_modules node -e 'const t = require("prototoy")' -i
}
Now you can easily generate objects to test things with:
> t.string(5) // or t.str() or t.s()
'd zzzz ww uuuuuuu aa'
> t.array() // or t.a()
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
> t.array(5, Date)
[ 2018-02-11T05:50:06.271Z,
2018-02-12T05:50:06.271Z,
2018-02-13T05:50:06.271Z,
2018-02-14T05:50:06.271Z,
2018-02-15T05:50:06.271Z ]
> t.obj() // or t.o() or t.object()
{ a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10 }
> t.obj(2)
{ a: 1, b: 2 }
> t.obj(['foo', 'bar'])
{ foo: 1, bar: 2 }
> i = t.iter(2) // or t.i()
{}
> i.next()
{ value: 1, done: false }
> i.next()
{ value: 2, done: false }
> i.next()
{ value: undefined, done: true }
> i = t.iter(2, true) // true means it will cycle; also: t.cycle()
{}
> i.next()
{ value: 1, done: false }
> i.next()
{ value: 2, done: false }
> i.next()
{ value: 1, done: false }
> i.next()
{ value: 2, done: false }
> i.next()
{ value: 1, done: false }
...
> i = t.iter(['foo', 'bar'], true)
{}
> i.next()
{ value: 'foo', done: false }
> i.next()
{ value: 'bar', done: false }
> i.next()
{ value: 'foo', done: false }
...
> t.map(2) // or t.m()
Map { 'a' => 1, 'b' => 2 }
> t.map(2, Number)
Map { 1 => 1, 2 => 2 }
> t.map(2, Number, Boolean)
Map { 1 => true, 2 => false }
And others...
All functions will create items of defaultLength
length
/size
if no arguments are given.
length
- The length of the array to create, defaults todefaultLength
type|function
- The type of object to generate or a function that will be called to generate each element.
type
can be one of Date
, Number
, Object
, Boolean
or String
.
The function's signature is callback(index)
, where index
starts at 0
.
An array.
Same as iterator(list, true)
Generate some JSON.
Can also be called as j
.
properties
- An array of property names to use
A JSON string.
Can also be called as m
.
spec
- An integer representing the number of entries to generate or an array of key names to usekeyType
- The key's data type, defaults toString
valueType
- The value's data type, defaults to an integer
An instance of Map
populated according to the arguments.
Generate an object with some properties.
Can also be called as o
.
spec
- An integer representing the number of properties to add to the object or an array of property names to create.
An object as defined by spec
.
Create an iterator. Can also be called as iter
or i
.
spec
- An integer representing the number of entries to generate or an array of values to use in the generatorcycle
- Iftrue
the iterator'sdone
property will never befalse
. You will iterate "forever".
An iteratable object create by a Generator.
Same arguments as array()
, excepts it returns a Set
.
Return a string containing length
words. length
defaults to defaultLength
.
Can also be called as s
.
- Pry-Toys - The Ruby library that served as inspiration
- randexp
- Chance
- randomstring
- random-input-generator
- templated-object-generator
- unionized
Skye Shaw (skye.shaw -AT- gmail)
Released under the MIT License: http://www.opensource.org/licenses/MIT