Skip to content
qiu8310Public

About

Powerful data generator tool.

Resources

Stars

122 stars

Watchers

15 watching

Forks

Repository files navigation

yod-mock

Join the chat at https://gitter.im/qiu8310/yod-mock NPM version Build Status Coverage Status Dependency Status

本项目主要用来在测试环境中生成测试数据用的,另外也可以在命令行上快速生成简单测试数据。

背景

  • 前端程序员会因为后端没有接口数据,开发某些功能可能就不太方便
  • 而后端程序员常会被前端程序员催促给接口,给数据,但你也还没开发完时会显得他们很烦人

此工具就是希望在前后端都没有数据的情况下生成大量随机的测试数据

Features:

  • 语法简洁,学习成本低,功能强大
  • 支持所有 JS 原生的函数
  • 支持所有 lodash 的函数
  • 支持自定义新的 type 和 modifier
  • 支持配置系统现有的数据(使用 yod.config('system.xxx', ...))

在线体验本项目的功能

Install

CLI

npm install --global yod-mock
  • Using yod image to get a random image
  • Using yod image -c to get a random image and copy it to clipboard
  • Using yod image .repeat 10 to get 10 random images

Node.js

npm install --save-dev yod-mock

Browser

bower install --save-dev yod-mock

Usage

DEMO:(Edit this demo online)

/*

@Self:   Current plain object
@Parent: Parent plain object
@Config: Config item

*/

// Define an `User` type
yod.type('User', {
  firstName: '@First',              // Get a random firstName
  lastName: '@Last',
  sex: '@Sex',
  fullName: '@Self.firstName @Self.lastName',  // Join current object's firstName and lastName
  nickName: '@Nick',
  chineseName: '@ChineseName',
  age: '@Age(adult)',               // Call @Age with a argument
  to100: '` 100 - @Self.age `',     // Execute javascript code
  telephone: '@Tel',
  avatar: '@Avatar',
  others: {
     words: 'Hello, my name is @Parent.fullName, you can call me @Parent.nickName.',
     bestFriends: '@Nick.repeat(2, 3).join(", ")',    // Call native array's join function
     myFirstNameLength: '@Parent.firstName.length',   // Call native string's length attribute
     favouriteLetter: '@([A, B, C]).sample'           // Call lodash sample function
  }
});

// Use `User` type to generate an object with ten users in it
console.log(yod({
    status: 'ok',
    list: '@User.repeat(10)'
}));

// or Use `User` type to generate directly array
// console.log(yod('@User.repeat(10)'));

BASIC

LOCATION

MEDIA

MIXED

TEXT

USER

WEB

Support all function in lodash:

yod('@([1, 2, 3]).sample');   // equal to `_.sample([1, 2, 3])`
yod('@Int.repeat(10).uniq');  // equal to `_.uniq(yod('@Int.repeat(10)'))`

Support all function in native javascript:

yod('@([a, b, c]).join("-")'); // equal to `['a', 'b', 'c'].join('-')`
yod('@String.replace(a, b)');  // equal to `yod('@String').replace('a', 'b')`

BASIC

Reference

  • yod:本项目的核心引擎(我写的)。
  • mockjs:阿里出的,也不错,但学习成本较高,也不够强大。
  • chance:国外的一个 mock 库,代码很简洁,但不适合中国人用。
  • jsonfy:将任意的字符串解析成 JS 里的数据,类似于 JSON.parse,但语法要求没它那么严格(我写的)。
  • sscan:字符串分析器,很多复杂的解析用正则表达式是满足不了要求的,所以需要把字符串化分成字符来一个个解析(我写的)。
  • elegant.def:优雅的定义 JS 函数,很多情况下我们定义了函数要处理参数的各种情况,用了它处理函数参数就非常简单了(我写的)。

TODO

History

ChangeLog

License

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

About

Powerful data generator tool.

Resources

Stars

122 stars

Watchers

15 watching

Forks

Releases

Packages

Used by

Contributors

Languages