-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequal.ts
31 lines (26 loc) · 953 Bytes
/
equal.ts
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
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
/** Check two `Headers` field name and field value equality.
*
* ```ts
* import { equalsHeaders } from "https://deno.land/x/headers_utils@$VERSION/equal.ts";
* import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts";
*
* assert(equalsHeaders(new Headers({ a: "b" }), new Headers({ a: "b" })));
* assertFalse(equalsHeaders(new Headers({ a: "b" }), new Headers({ c: "d" })));
* ```
*/
export function equalsHeaders(left: Headers, right: Headers): boolean {
const lefts = [...left];
const rights = [...right];
if (lefts.length !== rights.length) return false;
for (const [key, value] of lefts.concat(rights)) {
if (!left.has(key) || !right.has(key)) {
return false;
}
if (left.get(key) !== value || right.get(key) !== value) {
return false;
}
}
return true;
}