{"id":5370,"date":"2021-09-22T16:24:32","date_gmt":"2021-09-22T15:24:32","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=5370"},"modified":"2021-09-22T16:24:32","modified_gmt":"2021-09-22T15:24:32","slug":"typescript-localstorage-helper-functions","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-localstorage-helper-functions\/","title":{"rendered":"Typescript: LocalStorage helper functions"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const getCircularReplacer = () => {\n    const seen = new WeakSet();\n    return (key: any, value: any) => {\n        if (typeof value === \"object\" &amp;&amp; value !== null) {\n            if (seen.has(value)) {\n                return;\n            }\n            seen.add(value);\n        }\n        return value;\n    };\n};\n\nconst toJson = (x: any) => JSON.stringify(x, getCircularReplacer());\n\nconst parseJson = (x: any) => {\n    if (typeof x !== \"string\") return undefined;\n    try {\n        return JSON.parse(x);\n    } catch {\n        return undefined;\n    }\n};\n\nconst getLocalStorage = &lt;T>(key: string): T => parseJson(localStorage.getItem(key)) as T;\nconst setLocalStorage = (key: string, state: any): any => localStorage.setItem(key, toJson(state));\n\nconst createGuid = (): string => {\n    let d = new Date().getTime();\n    const uuid = \"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx\".replace(\/[xy]\/g, function (c) {\n        const r = (d + Math.random() * 16) % 16 | 0;\n        d = Math.floor(d \/ 16);\n        \/\/ eslint-disable-next-line no-mixed-operators\n        return (c === \"x\" ? r : (r &amp; 0x3 | 0x8)).toString(16);\n    });\n    return uuid;\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5370","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=5370"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5370\/revisions"}],"predecessor-version":[{"id":5371,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/5370\/revisions\/5371"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=5370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=5370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=5370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}