Browse Source

KERNEL-13991 feat: core/func文件夹

es6
zsmj 2 years ago
parent
commit
df41ae3b78
  1. 4
      src/core/func/__test__/function.test.js
  2. 14
      src/core/func/alias.js
  3. 22
      src/core/func/function.js
  4. 2
      src/core/func/index.js
  5. 24
      src/core/func/number.js
  6. 8
      src/core/func/string.js

4
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,7 +17,7 @@ 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");
});

14
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];

22
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;
}

2
src/core/func/index.js

@ -19,5 +19,5 @@ Object.assign(BI, {
..._date,
..._function,
..._number,
..._string
..._string,
});

24
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)));
}
}

8
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;
}

Loading…
Cancel
Save