Skip to content

Commit 79f0990

Browse files
committed
Some cookie changes
1 parent 16b8946 commit 79f0990

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

packages/headers/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
This is the changelog for [`headers`](https://github.com/mjackson/remix-the-web/tree/main/packages/headers). It follows [semantic versioning](https://semver.org/).
44

5+
## HEAD
6+
7+
- BREAKING CHANGE: `cookie.delete(name)` returns `void` instead of `boolean`
8+
- BREAKING CHANGE: `cookie.forEach()` calls its callback with `(name, value, cookie)` instead of `(value, name, map)`
9+
510
## v0.9.0 (2024-12-20)
611

712
This release tightens up the type safety and brings `SuperHeaders` more in line with the built-in `Headers` interface.

packages/headers/src/lib/cookie.test.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,21 @@ describe('Cookie', () => {
4343
});
4444

4545
it('sets and gets values', () => {
46-
let header = new Cookie('');
46+
let header = new Cookie();
4747
header.set('name', 'value');
4848
assert.equal(header.get('name'), 'value');
4949
});
5050

51+
it('returns `null` for nonexistent values', () => {
52+
let header = new Cookie();
53+
assert.equal(header.get('name'), null);
54+
});
55+
5156
it('deletes values', () => {
5257
let header = new Cookie('name=value');
53-
assert.equal(header.delete('name'), true);
54-
assert.equal(header.delete('nonexistent'), false);
55-
assert.equal(header.get('name'), undefined);
58+
assert.equal(header.has('name'), true);
59+
header.delete('name');
60+
assert.equal(header.has('name'), false);
5661
});
5762

5863
it('checks if value exists', () => {
@@ -63,19 +68,11 @@ describe('Cookie', () => {
6368

6469
it('clears all values', () => {
6570
let header = new Cookie('name1=value1; name2=value2');
71+
assert.equal(header.size, 2);
6672
header.clear();
6773
assert.equal(header.size, 0);
6874
});
6975

70-
it('iterates over entries', () => {
71-
let header = new Cookie('name1=value1; name2=value2');
72-
let entries = Array.from(header.entries());
73-
assert.deepEqual(entries, [
74-
['name1', 'value1'],
75-
['name2', 'value2'],
76-
]);
77-
});
78-
7976
it('iterates over names', () => {
8077
let header = new Cookie('name1=value1; name2=value2');
8178
let names = Array.from(header.names());
@@ -88,10 +85,19 @@ describe('Cookie', () => {
8885
assert.deepEqual(values, ['value1', 'value2']);
8986
});
9087

88+
it('iterates over entries', () => {
89+
let header = new Cookie('name1=value1; name2=value2');
90+
let entries = Array.from(header.entries());
91+
assert.deepEqual(entries, [
92+
['name1', 'value1'],
93+
['name2', 'value2'],
94+
]);
95+
});
96+
9197
it('uses forEach correctly', () => {
9298
let header = new Cookie('name1=value1; name2=value2');
9399
let result: [string, string][] = [];
94-
header.forEach((value, name) => {
100+
header.forEach((name, value) => {
95101
result.push([name, value]);
96102
});
97103
assert.deepEqual(result, [

packages/headers/src/lib/cookie.ts

+25-19
Original file line numberDiff line numberDiff line change
@@ -37,44 +37,47 @@ export class Cookie implements HeaderValue, Iterable<[string, string]> {
3737
}
3838

3939
/**
40-
* Gets the value of a cookie with the given name from the `Cookie` header.
40+
* Gets the value of a cookie with the given name from the header.
41+
* @param name The name of the cookie.
42+
* @returns The value of the cookie, or `null` if the cookie does not exist.
4143
*/
42-
get(name: string): string | undefined {
43-
return this.#map.get(name);
44+
get(name: string): string | null {
45+
return this.#map.get(name) ?? null;
4446
}
4547

4648
/**
47-
* Sets a cookie with the given name and value in the `Cookie` header.
49+
* Sets a cookie with the given name and value in the header.
50+
* @param name The name of the cookie.
51+
* @param value The value of the cookie.
4852
*/
4953
set(name: string, value: string): void {
5054
this.#map.set(name, value);
5155
}
5256

5357
/**
54-
* Removes a cookie with the given name from the `Cookie` header.
58+
* Removes a cookie with the given name from the header.
59+
* @param name The name of the cookie.
5560
*/
56-
delete(name: string): boolean {
57-
return this.#map.delete(name);
61+
delete(name: string): void {
62+
this.#map.delete(name);
5863
}
5964

6065
/**
61-
* True if a cookie with the given name exists in the `Cookie` header.
66+
* True if a cookie with the given name exists in the header.
67+
* @param name The name of the cookie.
68+
* @returns True if a cookie with the given name exists in the header.
6269
*/
6370
has(name: string): boolean {
6471
return this.#map.has(name);
6572
}
6673

6774
/**
68-
* Removes all cookies from the `Cookie` header.
75+
* Removes all cookies from the header.
6976
*/
7077
clear(): void {
7178
this.#map.clear();
7279
}
7380

74-
entries(): IterableIterator<[string, string]> {
75-
return this.#map.entries();
76-
}
77-
7881
names(): IterableIterator<string> {
7982
return this.#map.keys();
8083
}
@@ -83,19 +86,22 @@ export class Cookie implements HeaderValue, Iterable<[string, string]> {
8386
return this.#map.values();
8487
}
8588

89+
entries(): IterableIterator<[string, string]> {
90+
return this.#map.entries();
91+
}
92+
8693
[Symbol.iterator](): IterableIterator<[string, string]> {
8794
return this.entries();
8895
}
8996

90-
forEach(
91-
callback: (value: string, key: string, map: Map<string, string>) => void,
92-
thisArg?: any,
93-
): void {
94-
this.#map.forEach(callback, thisArg);
97+
forEach(callback: (name: string, value: string, header: Cookie) => void, thisArg?: any): void {
98+
for (let [name, value] of this) {
99+
callback.call(thisArg, name, value, this);
100+
}
95101
}
96102

97103
/**
98-
* The number of cookies in the `Cookie` header.
104+
* The number of cookies in the header.
99105
*/
100106
get size(): number {
101107
return this.#map.size;

0 commit comments

Comments
 (0)