dummy-json:在Node.js中生成随机虚拟JSON数据

  • w8_742140
    了解作者
  • 41.2KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-24 05:43
    上传日期
虚拟JSON 虚拟JSON是Node实用程序,可让您使用Handlebars模板生成随机JSON数据。 它带有内置的Handlebars帮助程序集合,这些帮助程序生成常见的数据值,例如名称,数字,日期,并且还允许您编写自己的数据。 例子 如果格式不正确,请在上查看以下示例。 模板字符串 输出字符串 { "users": [ {{#repeat 2}} { "id": {{@index}}, "name": "{{firstName}} {{lastName}}", "work": "{{company}}", "email": "{{email}}", "dob": "{{date '1900' '2000' 'YYYY'}}", "address": "{{int 1 100}} {{stre
dummy-json-master.zip
  • dummy-json-master
  • lib
  • helpers.js
    15.6KB
  • utils.js
    1.1KB
  • mockdata.js
    15.2KB
  • test
  • helpers.spec.js
    38.9KB
  • dummyjson.spec.js
    4KB
  • .npmignore
    6B
  • LICENSE
    1KB
  • README.md
    27.8KB
  • index.d.ts
    2.5KB
  • .gitignore
    27B
  • index.js
    1.4KB
  • cli.js
    293B
  • package-lock.json
    45.1KB
  • package.json
    645B
内容介绍
# Dummy JSON Dummy JSON is a Node utility that allows you to generate random JSON data using Handlebars templates. It comes with a built-in collection of Handlebars helpers that generate common data values, such as names, numbers, dates, and also allows you to write your own. * [Getting started](#getting-started) * [Built-in helpers](#built-in-helpers) * [Writing your own helpers](#writing-your-own-helpers) * [Replacing default mock data](#replacing-default-mock-data) * [Seeded random data](#seeded-random-data) * [Advanced usage](#advanced-usage) * [API](#api) ## Example Please view the following example on the [github page](https://github.com/webroo/dummy-json) if it's not formatted correctly. <table> <thead><tr><td width="50%">Template string</td><td width="50%">Output string</td></tr></thead> <tbody><tr><td align="left" valign="top"> <pre style="padding: 0"> { "users": [ {{#repeat 2}} { "id": {{@index}}, "name": "{{firstName}} {{lastName}}", "work": "{{company}}", "email": "{{email}}", "dob": "{{date '1900' '2000' 'YYYY'}}", "address": "{{int 1 100}} {{street}}", "city": "{{city}}", "optedin": {{boolean}} } {{/repeat}} ], "images": [ {{#repeat 3}} "img{{@index}}.png" {{/repeat}} ], "coordinates": { "x": {{float -50 50 '0.00'}}, "y": {{float -25 25 '0.00'}} }, "price": "${{int 0 99999 '0,0'}}" } </pre> </td><td align="left" valign="top"> <pre style="padding: 0"> { "users": [ { "id": 0, "name": "Adam Carter", "work": "Unilogic", "email": "adam.carter@unilogic.com", "dob": "1978", "address": "83 Warner Street", "city": "Boston", "optedin": true }, { "id": 1, "name": "Leanne Brier", "work": "Connic", "email": "leanne.brier@connic.org", "dob": "1987", "address": "9 Coleman Avenue", "city": "Toronto", "optedin": false } ], "images": [ "img0.png", "img1.png", "img2.png" ], "coordinates": { "x": 35.12, "y": -21.49 }, "price": "$59,395" } </pre> </td></tr></tbody></table> ## Getting started Install via npm: npm install dummy-json #### Generate a JSON string ```js const dummyjson = require('dummy-json'); const template = `{ "name": "{{firstName}}", "age": "{{int 18 65}}" }`; const result = dummyjson.parse(template); // Returns a string ``` #### Generate from a template file Instead of using template strings directly in your code you can create a template file and load it using Node's `fs` utility: ```js const fs = require('fs'); const dummyjson = require('dummy-json'); const template = fs.readFileSync('mytemplate.hbs', { encoding: 'utf8' }); const result = dummyjson.parse(template); ``` #### Converting the generated string to a JavaScript object If the generated output is a valid JSON string then it can be parsed into a JavaScript object: ```js const result = dummyjson.parse(template); const obj = JSON.parse(result); ``` #### Create a dummy API endpoint A common use of Dummy JSON is to create a mock API endpoint that returns random data. Here's a quick example using Express: ```js const fs = require('fs'); const express = require('express'); const dummyjson = require('dummy-json'); const template = fs.readFileSync('template.hbs', { encoding: 'utf8' }); const app = express(); app.get('/api/people', function(req, res) { res.set('Content-Type', 'application/json'); res.status(200).send(dummyjson.parse(template)); }); app.listen(3000); ``` #### Command line iterface If you install Dummy JSON globally with `npm install -g dummy-json` you can use it from the command line. Dummy JSON will write to stdout by default but you can redirect to a file like so: dummyjson template.hbs > output.json ## Built-in helpers Dummy JSON uses custom Handlebars helpers to generate the random data. Handlebars helpers are functions that are called whenever an expression is encountered in a template, such as `{{firstName}}`. You can learn how to write your own helpers in the section: [Writing your own helpers](#writing-your-own-helpers). ### Repeat `{{#repeat count [comma=true]}} ... {{/repeat}}` * `count: number` The number of times to repeat the content (required) * `comma?: boolean` Add or remove the separating comma between blocks of content (optional, default is true) `{{#repeat min=number max=number [comma=true]}} ... {{/repeat}}` * `min: number` Minimum range for the random repeat count (required) * `max: number` Maximum range for the random repeat count (required) * `comma?: boolean` Add or remove the separating comma between blocks of content (optional, default is true) There are two ways in which this helper can be used. Both repeat blocks of content, similar to Handlebars' built-in `each`, and can be used anywhere in your template, not just inside arrays. It automatically adds a comma between repeated blocks unless specified. The first way it can be used is to repeat the block a fixed a number of times: ```js // Repeat the block 3 times, automatically adding a comma between blocks "messages": [ {{#repeat 3}} "hello" {{/repeat}} ] // Output "messages": [ "hello", "hello", "hello" ] ``` The second way it can be used is to repeat the block a random number of times: ```js // Repeat the block a random number of times between 1 and 5 "messages": [ {{#repeat min=1 max=5}} "hello" {{/repeat}} ]; // Output "messages": [ "hello", "hello" ]; ``` You can omit the comma by using `comma=false`, for example: ```js {{#repeat 3 comma=false}}hello{{/repeat}} // hellohellohello ``` You can get iteration position information inside the repeat block using the standard Handlebars variables `@index`, `@first`, `@last` and `@total`. Check out the helpers [Add](#add) and [Step](#step) to see how you can further modify the position values to create interesting indexes. ```js // Repeat the block 3 times using @index to modify the filename {{#repeat 3}} "img{{@index}}.png" {{/repeat}} // Output "img0.png", "img1.png", "img2.png" ``` ### Integer `{{int min max [format] [round=null]}}` * `min: number` Minimum value (required) * `max: number` Maximum value (required) * `format?: string` Formatting string (optional, default is null) * `round?: number` Rounds to the nearest multiple of the value (optional, default is no rounding) Generates a random integer from `min` (inclusive) up to and including `max` (inclusive). The optional `round` parameter will round the number to the nearest multiple of the given value. The output can be formatted using a numeric format string, provided by numbro. For a complete list of formatting options see [http://numbrojs.com/format.html](http://numbrojs.com/format.html). ```js // Generates a random integer between 0 and 100 {{int 0 100}} // 43 // Rounds the random integer to the nearest multiple of 5 {{int 0 100 round=5}} // 65 // Formats the random integer using numbro {{int 10000 50000 '0,0.00'}} // 23,462.00 ``` ### Float `{{float min max [format] [round=null]}}` * `min: number` Minimum value (required) * `max: number` Maximum value (required) * `format?: string` Formatting string (optional, default is null) * `round?: number` Rounds to the nearest multiple of the value (optional, default is no rounding) Generates a random floating point number from `min` (inclusive) up to but excluding `max` (exclusive). The optional `round` parameter will round the number to the nearest multiple of the given value. The output can be formatted using a numeric format string, provided by numbro. For a complete list of formatting options see [http://numbrojs.com/format.html](http://numbrojs.com/format.html). ```js // Generates a random float between 0 and 1 {{float 0 1}} // 0.4319351462490857 // Rounds the random float to the nearest multiple of 0.1 {{float 0 1 round=0.1}} // 0.4 // Formats the random float using numbro {{float 10000 50000 '0,0.00'}} // 33
评论
    相关推荐