@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
1 lines • 2.69 kB
Source Map (JSON)
{"version":3,"file":"storage-manager.cjs","names":[],"sources":["../../../../src/core/system/storage-manager.ts"],"sourcesContent":["\"use client\"\n\nexport type Storage = \"cookie\" | \"localStorage\"\n\nconst hasSupport = !!(globalThis.document as Document | undefined)\n\nfunction parseCookie<Y extends string>(\n cookie: string,\n key: string,\n): undefined | Y {\n const match = cookie.match(new RegExp(`(^| )${key}=([^;]+)`))\n\n return match?.[2] as undefined | Y\n}\n\nexport interface StorageManager<Y extends string, M extends Y> {\n default(value: M): M\n get(value?: Y): Y\n set(value: Y): void\n}\n\nexport function createStorageManager<Y extends string, M extends Y>(\n storage: Storage,\n storageKey: string,\n defaultValue: Y,\n cookie?: string,\n): StorageManager<Y, M> {\n return {\n default(value) {\n const defaultStorageKey = `default-${storageKey}`\n\n if (cookie) return parseCookie(cookie, defaultStorageKey) || value\n\n if (storage === \"localStorage\") {\n try {\n return (localStorage.getItem(defaultStorageKey) as M | null) || value\n } catch {\n return value\n }\n } else {\n if (!hasSupport) return value\n\n return parseCookie(document.cookie, defaultStorageKey) || value\n }\n },\n get(value = defaultValue) {\n if (cookie) return parseCookie(cookie, storageKey) || value\n\n if (storage === \"localStorage\") {\n try {\n return (localStorage.getItem(storageKey) as null | Y) || value\n } catch {\n return value\n }\n } else {\n if (!hasSupport) return value\n\n return parseCookie(document.cookie, storageKey) || value\n }\n },\n set(value: Y) {\n if (storage === \"localStorage\") {\n localStorage.setItem(storageKey, value)\n } else {\n document.cookie = `${storageKey}=${value}; max-age=31536000; path=/`\n }\n },\n }\n}\n"],"mappings":";;;;;AAIA,MAAM,aAAa,CAAC,CAAE,WAAW;AAEjC,SAAS,YACP,QACA,KACe;AAGf,QAFc,OAAO,sBAAM,IAAI,OAAO,QAAQ,IAAI,UAAU,CAAC,GAE9C;;AASjB,SAAgB,qBACd,SACA,YACA,cACA,QACsB;AACtB,QAAO;EACL,QAAQ,OAAO;GACb,MAAM,oBAAoB,WAAW;AAErC,OAAI,OAAQ,QAAO,YAAY,QAAQ,kBAAkB,IAAI;AAE7D,OAAI,YAAY,eACd,KAAI;AACF,WAAQ,aAAa,QAAQ,kBAAkB,IAAiB;WAC1D;AACN,WAAO;;QAEJ;AACL,QAAI,CAAC,WAAY,QAAO;AAExB,WAAO,YAAY,SAAS,QAAQ,kBAAkB,IAAI;;;EAG9D,IAAI,QAAQ,cAAc;AACxB,OAAI,OAAQ,QAAO,YAAY,QAAQ,WAAW,IAAI;AAEtD,OAAI,YAAY,eACd,KAAI;AACF,WAAQ,aAAa,QAAQ,WAAW,IAAiB;WACnD;AACN,WAAO;;QAEJ;AACL,QAAI,CAAC,WAAY,QAAO;AAExB,WAAO,YAAY,SAAS,QAAQ,WAAW,IAAI;;;EAGvD,IAAI,OAAU;AACZ,OAAI,YAAY,eACd,cAAa,QAAQ,YAAY,MAAM;OAEvC,UAAS,SAAS,GAAG,WAAW,GAAG,MAAM;;EAG9C"}