From df41ae3b784e874d30427f262365059fb8967581 Mon Sep 17 00:00:00 2001 From: zsmj Date: Wed, 4 Jan 2023 11:21:06 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-13991=20feat:=20core/func=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/func/__test__/function.test.js | 6 +++--- src/core/func/alias.js | 14 +++++++------- src/core/func/function.js | 22 +++++++++++----------- src/core/func/index.js | 2 +- src/core/func/number.js | 24 ++++++++++++------------ src/core/func/string.js | 8 ++++---- 6 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/core/func/__test__/function.test.js b/src/core/func/__test__/function.test.js index 388af5fe6..db6901582 100644 --- a/src/core/func/__test__/function.test.js +++ b/src/core/func/__test__/function.test.js @@ -8,7 +8,7 @@ describe("core-function-test", function () { * test_author_lei.wang */ it("createDistinctName-支持字符串数组", function () { - var names = ["name", "name1"]; + const names = ["name", "name1"]; expect(BI.Func.createDistinctName(names, "name")).to.equal("name2"); expect(BI.Func.createDistinctName(names, "name2")).to.equal("name2"); }); @@ -17,8 +17,8 @@ describe("core-function-test", function () { * test_author_lei.wang */ it("createDistinctName-支持对象数组数组", function () { - var names = [{ name: "name" }, { name: "name1" }]; + const names = [{ name: "name" }, { name: "name1" }]; expect(BI.Func.createDistinctName(names, "name")).to.equal("name2"); expect(BI.Func.createDistinctName(names, "name2")).to.equal("name2"); }); -}); \ No newline at end of file +}); diff --git a/src/core/func/alias.js b/src/core/func/alias.js index 7de1e9f60..b8596e7cf 100644 --- a/src/core/func/alias.js +++ b/src/core/func/alias.js @@ -209,7 +209,7 @@ function _numberFormat(text, format) { // 百分比,千分比的小数点移位处理 if (/[%‰]$/.test(format)) { - let paddingZero = /[%]$/.test(format) ? "00" : "000"; + const paddingZero = /[%]$/.test(format) ? "00" : "000"; tright += paddingZero; tleft += tright.substring(0, paddingZero.length); tleft = tleft.replace(/^0+/gi, ""); @@ -398,7 +398,7 @@ export const cjkDecode = function (text) { if (ch === "[") { const rightIdx = text.indexOf("]", i + 1); if (rightIdx > i + 1) { - let subText = text.substring(i + 1, rightIdx); + const subText = text.substring(i + 1, rightIdx); // james:主要是考虑[CDATA[]]这样的值的出现 if (subText.length > 0) { ch = String.fromCharCode(eval("0x" + subText)); @@ -452,7 +452,7 @@ export const htmlDecode = function (text) { export const cjkEncodeDO = function (o) { if (isPlainObject(o)) { - let result = {}; + const result = {}; each(o, function (v, k) { if (!(typeof v === "string")) { v = jsonEncode(v); @@ -468,12 +468,12 @@ export const cjkEncodeDO = function (o) { export const jsonEncode = function (o) { // james:这个Encode是抄的EXT的 - let useHasOwn = !!{}.hasOwnProperty; + const useHasOwn = !!{}.hasOwnProperty; // crashes Safari in some instances // var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/; - let m = { + const m = { "\b": "\\b", "\t": "\\t", "\n": "\\n", @@ -483,7 +483,7 @@ export const jsonEncode = function (o) { "\\": "\\\\", }; - let encodeString = function (s) { + const encodeString = function (s) { if (/["\\\x00-\x1f]/.test(s)) { return "\"" + s.replace(/([\x00-\x1f\\"])/g, function (a, b) { let c = m[b]; @@ -499,7 +499,7 @@ export const jsonEncode = function (o) { return "\"" + s + "\""; }; - let encodeArray = function (o) { + const encodeArray = function (o) { let a = ["["], b, i, l = o.length, v; for (i = 0; i < l; i += 1) { v = o[i]; diff --git a/src/core/func/function.js b/src/core/func/function.js index f8fc3cdca..ecbd78f2d 100644 --- a/src/core/func/function.js +++ b/src/core/func/function.js @@ -43,7 +43,7 @@ export function getGBWidth(str) { * @param param 搜索哪个属性 */ export function getSearchResult(items, keyword, param) { - let array = isArray(items); + const array = isArray(items); items = array ? BI.flatten(items) : items; param || (param = "text"); if (!isKey(keyword)) { @@ -54,7 +54,7 @@ export function getSearchResult(items, keyword, param) { } let t, text, py; keyword = toUpperCase(keyword); - let matched = array ? [] : {}, find = array ? [] : {}; + const matched = array ? [] : {}, find = array ? [] : {}; each(items, function (i, item) { // 兼容item为null的处理 if (isNull(item)) { @@ -101,7 +101,7 @@ export function getSearchResult(items, keyword, param) { * @return {any[]} */ export function getSortedResult(items, key) { - let getTextOfItem = BI.isFunction(key) ? key : + const getTextOfItem = BI.isFunction(key) ? key : function (item, key) { if (BI.isNotNull(key)) { return item[key]; @@ -116,8 +116,8 @@ export function getSortedResult(items, key) { }; return items.sort(function (item1, item2) { - let str1 = getTextOfItem(item1, key); - let str2 = getTextOfItem(item2, key); + const str1 = getTextOfItem(item1, key); + const str2 = getTextOfItem(item2, key); if (BI.isNull(str1) && BI.isNull(str2)) { return 0; } @@ -130,10 +130,10 @@ export function getSortedResult(items, key) { if (str1 === str2) { return 0; } - let len1 = str1.length, len2 = str2.length; + const len1 = str1.length, len2 = str2.length; for (let i = 0; i < len1 && i < len2; i++) { - let char1 = str1[i]; - let char2 = str2[i]; + const char1 = str1[i]; + const char2 = str2[i]; if (char1 !== char2) { // 找不到的字符都往后面放 return (BI.isNull(BI.CODE_INDEX[char1]) ? BI.MAX : BI.CODE_INDEX[char1]) - (BI.isNull(BI.CODE_INDEX[char2]) ? BI.MAX : BI.CODE_INDEX[char2]); @@ -144,7 +144,7 @@ export function getSortedResult(items, key) { } export function beforeFunc(sFunc, func) { - let __self = sFunc; + const __self = sFunc; return function () { if (func.apply(sFunc, arguments) === false) { return false; @@ -154,9 +154,9 @@ export function beforeFunc(sFunc, func) { } export function afterFunc(sFunc, func) { - let __self = sFunc; + const __self = sFunc; return function () { - let ret = __self.apply(sFunc, arguments); + const ret = __self.apply(sFunc, arguments); if (ret === false) { return false; } diff --git a/src/core/func/index.js b/src/core/func/index.js index 3475bf8d6..9b24275bf 100644 --- a/src/core/func/index.js +++ b/src/core/func/index.js @@ -19,5 +19,5 @@ Object.assign(BI, { ..._date, ..._function, ..._number, - ..._string + ..._string, }); diff --git a/src/core/func/number.js b/src/core/func/number.js index 9f9640f66..d7a4f31f5 100644 --- a/src/core/func/number.js +++ b/src/core/func/number.js @@ -23,7 +23,7 @@ export function add(num, arg) { c = Math.abs(r1 - r2); m = Math.pow(10, Math.max(r1, r2)); if (c > 0) { - let cm = Math.pow(10, c); + const cm = Math.pow(10, c); if (r1 > r2) { arg1 = Number(arg1.toString().replace(".", "")); arg2 = Number(arg2.toString().replace(".", "")) * cm; @@ -101,8 +101,8 @@ export function div(num, arg) { */ function digitLength(num) { // Get digit length of e - let eSplit = num.toString().split(/[eE]/); - let len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0)); + const eSplit = num.toString().split(/[eE]/); + const len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0)); return len > 0 ? len : 0; } @@ -114,7 +114,7 @@ export function div(num, arg) { if (num.toString().indexOf("e") === -1) { return Number(num.toString().replace(".", "")); } - let dLen = digitLength(num); + const dLen = digitLength(num); return dLen > 0 ? num * Math.pow(10, dLen) : num; } @@ -122,17 +122,17 @@ export function div(num, arg) { * 精确乘法 */ function times(num1, num2) { - let others = []; + const others = []; for (let _i = 2; _i < arguments.length; _i++) { others[_i - 2] = arguments[_i]; } if (others.length > 0) { return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1))); } - let num1Changed = float2Fixed(num1); - let num2Changed = float2Fixed(num2); - let baseNum = digitLength(num1) + digitLength(num2); - let leftValue = num1Changed * num2Changed; + const num1Changed = float2Fixed(num1); + const num2Changed = float2Fixed(num2); + const baseNum = digitLength(num1) + digitLength(num2); + const leftValue = num1Changed * num2Changed; return leftValue / Math.pow(10, baseNum); } @@ -140,15 +140,15 @@ export function div(num, arg) { * 精确除法 */ function accDivide(num1, num2) { - let others = []; + const others = []; for (let _i = 2; _i < arguments.length; _i++) { others[_i - 2] = arguments[_i]; } if (others.length > 0) { return accDivide.apply(void 0, [accDivide(num1, num2), others[0]].concat(others.slice(1))); } - let num1Changed = float2Fixed(num1); - let num2Changed = float2Fixed(num2); + const num1Changed = float2Fixed(num1); + const num2Changed = float2Fixed(num2); return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1))); } } diff --git a/src/core/func/string.js b/src/core/func/string.js index 8b5d5ceba..d273db525 100644 --- a/src/core/func/string.js +++ b/src/core/func/string.js @@ -34,8 +34,8 @@ export function endWith(str, endTag) { * @return {String} 参数的值 */ export function getQuery(str, name) { - let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); - let r = str.substr(str.indexOf("?") + 1).match(reg); + const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); + const r = str.substr(str.indexOf("?") + 1).match(reg); if (r) { return unescape(r[2]); } @@ -106,10 +106,10 @@ export function allIndexOf(str, sub) { if (typeof sub !== "string") { return []; } - let location = []; + const location = []; let offset = 0; while (str.length > 0) { - let loc = str.indexOf(sub); + const loc = str.indexOf(sub); if (loc === -1) { break; }