UNPKG

hardtack

Version:

An ultra-light (373 bytes) library for working with cookies in JavaScript

22 lines (15 loc) 494 B
function get(name) { const { cookie } = document; if (!cookie) { return name ? undefined : {}; } const parsedCookie = cookie.split('; ').reduce((accumulator, item) => { const cookieItem = item.split('='); const cookieName = decodeURIComponent(cookieItem[0]); const cookieValue = decodeURIComponent(cookieItem[1]); accumulator[cookieName] = cookieValue; return accumulator; }, {}); return name ? parsedCookie[name] : parsedCookie; } export default get;