All files / src/reactivity url-search-params.js

88% Statements 132/150
100% Branches 21/21
64.28% Functions 9/14
87.67% Lines 128/146

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 1472x 2x 2x 2x 2x 2x 2x 2x 14x 14x 14x 14x 14x 14x 18x 5x 5x 5x 18x 18x 18x 18x 14x 14x 14x 14x 14x 12x 7x 7x 12x 8x 8x 7x 12x 8x 8x 7x 7x 7x 7x 14x 14x 14x 14x 14x 14x 14x 7x 7x 7x 7x 14x 14x 14x 14x 14x 14x 14x 5x 5x 5x 4x 4x 4x 5x 14x 14x 14x 14x 14x 14x 25x 25x 25x 14x 14x 14x 14x 14x 14x 6x 6x 6x 14x 14x 14x 14x 14x 14x 14x 4x 4x 4x 14x 14x       14x 14x 14x 14x 14x 14x 14x 11x 11x 11x 11x 11x 7x 7x 7x 11x 14x 14x         14x 14x 19x 19x 19x 14x 14x       14x 14x       14x 14x     14x 14x       14x  
import { source } from '../internal/client/reactivity/sources.js';
import { get } from '../internal/client/runtime.js';
import { get_current_url } from './url.js';
import { increment } from './utils.js';
 
export const REPLACE = Symbol();
 
export class SvelteURLSearchParams extends URLSearchParams {
	#version = source(0);
	#url = get_current_url();
 
	#updating = false;
 
	#update_url() {
		if (!this.#url || this.#updating) return;
		this.#updating = true;
 
		const search = this.toString();
		this.#url.search = search && `?${search}`;
 
		this.#updating = false;
	}
 
	/**
	 * @param {URLSearchParams} params
	 */
	[REPLACE](params) {
		if (this.#updating) return;
		this.#updating = true;
 
		for (const key of [...super.keys()]) {
			super.delete(key);
		}
 
		for (const [key, value] of params) {
			super.append(key, value);
		}
 
		increment(this.#version);
		this.#updating = false;
	}
 
	/**
	 * @param {string} name
	 * @param {string} value
	 * @returns {void}
	 */
	append(name, value) {
		super.append(name, value);
		this.#update_url();
		increment(this.#version);
	}
 
	/**
	 * @param {string} name
	 * @param {string=} value
	 * @returns {void}
	 */
	delete(name, value) {
		var has_value = super.has(name, value);
		super.delete(name, value);
		if (has_value) {
			this.#update_url();
			increment(this.#version);
		}
	}
 
	/**
	 * @param {string} name
	 * @returns {string|null}
	 */
	get(name) {
		get(this.#version);
		return super.get(name);
	}
 
	/**
	 * @param {string} name
	 * @returns {string[]}
	 */
	getAll(name) {
		get(this.#version);
		return super.getAll(name);
	}
 
	/**
	 * @param {string} name
	 * @param {string=} value
	 * @returns {boolean}
	 */
	has(name, value) {
		get(this.#version);
		return super.has(name, value);
	}
 
	keys() {
		get(this.#version);
		return super.keys();
	}
 
	/**
	 * @param {string} name
	 * @param {string} value
	 * @returns {void}
	 */
	set(name, value) {
		var previous = super.getAll(name).join('');
		super.set(name, value);
		// can't use has(name, value), because for something like https://svelte.dev?foo=1&bar=2&foo=3
		// if you set `foo` to 1, then foo=3 gets deleted whilst `has("foo", "1")` returns true
		if (previous !== super.getAll(name).join('')) {
			this.#update_url();
			increment(this.#version);
		}
	}
 
	sort() {
		super.sort();
		this.#update_url();
		increment(this.#version);
	}
 
	toString() {
		get(this.#version);
		return super.toString();
	}
 
	values() {
		get(this.#version);
		return super.values();
	}
 
	entries() {
		get(this.#version);
		return super.entries();
	}
 
	[Symbol.iterator]() {
		return this.entries();
	}
 
	get size() {
		get(this.#version);
		return super.size;
	}
}