const formatRichText = function ( html ) {
let newContent = html;
newContent = newContent. replace ( / <(img).*?(\/>|<\/img>|>) / g , function ( mats ) {
if ( mats. indexOf ( 'style' ) < 0 ) {
mats = mats. replace ( / <\s*img / , '<img style="max-width:100%;height:auto; display:block;"' ) ;
} else {
mats = mats. replace ( / (max-)*width="[^"]*" / mg , '' ) . replace ( / (max-)*width='[^']*' / gi , '' ) ;
mats = mats. replace ( / (max-)*height="[^"]*" / mg , '' ) . replace ( / (max-)*height='[^']*' / gi , '' ) ;
mats = mats. replace ( / style=("|') / mg , 'style=$1max-width:100%;height:auto; display:block;' ) ;
}
return mats;
} ) ;
newContent = newContent. replace ( / <br[^>]*\/> / mg , '' ) ;
newContent = newContent. replace ( / <table(.*?)width="\d+"\> / mg , '<table width="100%">' ) ;
return newContent;
}
const getCurrentPageUrlWithArgs = function ( ) {
const pages = getCurrentPages ( ) ;
const currentPage = pages[ pages. length - 1 ] ;
const url = currentPage. route;
const options = currentPage. options;
let urlWithArgs = ` / ${ url} ? ` ;
for ( let key in options) {
const value = options[ key] ;
urlWithArgs += ` ${ key} = ${ value} & ` ;
}
urlWithArgs = urlWithArgs. substring ( 0 , urlWithArgs. length - 1 ) ;
return urlWithArgs;
}
const getDataType = function ( data ) {
let type_str = Object . prototype. toString . call ( data) ;
switch ( type_str) {
case '[object Number]' : return 'number' ;
case '[object String]' : return 'string' ;
case '[object Boolean]' : return 'boolean' ;
case '[object Undefined]' : return 'undefined' ;
case '[object Symbol]' : return 'symbol' ;
case '[object Object]' : return 'object' ;
case '[object Array]' : return 'array' ;
case '[object Function]' : return 'function' ;
case '[object Null]' : return 'null' ;
case '[object BigInt]' : return 'bigint' ;
default : return '' ;
}
}
const isEmpty = function ( obj ) {
if ( Object . prototype. toString . call ( obj) == '[object Object]' ) {
return ! Object. getOwnPropertyNames ( obj) . length && ! Object. getOwnPropertySymbols ( obj) . length;
} else {
return false ;
}
}
const isEmptyObject = function ( obj ) {
if ( JSON . stringify ( obj) == '{}' ) return true ;
return false ;
}
const isPhone = function ( phone ) {
if ( ! phone) return false ;
return / ^1[3-9]\d{9}$ / . test ( phone) ;
}
const isSameArr = ( arr, arr1 ) => {
if ( arr. length != arr1. length) return false ;
if ( arr. length == arr1. length && arr. length == 0 ) return true ;
const temp_arr = [ ... arr1] ;
return arr. every ( val => {
const index = temp_arr. findIndex ( v => v === val) ;
return index >= 0 && temp_arr. splice ( index, 1 ) ;
} )
}
const round = function ( number, precision ) {
return Math. round ( + number + 'e' + precision) / Math. pow ( 10 , precision) ;
}
const getRandomInt = function ( min, max ) {
min = + min;
max = + max;
if ( isNaN ( min) || isNaN ( max) ) {
return false ;
}
min = Math. ceil ( min) ;
max = Math. floor ( max) ;
let diff = Math. abs ( max - min) ;
let true_min = min < max ? min : max;
return Math. floor ( Math. random ( ) * diff) + true_min;
}
const resData = function ( return_data ) {
return_data = return_data || { } ;
let {
code = 1 , msg = 'ok' , data = { } , ... extra
} = return_data;
let ret_data = { } ;
ret_data. code = code;
ret_data. msg = msg;
ret_data. data = data;
ret_data. extra = extra;
return ret_data;
}
const getDistance = function ( { longitude_start= 0 , latitude_start= 0 , longitude_end= 0 , latitude_end= 0 , unit = 1 , precision = 2 } ) {
let EARTH_RADIUS = 6370.996 ;
let PI = 3.1415926 ;
let lat_start = latitude_start * PI / 180.0 ;
let lat_end = latitude_end * PI / 180.0 ;
let lng_start = longitude_start * PI / 180.0 ;
let lng_end = longitude_end * PI / 180.0 ;
let lat_diff = lat_start - lat_end;
let lng_diff = lng_start - lng_end;
let distance = 2 * Math. asin ( Math. sqrt ( Math. pow ( Math. sin ( lat_diff / 2 ) , 2 ) + Math. cos ( lat_start) * Math. cos ( lat_end) * Math. pow ( Math. sin ( lng_diff / 2 ) , 2 ) ) ) ;
distance = distance * EARTH_RADIUS * 1000 ;
if ( unit == 2 ) {
distance = distance / 1000 ;
}
return Math. round ( + distance + 'e' + precision) / Math. pow ( 10 , precision) ;
}