url 处理
url search 参数转化为对象
ts
import { getUrlSearch } from 'one-js-utils-library';
const url1 = 'https://example.com';
getUrlSearch(url1); // {}
const url2 = 'https://example.com?param1=value1';
getUrlSearch(url2); // { param1: 'value1' }
const url3 = 'https://example.com?param1=value1¶m2=value2¶m3=value3';
getUrlSearch(url3);
// {
// param1: 'value1',
// param2: 'value2',
// param3: 'value3',
// };
const url4 = 'https://example.com?param1=value1¶m1=value2¶m1=value3';
getUrlSearch(url4); // { param1: 'value3' }ts
const getUrlSearch = (): Record<string, string> => {
const params: Record<string, string> = {};
let queryString = window.location.search.substring(1);
queryString = decodeURIComponent(queryString);
if (queryString) {
const paramPairs = queryString.split('&');
for (const pair of paramPairs) {
const [key, value] = pair.split('=');
params[key] = value;
}
}
return params;
};对象转化为 url search
ts
import { object2UrlSearch } from 'one-js-utils-library';
const params = {
param1: 'hello world',
param2: '@openai',
param3: '$1234!',
};
object2UrlSearch(params); // param1=hello%20world¶m2=%40openai¶m3=%241234!,ts
const object2UrlSearch = (params: Record<string, string>) => {
const paramPairs: string[] = [];
const hasOwnProperty = Object.prototype.hasOwnProperty;
for (const key in params) {
if (hasOwnProperty.call(params, key)) {
const value = encodeURIComponent(params[key]);
paramPairs.push(`${key}=${value}`);
}
}
return paramPairs.join('&');
};http 转化为 https
ts
import { http2https } from 'one-js-utils-library';
http2https('http://example.com'); // https://example.com
http2https('https://example.com'); // https://example.com
http2https('ftp://example.com'); // ftp://example.comts
const http2https = (url: string) => {
const httpsUrl = url.replace(/^http:/i, 'https:');
return httpsUrl;
};