Skip to content

Commit

Permalink
remove redundant stringify (#7)
Browse files Browse the repository at this point in the history
* string check

* encode cookie
  • Loading branch information
aryasaatvik authored Oct 18, 2023
1 parent b7fffe4 commit 00bbd18
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/addon/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Cookie = Record<string, unknown>;
export type Cookie = Record<string, string>;

export type CookieParameter = {
cookie?: Cookie;
Expand Down
11 changes: 8 additions & 3 deletions packages/addon/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Cookie } from './types';

export function setCookie(name: string, value: unknown) {
document.cookie = `${name}=${JSON.stringify(value)}`;
export function setCookie(name: string, value: string) {
if (typeof value !== 'string') {
document.cookie = `${name}=${JSON.stringify(value)};`;
} else {
const encodedValue = encodeURIComponent(value);
document.cookie = `${name}=${encodedValue};`;
}
}

export function setCookies(cookie: Cookie) {
const entries: [string, unknown][] = Object.keys(cookie).map((name) => [
const entries: [string, string][] = Object.keys(cookie).map((name) => [
name,
cookie[name],
]);
Expand Down

0 comments on commit 00bbd18

Please sign in to comment.