Browse Source

KERNEL-14316 chore: 修复 string 的一些细节 BUG

es6
Treecat 2 years ago
parent
commit
07aec861b9
  1. 21
      src/core/func/string.js

21
src/core/func/string.js

@ -1,4 +1,4 @@
import {isString, each} from "../2.base";
import { isString, each } from "../2.base";
/**
* 判断字符串是否已指定的字符串开始
@ -11,6 +11,7 @@ export function startWith(str, startTag) {
if (startTag == null || startTag == "" || str.length === 0 || startTag.length > str.length) {
return false;
}
return str.substring(0, startTag.length) == startTag;
}
@ -24,6 +25,7 @@ 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;
}
@ -34,11 +36,12 @@ export function endWith(str, endTag) {
* @return {String} 参数的值
*/
export function getQuery(str, name) {
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`);
const r = str.substr(str.indexOf("?") + 1).match(reg);
if (r) {
return unescape(r[2]);
}
return null;
}
@ -58,16 +61,17 @@ export function appendQuery(str, paras) {
src += "?";
}
// 如果以问号结尾,说明没有其他参数
if (BI.endWith(src, "?") !== false) {
} else {
if (!src.endsWith("?")) {
src += "&";
}
each(paras, function (value, name) {
each(paras, (name, value) => {
if (typeof (name) === "string") {
src += name + "=" + value + "&";
src += `${name}=${value}&`;
}
});
src = src.substr(0, src.length - 1);
return src;
}
@ -89,11 +93,11 @@ export function replaceAll(str, s1, s2) {
* @returns {String} 以指定字符开头的字符串
*/
export function perfectStart(str, start) {
if (BI.startWith(str, start)) {
if (str.startsWith(start)) {
return str;
}
return start + str;
return start + str;
}
/**
@ -117,5 +121,6 @@ export function allIndexOf(str, sub) {
str = str.substring(loc + sub.length, str.length);
offset += loc + sub.length;
}
return location;
}

Loading…
Cancel
Save