# URL

  1. toURL

URL 模块的工具函数使用此函数以兼容各种类型的链接写法

当传入 undefined 时,会解析当前 Location,并且任何不完整的 URL 都将以当前 Location 作为补全

定义:

export declare type URLLike = Location | URL | string;
export declare const toURL: (url?: URLLike) => URL;
  1. getURLParam

获取指定名称的 url 参数

定义:

export declare const getURLParam: (name: string, url?: URLLike) => string;
  1. parseSearch (alias: parseQuery)

将 URL.search 解析为 map

定义:

export declare const parseSearch: (url?: string | Location | URL | undefined) => {
    [k: string]: string;
};
  1. newSearch (alias: newQuery)

将 map 编码为 a=1&b=2 形式(不带问号前缀)

定义:

export declare const newSearch: (params: {
    [k: string]: any;
}) => string;
  1. searchBatchAppend (alias: queryBatchAppend)

增加参数(会对旧的同名参数进行覆盖)

定义:

export declare const searchBatchAppend: (paramsOrSearch?: string | {
    [k: string]: any;
}, url?: string | Location | URL | undefined) => URL;
  1. searchBatchDelete (alias: queryBatchDelete)

删减参数,其余同上

定义:

export declare const searchBatchDelete: (keys?: string[], url?: string | Location | URL | undefined) => URL;
  1. hashModeURL

矫正 hash & query 的顺序

定义:

export declare const hashModeURL: (url?: URLLike) => URL;
  1. toHttps

转 https 协议

定义:

export declare const toHttps: (url?: string | Location | URL | undefined) => URL;
  1. isURLLike

判断 URLLike 类型,行为逻辑与 toURL 一致,可将符合条件的变量转为 URLLike

定义:

export declare const isURLLike: (url: any) => url is URLLike;

用法:

// opts's type is unknown/any/... and match `{ href: string; } | string`
const url = (isURLLike(opts)
  ? toHttps(opts)
  : ...;
更新于: 12/10/2020, 7:25:56 PM