@antdv/pro-utils
Version:
@antdv/pro-utils
143 lines (142 loc) • 5.03 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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;
};
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
useUrlSearchParams: () => useUrlSearchParams
});
module.exports = __toCommonJS(stdin_exports);
var import_vue = require("vue");
var import_useEffect = require("../useEffect");
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 = (0, import_vue.computed)(() => {
if (config.disabled) return {};
return new URLSearchParams(locationSearch || {});
});
const params = (0, import_vue.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());
}
}
(0, import_useEffect.useEffect)(() => {
if (config.disabled) return;
if (typeof window === "undefined" || !window.URL) return;
redirectToNewSearchParams(__spreadValues(__spreadValues({}, initial), params.value));
}, (0, import_vue.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;
}