Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 8x 8x 8x 8x 2x 2x 8x 1x 8x 1x 1x 1x 1x 1x 8x 8x 3x 2x 1x 8x 1x 10x 5x 2x 2x 2x 1x 8x 23x 8x 10x 8x 6x 8x 7x 3x 3x 8x 3x 1x 1x 8x 3x 3x 3x 8x 2x 2x 62x 62x 62x 2x 8x 3x 1x 2x 8x 3x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x 3x 3x 3x 3x 1x | import { IAnyObject, IntegrationError } from '../types/common'
import { voidFun, globalVar, HTTP_CODE, ERRORTYPES } from '../common/constant'
import { logger } from './logger'
import { nativeToString, variableTypeDetection } from './is'
export function getLocationHref(): string {
Iif (typeof document === 'undefined' || document.location == null) return ''
return document.location.href
}
// 用到所有事件名称
type TotalEventName = keyof GlobalEventHandlersEventMap | keyof XMLHttpRequestEventTargetEventMap | keyof WindowEventMap
/**
* 添加事件监听器
*
* ../export
* ../param {{ addEventListener: Function }} target
* ../param {keyof TotalEventName} eventName
* ../param {Function} handler
* ../param {(boolean | Object)} opitons
* ../returns
*/
export function on(target: { addEventListener: Function }, eventName: TotalEventName, handler: Function, Eopitons: boolean | unknown = false): void {
target.addEventListener(eventName, handler, opitons)
}
/**
*
* 重写对象上面的某个属性
* ../param source 需要被重写的对象
* ../param name 需要被重写对象的key
* ../param replacement 以原有的函数作为参数,执行并重写原有函数
* ../returns void
*/
export function replaceOld(source: IAnyObject, name: string, replacement: (...args: any[]) => any, EisForced = false): void {
Eif (name in source || isForced) {
const original = source[name]
const wrapped = replacement(original)
Eif (typeof wrapped === 'function') {
source[name] = wrapped
}
}
}
/**
* 用&分割对象,返回a=1&b=2
* ../param obj 需要拼接的对象
*/
// export function splitObjToQuery(obj: Record<string, unknown>): string {
// return Object.entries(obj).reduce((result, [key, value], index) => {
// if (index !== 0) {
// result += '&'
// }
// result += `${key}=${value}`
// return result
// }, '')
// }
export const defaultFunctionName = '<anonymous>'
/**
* 需要获取函数名,匿名则返回<anonymous>
* ../param {unknown} fn 需要获取函数名的函数本体
* ../returns 返回传入的函数的函数名
*/
export function getFunctionName(fn: unknown): string {
if (!fn || typeof fn !== 'function') {
return defaultFunctionName
}
return fn.name || defaultFunctionName
}
// 函数防抖
/**
*
* ../param fn 需要防抖的函数
* ../param delay 防抖的时间间隔
* ../param isImmediate 是否需要立即执行,默认为false,第一次是不执行的
* ../returns 返回一个包含防抖功能的函数
*/
// export const debounce = (fn: voidFun, delay: number, isImmediate = false): voidFun => {
// let timer = null
// return function (...args: any) {
// if (isImmediate) {
// fn.apply(this, args)
// isImmediate = false
// return
// }
// clearTimeout(timer)
// timer = setTimeout(() => {
// fn.apply(this, args)
// }, delay)
// }
// }
// 函数节流
/**
*
* ../param fn 需要节流的函数
* ../param delay 节流的时间间隔
* ../returns 返回一个包含节流功能的函数
*/
export const throttle = (fn: Function, delay: number): Function => {
let canRun = true
return function (...args: any) {
if (!canRun) return
fn.apply(this, args)
canRun = false
setTimeout(() => {
canRun = true
}, delay)
}
}
/**
* 获取当前的时间戳
* ../returns 返回当前时间戳
*/
export function getTimestamp(): number {
return Date.now()
}
export function typeofAny(target: any, type: string): boolean {
return typeof target === type
}
export function toStringAny(target: any, type: string): boolean {
return nativeToString.call(target) === type
}
export function validateOption(target: any, targetName: string, expectType: string): boolean {
if (typeofAny(target, expectType)) return true
typeof target !== 'undefined' && logger.error(`${targetName}期望传入${expectType}类型,目前是${typeof target}类型`)
return false
}
export function toStringValidateOption(target: any, targetName: string, expectType: string): boolean {
if (toStringAny(target, expectType)) return true
typeof target !== 'undefined' && logger.error(`${targetName}期望传入${expectType}类型,目前是${nativeToString.call(target)}类型`)
return false
}
export function slientConsoleScope(callback: Function) {
globalVar.isLogAddBreadcrumb = false
callback()
globalVar.isLogAddBreadcrumb = true
}
export function generateUUID(): string {
let d = new Date().getTime()
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (d + Math.random() * 16) % 16 | 0
d = Math.floor(d / 16)
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16)
})
return uuid
}
export function unknownToString(target: unknown): string {
if (variableTypeDetection.isString(target)) {
return target as string
}
return JSON.stringify(target)
}
export function getBigVersion(version: string) {
return Number(version.split('.')[0])
}
export function isHttpFail(code: number) {
return code === 0 || code === HTTP_CODE.BAD_REQUEST || code > HTTP_CODE.UNAUTHORIZED
}
/**
* 给url添加query
* @param url
* @param query
*/
export function setUrlQuery(url: string, query: object) {
const queryArr = []
Object.keys(query).forEach((k) => {
queryArr.push(`${k}=${query[k]}`)
})
if (url.indexOf('?') !== -1) {
url = `${url}&${queryArr.join('&')}`
} else {
url = `${url}?${queryArr.join('&')}`
}
return url
}
/**
* 解析字符串错误信息,返回message、name、stacks
* @param str error string
*/
export function parseErrorString(str: string): IntegrationError {
const splitLine: string[] = str.split('\n')
Iif (splitLine.length < 2) return null
Eif (splitLine[0].indexOf('MiniProgramError') !== -1) {
splitLine.splice(0, 1)
}
const message = splitLine.splice(0, 1)[0]
const name = splitLine.splice(0, 1)[0].split(':')[0]
const stacks = []
splitLine.forEach((errorLine: string) => {
const regexpGetFun = /at\s+([\S]+)\s+\(/ // 获取 [ 函数名 ]
const regexGetFile = /\(([^)]+)\)/ // 获取 [ 有括号的文件 , 没括号的文件 ]
const regexGetFileNoParenthese = /\s+at\s+(\S+)/ // 获取 [ 有括号的文件 , 没括号的文件 ]
const funcExec = regexpGetFun.exec(errorLine)
let fileURLExec = regexGetFile.exec(errorLine)
if (!fileURLExec) {
// 假如为空尝试解析无括号的URL
fileURLExec = regexGetFileNoParenthese.exec(errorLine)
}
const funcNameMatch = Array.isArray(funcExec) && funcExec.length > 0 ? funcExec[1].trim() : ''
const fileURLMatch = Array.isArray(fileURLExec) && fileURLExec.length > 0 ? fileURLExec[1] : ''
const lineInfo = fileURLMatch.split(':')
stacks.push({
args: [], // 请求参数
func: funcNameMatch || ERRORTYPES.UNKNOWN_FUNCTION, // 前端分解后的报错
column: Number(lineInfo.pop()), // 前端分解后的列
line: Number(lineInfo.pop()), // 前端分解后的行
url: lineInfo.join(':') // 前端分解后的URL
})
})
return {
message,
name,
stacks
}
}
|