-
Notifications
You must be signed in to change notification settings - Fork 0
Iteration
Cédric Belin edited this page Nov 7, 2024
·
5 revisions
The CookieStore
class is iterable: it implements the Symbol.iterator
function.
You can go through all key/value pairs contained using a for...of
loop:
import {CookieStore} from "@cedx/cookies";
const cookieStore = new CookieStore()
.set("foo", "bar")
.set("bar", "baz")
.set("baz", "qux");
for (const [key, value] of cookieStore) console.log(`${key} => ${value}`);
// Round 1: "foo => bar"
// Round 2: "bar => baz"
// Round 3: "baz => qux"
Warning
The order of keys is user-agent defined, so you should not rely on it.
If you have configured the instance to use a key prefix, the iteration will only loop over the values that have that same key prefix:
import {CookieStore} from "@cedx/cookies";
const cookieStore = new CookieStore()
.set("foo", "bar")
.set("prefix:bar", "baz");
const prefixedStore = new CookieStore({keyPrefix: "prefix:"}).set("baz", "qux");
for (const [key, value] of prefixedStore) console.log(`${key} => ${value}`);
// Round 1: "bar => baz"
// Round 2: "baz => qux"
Note
The prefix is stripped from the keys returned by the iteration.