Skip to content

Commit

Permalink
Merge pull request #14 from krutoo/url-utils
Browse files Browse the repository at this point in the history
url-utils
  • Loading branch information
krutoo authored Oct 15, 2024
2 parents 02197b6 + d009574 commit 7c2ec26
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 11 deletions.
4 changes: 3 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.41.0",
"@std/assert": "jsr:@std/assert@^0.220.1",
"@std/expect": "jsr:@std/expect@^1.0.5",
"@std/testing": "jsr:@std/testing@^0.220.1"
},
"exports": {
".": "./src/mod.ts",
"./middleware": "./src/middleware/mod.ts",
"./response": "./src/response.ts",
"./server": "./src/server.ts"
"./server": "./src/server.ts",
"./url": "./src/url/mod.ts"
},
"fmt": {
"lineWidth": 100,
Expand Down
51 changes: 45 additions & 6 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions scripts/build-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ await build({
name: './server',
path: './src/server.ts',
},
{
name: './url',
path: './src/url/mod.ts',
},
],
outDir: './npm',
shims: {
Expand All @@ -40,8 +44,7 @@ await build({
compilerOptions: {
lib: ['ES2022', 'DOM', 'DOM.Iterable'],
},
async postBuild() {
await Deno.copyFile('README.md', './npm/README.md');
await Deno.copyFile('LICENSE', './npm/LICENSE');
},
});

await Deno.copyFile('README.md', './npm/README.md');
await Deno.copyFile('LICENSE', './npm/LICENSE');
57 changes: 57 additions & 0 deletions src/url/__test__/url.test.ts
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);
});
2 changes: 2 additions & 0 deletions src/url/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './url.ts';
export * from './types.ts';
1 change: 1 addition & 0 deletions src/url/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type URLSearchParamsInit = Record<string, unknown>;
55 changes: 55 additions & 0 deletions src/url/url.ts
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;

0 comments on commit 7c2ec26

Please sign in to comment.