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

@unpourtous/mocker

v1.1.16

Published

Mocker data generator

Downloads

17

Readme

FOSSA Status npm version Open Source Love JavaScript Style Guide

Mocker

0. Introduction

Mocker is a simple and lightweight library which is used for generating random js object as defined schema.

Features

  1. Lightweight: Only 4 basic type, pretty easy to get start.
  2. Extendable: String type support RegExp which allow your to generate custom type, but we support array and object too
  3. Non-invasive: Keep all properties key clean, do not modify it since you may need it anywhere else.

1. Installation

npm install @unpourtous/mocker --save

2. Usage

a) First, define your object, the object schema.

import { Types } from '@unpourtous/mocker'
const objectSchema = {
  stringDate: Types.string('date'),
  stringRange: Types.string().range(10, 100),
  numberRange: Types.number().range(0, 100),
  enum: Types.enum(['A', 'B', 'C']),
  fixNumber: 520, // If a valid type was set, the valid type will be the value
  fixString: 'this is a fixString',
  plainObject: { // define a object array 
    far: Types.string(),
    bar: Types.number()
  },  
  stringArray: [Types.string()],  // define a primary type array
  objectArray: [{ // define a object array 
    far: Types.string(),
    bar: Types.number()
  }]
}

b) Secondly, use Mocker to generate a random object.

import { Mocker } from '@unpourtous/mocker'
const mockerObject = Mocker.mockObject(objectSchema)

// the mockerObject should like 
// { 
//   stringDate: '1941-10-03',
//   stringRange: 'PKlDGzTY10pMZjYDIMtEWThTlXGcQE1gp2rYcStO2n52vw',
//   numberRange: 62.61,
//   enum: 'B',
//   default: 'GXvmNy',
//   fixNumber: 520, 
//   fixString: 'this is a fixString',
//   plainObject: { far: 'O0qBn', bar: 464255.58 },
//   stringArray: [ 'X5iH5' ],
//   objectArray: [ { far: '', bar: 574378.47 } ] 
// }

3. Detail Types & API

String

API | Description --- | --- string() | Create a instance of TPString. range(min, max) | Set min and max length for string, this is only used if this string dose not set regExp regExp(regExp) | Set regExp for this string, if this is set, the generated string match this regexp. fixValue(fixValue) | Set fix value

Number

API | Description --- | --- number() | Create a instance of Number. range (min, max) | Set min value and max value for number format(format) | Set number format, get more detail from Numeral.js fixValue(fixValue) | Set fix value

Enum

API | Description --- | --- enum(enumValues) | Create a instance of Enum. enumValues should be a array containers all possible value.

Boolean

API | Description --- | --- boolean() | Create a instance of Boolean. fixValue(fixValue) | Set fix value

APIS

4. Test & Demo

All test case was put in test/.

Test result:

image

You can also clone it run the test by youself.

git clone https://github.com/UnPourTous/mocker.git
cd mocker
yarn 
yarn run test

5. Thanks

  • Randexp.js used to generate random string match a specified regexp
  • Numeral.js as number format library
  • Chai as test assert library
  • Mocha as test framework

6. TODO

  1. Add browser support

7. License

MIT

Refs

  • http://babeljs.io/docs/plugins/transform-es2015-modules-umd/
  • http://krasimirtsonev.com/blog/article/javascript-library-starter-using-webpack-es6
  • https://github.com/webpack/webpack/issues/2785#issuecomment-293584946
  • https://github.com/gaearon/redux-thunk/issues/43

FOSSA Status