Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

module-id.ts

31 lines912 Bytes

1/** Checks for a query parameter without rewriting the opaque module ID. */
2export function hasIdQueryFlag(id: string, flag: string): boolean {
3 const queryIndex = id.indexOf('?')
4 if (queryIndex === -1) {
5 return false
6 }
7​
8 return new URLSearchParams(id.slice(queryIndex + 1)).has(flag)
9}
10​
11/** Appends an owned query flag without normalizing the existing query. */
12export function appendIdQueryFlag(id: string, flag: string): string {
13 if (!id.includes('?')) {
14 return `${id}?${flag}`
15 }
16​
17 const separator = id.endsWith('&') ? '' : '&'
18 return `${id}${separator}${flag}`
20​
21/** Removes the owned query flag appended by {@link appendIdQueryFlag}. */
22export function removeIdQueryFlag(id: string, flag: string): string {
23 for (const separator of ['?', '&']) {
24 const suffix = `${separator}${flag}`
25 if (id.endsWith(suffix)) {
26 return id.slice(0, -suffix.length)
27 }
28 }
29​
30 return id