Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

改进 getSearch 函数实现 #7089

Merged
merged 4 commits into from Jan 16, 2023
Merged

Conversation

Zuoqiu-Yingyi
Copy link
Contributor

兼容 key 为空字符串与 value 为空字符串的 URL search params

  • Please commit to the dev branch 请提交到 dev 开发分支
  • For bug fixes, please describe the problem and solution via code comments 对于修复缺陷,请通过代码注释描述问题和解决方案

URL search params 中 keyvalue 均可为空字符串, 且其中的字符可能会进行 URI 编码, 因此需要进行规范处理

@88250 88250 requested a review from Vanessa219 January 16, 2023 15:39
@88250 88250 added the Refactor label Jan 16, 2023
@88250 88250 changed the title 🎨 改进 getSearch 方法 改进 getSearch 函数实现 Jan 16, 2023
@88250 88250 added this to the 2.7.0 milestone Jan 16, 2023
@Vanessa219 Vanessa219 merged commit 67743b5 into siyuan-note:dev Jan 16, 2023
@Zuoqiu-Yingyi Zuoqiu-Yingyi deleted the dev-url-search branch January 16, 2023 15:56
@Vanessa219
Copy link
Member

我回滚了, getSearch("page", "assets/第13章 编程陷阱-20220524015630-wd53jod.pdf?page=12") 这个无法使用

@Zuoqiu-Yingyi
Copy link
Contributor Author

我回滚了, getSearch("page", "assets/第13章 编程陷阱-20220524015630-wd53jod.pdf?page=12") 这个无法使用

哎呀, 下面这个应该可以兼容😂

export const getSearch: (key: string, link?: string) => string | null = (key: string, link = window.location.search) => {
    // REF https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams
    const urlSearchParams = new URLSearchParams(link.substring(link.indexOf('?')));
    return urlSearchParams.get(key);
};

@Vanessa219
Copy link
Member

可以继续 PR,最后如果是 null 的话就 return "", 这样其他地方就不用再改了。 😊

@Zuoqiu-Yingyi Zuoqiu-Yingyi restored the dev-url-search branch January 16, 2023 17:24
@Zuoqiu-Yingyi
Copy link
Contributor Author

Zuoqiu-Yingyi commented Jan 16, 2023

可以继续 PR

👌

最后如果是 null 的话就 return "", 这样其他地方就不用再改了。 😊

这里返回 "" 的话有歧义, 因为值为空的字段也是合法的:
?key1=&key2=value2
这里 key1 的 value 就是一个空字符串

Zuoqiu-Yingyi added a commit to Zuoqiu-Yingyi/siyuan that referenced this pull request Jan 16, 2023
兼容 `getSearch("page", "assets/第13章 编程陷阱-20220524015630-wd53jod.pdf?page=12")`
REF: siyuan-note#7089
@Zuoqiu-Yingyi Zuoqiu-Yingyi mentioned this pull request Jan 16, 2023
2 tasks
@88250 88250 modified the milestone: 2.7.0 Jan 17, 2023
Vanessa219 pushed a commit that referenced this pull request Jan 19, 2023
* 🎨 改进 `getSearch` 方法

兼容 key 为空字符串与 value 为空字符串的 URL search params

* 🎨 改进 `getSearch` 方法

* 🎨 style

* 🎨 改进 `getSearch` 方法

兼容 `getSearch("page", "assets/第13章 编程陷阱-20220524015630-wd53jod.pdf?page=12")`
REF: #7089

* 🎨 改进 `getSearch` 方法

* `getSearch` 返回值兼容 `null` 与 `""`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants