Last active
January 9, 2020 10:54
-
-
Save davidalekna/bd32e0bb0221f0821bce4e47ab82e29f to your computer and use it in GitHub Desktop.
Revisions
-
davidalekna revised this gist
Jan 9, 2020 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,13 +11,19 @@ const setCookie = ( document.cookie = cookie; }; // setCookie('tasty_cookie', 'strawberry') const getCookie = (name: string): string | void => { return document.cookie?.split(";").reduce((acc, cookie) => { const [key, value] = cookie.trim().split("="); return { ...acc, [key]: decodeURIComponent(value) }; }, {})[name]; }; // getCookie('tasty_cookie'): 'strawberry' const deleteCookie = (name: string, path?: string): void => { setCookie(name, "", -1, path); }; // deleteCookie('tasty_cookie') -
davidalekna revised this gist
Jan 9, 2020 . No changes.There are no files selected for viewing
-
Alekna revised this gist
Jan 9, 2020 . 1 changed file with 17 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,23 @@ const setCookie = ( name: string, value: string, days: number = 7, path: string = "/" ): void => { const expires = new Date(Date.now() + days * 864e5).toUTCString(); const cookie = `${name}=${encodeURIComponent( value )}; expires=${expires}; path=${path}`; document.cookie = cookie; }; const getCookie = (name: string): string | void => { return document.cookie?.split(";").reduce((acc, cookie) => { const [key, value] = cookie.trim().split("="); return { ...acc, [key]: decodeURIComponent(value) }; }, {})[name]; }; const deleteCookie = (name: string, path?: string): void => { setCookie(name, "", -1, path); }; -
davidalekna created this gist
Jan 9, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ const setCookie = (name: string, value: string, days: number = 7, path: string = '/'): void => { const expires = new Date(Date.now() + days * 864e5).toUTCString(); const cookie = `${name}=${encodeURIComponent(value)}; expires=${expires}; path=${path}`; document.cookie = cookie; }; const getCookie = (name: string): string | void => { return document.cookie?.split(';').reduce((acc, cookie) => { const [key, value] = cookie.trim().split('='); return { ...acc, [key]: decodeURIComponent(value) }; }, {})[name]; }; const deleteCookie = (name: string, path?: string): void => { setCookie(name, '', -1, path); };