Skip to content

Commit

Permalink
Merge pull request #26 from AegisJSProject/patch/remove-url
Browse files Browse the repository at this point in the history
Remove `url` and `SearchParam` functions
  • Loading branch information
shgysk8zer0 authored Nov 26, 2024
2 parents 9bac048 + 455bfd9 commit a9280d8
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 97 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v1.1.1] - 2024-11-25

### Removed
- Remove `url` tagged template and `SearchParam`, as are now in `@aegisjsproject/url`

## [v1.1.0] - 2024-11-19

### Added
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aegisjsproject/router",
"version": "1.1.0",
"version": "1.1.1",
"description": "A simple but powerful router module",
"keywords": [],
"type": "module",
Expand Down
90 changes: 0 additions & 90 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,96 +501,6 @@ async function _get404(url = location, method = 'GET', { signal, formData, integ
}
}

/**
* Class representing a URL search parameter accessor.
* Extends `EventTarget` to support listening for updates on the parameter.
*/
export class SearchParam extends EventTarget {
#name;
#fallbackValue = '';

/**
* Creates a search parameter accessor.
* @param {string} name - The name of the URL search parameter to manage.
* @param {string|number} fallbackValue - The default value if the search parameter is not set.
*/
constructor(name, fallbackValue) {
super();
this.#name = name;
this.#fallbackValue = fallbackValue;
}

toString() {
return this.#value;
}

get [Symbol.toStringTag]() {
return 'SearchParam';
}

[Symbol.toPrimitive](hint = 'default') {
return hint === 'number' ? parseFloat(this.#value) : this.#value;
}

get #value() {
const params = new URLSearchParams(location.search);
return params.get(this.#name) ?? this.#fallbackValue?.toString() ?? '';
}
}

export function getSearch(key, fallbackValue, onChange, { signal, passive, once } = {}) {
if (onChange instanceof Function) {
const param = new SearchParam(key, fallbackValue);
param.addEventListener('change', onChange, { signal, passive, once });
return param;
} else {
return new SearchParam(key, fallbackValue);
}
}

/**
* Manages a specified URL search parameter as a live-updating stateful value.
*
* @param {string} key - The name of the URL search parameter to manage.
* @param {string|number} [fallbackValue=''] - The initial/fallback value if the search parameter is not set.
* @returns {[SearchParam, function(string|number): void]} - Returns a two-element array:
* - Returns a two-element array:
* - The first element is an object with:
* - A `toString` method, returning the current value of the URL parameter as a string.
* - A `[Symbol.toPrimitive]` method, allowing automatic conversion of the value based on the context (e.g., string or number).
* - The second element is a setter function that updates the URL search parameter to a new value, reflected immediately in the URL without reloading the page.
*/
export function manageSearch(key, fallbackValue = '', onChange, { signal, passive, once } = {}) {
const param = getSearch(key, fallbackValue, onChange, { once, passive, signal });

return [
param,
(newValue, { method = 'replace', cause = null } = {}) => {
const url = new URL(location.href);
const oldValue = url.searchParams.get(key);
url.searchParams.set(key, newValue);

const event = new CustomEvent('change', {
cancelable: true,
detail: { name: key, newValue, oldValue, method, url, cause },
});

param.dispatchEvent(event);

if (event.defaultPrevented) {
return;
} else if (method === 'replace') {
history.replaceState(history.state, '', url.href);
} else if (method === 'push') {
history.pushState(history.state, '', url.href);
} else {
throw new TypeError(`Invalid update method: ${method}.`);
}
}
];
}


/**
* Finds the matching URL pattern for a given input.
*
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { init, back, forward, reload, registerPath, url, observePreloadsOn } from '@aegisjsproject/router/router.js';
import { init, back, forward, reload, registerPath, observePreloadsOn } from '@aegisjsproject/router/router.js';
import { observeEvents } from '@aegisjsproject/core/events.js';
import { reset } from '@aegisjsproject/styles/reset.js';
import { baseTheme, lightTheme, darkTheme } from '@aegisjsproject/styles/theme.js';
Expand Down Expand Up @@ -48,7 +48,7 @@ init('#routes', {

observePreloadsOn('#nav');

registerPath('/product/?id=:productId', ({ matches }) => url`${location.origin}/product/${matches.search.groups.productId}`);
registerPath('/product/?id=:productId', ({ matches }) => URL.parse(`${location.origin}/product/${matches.search.groups.productId}`));

document.querySelectorAll('[data-nav]').forEach(el => {
el.addEventListener('click', ({ currentTarget }) => {
Expand Down
3 changes: 1 addition & 2 deletions test/views/github.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { manageState } from '@aegisjsproject/state';
import { preconnect } from '@aegisjsproject/router/router.js';
import { url } from '@aegisjsproject/url/url.js';

const [users, setUsers] = manageState('github:users', {});

Expand All @@ -13,7 +12,7 @@ export default async ({ matches, signal }) => {
if (typeof username !== 'string') {
return `<p>Error: No GitHub username provided.</p>`;
} else if (! (username in users)) {
const apiUrl = url`https://api.github.com/users/${username}`;
const apiUrl = URL.parse(`https://api.github.com/users/${username}`);
const resp = await fetch(apiUrl, { referrerPolicy: 'no-referrer' });

if (!resp.ok) {
Expand Down

0 comments on commit a9280d8

Please sign in to comment.