From 89f48728261dff9e8153f8c2cec414be1eae470a Mon Sep 17 00:00:00 2001 From: w1ida Date: Sun, 10 Feb 2019 13:22:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=B5=AE=E7=82=B9?= =?UTF-8?q?=E6=95=B0=E8=87=AA=E5=A2=9E=20'id|+0.1':=200.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持浮点数自增 'id|+0.1': 0.2,修复精度丢失 Mock.mock({"a|6":[{"b|+0.1":0}]}) --- src/mock/handler.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/mock/handler.js b/src/mock/handler.js index 1c3c4d32..410bb2af 100644 --- a/src/mock/handler.js +++ b/src/mock/handler.js @@ -262,7 +262,11 @@ Handler.extend({ // 'id|+1': 1 inc = key.match(Constant.RE_KEY) if (inc && inc[2] && Util.type(options.template[key]) === 'number') { - options.template[key] += parseInt(inc[2], 10) + // 支持浮点数自增 'id|+0.1': 0.2 + options.template[key] = (function(f,d){var c,b,a;try{c=f.toString().split(".")[1].length}catch(g){c=0} + try{b=d.toString().split(".")[1].length}catch(g){b=0} + a=Math.pow(10,Math.max(c,b));return(f*a+d*a)/a}) + (options.template[key],parseFloat(inc[2])); } } } @@ -537,4 +541,4 @@ Handler.extend({ } }) -module.exports = Handler \ No newline at end of file +module.exports = Handler From a1b0972d3f9f78b7461a0e4858e28183a33f0fe5 Mon Sep 17 00:00:00 2001 From: w1ida Date: Sun, 10 Feb 2019 13:26:10 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=8C=B9=E9=85=8D=E6=B5=AE=E7=82=B9?= =?UTF-8?q?=E6=95=B0=EF=BC=8Cnumber|+0.1:0.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 匹配浮点数,number|+0.1:0.2 --- src/mock/constant.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mock/constant.js b/src/mock/constant.js index 3a83fa55..599a7c1c 100644 --- a/src/mock/constant.js +++ b/src/mock/constant.js @@ -24,10 +24,10 @@ */ module.exports = { GUID: 1, - RE_KEY: /(.+)\|(?:\+(\d+)|([\+\-]?\d+-?[\+\-]?\d*)?(?:\.(\d+-?\d*))?)/, + RE_KEY: /(.+)\|(?:\+([\d\.]+)|([\+\-]?\d+-?[\+\-]?\d*)?(?:\.(\d+-?\d*))?)/, RE_RANGE: /([\+\-]?\d+)-?([\+\-]?\d+)?/, RE_PLACEHOLDER: /\\*@([^@#%&()\?\s]+)(?:\((.*?)\))?/g // /\\*@([^@#%&()\?\s\/\.]+)(?:\((.*?)\))?/g // RE_INDEX: /^index$/, // RE_KEY: /^key$/ -} \ No newline at end of file +}