forked from fanruan/fineui
Browse Source
Merge in VISUAL/fineui from ~DAILER/fineui:es6 to es6 * commit '3955ff070b17a9c8eb1b7020bd8adf0bdffac767': KERNEL-13991 feat: core/func文件夹 KERNEL-13991 feat: core/func文件夹 KERNEL-13991 feat: core/func文件夹es6
Dailer-刘荣歆
2 years ago
12 changed files with 2136 additions and 2026 deletions
@ -0,0 +1,23 @@
|
||||
export * from "./alias"; |
||||
export * from "./array"; |
||||
export * from "./date"; |
||||
export * from "./function"; |
||||
export * from "./number"; |
||||
export * from "./string"; |
||||
|
||||
// need delete
|
||||
import * as _alias from "./alias"; |
||||
import * as _array from "./array"; |
||||
import * as _date from "./date"; |
||||
import * as _function from "./function"; |
||||
import * as _number from "./number"; |
||||
import * as _string from "./string"; |
||||
|
||||
Object.assign(BI, { |
||||
..._alias, |
||||
..._array, |
||||
..._date, |
||||
..._function, |
||||
..._number, |
||||
..._string, |
||||
}); |
@ -1,156 +1,154 @@
|
||||
BI._.extend(BI, { |
||||
// 给Number类型增加一个add方法,调用起来更加方便。
|
||||
add: function (num, arg) { |
||||
return accAdd(arg, num); |
||||
// 给Number类型增加一个add方法,调用起来更加方便。
|
||||
export function add(num, arg) { |
||||
return accAdd(arg, num); |
||||
|
||||
/** |
||||
** 加法函数,用来得到精确的加法结果 |
||||
** 说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。 |
||||
** 调用:accAdd(arg1,arg2) |
||||
** 返回值:arg1加上arg2的精确结果 |
||||
**/ |
||||
function accAdd (arg1, arg2) { |
||||
var r1, r2, m, c; |
||||
try { |
||||
r1 = arg1.toString().split(".")[1].length; |
||||
} catch (e) { |
||||
r1 = 0; |
||||
} |
||||
try { |
||||
r2 = arg2.toString().split(".")[1].length; |
||||
} catch (e) { |
||||
r2 = 0; |
||||
} |
||||
c = Math.abs(r1 - r2); |
||||
m = Math.pow(10, Math.max(r1, r2)); |
||||
if (c > 0) { |
||||
var cm = Math.pow(10, c); |
||||
if (r1 > r2) { |
||||
arg1 = Number(arg1.toString().replace(".", "")); |
||||
arg2 = Number(arg2.toString().replace(".", "")) * cm; |
||||
} else { |
||||
arg1 = Number(arg1.toString().replace(".", "")) * cm; |
||||
arg2 = Number(arg2.toString().replace(".", "")); |
||||
} |
||||
} else { |
||||
/** |
||||
** 加法函数,用来得到精确的加法结果 |
||||
** 说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。 |
||||
** 调用:accAdd(arg1,arg2) |
||||
** 返回值:arg1加上arg2的精确结果 |
||||
**/ |
||||
function accAdd(arg1, arg2) { |
||||
let r1, r2, m, c; |
||||
try { |
||||
r1 = arg1.toString().split(".")[1].length; |
||||
} catch (e) { |
||||
r1 = 0; |
||||
} |
||||
try { |
||||
r2 = arg2.toString().split(".")[1].length; |
||||
} catch (e) { |
||||
r2 = 0; |
||||
} |
||||
c = Math.abs(r1 - r2); |
||||
m = Math.pow(10, Math.max(r1, r2)); |
||||
if (c > 0) { |
||||
const cm = Math.pow(10, c); |
||||
if (r1 > r2) { |
||||
arg1 = Number(arg1.toString().replace(".", "")); |
||||
arg2 = Number(arg2.toString().replace(".", "")) * cm; |
||||
} else { |
||||
arg1 = Number(arg1.toString().replace(".", "")) * cm; |
||||
arg2 = Number(arg2.toString().replace(".", "")); |
||||
} |
||||
return (arg1 + arg2) / m; |
||||
} else { |
||||
arg1 = Number(arg1.toString().replace(".", "")); |
||||
arg2 = Number(arg2.toString().replace(".", "")); |
||||
} |
||||
}, |
||||
return (arg1 + arg2) / m; |
||||
} |
||||
} |
||||
|
||||
// 给Number类型增加一个sub方法,调用起来更加方便。
|
||||
sub: function (num, arg) { |
||||
return accSub(num, arg); |
||||
// 给Number类型增加一个sub方法,调用起来更加方便。
|
||||
export function sub(num, arg) { |
||||
return accSub(num, arg); |
||||
|
||||
/** |
||||
** 减法函数,用来得到精确的减法结果 |
||||
** 说明:javascript的减法结果会有误差,在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果。 |
||||
** 调用:accSub(arg1,arg2) |
||||
** 返回值:arg1加上arg2的精确结果 |
||||
**/ |
||||
function accSub (arg1, arg2) { |
||||
var r1, r2, m, n; |
||||
try { |
||||
r1 = arg1.toString().split(".")[1].length; |
||||
} catch (e) { |
||||
r1 = 0; |
||||
} |
||||
try { |
||||
r2 = arg2.toString().split(".")[1].length; |
||||
} catch (e) { |
||||
r2 = 0; |
||||
} |
||||
m = Math.pow(10, Math.max(r1, r2)); // last modify by deeka //动态控制精度长度
|
||||
n = (r1 >= r2) ? r1 : r2; |
||||
return ((arg1 * m - arg2 * m) / m).toFixed(n); |
||||
/** |
||||
** 减法函数,用来得到精确的减法结果 |
||||
** 说明:javascript的减法结果会有误差,在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果。 |
||||
** 调用:accSub(arg1,arg2) |
||||
** 返回值:arg1加上arg2的精确结果 |
||||
**/ |
||||
function accSub(arg1, arg2) { |
||||
let r1, r2, m, n; |
||||
try { |
||||
r1 = arg1.toString().split(".")[1].length; |
||||
} catch (e) { |
||||
r1 = 0; |
||||
} |
||||
try { |
||||
r2 = arg2.toString().split(".")[1].length; |
||||
} catch (e) { |
||||
r2 = 0; |
||||
} |
||||
}, |
||||
m = Math.pow(10, Math.max(r1, r2)); // last modify by deeka //动态控制精度长度
|
||||
n = (r1 >= r2) ? r1 : r2; |
||||
return ((arg1 * m - arg2 * m) / m).toFixed(n); |
||||
} |
||||
} |
||||
|
||||
// 给Number类型增加一个mul方法,调用起来更加方便。
|
||||
mul: function (num, arg) { |
||||
return accMul(arg, num); |
||||
// 给Number类型增加一个mul方法,调用起来更加方便。
|
||||
export function mul(num, arg) { |
||||
return accMul(arg, num); |
||||
|
||||
/** |
||||
** 乘法函数,用来得到精确的乘法结果 |
||||
** 说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。 |
||||
** 调用:accMul(arg1,arg2) |
||||
** 返回值:arg1乘以 arg2的精确结果 |
||||
**/ |
||||
function accMul (arg1, arg2) { |
||||
var m = 0, s1 = arg1.toString(), s2 = arg2.toString(); |
||||
try { |
||||
m += s1.split(".")[1].length; |
||||
} catch (e) { |
||||
} |
||||
try { |
||||
m += s2.split(".")[1].length; |
||||
} catch (e) { |
||||
} |
||||
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m); |
||||
/** |
||||
** 乘法函数,用来得到精确的乘法结果 |
||||
** 说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。 |
||||
** 调用:accMul(arg1,arg2) |
||||
** 返回值:arg1乘以 arg2的精确结果 |
||||
**/ |
||||
function accMul(arg1, arg2) { |
||||
let m = 0, s1 = arg1.toString(), s2 = arg2.toString(); |
||||
try { |
||||
m += s1.split(".")[1].length; |
||||
} catch (e) { |
||||
} |
||||
}, |
||||
|
||||
// 给Number类型增加一个div方法,调用起来更加方便。
|
||||
div: function (num, arg) { |
||||
return accDivide(num, arg); |
||||
|
||||
/** |
||||
* Return digits length of a number |
||||
* @param {*number} num Input number |
||||
*/ |
||||
function digitLength (num) { |
||||
// Get digit length of e
|
||||
var eSplit = num.toString().split(/[eE]/); |
||||
var len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0)); |
||||
return len > 0 ? len : 0; |
||||
} |
||||
/** |
||||
* 把小数转成整数,支持科学计数法。如果是小数则放大成整数 |
||||
* @param {*number} num 输入数 |
||||
*/ |
||||
function float2Fixed (num) { |
||||
if (num.toString().indexOf("e") === -1) { |
||||
return Number(num.toString().replace(".", "")); |
||||
} |
||||
var dLen = digitLength(num); |
||||
return dLen > 0 ? num * Math.pow(10, dLen) : num; |
||||
try { |
||||
m += s2.split(".")[1].length; |
||||
} catch (e) { |
||||
} |
||||
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 精确乘法 |
||||
*/ |
||||
function times (num1, num2) { |
||||
var others = []; |
||||
for (var _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))); |
||||
} |
||||
var num1Changed = float2Fixed(num1); |
||||
var num2Changed = float2Fixed(num2); |
||||
var baseNum = digitLength(num1) + digitLength(num2); |
||||
var leftValue = num1Changed * num2Changed; |
||||
return leftValue / Math.pow(10, baseNum); |
||||
// 给Number类型增加一个div方法,调用起来更加方便。
|
||||
export function div(num, arg) { |
||||
return accDivide(num, arg); |
||||
|
||||
/** |
||||
* Return digits length of a number |
||||
* @param {*number} num Input number |
||||
*/ |
||||
function digitLength(num) { |
||||
// Get digit length of e
|
||||
const eSplit = num.toString().split(/[eE]/); |
||||
const len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0)); |
||||
return len > 0 ? len : 0; |
||||
} |
||||
|
||||
/** |
||||
* 把小数转成整数,支持科学计数法。如果是小数则放大成整数 |
||||
* @param {*number} num 输入数 |
||||
*/ |
||||
function float2Fixed(num) { |
||||
if (num.toString().indexOf("e") === -1) { |
||||
return Number(num.toString().replace(".", "")); |
||||
} |
||||
const dLen = digitLength(num); |
||||
return dLen > 0 ? num * Math.pow(10, dLen) : num; |
||||
} |
||||
|
||||
/** |
||||
* 精确除法 |
||||
*/ |
||||
function accDivide (num1, num2) { |
||||
var others = []; |
||||
for (var _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))); |
||||
} |
||||
var num1Changed = float2Fixed(num1); |
||||
var num2Changed = float2Fixed(num2); |
||||
return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1))); |
||||
/** |
||||
* 精确乘法 |
||||
*/ |
||||
function times(num1, num2) { |
||||
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))); |
||||
} |
||||
const num1Changed = float2Fixed(num1); |
||||
const num2Changed = float2Fixed(num2); |
||||
const baseNum = digitLength(num1) + digitLength(num2); |
||||
const leftValue = num1Changed * num2Changed; |
||||
return leftValue / Math.pow(10, baseNum); |
||||
} |
||||
|
||||
}); |
||||
/** |
||||
* 精确除法 |
||||
*/ |
||||
function accDivide(num1, num2) { |
||||
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))); |
||||
} |
||||
const num1Changed = float2Fixed(num1); |
||||
const num2Changed = float2Fixed(num2); |
||||
return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1))); |
||||
} |
||||
} |
||||
|
@ -1,123 +1,121 @@
|
||||
import {isString} from "../2.base"; |
||||
|
||||
/** |
||||
* 对字符串对象的扩展 |
||||
* @class String |
||||
* 判断字符串是否已指定的字符串开始 |
||||
* @param str source字符串 |
||||
* @param {String} startTag 指定的开始字符串 |
||||
* @return {Boolean} 如果字符串以指定字符串开始则返回true,否则返回false |
||||
*/ |
||||
BI._.extend(BI, { |
||||
export function startWith(str, startTag) { |
||||
str = str || ""; |
||||
if (startTag == null || startTag == "" || str.length === 0 || startTag.length > str.length) { |
||||
return false; |
||||
} |
||||
return str.substring(0, startTag.length) == startTag; |
||||
} |
||||
|
||||
/** |
||||
* 判断字符串是否已指定的字符串开始 |
||||
* @param str source字符串 |
||||
* @param {String} startTag 指定的开始字符串 |
||||
* @return {Boolean} 如果字符串以指定字符串开始则返回true,否则返回false |
||||
*/ |
||||
startWith: function (str, startTag) { |
||||
str = str || ""; |
||||
if (startTag == null || startTag == "" || str.length === 0 || startTag.length > str.length) { |
||||
return false; |
||||
} |
||||
return str.substr(0, startTag.length) == startTag; |
||||
}, |
||||
/** |
||||
* 判断字符串是否以指定的字符串结束 |
||||
* @param str source字符串 |
||||
* @param {String} endTag 指定的字符串 |
||||
* @return {Boolean} 如果字符串以指定字符串结束则返回true,否则返回false |
||||
*/ |
||||
endWith: function (str, endTag) { |
||||
if (endTag == null || endTag == "" || str.length === 0 || endTag.length > str.length) { |
||||
return false; |
||||
} |
||||
return str.substring(str.length - endTag.length) == endTag; |
||||
}, |
||||
/** |
||||
* 判断字符串是否以指定的字符串结束 |
||||
* @param str source字符串 |
||||
* @param {String} endTag 指定的字符串 |
||||
* @return {Boolean} 如果字符串以指定字符串结束则返回true,否则返回false |
||||
*/ |
||||
export function endWith(str, endTag) { |
||||
if (endTag == null || endTag == "" || str.length === 0 || endTag.length > str.length) { |
||||
return false; |
||||
} |
||||
return str.substring(str.length - endTag.length) == endTag; |
||||
} |
||||
|
||||
/** |
||||
* 获取url中指定名字的参数 |
||||
* @param str source字符串 |
||||
* @param {String} name 参数的名字 |
||||
* @return {String} 参数的值 |
||||
*/ |
||||
getQuery: function (str, name) { |
||||
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); |
||||
var r = str.substr(str.indexOf("?") + 1).match(reg); |
||||
if (r) { |
||||
return unescape(r[2]); |
||||
} |
||||
return null; |
||||
}, |
||||
/** |
||||
* 获取url中指定名字的参数 |
||||
* @param str source字符串 |
||||
* @param {String} name 参数的名字 |
||||
* @return {String} 参数的值 |
||||
*/ |
||||
export function getQuery(str, name) { |
||||
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); |
||||
const r = str.substr(str.indexOf("?") + 1).match(reg); |
||||
if (r) { |
||||
return unescape(r[2]); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 给url加上给定的参数 |
||||
* @param str source字符串 |
||||
* @param {Object} paras 参数对象,是一个键值对对象 |
||||
* @return {String} 添加了给定参数的url |
||||
*/ |
||||
appendQuery: function (str, paras) { |
||||
if (!paras) { |
||||
return str; |
||||
} |
||||
var src = str; |
||||
// 没有问号说明还没有参数
|
||||
if (src.indexOf("?") === -1) { |
||||
src += "?"; |
||||
} |
||||
// 如果以问号结尾,说明没有其他参数
|
||||
if (BI.endWith(src, "?") !== false) { |
||||
} else { |
||||
src += "&"; |
||||
} |
||||
BI._.each(paras, function (value, name) { |
||||
if (typeof(name) === "string") { |
||||
src += name + "=" + value + "&"; |
||||
} |
||||
}); |
||||
src = src.substr(0, src.length - 1); |
||||
return src; |
||||
}, |
||||
/** |
||||
* 将所有符合第一个字符串所表示的字符串替换成为第二个字符串 |
||||
* @param str source字符串 |
||||
* @param {String} s1 要替换的字符串的正则表达式 |
||||
* @param {String} s2 替换的结果字符串 |
||||
* @returns {String} 替换后的字符串 |
||||
*/ |
||||
replaceAll: function (str, s1, s2) { |
||||
return BI.isString(str) ? str.replace(new RegExp(s1, "gm"), s2) : str; |
||||
}, |
||||
/** |
||||
* 总是让字符串以指定的字符开头 |
||||
* @param str source字符串 |
||||
* @param {String} start 指定的字符 |
||||
* @returns {String} 以指定字符开头的字符串 |
||||
*/ |
||||
perfectStart: function (str, start) { |
||||
if (BI.startWith(str, start)) { |
||||
return str; |
||||
/** |
||||
* 给url加上给定的参数 |
||||
* @param str source字符串 |
||||
* @param {Object} paras 参数对象,是一个键值对对象 |
||||
* @return {String} 添加了给定参数的url |
||||
*/ |
||||
export function appendQuery(str, paras) { |
||||
if (!paras) { |
||||
return str; |
||||
} |
||||
let src = str; |
||||
// 没有问号说明还没有参数
|
||||
if (src.indexOf("?") === -1) { |
||||
src += "?"; |
||||
} |
||||
// 如果以问号结尾,说明没有其他参数
|
||||
if (BI.endWith(src, "?") !== false) { |
||||
} else { |
||||
src += "&"; |
||||
} |
||||
BI._.each(paras, function (value, name) { |
||||
if (typeof (name) === "string") { |
||||
src += name + "=" + value + "&"; |
||||
} |
||||
return start + str; |
||||
}); |
||||
src = src.substr(0, src.length - 1); |
||||
return src; |
||||
} |
||||
|
||||
}, |
||||
/** |
||||
* 将所有符合第一个字符串所表示的字符串替换成为第二个字符串 |
||||
* @param str source字符串 |
||||
* @param {String} s1 要替换的字符串的正则表达式 |
||||
* @param {String} s2 替换的结果字符串 |
||||
* @returns {String} 替换后的字符串 |
||||
*/ |
||||
export function replaceAll(str, s1, s2) { |
||||
return isString(str) ? str.replace(new RegExp(s1, "gm"), s2) : str; |
||||
} |
||||
|
||||
/** |
||||
* 获取字符串中某字符串的所有项位置数组 |
||||
* @param str source字符串 |
||||
* @param {String} sub 子字符串 |
||||
* @return {Number[]} 子字符串在父字符串中出现的所有位置组成的数组 |
||||
*/ |
||||
allIndexOf: function (str, sub) { |
||||
if (typeof sub !== "string") { |
||||
return []; |
||||
} |
||||
var location = []; |
||||
var offset = 0; |
||||
while (str.length > 0) { |
||||
var loc = str.indexOf(sub); |
||||
if (loc === -1) { |
||||
break; |
||||
} |
||||
location.push(offset + loc); |
||||
str = str.substring(loc + sub.length, str.length); |
||||
offset += loc + sub.length; |
||||
/** |
||||
* 总是让字符串以指定的字符开头 |
||||
* @param str source字符串 |
||||
* @param {String} start 指定的字符 |
||||
* @returns {String} 以指定字符开头的字符串 |
||||
*/ |
||||
export function perfectStart(str, start) { |
||||
if (BI.startWith(str, start)) { |
||||
return str; |
||||
} |
||||
return start + str; |
||||
|
||||
} |
||||
|
||||
/** |
||||
* 获取字符串中某字符串的所有项位置数组 |
||||
* @param str source字符串 |
||||
* @param {String} sub 子字符串 |
||||
* @return {Number[]} 子字符串在父字符串中出现的所有位置组成的数组 |
||||
*/ |
||||
export function allIndexOf(str, sub) { |
||||
if (typeof sub !== "string") { |
||||
return []; |
||||
} |
||||
const location = []; |
||||
let offset = 0; |
||||
while (str.length > 0) { |
||||
const loc = str.indexOf(sub); |
||||
if (loc === -1) { |
||||
break; |
||||
} |
||||
return location; |
||||
location.push(offset + loc); |
||||
str = str.substring(loc + sub.length, str.length); |
||||
offset += loc + sub.length; |
||||
} |
||||
}); |
||||
return location; |
||||
} |
||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue