npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prototoy

v0.1.0

Published

Lightweight JavaScript object generator for your node REPL (or elsewhere, I suppose)

Downloads

6

Readme

prototoy

Build Status

Lightweight JavaScript object generator for your node REPL (or elsewhere, I suppose).

Usage

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.

Locally

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

Globally

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...

API

All functions will create items of defaultLength length/size if no arguments are given.

array([length [, type|function])

Arguments

  • length - The length of the array to create, defaults to defaultLength
  • 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.

Returns

An array.

cycle([list])

Same as iterator(list, true)

json([properties])

Generate some JSON.

Can also be called as j.

Arguments

properties - An array of property names to use

Returns

A JSON string.

map([spec [, keyType [, valueType]]])

Can also be called as m.

Arguments

  • spec - An integer representing the number of entries to generate or an array of key names to use
  • keyType - The key's data type, defaults to String
  • valueType - The value's data type, defaults to an integer

Returns

An instance of Map populated according to the arguments.

object([spec])

Generate an object with some properties.

Can also be called as o.

Arguments

  • spec - An integer representing the number of properties to add to the object or an array of property names to create.

Returns

An object as defined by spec.

iterator(spec, [cycle = false])

Create an iterator. Can also be called as iter or i.

Arguments

  • spec - An integer representing the number of entries to generate or an array of values to use in the generator
  • cycle - If true the iterator's done property will never be false. You will iterate "forever".

Returns

An iteratable object create by a Generator.

set()

Same arguments as array(), excepts it returns a Set.

string([length])

Return a string containing length words. length defaults to defaultLength.

Can also be called as s.

See Also

Author

Skye Shaw (skye.shaw -AT- gmail)

License

Released under the MIT License: http://www.opensource.org/licenses/MIT