Code Monkey home page Code Monkey logo

adleroliveira.dreamjs's Introduction

Build Status

Dream Logo

A lightweight json data generator.

This library can output random data from a Json Schema using standard types like String, Number, Date, Boolean, Array, or with the 60+ built-in custom types like Name, Age, Address, Word, Sentence, paragraph, gender, (RGB) color etc.

The built-in Custom Types are mostly provided by the module Chance but the library also allows you to create your own Custom Types.

It can be used with multiple Schemas that can be selected before usage and the it is also chainable, meaning that you can chain several configurations before finally output the processed json.

Installation

npm install --save dreamjs

Usage

Hello World

var dream = require('dreamjs');

var helloworld = dream.output();

The variable helloworld now contains:

{ Dream: 'Hello World' }

Callbacks

Currently there is two ways you can get the output from DreamJS. By storing it in a variable or by passing a callback to it to receive the result. In the future it will also be possible to use it with Promises and Streams.

Storing in a variable:

var helloword = dream.output();

Passing a callback:

dream.output(function (err, result) {
  console.log(result);
});

Generic Schema / Named Schema

The simplest way to use DreamJS is by defining a generic schema. If any other generic Schema is created after that it will replace the previous one.

var data = dream
  .schema({
    name: String
  })
  .output();

The variable data now contains:

{ name: '' }

It is also possible to create named Schemas and use them when necessary.

dream.schema('User', {
  name: String,
  age: Number
});

dream.schema('Location', {
  address: String,
  postcode: Number
});

var data = dream
  .useSchema('Location')
  .output();

The variable data now contains:

{ address: '', postcode: 0 }

Generate() and GenerateRnd()

The methods Generate() and GenerateRnd() will generate a given amount of instances of the selected Schema respecting the data types specified on the Schema. The method Generate() will bring empty values and the method GenerateRnd() will bring values with random data.

dream.schema('User', {
  name: String
});

var data1 = dream
  .useSchema('User')
  .generate(3)
  .output();

var data2 = dream
  .useSchema('User')
  .generateRnd(3)
  .output();

The variable data1 and data2 now contains:

// data1
[ { name: '' }, { name: '' }, { name: '' } ]

// data2
[ { name: 'Jlxokrs' }, { name: 'oHiklkss' }, { name: 'mNeiOlsaa' } ]

Custom Types

DreamJS comes with the power of the Chance library integrated with it and allow you to use their 60+ random generator as built-in Custom Types. It is also possible to create your own Custom Types or just just pass a function or a RegularExpression statement to use a generic Custom Type.

A full list of all the possible built-in Custom Types provided by chance can be found on their website: http://chancejs.com

dream.customType('pi', function () {
  return Math.PI;
});

dream.customType('hello', /hello+ (world|to you)/);

dream
  .schema({
    name: 'name',
    age: 'age',
    address: 'address',
    contact: {
      phone: 'phone',
      servicePhone: /^(800[1-9]{6})$/
    },
    foo: function () {
      return 'bar';
    },
    pi: 'pi',
    hello: 'hello'
  })
  .generateRnd(2)
  .output(function (err, result) {
    console.log(result);
  });

Result:

[
  {
    name: 'Dorothy Conner',
    age: 32,
    address: '702 Kimes Extension',
    contact: {
      phone: '(985) 255-2142',
      servicePhone: '800493159'
    },
    foo: 'bar',
    pi: 3.141592653589793,
    hello: 'hellooooooooooo world'
  },
  {
    name: 'Nathaniel Payne',
    age: 50,
    address: '1811 Bani Manor',
    contact: {
      phone: '(212) 389-6644',
      servicePhone: '800157977'
    },
    foo: 'bar',
    pi: 3.141592653589793,
    hello: 'hellooooooooooooooooooooooo to you'
  }
]

Dream Helper

Whenever you build your own Custom Type, DreamJS provides to your Custom Type callback a helper object that contains some useful tools like:

Chance Instance

An instance of the library chance, that allows you to make full use of their methods with custom configuration.

dream.customType('FiveWordsSentence', function (helper) {
  return helper.chance.sentence({words: 5});
});

dream
  .schema({
    phrase: 'FiveWordsSentence'
  })
  .generateRnd(2)
  .output(function (err, result) {
    console.log(result);
  });

Result:

[
  { phrase: 'Laf woelimev vu vazpazap upecu.' },
  { phrase: 'Batunwa ziti laralsuc ve rudeoze.' }
]

Input

It is possible to define an input to the DreamJS data flow, that will be available through the helper so you can use this data to interact with your Custom Type.

dream.customType('customTypeWithInput', function (helper) {
  return helper.input.value;
});

dream
  .input({value: 'Provided by an input'})
  .schema({
    result: 'customTypeWithInput'
  })
  .generateRnd()
  .output(function (err, result) {
    console.log(result);
  });

Result:

{ result: 'Provided by an input' }

oneOf()

A method that allows you to pick a single random value from a provided array. This way you can explicitly specify the scope of what is being returned from your Custom Type.

dream.customType('iceCreamTruckDay', function (helper) {
  var businessDays = ['Monday', 'Wednesday', 'Friday'];
  return helper.oneOf(businessDays);
});

dream
  .schema({
    iceCreamDay: 'iceCreamTruckDay'
  })
  .generateRnd(2)
  .output(function (err, result) {
    console.log(result);
  });

Result:

[
  { iceCreamDay: 'Wednesday' },
  { iceCreamDay: 'Friday' }
]

Previous Item

The helper also exposes the previous generated item to the current one. This way, you will always have access to previous generated values if you need them to generate the next ones, like for example an incremental id.

dream.schema('User', {
	id: 'incrementalId',
	name: 'name',
	age: 'age'
});

dream.customType('incrementalId', function(helper){
	return helper.previousItem ? helper.previousItem.id+1 : 1;
});

dream
	.useSchema('User')
	.generateRnd(5)
	.output(function(err, result){
		console.log(result);
	});

Result:

[ 
  { id: 1, name: 'Bess Franklin', age: 60 },
  { id: 2, name: 'Verna Brown', age: 45 },
  { id: 3, name: 'Travis Poole', age: 42 },
  { id: 4, name: 'Theresa Sanchez', age: 29 },
  { id: 5, name: 'Ruth Lane', age: 33 } 
]

TODO

The next step is to update DreamJS to allow the use with promises and streams.

adleroliveira.dreamjs's People

Contributors

adleroliveira avatar ramikhalaf avatar yikaj avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.