@ -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 ) {
// 判断是否为空值
// 判断是否为空值
@ -350,7 +362,7 @@ function _dealWithLeft(tleft, fleft) {
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 ) => {
@ -459,7 +471,7 @@ export const cjkEncodeDO = function (o) {
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 ] ;
@ -494,7 +506,7 @@ export const jsonEncode = function (o) {
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 ] ;
@ -560,7 +572,7 @@ export const jsonEncode = function (o) {
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 ) ;
@ -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 ) {
@ -637,7 +650,8 @@ export const encodeURIComponent = function (url) {
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 ;
@ -648,7 +662,7 @@ export const decodeURIComponent = function (url) {
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 "" ;
@ -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,7 +756,7 @@ 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 ;
}
}
@ -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 "" ;
}
}
@ -868,7 +882,7 @@ export const date2Str = function (date, format) {
}
}
} ;
} ;
export const object2Number = function ( value ) {
export const object2Number = function ( value ) {
if ( value == null ) {
if ( value == null ) {
return 0 ;
return 0 ;
}
}
@ -883,7 +897,7 @@ export const object2Number = function (value) {
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 ( ) ;
}
}