-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from krutoo/url-utils
url-utils
- Loading branch information
Showing
7 changed files
with
170 additions
and
11 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { describe } from '@std/testing/bdd'; | ||
import { expect } from '@std/expect'; | ||
import { resetParams, setParams, withoutParams, withParams } from '../url.ts'; | ||
|
||
describe('withParams', () => { | ||
const input = new URL('http://hello.com?foo=1&bar=2'); | ||
|
||
expect(input.searchParams.get('foo')).toBe('1'); | ||
expect(input.searchParams.get('bar')).toBe('2'); | ||
|
||
const output = withParams(input, { foo: null, bar: 3, baz: 4 }); | ||
|
||
expect(output.searchParams.get('foo')).toBe(null); | ||
expect(output.searchParams.get('bar')).toBe('3'); | ||
expect(output.searchParams.get('baz')).toBe('4'); | ||
}); | ||
|
||
describe('withoutParams', () => { | ||
const input = new URL('http://hello.com?foo=1&bar=2'); | ||
|
||
expect(input.searchParams.get('foo')).toBe('1'); | ||
expect(input.searchParams.get('bar')).toBe('2'); | ||
|
||
const output = withoutParams(input); | ||
|
||
expect(output.searchParams.get('foo')).toBe(null); | ||
expect(output.searchParams.get('bar')).toBe(null); | ||
expect(output.searchParams.get('baz')).toBe(null); | ||
}); | ||
|
||
describe('setParams', () => { | ||
const input = new URL('http://hello.com?foo=1&bar=2'); | ||
|
||
expect(input.searchParams.get('foo')).toBe('1'); | ||
expect(input.searchParams.get('bar')).toBe('2'); | ||
|
||
const output = setParams(input, { foo: null, bar: 3, baz: 4 }); | ||
|
||
expect(Object.is(input, output)).toBe(true); | ||
expect(output.searchParams.get('foo')).toBe(null); | ||
expect(output.searchParams.get('bar')).toBe('3'); | ||
expect(output.searchParams.get('baz')).toBe('4'); | ||
}); | ||
|
||
describe('resetParams', () => { | ||
const input = new URL('http://hello.com?foo=1&bar=2'); | ||
|
||
expect(input.searchParams.get('foo')).toBe('1'); | ||
expect(input.searchParams.get('bar')).toBe('2'); | ||
|
||
const output = resetParams(input); | ||
|
||
expect(Object.is(input, output)).toBe(true); | ||
expect(input.searchParams.get('foo')).toBe(null); | ||
expect(input.searchParams.get('bar')).toBe(null); | ||
expect(input.searchParams.get('baz')).toBe(null); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './url.ts'; | ||
export * from './types.ts'; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export type URLSearchParamsInit = Record<string, unknown>; |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { URLSearchParamsInit } from './types.ts'; | ||
|
||
/** | ||
* Получив параметры применит их к переданному URL. | ||
* @param url URL. | ||
* @param params Параметры. | ||
*/ | ||
export function setParams(url: URL, params: URLSearchParamsInit): URL { | ||
for (const [paramName, paramValue] of Object.entries(params)) { | ||
if (paramValue === null) { | ||
url.searchParams.delete(paramName); | ||
continue; | ||
} | ||
|
||
if (paramValue !== undefined) { | ||
url.searchParams.set(paramName, String(paramValue)); | ||
continue; | ||
} | ||
} | ||
|
||
return url; | ||
} | ||
|
||
export function resetParams(url: URL): URL { | ||
url.search = ''; | ||
return url; | ||
} | ||
|
||
/** | ||
* Получив URL и параметры вернет новый URL с примененными параметрами. | ||
* @param url URL. | ||
* @param params Параметры. | ||
* @return URL. | ||
*/ | ||
export function withParams(url: string | URL, params: URLSearchParamsInit): URL { | ||
return setParams(new URL(url), params); | ||
} | ||
|
||
/** | ||
* Получив URL вернет его копию но без параметров. | ||
* @param url URL. | ||
* @return URL. | ||
*/ | ||
export function withoutParams(url: string | URL): URL { | ||
return resetParams(new URL(url)); | ||
} | ||
|
||
export const URLUtil = { | ||
setParams, | ||
resetParams, | ||
withParams, | ||
withoutParams, | ||
}; | ||
|
||
export default URLUtil; |