UNPKG

@coveord/plasma-mantine

Version:

A Plasma flavoured Mantine theme

175 lines (174 loc) 5.49 kB
/** * Versioned localStorage utility for Plasma. * * All Plasma data is stored under a single `"plasma"` key with the shape: * ```json * { * "storage-version": 1, * "storage": { * "table": { * "my-table": { "columnVisibility": { ... } } * } * } * } * ``` */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: Object.getOwnPropertyDescriptor(all, name).get }); } _export(exports, { get CURRENT_STORAGE_VERSION () { return CURRENT_STORAGE_VERSION; }, get STORAGE_KEY () { return STORAGE_KEY; }, get getStorageItem () { return getStorageItem; }, get removeStorageItem () { return removeStorageItem; }, get setStorageItem () { return setStorageItem; } }); var _type_of = require("@swc/helpers/_/_type_of"); var STORAGE_KEY = 'plasma'; var CURRENT_STORAGE_VERSION = 1; var createStorageObject = function createStorageObject() { return Object.create(null); }; var isUnsafeKey = function isUnsafeKey(key) { return key === '__proto__' || key === 'constructor' || key === 'prototype'; }; var isSafePath = function isSafePath(path) { return path.every(function(key) { return !isUnsafeKey(key); }); }; var readStorage = function readStorage() { try { var raw = localStorage.getItem(STORAGE_KEY); if (raw === null) { return null; } var parsed = JSON.parse(raw); if ((typeof parsed === "undefined" ? "undefined" : _type_of._(parsed)) !== 'object' || parsed === null || Array.isArray(parsed)) { localStorage.removeItem(STORAGE_KEY); return null; } return parsed; } catch (unused) { try { localStorage.removeItem(STORAGE_KEY); } catch (unused) { console.warn('Unable to clean up corrupted localStorage key "'.concat(STORAGE_KEY, '".')); } return null; } }; var writeStorage = function writeStorage(data) { try { localStorage.setItem(STORAGE_KEY, JSON.stringify(data)); } catch (unused) { console.warn('Unable to write to localStorage key "'.concat(STORAGE_KEY, '".')); } }; var getNestedValue = function getNestedValue(obj, path) { var current = obj; var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined; try { for(var _iterator = path[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){ var key = _step.value; if ((typeof current === "undefined" ? "undefined" : _type_of._(current)) !== 'object' || current === null) { return undefined; } current = current[key]; } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally{ try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally{ if (_didIteratorError) { throw _iteratorError; } } } return current; }; var setNestedValue = function setNestedValue(obj, path, value) { var current = obj; for(var i = 0; i < path.length - 1; i++){ var key = path[i]; if (isUnsafeKey(key)) { return; } if (_type_of._(current[key]) !== 'object' || current[key] === null) { current[key] = createStorageObject(); } current = current[key]; } var lastKey = path[path.length - 1]; if (!isUnsafeKey(lastKey)) { current[lastKey] = value; } }; var getStorageItem = function getStorageItem(path) { var _data_storage; if (!isSafePath(path)) { return null; } var data = readStorage(); if (!data || data['storage-version'] !== CURRENT_STORAGE_VERSION) { return null; } var value = getNestedValue((_data_storage = data.storage) !== null && _data_storage !== void 0 ? _data_storage : createStorageObject(), path); return value !== undefined ? value : null; }; var setStorageItem = function setStorageItem(path, value) { if (!isSafePath(path)) { return; } var data = readStorage(); if (!data || data['storage-version'] !== CURRENT_STORAGE_VERSION) { data = { 'storage-version': CURRENT_STORAGE_VERSION, storage: createStorageObject() }; } if (!data.storage) { data.storage = createStorageObject(); } setNestedValue(data.storage, path, value); writeStorage(data); }; var removeStorageItem = function removeStorageItem(path) { var _data_storage; if (!isSafePath(path)) { return; } var data = readStorage(); if (!data || data['storage-version'] !== CURRENT_STORAGE_VERSION) { return; } var parentPath = path.slice(0, -1); var key = path[path.length - 1]; var parent = getNestedValue((_data_storage = data.storage) !== null && _data_storage !== void 0 ? _data_storage : {}, parentPath); if ((typeof parent === "undefined" ? "undefined" : _type_of._(parent)) === 'object' && parent !== null) { delete parent[key]; writeStorage(data); } }; //# sourceMappingURL=local-storage.js.map