UNPKG

@antdv/pro-utils

Version:

@antdv/pro-utils

126 lines (125 loc) 4.16 kB
var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; import { computed } from "vue"; import { useEffect } from "../useEffect/index.mjs"; function setQueryToCurrentUrl(params) { var _a; const { URL } = typeof window !== "undefined" ? window : {}; const url = new URL((_a = window == null ? void 0 : window.location) == null ? void 0 : _a.href); Object.keys(params).forEach((key) => { const value = params[key]; if (value !== null && value !== void 0) { if (Array.isArray(value)) { url.searchParams.delete(key); value.forEach((valueItem) => { url.searchParams.append(key, valueItem); }); } else if (value instanceof Date) { if (!Number.isNaN(value.getTime())) { url.searchParams.set(key, value.toISOString()); } } else if (typeof value === "object") { url.searchParams.set(key, JSON.stringify(value)); } else { url.searchParams.set(key, value); } } else { url.searchParams.delete(key); } }); return url; } function useUrlSearchParams(initial = {}, config = { disabled: false }) { var _a; const locationSearch = typeof window !== "undefined" && ((_a = window == null ? void 0 : window.location) == null ? void 0 : _a.search); const urlSearchParams = computed(() => { if (config.disabled) return {}; return new URLSearchParams(locationSearch || {}); }); const params = computed(() => { var _a2; if (config.disabled) return {}; if (typeof window === "undefined" || !window.URL) return {}; let result = []; (_a2 = urlSearchParams.value) == null ? void 0 : _a2.forEach((value, key) => { result.push({ key, value }); }); result = result.reduce((acc, val) => { (acc[val.key] = acc[val.key] || []).push(val); return acc; }, {}); result = Object.keys(result).map((key) => { const valueGroup = result[key]; if (valueGroup.length === 1) { return [key, valueGroup[0].value]; } return [key, valueGroup.map(({ value }) => value)]; }); const newParams = __spreadValues({}, initial); result.forEach(([key, value]) => { newParams[key] = parseValue(key, value, {}, initial); }); return newParams; }); function redirectToNewSearchParams(newParams) { if (typeof window === "undefined" || !window.URL) return; const url = setQueryToCurrentUrl(newParams); if (window.location.search !== url.search) { window.history.replaceState(window.history.state, "", url.toString()); } } useEffect(() => { if (config.disabled) return; if (typeof window === "undefined" || !window.URL) return; redirectToNewSearchParams(__spreadValues(__spreadValues({}, initial), params.value)); }, computed(() => [config.disabled, params.value])); const setParams = (newParams) => { redirectToNewSearchParams(newParams); }; return [params.value, setParams]; } const booleanValues = { true: true, false: false }; function parseValue(key, _value, types, defaultParams) { if (!types) return _value; const type = types[key]; const value = _value === void 0 ? defaultParams[key] : _value; if (type === Number) { return Number(value); } if (type === Boolean || _value === "true" || _value === "false") { return booleanValues[value]; } if (Array.isArray(type)) { return type.find((item) => item == value) || defaultParams[key]; } return value; } export { useUrlSearchParams };