char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somport URL extension to typescriptbf382b3

main
1.5 KiB54 linesraw
1import { registerUncloneable } from "ext:web/serialization.js";
2import {
3  ObjectSetPrototypeOf,
4  StringPrototypeSlice,
5  WeakMap,
6} from "ext:core/primordials";
7import {
8  op_url_search_parse,
9  op_url_search_serialize,
10  op_url_set,
11} from "ext:core/ops";
12import type { SearchPair, URLParts } from "ext:core/ops";
13import type { URLSearchParams } from "./search_params.js";
14
15interface URLState {
16  parts: URLParts;
17  searchParams: URLSearchParams;
18}
19interface ParamsState {
20  list: SearchPair[];
21  url: URLState | null;
22}
23
24const urls = new WeakMap<object, URLState>();
25const params = new WeakMap<object, ParamsState>();
26registerUncloneable(urls);
27registerUncloneable(params);
28
29function privateArray<T = unknown>(): T[];
30function privateArray<T extends unknown[]>(value: T): T;
31function privateArray(value: unknown[] = []): unknown[] {
32  // Private storage must not invoke inherited numeric setters or Array species hooks.
33  ObjectSetPrototypeOf(value, null);
34  return value;
35}
36function queryList(parts: URLParts): SearchPair[] {
37  const list = privateArray(
38    op_url_search_parse(StringPrototypeSlice(parts[9], 1)),
39  );
40  for (let index = 0; index < list.length; index++) privateArray(list[index]);
41  return list;
42}
43function update(value: ParamsState): void {
44  if (value.url) {
45    value.url.parts = op_url_set([
46      value.url.parts[0],
47      "search",
48      op_url_search_serialize(value.list),
49    ]);
50  }
51}
52
53export { params, privateArray, queryList, update, urls };
54export type { ParamsState };