/* @alwatr/local-storage v5.0.0 */ "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/main.ts var main_exports = {}; __export(main_exports, { localJsonStorage: () => localJsonStorage }); module.exports = __toCommonJS(main_exports); var import_package_tracer = require("@alwatr/package-tracer"); __dev_mode__: import_package_tracer.packageTracer.add("@alwatr/local-storage", "5.0.0"); function parseJson(content) { try { return JSON.parse(content); } catch (err) { console.error("parseJson", "invalid_json", err); return null; } } var localJsonStorage = { /** * Generate local storage key. * * @param name - Name of the item. * @param version - Version of the item (default: 1). * @returns The generated local storage key. * @example * ```typescript * localJsonStorage.key_('myItem', 1); // myItem.v1 * ``` */ key_(name, version = 1) { return `${name}.v${version}`; }, /** * Get the local storage item and parse it as JSON. * If the item is not found, return the default value. * If the version is greater than 1, remove the previous version. * If the item is not a valid JSON object, return the default value. * * @param name - The name of the item. * @param defaultValue - The default value of the item. * @param version - The data structure version of the item (default: 1). * @returns The parsed JSON value or the default value if the item is not found. * @example * ```typescript * const value = localJsonStorage.getItem('myItem', {a: 1, b: 2}); * ``` */ getItem(name, defaultValue, version = 1) { if (version > 1) { this.removeItem(name, version - 1); } const key = this.key_(name, version); const value = localStorage.getItem(key); if (value === null) return defaultValue; const json = parseJson(value); if (json === null || typeof json !== "object") return defaultValue; return json; }, /** * Set local storage item as JSON. * * @param name - Name of the item. * @param value - Value of the item. * @param version - Version of the item. * @example * ```typescript * localJsonStorage.setItem('myItem', {a: 1, b: 2}); * ``` */ setItem(name, value, version = 1) { const key = this.key_(name, version); localStorage.setItem(key, JSON.stringify(value)); }, /** * Removes an item from the local storage. * * @param name - The name of the item to remove. * @param version - The version of the item to remove. Default is 1. * @example * ```typescript * localJsonStorage.removeItem('myItem'); * ``` */ removeItem(name, version = 1) { const key = this.key_(name, version); localStorage.removeItem(key); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { localJsonStorage }); /*! For license information please see main.cjs.LEGAL.txt */ //# sourceMappingURL=main.cjs.map