Mock.js 的语法规范

//1. 属性值是字符串 String
 console.log(
   Mock.mock({
     "data1|1-4": "陈学辉", //随机重复1-4次
     "data2|3": "好帅", //固定重置3次
    })
 );
 //2. 属性值是数字 Number
 console.log(
  Mock.mock({
   "number1|+1": 100, //整数,自动加1并且初始值为100
   "number2|1-100": 12, //整数,1-100之间的随机数,包括1和100(1=<数字<=100)  12用来确定是数据为数字类型
   "number3|1-100.5": 12, //小数,整数部分为为1-100间随机数,包括1和100;小数部分为固定5位随机数
   "number4|1-100.1-10": 12, //小数,整数部分为为1-100间随机数,包括1和100;小数部分为1-10个随机数(位数随机,数字也随机)
   "number5|123.1-10": 12, //数字123后面随机添加1-10位小数
   "number6|123.10": 12, //数字123后面固定添加10位小数,但小数的值是随机的
 })
);
//3. 属性值是布尔型 Boolean
console.log(
 Mock.mock({
   "b1|1": false, //随机生成一个布尔值,true与false的概率各为一半
   "b2|1-5": true,
 //随机生成一个布尔值,值为value的概率是min / (min + max),值为!value的概率是max / (min + max)
  })
);
//4. 属性值是对象 Object
console.log(
 Mock.mock({
   "num1|1-3": { a: 10, b: 20, c: 30, d: 40 }, //随机选取对象里1-3个属性
   "num2|2": { a: 10, b: 20, c: 30, d: 40 }, //随机选取对象里2个属性
 })
);
//属性值是数组 Array
console.log(
 Mock.mock({
   "arr1|1": ["a", "b", "c", "d"], //随机选取数组里1个数据
   "arr2|1-3": ["a", "b", "c", "d"], //通过重复属性值生成一个新数组,数组长度为1-3,min<=重复次数<=max
  })
);
//6. 属性值是函数 Function
 console.log(
    Mock.mock({
      result: function () {
        return 1 + 2;
      }, //把函数的返回值当作属性的结果
    })
  );
  //7. 属性值是正则表达式 RegExp
  console.log(
    Mock.mock({
      reg1: /[a-z][A-Z][0-9]/,
      reg2: /\w\W\s\S\d\D/,
      reg3: /\d{5,10}/,
    })
  );
  //Mock.Random
  var Random = Mock.Random;
  // console.log(Random);
  //1、Basics  基础类里的方法,共7个
  //Random.boolean()      随机一个布尔值
  console.log(
    Random.boolean(),
    Random.boolean(1, 9, true),
    Random.boolean(1, 2, false)
  );
  //Random.natural()      随机一个自然数(大于等于 0 的整数)
  console.log(Random.natural(), Random.natural(100), Random.natural(0, 50));
  //Random.integer()  随机一个整数(包含负数)
  console.log(
    Random.integer(),
    Random.integer(-100),
    Random.integer(-50, 50)
  );
  //Random.float()    随机一个小数
  console.log(
    Random.float(),
    Random.float(0),
    Random.float(-10, 10),
    Random.float(-10, 10, 3),
    Random.float(-10, 10, 2, 5)
  );
  //Random.character()    //随机一个字符
  console.log(
    Random.character(),
    Random.character("abc123"),
    Random.character("lower"),
    Random.character("symbol")
  );
  //Random.string()   随机一个字符串
  console.log(
    Random.string(),
    Random.string(5),
    Random.string(7, 10),
    Random.string("symbol", 5),
    Random.string("abc123", 1, 3)
  );
  //Random.range()    随机一个整数数据的数组
  console.log(Random.range(7), Random.range(3, 7), Random.range(1, 10, 2));
  //数据占位符使用
  console.log(
    Mock.mock({
      integer: "@integer(10, 30)", //随机生成一个10~30之间的正整数
      float: "@float(60, 100, 2, 2)",
     //随机生成浮点数,参数分别为整数部分最小值和最大值、小数部分保留最小位数和最大位数
      boolean: "@boolean", //随机生成boolean
      "string|1-2": "@string", //随机生成字符串
      name: "@cname", //随机生成名字
      //生成时间
      date: "@date(yyyy-MM-dd)", //按照格式随机生成时间
      datetime: "@datetime", //随机生成时间
      now: "@now", //当前时间
      id: "@id", //随机生成一个 18 位身份证
      guid: "@guid", //随机生成一个 GUID
      url: "@url", //随机生成url字符串
      email: "@email", //随机生成邮箱
      image: "@image(200x200)", //随机生成一个大小为200x200的图片链接
      title: "@title", //随机生成一句标题,其中每个单词的首字母大写
      upper: "@upper(@title)", //把生随机成的标题全部转为大写
      cparagraph: "@cparagraph", //随机生成一段中文文本
      csentence: "@csentence", //随机生成一段中文文本
      range: "@range(2, 10)", //返回一个内容从2开始到9的整型数组
      //地区
      region: "@region", //随机生成地区     华中
      province: "@province", //随机生成省会  省
      city: "@city", //随机生成城市          市
      county: "@county", //随机生成一个(中国)县
    })
  );

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值