-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: url parse add parseQueryInts function and tests (#81)
* feat: add parseQueryInts function and corresponding tests - Introduced parseQueryInts function to convert URL query parameter values into an array of integers, with support for custom separators. - Enhanced error handling to throw TypeError for invalid inputs, ensuring robust type safety. - Added unit tests for parseQueryInts in url-parse.test.ts, covering various input scenarios including valid integers, empty strings, and invalid formats. - Updated documentation with detailed JSDoc comments in both English and Chinese, clarifying parameters, return values, and error handling. * feat: add parseQueryNumbers function and corresponding tests - Introduced parseQueryNumbers function to convert URL query parameter values into an array of numbers, supporting custom separators. - Enhanced error handling to throw TypeError for invalid inputs, including NaN and Infinity values. - Added unit tests for parseQueryNumbers in url-parse.test.ts, covering various input scenarios including valid numbers, empty strings, and invalid formats. - Updated documentation with detailed JSDoc comments in both English and Chinese, clarifying parameters, return values, and error handling. * chore: update version to 0.2.1 in deno.json
- Loading branch information
Showing
3 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -227,3 +227,95 @@ export function parseQueryPositiveInts( | |
throw new TypeError(`invalid query positive int array: ${query}`); | ||
} | ||
} | ||
|
||
/** | ||
* Convert URL query parameter value to array of integers | ||
* 将 URL 查询参数值转换为整数数组 | ||
* | ||
* @param query - URL query parameter value (typically from url.searchParams.get()) | ||
* URL 查询参数值(通常来自 url.searchParams.get()) | ||
* @param options - Configuration options | ||
* 配置选项 | ||
* @param options.separator - Delimiter used to split the string (默认为逗号 ',') | ||
* 用于分割字符串的分隔符 | ||
* @returns An array of integers or undefined | ||
* 返回整数数组或 undefined | ||
* - Returns undefined if input is null, "undefined" or empty string | ||
* 当输入为 null、"undefined" 或空字符串时返回 undefined | ||
* - Returns array of integers for valid input | ||
* 对于有效的数字输入返回整数数组 | ||
* - Throws TypeError for invalid input | ||
* 当输入无效时抛出 TypeError | ||
* | ||
* @example | ||
* parseQueryInts('1,2,3') // returns [1, 2, 3] | ||
* parseQueryInts('-1,0,1') // returns [-1, 0, 1] | ||
* parseQueryInts('') // returns undefined | ||
* parseQueryInts('1|2|3', { separator: '|' }) // returns [1, 2, 3] | ||
* parseQueryInts('abc') // throws TypeError | ||
* | ||
* @author iugo <[email protected]> | ||
*/ | ||
export function parseQueryInts( | ||
query: string | null, | ||
{ separator = ',' }: { separator?: string } = {}, | ||
): number[] | undefined { | ||
if (query === null || query === 'undefined' || query === '') { | ||
return undefined; | ||
} | ||
try { | ||
const arr = query.split(separator).map(toInt); | ||
return arr.length === 0 ? undefined : arr; | ||
} catch (_err) { | ||
throw new TypeError(`invalid query int array: ${query}`); | ||
} | ||
} | ||
|
||
/** | ||
* Convert URL query parameter value to array of numbers | ||
* 将 URL 查询参数值转换为数字数组 | ||
* | ||
* @param query - URL query parameter value (typically from url.searchParams.get()) | ||
* URL 查询参数值(通常来自 url.searchParams.get()) | ||
* @param options - Configuration options | ||
* 配置选项 | ||
* @param options.separator - Delimiter used to split the string (默认为逗号 ',') | ||
* 用于分割字符串的分隔符 | ||
* @returns An array of numbers or undefined | ||
* 返回数字数组或 undefined | ||
* - Returns undefined if input is null, "undefined" or empty string | ||
* 当输入为 null、"undefined" 或空字符串时返回 undefined | ||
* - Returns array of numbers for valid input (包括整数和浮点数) | ||
* 对于有效的数字输入返回数字数组(包括整数和浮点数) | ||
* - Throws TypeError for invalid input or NaN/Infinity values | ||
* 当输入无效或为 NaN/Infinity 时抛出 TypeError | ||
* | ||
* @example | ||
* parseQueryNumbers('1,2,3') // returns [1, 2, 3] | ||
* parseQueryNumbers('-1.5,0,1.5') // returns [-1.5, 0, 1.5] | ||
* parseQueryNumbers('') // returns undefined | ||
* parseQueryNumbers('1|2|3', { separator: '|' }) // returns [1, 2, 3] | ||
* parseQueryNumbers('abc') // throws TypeError | ||
* | ||
* @author iugo <[email protected]> | ||
*/ | ||
export function parseQueryNumbers( | ||
query: string | null, | ||
{ separator = ',' }: { separator?: string } = {}, | ||
): number[] | undefined { | ||
if (query === null || query === 'undefined' || query === '') { | ||
return undefined; | ||
} | ||
try { | ||
const arr = query.split(separator).map((v) => { | ||
const num = parseQueryNumber(v); | ||
if (num === undefined) { | ||
throw new TypeError(`invalid number: ${v}`); | ||
} | ||
return num; | ||
}); | ||
return arr.length === 0 ? undefined : arr; | ||
} catch (_err) { | ||
throw new TypeError(`invalid query number array: ${query}`); | ||
} | ||
} |