Skip to content

Commit dc7f68d

Browse files
committed
d
1 parent ab1ad36 commit dc7f68d

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

tests/url/toolsURLSearchParams.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,35 @@ export function sortSearchParamsByKeyThenValue(params: URLSearchParams): URLSear
8585
})
8686
);
8787
}
88+
89+
/**
90+
* Merges consecutive source URLSearchParams into a base URLSearchParams, but strictly
91+
* limits updates and deletions to a specific list of governed keys.
92+
* Sources are applied in sequence. For each key in governedKeys and each source:
93+
* - If the key is present in the source, it overwrites the value in result.
94+
* - If the key is absent from the source, it is deleted from the result.
95+
*/
96+
export function syncURLSearchParams(
97+
base: URLSearchParams,
98+
governedKeys: string[] | Set<string>,
99+
...sources: URLSearchParams[]
100+
): URLSearchParams {
101+
const result = new URLSearchParams(base);
102+
const keys = Array.isArray(governedKeys) ? governedKeys : Array.from(governedKeys);
103+
104+
for (const source of sources) {
105+
for (const key of keys) {
106+
if (source.has(key)) {
107+
result.delete(key);
108+
source.getAll(key).forEach((value) => {
109+
result.append(key, value);
110+
});
111+
} else {
112+
result.delete(key);
113+
}
114+
}
115+
}
116+
117+
result.sort();
118+
return result;
119+
}

0 commit comments

Comments
 (0)