All files / utils/dist helpers.js

89.62% Statements 95/106
75.44% Branches 43/57
86.36% Functions 19/22
89.32% Lines 92/103

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  9x 9x 9x 9x 9x   16x   16x   9x   3x 3x   9x   11x 11x 11x 11x 11x 11x       9x 9x   3x 2x   1x   9x 9x 3x 3x 5x 5x     5x 3x 2x 2x 2x 1x         30x   9x   23x   9x   10x   9x   20x 6x 14x 14x   9x   7x 3x 4x 4x   9x   3x 3x 3x   9x   3x 3x 93x 93x 93x   3x   9x   7x 3x   4x 1x   3x   9x   3x   9x       9x                           9x   1x 1x   1x 1x   1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x   3x 3x 3x 3x               1x           9x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseErrorString = exports.setUrlQuery = exports.isHttpFail = exports.getBigVersion = exports.unknownToString = exports.generateUUID = exports.slientConsoleScope = exports.toStringValidateOption = exports.validateOption = exports.toStringAny = exports.typeofAny = exports.getTimestamp = exports.throttle = exports.getFunctionName = exports.defaultFunctionName = exports.replaceOld = exports.on = exports.getLocationHref = void 0;
var common_1 = require("@mito/common");
var logger_1 = require("./logger");
var is_1 = require("./is");
function getLocationHref() {
    Iif (typeof document === 'undefined' || document.location == null)
        return '';
    return document.location.href;
}
exports.getLocationHref = getLocationHref;
function on(target, eventName, handler, opitons) {
    if (opitons === void 0) { opitons = false; }
    target.addEventListener(eventName, handler, opitons);
}
exports.on = on;
function replaceOld(source, name, replacement, isForced) {
    Eif (isForced === void 0) { isForced = false; }
    Eif (name in source || isForced) {
        var original = source[name];
        var wrapped = replacement(original);
        Eif (typeof wrapped === 'function') {
            source[name] = wrapped;
        }
    }
}
exports.replaceOld = replaceOld;
exports.defaultFunctionName = '<anonymous>';
function getFunctionName(fn) {
    if (!fn || typeof fn !== 'function') {
        return exports.defaultFunctionName;
    }
    return fn.name || exports.defaultFunctionName;
}
exports.getFunctionName = getFunctionName;
exports.throttle = function (fn, delay) {
    var canRun = true;
    return function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        if (!canRun)
            return;
        fn.apply(this, args);
        canRun = false;
        setTimeout(function () {
            canRun = true;
        }, delay);
    };
};
function getTimestamp() {
    return Date.now();
}
exports.getTimestamp = getTimestamp;
function typeofAny(target, type) {
    return typeof target === type;
}
exports.typeofAny = typeofAny;
function toStringAny(target, type) {
    return is_1.nativeToString.call(target) === type;
}
exports.toStringAny = toStringAny;
function validateOption(target, targetName, expectType) {
    if (typeofAny(target, expectType))
        return true;
    typeof target !== 'undefined' && logger_1.logger.error(targetName + "\u671F\u671B\u4F20\u5165" + expectType + "\u7C7B\u578B\uFF0C\u76EE\u524D\u662F" + typeof target + "\u7C7B\u578B");
    return false;
}
exports.validateOption = validateOption;
function toStringValidateOption(target, targetName, expectType) {
    if (toStringAny(target, expectType))
        return true;
    typeof target !== 'undefined' && logger_1.logger.error(targetName + "\u671F\u671B\u4F20\u5165" + expectType + "\u7C7B\u578B\uFF0C\u76EE\u524D\u662F" + is_1.nativeToString.call(target) + "\u7C7B\u578B");
    return false;
}
exports.toStringValidateOption = toStringValidateOption;
function slientConsoleScope(callback) {
    common_1.globalVar.isLogAddBreadcrumb = false;
    callback();
    common_1.globalVar.isLogAddBreadcrumb = true;
}
exports.slientConsoleScope = slientConsoleScope;
function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16);
    });
    return uuid;
}
exports.generateUUID = generateUUID;
function unknownToString(target) {
    if (is_1.variableTypeDetection.isString(target)) {
        return target;
    }
    if (is_1.variableTypeDetection.isUndefined(target)) {
        return 'undefined';
    }
    return JSON.stringify(target);
}
exports.unknownToString = unknownToString;
function getBigVersion(version) {
    return Number(version.split('.')[0]);
}
exports.getBigVersion = getBigVersion;
function isHttpFail(code) {
    return code === 0 || code === common_1.HTTP_CODE.BAD_REQUEST || code > common_1.HTTP_CODE.UNAUTHORIZED;
}
exports.isHttpFail = isHttpFail;
function setUrlQuery(url, query) {
    var queryArr = [];
    Object.keys(query).forEach(function (k) {
        queryArr.push(k + "=" + query[k]);
    });
    if (url.indexOf('?') !== -1) {
        url = url + "&" + queryArr.join('&');
    }
    else {
        url = url + "?" + queryArr.join('&');
    }
    return url;
}
exports.setUrlQuery = setUrlQuery;
function parseErrorString(str) {
    var splitLine = str.split('\n');
    Iif (splitLine.length < 2)
        return null;
    Eif (splitLine[0].indexOf('MiniProgramError') !== -1) {
        splitLine.splice(0, 1);
    }
    var message = splitLine.splice(0, 1)[0];
    var name = splitLine.splice(0, 1)[0].split(':')[0];
    var stacks = [];
    splitLine.forEach(function (errorLine) {
        var regexpGetFun = /at\s+([\S]+)\s+\(/;
        var regexGetFile = /\(([^)]+)\)/;
        var regexGetFileNoParenthese = /\s+at\s+(\S+)/;
        var funcExec = regexpGetFun.exec(errorLine);
        var fileURLExec = regexGetFile.exec(errorLine);
        if (!fileURLExec) {
            fileURLExec = regexGetFileNoParenthese.exec(errorLine);
        }
        var funcNameMatch = Array.isArray(funcExec) && funcExec.length > 0 ? funcExec[1].trim() : '';
        var fileURLMatch = Array.isArray(fileURLExec) && fileURLExec.length > 0 ? fileURLExec[1] : '';
        var lineInfo = fileURLMatch.split(':');
        stacks.push({
            args: [],
            func: funcNameMatch || common_1.ERRORTYPES.UNKNOWN_FUNCTION,
            column: Number(lineInfo.pop()),
            line: Number(lineInfo.pop()),
            url: lineInfo.join(':')
        });
    });
    return {
        message: message,
        name: name,
        stacks: stacks
    };
}
exports.parseErrorString = parseErrorString;
//# sourceMappingURL=helpers.js.map