GithubHelp home page GithubHelp logo

bojson's Introduction

BoJSON

可以叫剥JSON,也是播JSON, 仿 http://www.json-generator.com/

点击这里预览效果 http://jser.me/BoJSON/

用法

var tmpl = {
    a: '{{numeric(3, 10)}}',
    b: ['{{repeat(1,3)}}',
        function(idx) {
            return 'i am ' + idx
        }
    ]
};

console.log(JSON.stringify(BoJSON(tmpl), null, 4));

//返回的结果
//{
//    "a": "4",
//    "b": [
//        "i am 0",
//        "i am 1",
//        "i am 2"
//    ]
//}

目前内置模板

  • repeat(n) : 必须是数组第一个元素, 循环n次输出数组第二个元素

  • numeric(m,n) : 返回一个m到n区间的随机数

  • gender : 随机返回性别

  • bool : 随机返回一个布尔值

扩展的模板

扩展的模板使用Mock.jsRandom来生成的 使用的时候需要额外再引入两个JS

详细的扩展如下, 部分支持参数,详见http://mockjs.com/#Mock.Random

模板 说明
natural 返回一个随机的自然数(大于等于 0 的整数),支持min,max参数
integer 返回一个随机的整数。
float 返回一个随机的浮点数。
character 返回一个随机字符。
string 返回一个随机字符串。
range 返回一个整型数组。
date 返回一个随机的日期字符串。详见http://mockjs.com/#date
time 返回一个随机的时间字符串。
datetime 返回一个随机的日期和时间字符串。
image 生成一个随机的图片地址。
dataImage 生成一段随机的 Base64 图片编码。
color 随机生成一个颜色,格式为 '#RRGGBB'。
paragraph 随机生成一段文本。
sentence 随机生成一个句子,第一个的单词的首字母大写。
word 随机生成一个单词。
title 随机生成一句标题,其中每个单词的首字母大写。
first/firstName 随机生成一个常见的英文名。
last/surname 随机生成一个常见的英文姓。
name 随机生成一个常见的英文姓名。
url 随机生成一个 URL。
domain 随机生成一个域名。
email 随机生成一个邮件地址。
ip 随机生成一个 IP 地址。
tld 随机生成一个顶级域名。
area 随机生成一个(**)大区。
region/city 随机生成一个(**)省(或直辖市、自治区、特别行政区)。
guid 随机生成一个 GUID。
id 随机生成一个 18 位身份证。
company 随机生成一个常见的英文名。

注册自定义的模板

    BoJSON.registeHelper('city', function(){
          var cities = ['北京', '天津', '杭州', '上海']
          return cities[this.numeric(cities.length-1)]
    }

bojson's People

Contributors

jserme avatar

Watchers

James Cloos 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.