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

yod

v1.0.0-alpha.4

Published

Fantasy data generator.

Downloads

71

Readme

yod

NPM version GitHub version Build Status Dependency Status Inline docs Code Climate Coverage Status

Fantasy data generator.

Use yod-mock for more pre-defined types and modifiers..

Online Object Data Generator

Usage

Create a String type to generate random string.

yod.type('String', function(len) {
  len = len || Math.round(Math.random() * 10);
  var pool = 'abcdefghigklmnopqrstuvwxyz';
  var poolLen = pool.length;
  var result = '';
  for (var i = 0; i < poolLen; i++) {
    result += pool[Math.round(Math.random() * (poolLen - 1))]
  }
  return result;
});

Create a Int type to generate random integer.

yod.type('Int', function() {
  return Math.round(Math.random() * 100);
});

Create a User type.

yod.type('User', {
  name: '@String(5)',
  age: '@Int'
});

And then you can generate random User by calling yod('@User').

yod('@User'); 

// Will create a random object like: `{name: 'atx', age: 30}`.

Terminology

  • Caller: Prepend with a "@" character, follow by series string which like function calling.

    e.g: @Bool, @String.repeat(3).join(","), @Self.someKey.

  • Generator: The source that can generate other thing, in yod, generator can be anything.

API

yod(generator)

Parameter: generator, Type: *

Use the generator as a generator to get other generated value.

In generator, you can use caller string, you can execute javascript, you can get config value.

Yod example:

// Execute javascript, wrapped in "`" (just calling `eval` to get the result)
yod("` 1 + 3 `");     // => 4
yod("` 'a' + 3 `");   // => 'a3'


// Use caller string
yod({ a: "a", b: "a's value is @Self.a" });   // => {a: "a", b: "a's value is a"}


// get config value
yod.config("a.b", "1"); // At first, use yod.config set a value
yod("@Config.a"); // => {b: "1"}

yod.type(name, generator [, aliases...])

Parameter: name, Type: String

Parameter: generator, Type: *

Parameter: aliases, Type: String or String Array, optional

Create a new type, so that you can use it in caller string.

Type example:

// Create a Bool type, and alias Boolean. it will random return true or false.
yod.type('Bool', function() {

  return Math.round(Math.random() * 100) % 2 === 0;

}, 'Boolean');



// Call your new type.
yod('@Bool');     // return true or false.
yod('@Boolean');  // return true or false.

yod.types

All defined types.

yod.isTypeNameExists(name)

Check if the type name exists.

yod.isTypeNameValid(name)

Check if the type name is a valid.

yod.emptyTypes()

Remove all defined types.

yod.modifier([filters,] name, modifierFn)

Parameter: filters, Type: String or Function or Array of String/Function, optional

Parameter: name, Type: String

Parameter: modifierFn, Type: Function

Create a new modifier.

There are two type or modifier

  • Value modifier: modifier generator value.
  • Function modifier: modifier generator function. Create it with ":" prepend to name

Modifier example:

Create a value modifier: index —— Get array's index item

yod.modifier('Array', 'index', function(val, index) {
  return val[index];
});


// Use it

yod({
  a: ['a', 'b', 'c']
  b: '@Self.a.index[1]'
});
// => {a: ['a', 'b', 'c'], b: 'b'}

Create a function modifier: repeat —— Generate value array using generator function

yod.modifier('repeat', function(generatorFn, times) {
  var result = [];
  for (var i = 0; i < times; i++) {
    result.push(generatorFn());
  }
  return result;
});


// Use it (@Bool is defined in yod.type area)

yod('@Bool.repeat(3)');
// Will generator a array like this: [true, false, false]. the boolean value is random. 

yod.modifiers

All defined modifiers.

yod.isModifierNameExists(name)

Check if the modifier name exists.

yod.isModifierNameValid(name)

Check if the modifier name is a valid.

yod.emptyModifiers()

Remove all defined modifiers.

yod.config(key [, val] [, meta])

Parameter: key, Type: String

Parameter: val, Type: *

Parameter: meta, Type: *

Get or set a config key. When set a key, you can also set a meta on this key.

If you want get the value and the meta, you can append a string ":meta" to the key, then the result will be something like this: {val: ..., meta: ...}

Config example:

// Set
yod.config('a.b', 'ab');
yod.config('a.c', 'ac', 'ac-meta');


// Get
yod.config('a');        // => {b: 'ab', c: 'ac'}
yod.config('a:meta');   // => {val: {b: 'ab', c: 'ac'}, meta: undefined}
yod.config('a.c');      // => 'ac'
yod.config('a.c:meta'); // => {val: 'ac', meta: 'ac-meta'}


// Using in caller string. (You can't get meta data in this way, but you can get it by adding a modifier)
yod('@Config.a');       // => {b: 'ab', c: 'ac'}
yod('@Config.a.c');     // => 'ac'

yod.config.all

Type: Object

All config data.

yod.config.meta

Type: Object

All meta data.

Wrong user case

  • Caller string's arguments can not include ")".

    @String.replace(")", "") 
    // Will parsed to `@String.replace('"')`
  • Object generator can not recycle depends.

    yod({
      a: '@Self.b',
      b: '@Self.a'
    });
      
    // Will throw error.
  • Child object can't depend on it parents, similar to recycle depends.

    yod({
      a: {
        b: {
          c: '@Parent.Parent.a'
        }
      }
    });
      
    // Will throw error.

Install

Browser

bower install yod --save-dev

Node

npm install yod --save

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using gulp.

License

Copyright (c) 2015 Zhonglei Qiu. Licensed under the MIT license.