From 61caab972eb14ccf6b20d9eb6328fedb05c6c0f0 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 7 Aug 2024 11:45:27 +0800 Subject: [PATCH] 2.3.0 --- History.md | 25 +++++++++++++++++++++++++ Readme.md | 2 ++ package.json | 2 +- src/lib/number.js | 19 ++++++++++++++++--- test/index.js | 4 ++++ 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 test/index.js diff --git a/History.md b/History.md index e4e5b45..f82695c 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,28 @@ +2.3.0 / 2024-08-07 +================== + * 增加修复版toFixed + +2.2.2 / 2024-03-28 +================== + * 增加Obejct.groupBy和Map.groupBy + + +2.2.1 / 2023-11-06 +================== + * 一些小修复 + + +2.2.0 / 2023-07-20 +================== + * 数组增加一组原型方法 + * 字符串增加版本号大小比较的原型方法 + + +2.1.1 / 2023-02-27 +================== + * 修复 Object.prototype.toParams()原型方法 + + 2.1.0 / 2022-01-27 ================== * Array.prototype.item 更名为 Array.prototype.at diff --git a/Readme.md b/Readme.md index c2897b4..ecbe684 100644 --- a/Readme.md +++ b/Readme.md @@ -53,6 +53,8 @@ ├── Number │ ├── .parse(str) // 将安全范围内的数字字符串转为数字类型 │ └── .fromString(str) // 将字符串转为数字类型 +├── Number.prototype +│ ├── .toFixed(digits) // 修正版的toFixed │ └── Promise └── .defer() // 创建一个延迟的Promise对象 diff --git a/package.json b/package.json index 2c24fcb..f8dc9c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "es.shim", - "version": "2.2.2", + "version": "2.3.0", "description": "实现部分新API, 以及一些常用的扩展方法", "keyworks": [ "es5", diff --git a/src/lib/number.js b/src/lib/number.js index 9b08791..b4a4d73 100644 --- a/src/lib/number.js +++ b/src/lib/number.js @@ -3,12 +3,22 @@ * @date 2020/09/16 11:58:40 */ +// Number.EPSILON + +const NATIVE_TO_FIXED = Number.prototype.toFixed + +Object.defineProperty(Number.prototype, 'toFixed', { + value: function (n) { + return NATIVE_TO_FIXED.call(this + Number.EPSILON, n) + } +}) + // 简单的数字处理 // 将安全范围内的数字字符串转为数字类型 // 否则转为字符串类型 if (!Number.parse) { Object.defineProperty(Number, 'parse', { - value: function(val) { + value: function (val) { if (typeof val === 'number' || typeof val === 'string') { val += '' if (val.startsWith('0') && !val.startsWith('0.')) { @@ -19,7 +29,10 @@ if (!Number.parse) { } } else { if (isFinite(val)) { - if (val >= Number.MIN_SAFE_INTEGER && val <= Number.MAX_SAFE_INTEGER) { + if ( + val >= Number.MIN_SAFE_INTEGER && + val <= Number.MAX_SAFE_INTEGER + ) { val = +val } } @@ -34,7 +47,7 @@ if (!Number.parse) { // 将字符串转为数字类型 if (!Number.fromString) { Object.defineProperty(Number, 'fromString', { - value: function(val) { + value: function (val) { return +val || 0 } }) diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..1357048 --- /dev/null +++ b/test/index.js @@ -0,0 +1,4 @@ +require('../dist/index.js') + +console.log((1.345).toFixed()) +console.log((1.345).toFixed(2))