From 32d0841e602886af564598544c2ee72d7ee4138c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Fri, 29 Jun 2018 19:54:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Number.parse=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- History.md | 5 +++++ Readme.md | 31 ++++++++++++++++--------------- index.js | 1 + lib/number.js | 32 ++++++++++++++++++++++++++++++++ package.json | 2 +- 5 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 lib/number.js diff --git a/History.md b/History.md index dc39684..9378c5e 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +1.1.0 / 2018-06-29 +================== + * add Number.parse + + 1.0.1 / 2018-05-25 ================== * delete global extends diff --git a/Readme.md b/Readme.md index d850b1f..72aba59 100644 --- a/Readme.md +++ b/Readme.md @@ -6,38 +6,39 @@ + Obejct - * empty() - + * empty() + Obejct.prototype - * merge() + * merge() + Array.prototype - * includes() + * includes() + Date - * isDate() + * isDate() + Date.prototype - * getFullWeek() - * getWeek() - * format() + * getFullWeek() + * getWeek() + * format() + String.prototype - * splice() - * htmlspecialchars() - * tohtml() - * xss() - * escape() - * padStart() - * padEnd() + * splice() + * htmlspecialchars() + * tohtml() + * xss() + * escape() + * padStart() + * padEnd() ++ Number + * parse() diff --git a/index.js b/index.js index 824980e..0d90b89 100644 --- a/index.js +++ b/index.js @@ -10,5 +10,6 @@ require('./lib/object') require('./lib/array') require('./lib/string') +require('./lib/number') require('./lib/date') require('./lib/promise') diff --git a/lib/number.js b/lib/number.js new file mode 100644 index 0000000..a51bad1 --- /dev/null +++ b/lib/number.js @@ -0,0 +1,32 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2018-06-29 19:29:19 + * @version $Id$ + */ +'use strict' + +// 简单的数字处理 +// 将安全范围内的数字字符串转为数字类型 +// 否则转为字符串类型 +if (!Number.parse) { + Object.defineProperty(Number, 'parse', { + value: function(val) { + val += '' + if (val.startsWith(0) && !val.startsWith('0.')) { + return val + } else { + if (isFinite(val)) { + if ( + val >= Number.MIN_SAFE_INTEGER && + val <= Number.MAX_SAFE_INTEGER + ) { + val = +val + } + } + return val + } + }, + enumerable: false + }) +} diff --git a/package.json b/package.json index cd68934..c418c31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "es.shim", - "version": "1.0.1", + "version": "1.1.0", "description": "Some shim api that let you can use in all node.js environment", "keyworks": [ "es5",