Browse Source

REPORT-95153 fix: BI.encodeURIComponent 编码异常

es6
zsmj 1 year ago
parent
commit
e17bf87f28
  1. 96
      packages/fineui/src/core/func/alias.js

96
packages/fineui/src/core/func/alias.js

@ -1,9 +1,21 @@
import { each, isFunction, isNull, isObject, isPlainObject, keys, leftPad, parseDateTime, values, isArray } from "../2.base"; import {
each,
get,
isFunction,
isNull,
isObject,
isPlainObject,
keys,
leftPad,
parseDateTime,
values,
isArray
} from "../2.base";
import { replaceAll } from "./string"; import { replaceAll } from "./string";
import { getFullDayName, getMonthName, getTimezone } from "./date"; import { getFullDayName, getMonthName, getTimezone } from "./date";
import { _global } from "../0.foundation"; import { _global } from "../0.foundation";
export const specialCharsMap = {}; const getSpecialCharsMap = () => (get(_global, ["BI", "specialCharsMap"]) || {});
function isEmpty(value) { function isEmpty(value) {
// 判断是否为空值 // 判断是否为空值
@ -142,7 +154,7 @@ function _eFormat(text, fmt) {
let roundNum = Math.round(num); let roundNum = Math.round(num);
num = (`${num}`).split(".")[0]; num = (`${num}`).split(".")[0];
roundNum = (`${roundNum}`).split(".")[0]; roundNum = (`${roundNum}`).split(".")[0];
return num.length !== roundNum.length; return num.length !== roundNum.length;
} }
} }
@ -161,7 +173,7 @@ function _dealNumberPrecision(text, fright) {
precision++; precision++;
} }
} }
return Number(text).toFixed(precision); return Number(text).toFixed(precision);
} }
@ -185,7 +197,7 @@ function _numberFormat(text, format) {
if (text >= 0) { if (text >= 0) {
return _numberFormat(`${text}`, format.substring(0, numMod)); return _numberFormat(`${text}`, format.substring(0, numMod));
} }
return _numberFormat(`${-text}`, format.substring(numMod + 1)); return _numberFormat(`${-text}`, format.substring(numMod + 1));
} else { } else {
// 兼容格式处理负数的情况(copy:fr-jquery.format.js) // 兼容格式处理负数的情况(copy:fr-jquery.format.js)
@ -275,7 +287,7 @@ function _dealWithRight(tright, fright) {
} }
} }
result.num = right; result.num = right;
return result; return result;
} }
@ -346,11 +358,11 @@ function _dealWithLeft(tleft, fleft) {
} }
left = left.replace(/[0-9]+,/, newstr); left = left.replace(/[0-9]+,/, newstr);
} }
return left; return left;
} }
export const cjkEncode = function (text) { export const cjkEncode = function(text) {
// alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的) // alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的)
if (typeof text !== "string") { if (typeof text !== "string") {
return text; return text;
@ -376,7 +388,7 @@ export const cjkEncode = function (text) {
* @param text 需要做解码的字符串 * @param text 需要做解码的字符串
* @return {String} 解码后的字符串 * @return {String} 解码后的字符串
*/ */
export const cjkDecode = function (text) { export const cjkDecode = function(text) {
if (text == null) { if (text == null) {
return ""; return "";
} }
@ -416,11 +428,11 @@ const SPECIAL_TAGS = {
"\x20": " ", "\x20": " ",
"\n": "
", "\n": "
",
}; };
export const htmlEncode = function (text) { export const htmlEncode = function(text) {
return isNull(text) ? "" : replaceAll(`${text}`, keys(SPECIAL_TAGS).join("|"), v => SPECIAL_TAGS[v] ? SPECIAL_TAGS[v] : v); return isNull(text) ? "" : replaceAll(`${text}`, keys(SPECIAL_TAGS).join("|"), v => SPECIAL_TAGS[v] ? SPECIAL_TAGS[v] : v);
}; };
// html decode // html decode
export const htmlDecode = function (text) { export const htmlDecode = function(text) {
return isNull(text) ? "" : replaceAll(`${text}`, values(SPECIAL_TAGS).join("|"), v => { return isNull(text) ? "" : replaceAll(`${text}`, values(SPECIAL_TAGS).join("|"), v => {
switch (v) { switch (v) {
case "&": case "&":
@ -441,7 +453,7 @@ export const htmlDecode = function (text) {
}); });
}; };
export const cjkEncodeDO = function (o) { export const cjkEncodeDO = function(o) {
if (isPlainObject(o)) { if (isPlainObject(o)) {
const result = {}; const result = {};
each(o, (v, k) => { each(o, (v, k) => {
@ -452,14 +464,14 @@ export const cjkEncodeDO = function (o) {
k = cjkEncode(k); k = cjkEncode(k);
result[k] = cjkEncode(v); result[k] = cjkEncode(v);
}); });
return result; return result;
} }
return o; return o;
}; };
export const jsonEncode = function (o) { export const jsonEncode = function(o) {
// james:这个Encode是抄的EXT的 // james:这个Encode是抄的EXT的
const useHasOwn = !!{}.hasOwnProperty; const useHasOwn = !!{}.hasOwnProperty;
@ -476,7 +488,7 @@ export const jsonEncode = function (o) {
"\\": "\\\\", "\\": "\\\\",
}; };
const encodeString = function (s) { const encodeString = function(s) {
if (/["\\\x00-\x1f]/.test(s)) { if (/["\\\x00-\x1f]/.test(s)) {
return `"${s.replace(/([\x00-\x1f\\"])/g, (a, b) => { return `"${s.replace(/([\x00-\x1f\\"])/g, (a, b) => {
let c = m[b]; let c = m[b];
@ -484,17 +496,17 @@ export const jsonEncode = function (o) {
return c; return c;
} }
c = b.charCodeAt(); c = b.charCodeAt();
return `\\u00${ return `\\u00${
Math.floor(c / 16).toString(16) Math.floor(c / 16).toString(16)
}${(c % 16).toString(16)}`; }${(c % 16).toString(16)}`;
})}"`; })}"`;
} }
return `"${s}"`; return `"${s}"`;
}; };
const encodeArray = function (o) { const encodeArray = function(o) {
let a = ["["], b, i, l = o.length, v; let a = ["["], b, i, l = o.length, v;
for (i = 0; i < l; i += 1) { for (i = 0; i < l; i += 1) {
v = o[i]; v = o[i];
@ -512,7 +524,7 @@ export const jsonEncode = function (o) {
} }
} }
a.push("]"); a.push("]");
return a.join(""); return a.join("");
}; };
@ -556,11 +568,11 @@ export const jsonEncode = function (o) {
} }
} }
a.push("}"); a.push("}");
return a.join(""); return a.join("");
}; };
export const jsonDecode = function (text) { export const jsonDecode = function(text) {
let jo; let jo;
try { try {
jo = JSON.parse(text); jo = JSON.parse(text);
@ -589,7 +601,7 @@ export const jsonDecode = function (text) {
if (!json || typeof json !== "string") { if (!json || typeof json !== "string") {
return false; return false;
} }
return json.indexOf("__time__") !== -1; return json.indexOf("__time__") !== -1;
} }
@ -619,11 +631,12 @@ export const jsonDecode = function (text) {
* @example * @example
* BI.getEncodeURL("design/{tableName}/{fieldName}",{tableName: "A", fieldName: "a"}) // design/A/a * BI.getEncodeURL("design/{tableName}/{fieldName}",{tableName: "A", fieldName: "a"}) // design/A/a
*/ */
export const getEncodeURL = function (urlTemplate, param) { export const getEncodeURL = function(urlTemplate, param) {
return replaceAll(urlTemplate, "\\{(.*?)\\}", (ori, str) => encodeURIComponent(isObject(param) ? param[str] : param)); return replaceAll(urlTemplate, "\\{(.*?)\\}", (ori, str) => encodeURIComponent(isObject(param) ? param[str] : param));
}; };
export const encodeURIComponent = function (url) { export const encodeURIComponent = function(url) {
const specialCharsMap = getSpecialCharsMap();
url = url || ""; url = url || "";
url = replaceAll(`${url}`, keys(specialCharsMap || []).join("|"), str => { url = replaceAll(`${url}`, keys(specialCharsMap || []).join("|"), str => {
switch (str) { switch (str) {
@ -633,22 +646,23 @@ export const encodeURIComponent = function (url) {
return specialCharsMap[str] || str; return specialCharsMap[str] || str;
} }
}); });
return _global.encodeURIComponent(url); return _global.encodeURIComponent(url);
}; };
export const decodeURIComponent = function (url) { export const decodeURIComponent = function(url) {
const specialCharsMap = getSpecialCharsMap();
const reserveSpecialCharsMap = {}; const reserveSpecialCharsMap = {};
each(specialCharsMap, (initialChar, encodeChar) => { each(specialCharsMap, (initialChar, encodeChar) => {
reserveSpecialCharsMap[encodeChar] = initialChar === "\\\\" ? "\\" : initialChar; reserveSpecialCharsMap[encodeChar] = initialChar === "\\\\" ? "\\" : initialChar;
}); });
url = url || ""; url = url || "";
url = replaceAll(`${url}`, keys(reserveSpecialCharsMap || []).join("|"), str => reserveSpecialCharsMap[str] || str); url = replaceAll(`${url}`, keys(reserveSpecialCharsMap || []).join("|"), str => reserveSpecialCharsMap[str] || str);
return _global.decodeURIComponent(url); return _global.decodeURIComponent(url);
}; };
export const contentFormat = function (cv, fmt) { export const contentFormat = function(cv, fmt) {
if (isEmpty(cv)) { if (isEmpty(cv)) {
// 原值为空,返回空字符 // 原值为空,返回空字符
return ""; return "";
@ -685,7 +699,7 @@ export const contentFormat = function (cv, fmt) {
} }
// ¤ - 货币格式 // ¤ - 货币格式
text = text.replace(/¤/g, "¥"); text = text.replace(/¤/g, "¥");
return text; return text;
}; };
@ -694,7 +708,7 @@ export const contentFormat = function (cv, fmt) {
* @param fmt 日期格式 * @param fmt 日期格式
* @returns {string} * @returns {string}
*/ */
export const parseFmt = function (fmt) { export const parseFmt = function(fmt) {
if (!fmt) { if (!fmt) {
return ""; return "";
} }
@ -742,12 +756,12 @@ export const parseFmt = function (fmt) {
* @param format 日期格式 * @param format 日期格式
* @returns {Date} * @returns {Date}
*/ */
export const str2Date = function (str, format) { export const str2Date = function(str, format) {
if (typeof str != "string" || typeof format != "string") { if (typeof str != "string" || typeof format != "string") {
return null; return null;
} }
const fmt = parseFmt(format); const fmt = parseFmt(format);
return parseDateTime(str, fmt); return parseDateTime(str, fmt);
}; };
@ -760,7 +774,7 @@ export const str2Date = function (str, format) {
* @param format * @param format
* @returns {string} * @returns {string}
*/ */
export const date2Str = function (date, format) { export const date2Str = function(date, format) {
if (!date) { if (!date) {
return ""; return "";
} }
@ -789,7 +803,7 @@ export const date2Str = function (date, format) {
len: len - start, len: len - start,
}, date); }, date);
} }
return result; return result;
function compileJFmt(jfmt, date) { function compileJFmt(jfmt, date) {
@ -863,12 +877,12 @@ export const date2Str = function (date, format) {
str = jfmt.str; str = jfmt.str;
break; break;
} }
return str; return str;
} }
}; };
export const object2Number = function (value) { export const object2Number = function(value) {
if (value == null) { if (value == null) {
return 0; return 0;
} }
@ -879,11 +893,11 @@ export const object2Number = function (value) {
if (str.indexOf(".") === -1) { if (str.indexOf(".") === -1) {
return parseInt(str); return parseInt(str);
} }
return parseFloat(str); return parseFloat(str);
}; };
export const object2Date = function (obj) { export const object2Date = function(obj) {
if (obj == null) { if (obj == null) {
return new Date(); return new Date();
} }
@ -902,7 +916,7 @@ export const object2Date = function (obj) {
return new Date(); return new Date();
}; };
export const object2Time = function (obj) { export const object2Time = function(obj) {
if (obj == null) { if (obj == null) {
return new Date(); return new Date();
} }
@ -925,6 +939,6 @@ export const object2Time = function (obj) {
if (!isInvalidDate(dt)) { if (!isInvalidDate(dt)) {
return dt; return dt;
} }
return new Date(); return new Date();
}; };

Loading…
Cancel
Save