@ -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 { getFullDayName , getMonthName , getTimezone } from "./date" ;
import { _global } from "../0.foundation" ;
export const specialCharsMap = { } ;
const getSpecialCharsMap = ( ) => ( get ( _global , [ "BI" , "specialCharsMap" ] ) || { } ) ;
function isEmpty ( value ) {
// 判断是否为空值
@ -350,7 +362,7 @@ function _dealWithLeft(tleft, fleft) {
return left ;
}
export const cjkEncode = function ( text ) {
export const cjkEncode = function ( text ) {
// alex:如果非字符串,返回其本身(cjkEncode(234) 返回 ""是不对的)
if ( typeof text !== "string" ) {
return text ;
@ -376,7 +388,7 @@ export const cjkEncode = function (text) {
* @ param text 需要做解码的字符串
* @ return { String } 解码后的字符串
* /
export const cjkDecode = function ( text ) {
export const cjkDecode = function ( text ) {
if ( text == null ) {
return "" ;
}
@ -416,11 +428,11 @@ const SPECIAL_TAGS = {
"\x20" : " " ,
"\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 ) ;
} ;
// html decode
export const htmlDecode = function ( text ) {
export const htmlDecode = function ( text ) {
return isNull ( text ) ? "" : replaceAll ( ` ${ text } ` , values ( SPECIAL _TAGS ) . join ( "|" ) , v => {
switch ( v ) {
case "&" :
@ -441,7 +453,7 @@ export const htmlDecode = function (text) {
} ) ;
} ;
export const cjkEncodeDO = function ( o ) {
export const cjkEncodeDO = function ( o ) {
if ( isPlainObject ( o ) ) {
const result = { } ;
each ( o , ( v , k ) => {
@ -459,7 +471,7 @@ export const cjkEncodeDO = function (o) {
return o ;
} ;
export const jsonEncode = function ( o ) {
export const jsonEncode = function ( o ) {
// james:这个Encode是抄的EXT的
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 ) ) {
return ` " ${ s . replace ( /([\x00-\x1f\\"])/g , ( a , b ) => {
let c = m [ b ] ;
@ -494,7 +506,7 @@ export const jsonEncode = function (o) {
return ` " ${ s } " ` ;
} ;
const 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 ] ;
@ -560,7 +572,7 @@ export const jsonEncode = function (o) {
return a . join ( "" ) ;
} ;
export const jsonDecode = function ( text ) {
export const jsonDecode = function ( text ) {
let jo ;
try {
jo = JSON . parse ( text ) ;
@ -619,11 +631,12 @@ export const jsonDecode = function (text) {
* @ example
* 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 ) ) ;
} ;
export const encodeURIComponent = function ( url ) {
export const encodeURIComponent = function ( url ) {
const specialCharsMap = getSpecialCharsMap ( ) ;
url = url || "" ;
url = replaceAll ( ` ${ url } ` , keys ( specialCharsMap || [ ] ) . join ( "|" ) , str => {
switch ( str ) {
@ -637,7 +650,8 @@ export const encodeURIComponent = function (url) {
return _global . encodeURIComponent ( url ) ;
} ;
export const decodeURIComponent = function ( url ) {
export const decodeURIComponent = function ( url ) {
const specialCharsMap = getSpecialCharsMap ( ) ;
const reserveSpecialCharsMap = { } ;
each ( specialCharsMap , ( initialChar , encodeChar ) => {
reserveSpecialCharsMap [ encodeChar ] = initialChar === "\\\\" ? "\\" : initialChar ;
@ -648,7 +662,7 @@ export const decodeURIComponent = function (url) {
return _global . decodeURIComponent ( url ) ;
} ;
export const contentFormat = function ( cv , fmt ) {
export const contentFormat = function ( cv , fmt ) {
if ( isEmpty ( cv ) ) {
// 原值为空,返回空字符
return "" ;
@ -694,7 +708,7 @@ export const contentFormat = function (cv, fmt) {
* @ param fmt 日期格式
* @ returns { string }
* /
export const parseFmt = function ( fmt ) {
export const parseFmt = function ( fmt ) {
if ( ! fmt ) {
return "" ;
}
@ -742,7 +756,7 @@ export const parseFmt = function (fmt) {
* @ param format 日期格式
* @ returns { Date }
* /
export const str2Date = function ( str , format ) {
export const str2Date = function ( str , format ) {
if ( typeof str != "string" || typeof format != "string" ) {
return null ;
}
@ -760,7 +774,7 @@ export const str2Date = function (str, format) {
* @ param format
* @ returns { string }
* /
export const date2Str = function ( date , format ) {
export const date2Str = function ( date , format ) {
if ( ! date ) {
return "" ;
}
@ -868,7 +882,7 @@ export const date2Str = function (date, format) {
}
} ;
export const object2Number = function ( value ) {
export const object2Number = function ( value ) {
if ( value == null ) {
return 0 ;
}
@ -883,7 +897,7 @@ export const object2Number = function (value) {
return parseFloat ( str ) ;
} ;
export const object2Date = function ( obj ) {
export const object2Date = function ( obj ) {
if ( obj == null ) {
return new Date ( ) ;
}
@ -902,7 +916,7 @@ export const object2Date = function (obj) {
return new Date ( ) ;
} ;
export const object2Time = function ( obj ) {
export const object2Time = function ( obj ) {
if ( obj == null ) {
return new Date ( ) ;
}