承接 webmodules/dummy-json 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

webmodules/dummy-json

最新稳定版本:0.0.9

Composer 安装命令:

composer require webmodules/dummy-json

包简介

Dummy JSON generator and templating

关键字:

README 文档

README

Dummy JSON is a Node utility that allows you to generate random JSON data using Handlebars templates. It returns a JSON compatible string you can use in your app. It's useful for creating mock API services that return dummy data.

Example

For a complete list of helpers see the available helpers section.

Template
{
  "people": [
    {{#repeat 2}}
    {
      "id": {{index}},
      "firstName": "{{firstName}}",
      "lastName": "{{lastName}}",
      "email": "{{email}}",
      "work": "{{company}}",
      "age": {{number 20 50}},
      "optedin": {{boolean}}
    }
    {{/repeat}}
  ],
  "images": [
    {{#repeat 3 6}}
    'img{{index}}.png'
    {{/repeat}}
  ],
  "revision": {{uniqueIndex}},
  "tolerance": {{number '0' '2'}},
}
Output
{
  "people": [
    {
      "id": 0,
      "firstName": "Leanne",
      "lastName": "Flinn",
      "email": "leanne.flinn@unilogic.com",
      "work": "Unilogic",
      "age": 26,
      "optedin": true
    },
    {
      "id": 1,
      "firstName": "Edward",
      "lastName": "Young",
      "email": "edward.young@solexis.com",
      "work": "Solexis",
      "age": 31,
      "optedin": false
    }
  ],
  "images": [
    'img0.png',
    'img1.png',
    'img2.png',
    'img3.png'
  ],
  "revision": 0,
  "tolerance": 1.7508240924216807,
}

Getting started

Install via npm:

npm install dummy-json

Generate JSON

var dummyjson = require('dummy-json');
var template = '{ "name": {{firstName}}, "age": {{number 18 65}} }';
var result = dummyjson.parse(template);

Generate from a file

Instead of writing multi-line strings you can load the template from a file using Node's fs utility:

var fs = require('fs');
var dummyjson = require('./dummy-json');

var template = fs.readFileSync('template.hbs', {encoding: 'utf8'});
var result = dummyjson.parse(template);

Converting to JavaScript object

If there are no errors in the output then the returned string can be parsed into a JavaScript object:

var result = dummyjson.parse(template);
var obj = JSON.parse(result);

Using with a HTTP response

A common use of Dummy JSON is to create a mock API service that return random data you can test with. Here's a quick example using Express:

var fs = require('fs');
var express = require('express');
var dummyjson = require('./dummy-json');

var template = fs.readFileSync('template.hbs', {encoding: 'utf8'});
var app = express();

app.get('/people', function(req, res) {
  res.set('Content-Type', 'application/json');
  res.send(dummyjson.parse(template));
});

app.listen(3000);

Available helpers

{{#repeat [count/array] [maxCount]}} ... {{/repeat}}

Repeats blocks of content. Similar to Handlebars' built-in each, but adds commas between items and tidies up whitespace.

{{#repeat 4}} // Repeats the block exactly 4 times
"hello"
{{/repeat}}

{{#repeat 5 10}} // Repeats the block a random number of times between 5 and 10
"hello"
{{/repeat}}

{{#repeat animals}} // Loops over array provided in the data options of parse()
"{{this}}"
{{/repeat}}

You can print the current index of the loop using {{index}}. This is a helper that's only available within repeat blocks, (outside of a repeat block it will print undefined).

{{#repeat 4}}
"hello {{index}}" // "hello 1", "hello 2", etc.
{{/repeat}}

{{number [min/max] [max] [pad=true]}}

Generates a random number. If just one number is provided it will generate a number between 0 and the given number. The min and max values are inclusive in the generated number. Floats can be generated by wrapping the numbers in quote marks. The pad option pads the generated number with leading zeros (integers only).

{{number 20}} // Generates a random integer between 0 and 20
{{number 50 100}} // Generates a random integer between 50 and 100
{{number 50 100 pad=true}} // Pad integer with leading zeros, eg: 076
{{number '5.5' '8.5'}} // Generates a random float between 5.5 and 8.5

{{boolean}}

Generates a random true or false boolean value.

{{firstName}}

Generates a random first name, from a predefined list.

{{lastName}}

Generates a random last name, from a predefined list.

{{company}}

Generates a random company name, from a predefined list.

{{email}}

Generates a random email address, using the most recently printed name and company. This means it keeps in sync when used in conjunction with names and companies.

{{uniqueIndex}}

Generates a unique index that always increments by 1 each time it's used, regardless of whether it's inside or outside a repeat loop.

Advanced usage

The parse method accepts a second argument that allows you to configure the parsing routine. It's a plain object that can contain one or more of the following options:

Using your own Handlebars helpers

var helpers = {
  orientation: function(options) {
    return Math.random() > 0.5 ? 'left' : 'right';
  }
};
var template = '{ "position": {{orientation}} }';
var result = dummyjson.parse(template, {helpers: helpers});

Custom helpers can override built-in ones, which allows you to modify how the Available helpers work. For more information on writing helpers see the Handlebars documentation.

Using your own data

var data = {
  animals: ['cat', 'dog', 'cow', 'wolf', 'giraffe']
};
var template = '{ "pets": [ {{#repeat animals}}{{this}}{{/repeat}} ] }';
var result = dummyjson.parse(template, {data: data});

Useful for splicing bits of real data into the generated reponse. All the regular Handlebars functionality is available to work with the data.

Using your own list of names and companies

var firstNames = ['Frasier', 'Olivia', 'Marge', 'Holbeck'];
var lastNames = ['Crane', 'Dunham', 'Gunderson', 'Ghyll'];
var companies = ['KACL', 'Fringe', 'MPD'];
var template = '{ "name": {{firstName}}, "company": {{company}} }';
var result = dummyjson.parse(template, {
  firstNames: firstNames,
  lastNames: lastNames,
  companies: companies
});

Using your own names and companies will completely override the built-in collections. You can specify just one array, or all of them, as has been done above. Note: Names and companies loop when used repeatedly - to keep them in sync the length of the smallest array will be used as the loop point. In the example above the companies array is smallest and so the final first and last names won't ever appear.

webmodules/dummy-json 适用场景与选型建议

webmodules/dummy-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 112 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「rudra」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 webmodules/dummy-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 webmodules/dummy-json 我们能提供哪些服务?
定制开发 / 二次开发

基于 webmodules/dummy-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 112
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 14
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-27