UNPKG

@oceanbase/util

Version:
103 lines (101 loc) 3.69 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/util/index.ts var util_exports = {}; __export(util_exports, { directTo: () => directTo, downloadFile: () => downloadFile, findBy: () => findBy, findByValue: () => findByValue, isNullValue: () => isNullValue, toArray: () => toArray }); module.exports = __toCommonJS(util_exports); var import_lodash = require("lodash"); var import_query_string = __toESM(require("query-string")); var import_is_url = __toESM(require("is-url")); function isNullValue(value) { return value === null || value === void 0 || value === "" || (0, import_lodash.isNaN)(value); } function findBy(array, key, value) { return (0, import_lodash.find)(array, (item) => item && item[key] === value) || {}; } function findByValue(array, value) { return (0, import_lodash.find)(array, (item) => item && item.value === value) || {}; } function directTo(url, blank = true) { const routerBase = window.routerBase; const displayMode = window.displayMode; const newUrl = ( // 如果部署在根路径,或者是完整 URL,则使用原始值 !routerBase || routerBase === "/" || (0, import_is_url.default)(url) ? url : `${routerBase}${url}` ); if (blank && displayMode !== "embed") { const blankWindow = window.open("about:blank"); if (blankWindow) { blankWindow.location.href = newUrl; } else { window.location.href = newUrl; } } else { window.location.href = import_query_string.default.stringifyUrl({ url: newUrl, query: { displayMode } }); } } function downloadFile(content, fileName, options) { const blob = new Blob([content], { // 如果是 string 类型文件,则指定默认的 MIME 类型,可以避免大部分场景的乱码 ...(0, import_lodash.isString)(content) ? { type: "text/plain;charset=utf-8" } : {}, ...options }); const blobUrl = window.URL.createObjectURL(blob); const a = document.createElement("a"); a.download = fileName; a.href = blobUrl; a.click(); a.remove(); } function toArray(value) { if (value === void 0 || value === null) { return []; } return Array.isArray(value) ? value : [value]; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { directTo, downloadFile, findBy, findByValue, isNullValue, toArray });