-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add getQueryParameter utility function #107
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| [dt-utils](../globals.md) / getQueryParameter | ||
|
|
||
| # Function: getQueryParameter() | ||
|
|
||
| > **getQueryParameter**(`name`, `url?`): `undefined` \| `null` \| `string` \| `number` \| `boolean` | ||
|
|
||
| Defined in: [getQueryParameter/index.ts:35](https://github.com/DTStack/dt-utils/blob/master/src/getQueryParameter/index.ts#L35) | ||
|
|
||
| 从给定的 URL 中获取指定查询参数的值。 | ||
|
|
||
| ## Parameters | ||
|
|
||
| ### name | ||
|
|
||
| `string` | ||
|
|
||
| 要获取的查询参数名 | ||
|
|
||
| ### url? | ||
|
|
||
| `string` | ||
|
|
||
| 目标 URL,若未提供则使用当前页面的 window.location.href | ||
|
|
||
| ## Returns | ||
|
|
||
| `undefined` \| `null` \| `string` \| `number` \| `boolean` | ||
|
|
||
| - 解析后的查询参数值 | ||
|
|
||
| ## Description | ||
|
|
||
| 解析目标 URL 的查询字符串,提取并转换指定名称的查询参数值。 | ||
| 支持处理特殊字符串值: | ||
| - 'null' 会被转换为 null | ||
| - 'undefined' 会被转换为 undefined | ||
| - 可以被 JSON 解析的字符串会被自动解析(例如数字、布尔值) | ||
| - 其他值保持字符串形式 | ||
|
|
||
| ## Example | ||
|
|
||
| ```typescript | ||
| import getQueryParameter from 'dt-utils'; | ||
|
|
||
| // 基本用法 | ||
| getQueryParameter('name', 'https://example.com?name=john&age=25'); // => "john" | ||
|
|
||
| // 处理特殊值 | ||
| getQueryParameter('isActive', 'https://example.com?isActive=true&count=null'); // => true | ||
| getQueryParameter('count', 'https://example.com?isActive=true&count=null'); // => null | ||
|
|
||
| // 不传入 url 时使用当前页面 URL | ||
| // 若当前页面 URL 为 https://current.com?page=home&limit=10 | ||
| getQueryParameter('limit'); // => 10 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import getQueryParameter from '..'; | ||
|
|
||
| describe('getQueryParameter', () => { | ||
| it('should return the parsed value for a parameter from the provided url', () => { | ||
| expect(getQueryParameter('age', 'https://example.com?name=john&age=25')).toBe(25); | ||
| expect(getQueryParameter('enabled', 'https://example.com?enabled=true')).toBe(true); | ||
| }); | ||
|
|
||
| it('should return undefined when the parameter does not exist', () => { | ||
| expect(getQueryParameter('missing', 'https://example.com?name=john')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should use window.location.href when url is not provided', () => { | ||
| const originalLocation = window.location; | ||
|
|
||
| Object.defineProperty(window, 'location', { | ||
| configurable: true, | ||
| value: { | ||
| href: 'https://current.com?page=home&limit=10', | ||
| }, | ||
| }); | ||
|
|
||
| expect(getQueryParameter('page')).toBe('home'); | ||
| expect(getQueryParameter('limit')).toBe(10); | ||
|
|
||
| Object.defineProperty(window, 'location', { | ||
| configurable: true, | ||
| value: originalLocation, | ||
| }); | ||
| }); | ||
|
|
||
| it('should preserve special parsed values from the query string', () => { | ||
| expect(getQueryParameter('empty', 'https://example.com?empty=null')).toBeNull(); | ||
| expect(getQueryParameter('unset', 'https://example.com?unset=undefined')).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import getQueryParameters from '../getQueryParameters'; | ||
|
|
||
| /** | ||
| * 从给定的 URL 中获取指定查询参数的值。 | ||
| * | ||
| * @category Utils | ||
| * @description | ||
| * 解析目标 URL 的查询字符串,提取并转换指定名称的查询参数值。 | ||
| * 支持处理特殊字符串值: | ||
| * - 'null' 会被转换为 null | ||
| * - 'undefined' 会被转换为 undefined | ||
| * - 可以被 JSON 解析的字符串会被自动解析(例如数字、布尔值) | ||
| * - 其他值保持字符串形式 | ||
| * | ||
| * @param {string} name - 要获取的查询参数名 | ||
| * @param {string} [url] - 目标 URL,若未提供则使用当前页面的 window.location.href | ||
| * @returns {string | null | undefined | number | boolean} - 解析后的查询参数值 | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * import getQueryParameter from 'dt-utils'; | ||
| * | ||
| * // 基本用法 | ||
| * getQueryParameter('name', 'https://example.com?name=john&age=25'); // => "john" | ||
| * | ||
| * // 处理特殊值 | ||
| * getQueryParameter('isActive', 'https://example.com?isActive=true&count=null'); // => true | ||
| * getQueryParameter('count', 'https://example.com?isActive=true&count=null'); // => null | ||
| * | ||
| * // 不传入 url 时使用当前页面 URL | ||
| * // 若当前页面 URL 为 https://current.com?page=home&limit=10 | ||
| * getQueryParameter('limit'); // => 10 | ||
| * ``` | ||
| */ | ||
| const getQueryParameter = (name: string, url?: string) => { | ||
|
jin-sir marked this conversation as resolved.
jin-sir marked this conversation as resolved.
|
||
| try { | ||
| const targetUrl = url || window.location.href; | ||
| const search = new URL(targetUrl).search; | ||
| const params = getQueryParameters(search); | ||
|
|
||
| return params[name]; | ||
| } catch (error: unknown) { | ||
| console.error( | ||
| 'Error parsing query parameter:', | ||
| error instanceof Error ? error.message : error | ||
| ); | ||
| return undefined; | ||
| } | ||
| }; | ||
| export default getQueryParameter; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.