@alwatr/local-storage 6.1.6 → 6.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/README.md +10 -10
- package/dist/local-storage.provider.d.ts +2 -2
- package/dist/local-storage.provider.d.ts.map +1 -1
- package/dist/main.cjs +3 -2
- package/dist/main.cjs.map +2 -2
- package/dist/main.mjs +3 -2
- package/dist/main.mjs.map +2 -2
- package/dist/type.d.ts +1 -1
- package/dist/type.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/main.test.js +23 -23
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [6.1.8](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@6.1.7...@alwatr/local-storage@6.1.8) (2025-09-22)
|
|
7
|
+
|
|
8
|
+
### 🐛 Bug Fixes
|
|
9
|
+
|
|
10
|
+
* update version references to schemaVersion in README and code ([c51c115](https://github.com/Alwatr/nanolib/commit/c51c115ea431bba10a248fe6e4e37dcdff6da059))
|
|
11
|
+
|
|
12
|
+
## [6.1.7](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@6.1.6...@alwatr/local-storage@6.1.7) (2025-09-21)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @alwatr/local-storage
|
|
15
|
+
|
|
6
16
|
## [6.1.6](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@6.1.5...@alwatr/local-storage@6.1.6) (2025-09-20)
|
|
7
17
|
|
|
8
18
|
### 🐛 Bug Fixes
|
package/README.md
CHANGED
|
@@ -14,9 +14,9 @@ It's designed to handle data structure migrations automatically, preventing issu
|
|
|
14
14
|
|
|
15
15
|
This library is built upon a few simple but powerful concepts:
|
|
16
16
|
|
|
17
|
-
1. **Provider Pattern**: Instead of using static functions, you create an _instance_ of a `LocalStorageProvider` for each unique data item you want to manage. This instance is configured once with a name,
|
|
17
|
+
1. **Provider Pattern**: Instead of using static functions, you create an _instance_ of a `LocalStorageProvider` for each unique data item you want to manage. This instance is configured once with a name, schemaVersion, and default value, and then used to interact with that specific item.
|
|
18
18
|
|
|
19
|
-
2. **Versioning & Automatic Migration**: When you initialize a provider with a new `
|
|
19
|
+
2. **Versioning & Automatic Migration**: When you initialize a provider with a new `schemaVersion` number, it automatically removes all older versions of that data from `localStorage`. This prevents conflicts and ensures the application is working with the correct data structure.
|
|
20
20
|
|
|
21
21
|
3. **Facade Factory Function**: The `createLocalStorageProvider` function acts as a clean entry point (Facade) to the library. This simplifies the API and decouples your code from the internal class implementation, making future library upgrades safer and easier.
|
|
22
22
|
|
|
@@ -51,7 +51,7 @@ interface UserSettings {
|
|
|
51
51
|
// Create a provider for user settings
|
|
52
52
|
const userSettingsProvider = createLocalStorageProvider<UserSettings>({
|
|
53
53
|
name: 'user-settings',
|
|
54
|
-
|
|
54
|
+
schemaVersion: 1,
|
|
55
55
|
defaultValue: {
|
|
56
56
|
theme: 'light',
|
|
57
57
|
notifications: true,
|
|
@@ -95,7 +95,7 @@ This method is highly efficient as it **does not** require a `defaultValue` and
|
|
|
95
95
|
```typescript
|
|
96
96
|
import {LocalStorageProvider} from '@alwatr/local-storage';
|
|
97
97
|
|
|
98
|
-
const formMeta = {name: 'user-survey-form',
|
|
98
|
+
const formMeta = {name: 'user-survey-form', schemaVersion: 1};
|
|
99
99
|
|
|
100
100
|
if (LocalStorageProvider.has(formMeta)) {
|
|
101
101
|
// The user has already filled out the form.
|
|
@@ -119,7 +119,7 @@ userSettingsProvider.remove();
|
|
|
119
119
|
|
|
120
120
|
- **Always use the `createLocalStorageProvider` factory function.** It provides a stable API that protects your code from internal library changes.
|
|
121
121
|
- **Prefer `LocalStorageProvider.has()` for existence checks.** It's the most performant and cleanest way to check for data without the overhead of creating an instance and providing a `defaultValue`.
|
|
122
|
-
- **Increment the `
|
|
122
|
+
- **Increment the `schemaVersion` number** whenever you make a breaking change to your data structure. The library will handle the cleanup of old data automatically.
|
|
123
123
|
|
|
124
124
|
---
|
|
125
125
|
|
|
@@ -152,9 +152,9 @@ Contributions are welcome! Please read our [contribution guidelines](https://git
|
|
|
152
152
|
|
|
153
153
|
این کتابخانه بر پایه چند مفهوم ساده اما قدرتمند بنا شده است:
|
|
154
154
|
|
|
155
|
-
1. **الگوی Provider (ارائهدهنده)**: به جای استفاده از توابع استاتیک، شما برای هر آیتم دادهای که میخواهید مدیریت کنید، یک _نمونه (instance)_ از `LocalStorageProvider` میسازید. این نمونه یک بار با `name`, `
|
|
155
|
+
1. **الگوی Provider (ارائهدهنده)**: به جای استفاده از توابع استاتیک، شما برای هر آیتم دادهای که میخواهید مدیریت کنید، یک _نمونه (instance)_ از `LocalStorageProvider` میسازید. این نمونه یک بار با `name`, `schemaVersion` و `defaultValue` پیکربندی شده و سپس برای تعامل با آن آیتم خاص استفاده میشود.
|
|
156
156
|
|
|
157
|
-
2. **نسخهبندی و مهاجرت خودکار**: هنگامی که شما یک Provider را با شماره `
|
|
157
|
+
2. **نسخهبندی و مهاجرت خودکار**: هنگامی که شما یک Provider را با شماره `schemaVersion` جدیدی مقداردهی اولیه میکنید، این کتابخانه به طور خودکار تمام نسخههای قدیمیتر آن داده را از `localStorage` حذف میکند. این کار از تداخل جلوگیری کرده و تضمین میکند که اپلیکیشن همیشه با ساختار داده صحیح کار میکند.
|
|
158
158
|
|
|
159
159
|
3. **تابع سازنده Facade**: تابع `createLocalStorageProvider` به عنوان یک نقطه ورود تمیز (Facade) به کتابخانه عمل میکند. این کار API را ساده کرده و کد شما را از پیادهسازی داخلی کلاسها جدا (decouple) میسازد، که باعث میشود ارتقاء کتابخانه در آینده امنتر و آسانتر باشد.
|
|
160
160
|
|
|
@@ -189,7 +189,7 @@ interface UserSettings {
|
|
|
189
189
|
// یک provider برای تنظیمات کاربر بسازید
|
|
190
190
|
const userSettingsProvider = createLocalStorageProvider<UserSettings>({
|
|
191
191
|
name: 'user-settings',
|
|
192
|
-
|
|
192
|
+
schemaVersion: 1,
|
|
193
193
|
defaultValue: {
|
|
194
194
|
theme: 'light',
|
|
195
195
|
notifications: true,
|
|
@@ -233,7 +233,7 @@ console.log(currentSettings.theme); // "dark"
|
|
|
233
233
|
```typescript
|
|
234
234
|
import {LocalStorageProvider} from '@alwatr/local-storage';
|
|
235
235
|
|
|
236
|
-
const formMeta = {name: 'user-survey-form',
|
|
236
|
+
const formMeta = {name: 'user-survey-form', schemaVersion: 1};
|
|
237
237
|
|
|
238
238
|
if (LocalStorageProvider.has(formMeta)) {
|
|
239
239
|
// داده وجود دارد. کاربر قبلاً فرم را پر کرده است.
|
|
@@ -257,7 +257,7 @@ userSettingsProvider.remove();
|
|
|
257
257
|
|
|
258
258
|
- **همیشه از تابع سازنده `createLocalStorageProvider` استفاده کنید.** این تابع یک API پایدار فراهم میکند که کد شما را در برابر تغییرات داخلی کتابخانه محافظت میکند.
|
|
259
259
|
- **برای بررسی وجود داده، `LocalStorageProvider.has()` را ترجیح دهید.** این کارآمدترین و تمیزترین روش برای بررسی وجود داده بدون سربار ساختن یک نمونه و تعریف `defaultValue` است.
|
|
260
|
-
- **هر زمان که یک تغییر ساختاری در دادههای خود ایجاد کردید که با نسخههای قبلی ناسازگار است، شماره `
|
|
260
|
+
- **هر زمان که یک تغییر ساختاری در دادههای خود ایجاد کردید که با نسخههای قبلی ناسازگار است، شماره `schemaVersion` را افزایش دهید.** کتابخانه پاکسازی دادههای قدیمی را به صورت خودکار انجام خواهد داد.
|
|
261
261
|
|
|
262
262
|
## حامیان (Sponsors)
|
|
263
263
|
|
|
@@ -27,7 +27,7 @@ export declare class LocalStorageProvider<T extends JsonValue> {
|
|
|
27
27
|
constructor(config_: LocalStorageProviderConfig<T>);
|
|
28
28
|
/**
|
|
29
29
|
* Generates the versioned storage key.
|
|
30
|
-
* @param meta - An object containing the name and
|
|
30
|
+
* @param meta - An object containing the name and schemaVersion.
|
|
31
31
|
* @returns The versioned key string.
|
|
32
32
|
*/
|
|
33
33
|
static getKey(meta: StorageMeta): string;
|
|
@@ -40,7 +40,7 @@ export declare class LocalStorageProvider<T extends JsonValue> {
|
|
|
40
40
|
*
|
|
41
41
|
* @example
|
|
42
42
|
* ```typescript
|
|
43
|
-
* const formExists = LocalStorageProvider.has({ name: 'user-form',
|
|
43
|
+
* const formExists = LocalStorageProvider.has({ name: 'user-form', schemaVersion: 1 });
|
|
44
44
|
* if (formExists) {
|
|
45
45
|
* // Show the "Thank you" message
|
|
46
46
|
* } else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-storage.provider.d.ts","sourceRoot":"","sources":["../src/local-storage.provider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,0BAA0B,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAEvE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,oBAAoB,CAAC,CAAC,SAAS,SAAS;IAMvC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,0BAA0B,CAAC,CAAC,CAAC;IALrE,gBAAuB,OAAO,SAAuB;IAErD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,SAAS,CAAC,QAAQ,CAAC,OAAO,
|
|
1
|
+
{"version":3,"file":"local-storage.provider.d.ts","sourceRoot":"","sources":["../src/local-storage.provider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,0BAA0B,EAAE,WAAW,EAAC,MAAM,WAAW,CAAC;AAEvE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,oBAAoB,CAAC,CAAC,SAAS,SAAS;IAMvC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,0BAA0B,CAAC,CAAC,CAAC;IALrE,gBAAuB,OAAO,SAAuB;IAErD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,SAAS,CAAC,QAAQ,CAAC,OAAO,wCAAkG;gBAE7F,OAAO,EAAE,0BAA0B,CAAC,CAAC,CAAC;IAMrE;;;;OAIG;WACW,MAAM,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM;IAI/C;;;;;;;;;;;;;;;;OAgBG;WACW,GAAG,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO;IAK7C;;OAEG;IACH,OAAO,CAAC,cAAc;IAMtB;;;;OAIG;IACI,IAAI,IAAI,CAAC;IAmBhB;;OAEG;IACI,KAAK,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAU5B;;OAEG;IACI,MAAM,IAAI,IAAI;IAIrB;;OAEG;IACH,OAAO,CAAC,SAAS;CASlB"}
|
package/dist/main.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
/** 📦 @alwatr/local-storage v6.1.
|
|
2
|
-
|
|
1
|
+
/** 📦 @alwatr/local-storage v6.1.8 */
|
|
2
|
+
__dev_mode__: console.debug("📦 @alwatr/local-storage v6.1.8");
|
|
3
|
+
"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);var main_exports={};__export(main_exports,{LocalStorageProvider:()=>LocalStorageProvider,createLocalStorageProvider:()=>createLocalStorageProvider});module.exports=__toCommonJS(main_exports);var import_logger=require("@alwatr/logger");var LocalStorageProvider=class _LocalStorageProvider{constructor(config_){this.config_=config_;this.logger_=(0,import_logger.createLogger)(`local-storage-provider: ${this.config_.name}, v: ${this.config_.schemaVersion}`);this.logger_.logMethodArgs?.("constructor",{config:this.config_});this.key__=_LocalStorageProvider.getKey(this.config_);this.migrate__()}static{this.version="6.1.8"}static getKey(meta){return`${meta.name}.v${meta.schemaVersion}`}static has(meta){const key=_LocalStorageProvider.getKey(meta);return localStorage.getItem(key)!==null}writeDefault__(){this.logger_.logMethodArgs?.("writeDefaultــ",this.config_.defaultValue);this.write(this.config_.defaultValue);return this.config_.defaultValue}read(){try{const value=localStorage.getItem(this.key__);if(value===null){this.logger_.logMethod?.("read//no_value");return this.writeDefault__()}const parsedValue=JSON.parse(value);this.logger_.logMethodFull?.("read//value",void 0,{parsedValue});return parsedValue}catch(err){this.logger_.error("read","read_parse_error",{err});return this.writeDefault__()}}write(value){this.logger_.logMethodArgs?.("write",{value});try{localStorage.setItem(this.key__,JSON.stringify(value))}catch(err){this.logger_.error("write","write_stringify_error",{err})}}remove(){localStorage.removeItem(this.key__)}migrate__(){if(this.config_.schemaVersion<=1)return;for(let i=1;i<this.config_.schemaVersion;i++){const oldKey=_LocalStorageProvider.getKey({name:this.config_.name,schemaVersion:i});localStorage.removeItem(oldKey)}}};function createLocalStorageProvider(config){return new LocalStorageProvider(config)}0&&(module.exports={LocalStorageProvider,createLocalStorageProvider});
|
|
3
4
|
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts", "../src/local-storage.provider.ts"],
|
|
4
|
-
"sourcesContent": ["import {LocalStorageProvider} from './local-storage.provider.js';\n\nimport type {LocalStorageProviderConfig} from './type.js';\n\nexport * from './local-storage.provider.js';\nexport type * from './type.js';\n\n/**\n * Factory function to create a new LocalStorageProvider.\n *\n * @param config - The configuration for the provider.\n * @returns An instance of LocalStorageProvider.\n *\n * @example\n * ```typescript\n * const userSettings = createLocalStorageProvider({\n * name: 'user-settings',\n * version: 1,\n * defaultValue: { theme: 'light', notifications: true }\n * });\n *\n * // Write new settings\n * userSettings.write({ theme: 'dark', notifications: false });\n *\n * // Read the current settings\n * const currentSettings = userSettings.read();\n * console.log(currentSettings); // { theme: 'dark', notifications: false }\n * ```\n */\nexport function createLocalStorageProvider<T extends JsonValue>(config: LocalStorageProviderConfig<T>): LocalStorageProvider<T> {\n return new LocalStorageProvider<T>(config);\n}\n", "import {createLogger} from '@alwatr/logger';\n\nimport type {LocalStorageProviderConfig, StorageMeta} from './type.js';\n\n/**\n * A provider class for managing a specific, versioned item in localStorage.\n * It encapsulates the logic for key generation, serialization, and migration.\n *\n * @example\n * ```typescript\n * const userSettings = new LocalStorageProvider({\n * name: 'user-settings',\n * version: 1,\n * defaultValue: { theme: 'light', notifications: true }\n * });\n *\n * // Write new settings\n * userSettings.write({ theme: 'dark', notifications: false });\n *\n * // Read the current settings\n * const currentSettings = userSettings.read();\n * console.log(currentSettings); // { theme: 'dark', notifications: false }\n * ```\n */\nexport class LocalStorageProvider<T extends JsonValue> {\n public static readonly version = __package_version__;\n\n private readonly key__: string;\n protected readonly logger_ = createLogger(`local-storage-provider: ${this.config_.name}, v: ${this.config_.
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import {LocalStorageProvider} from './local-storage.provider.js';\n\nimport type {LocalStorageProviderConfig} from './type.js';\n\nexport * from './local-storage.provider.js';\nexport type * from './type.js';\n\n/**\n * Factory function to create a new LocalStorageProvider.\n *\n * @param config - The configuration for the provider.\n * @returns An instance of LocalStorageProvider.\n *\n * @example\n * ```typescript\n * const userSettings = createLocalStorageProvider({\n * name: 'user-settings',\n * version: 1,\n * defaultValue: { theme: 'light', notifications: true }\n * });\n *\n * // Write new settings\n * userSettings.write({ theme: 'dark', notifications: false });\n *\n * // Read the current settings\n * const currentSettings = userSettings.read();\n * console.log(currentSettings); // { theme: 'dark', notifications: false }\n * ```\n */\nexport function createLocalStorageProvider<T extends JsonValue>(config: LocalStorageProviderConfig<T>): LocalStorageProvider<T> {\n return new LocalStorageProvider<T>(config);\n}\n", "import {createLogger} from '@alwatr/logger';\n\nimport type {LocalStorageProviderConfig, StorageMeta} from './type.js';\n\n/**\n * A provider class for managing a specific, versioned item in localStorage.\n * It encapsulates the logic for key generation, serialization, and migration.\n *\n * @example\n * ```typescript\n * const userSettings = new LocalStorageProvider({\n * name: 'user-settings',\n * version: 1,\n * defaultValue: { theme: 'light', notifications: true }\n * });\n *\n * // Write new settings\n * userSettings.write({ theme: 'dark', notifications: false });\n *\n * // Read the current settings\n * const currentSettings = userSettings.read();\n * console.log(currentSettings); // { theme: 'dark', notifications: false }\n * ```\n */\nexport class LocalStorageProvider<T extends JsonValue> {\n public static readonly version = __package_version__;\n\n private readonly key__: string;\n protected readonly logger_ = createLogger(`local-storage-provider: ${this.config_.name}, v: ${this.config_.schemaVersion}`);\n\n constructor(protected readonly config_: LocalStorageProviderConfig<T>) {\n this.logger_.logMethodArgs?.('constructor', {config: this.config_});\n this.key__ = LocalStorageProvider.getKey(this.config_);\n this.migrate__();\n }\n\n /**\n * Generates the versioned storage key.\n * @param meta - An object containing the name and schemaVersion.\n * @returns The versioned key string.\n */\n public static getKey(meta: StorageMeta): string {\n return `${meta.name}.v${meta.schemaVersion}`;\n }\n\n /**\n * Statically checks if a versioned item exists in localStorage.\n * This method provides a high-performance way to check for data existence without the overhead of creating a full provider instance.\n *\n * @param meta - An object containing the name and version of the item to check.\n * @returns `true` if the item exists, otherwise `false`.\n *\n * @example\n * ```typescript\n * const formExists = LocalStorageProvider.has({ name: 'user-form', schemaVersion: 1 });\n * if (formExists) {\n * // Show the \"Thank you\" message\n * } else {\n * // Show the form\n * }\n * ```\n */\n public static has(meta: StorageMeta): boolean {\n const key = LocalStorageProvider.getKey(meta);\n return localStorage.getItem(key) !== null;\n }\n\n /**\n * Writes the default value to localStorage and returns it.\n */\n private writeDefault__(): T {\n this.logger_.logMethodArgs?.('writeDefaultــ', this.config_.defaultValue);\n this.write(this.config_.defaultValue);\n return this.config_.defaultValue;\n }\n\n /**\n * Reads and parses the value from localStorage.\n * If the item doesn't exist, is invalid JSON, or doesn't match the expected type,\n * it writes and returns the default value.\n */\n public read(): T {\n try {\n const value = localStorage.getItem(this.key__);\n\n if (value === null) {\n this.logger_.logMethod?.('read//no_value');\n return this.writeDefault__();\n }\n\n const parsedValue = JSON.parse(value) as T;\n this.logger_.logMethodFull?.('read//value', undefined, {parsedValue});\n return parsedValue;\n }\n catch (err) {\n this.logger_.error('read', 'read_parse_error', {err});\n return this.writeDefault__();\n }\n }\n\n /**\n * Serializes and writes a value to localStorage.\n */\n public write(value: T): void {\n this.logger_.logMethodArgs?.('write', {value});\n try {\n localStorage.setItem(this.key__, JSON.stringify(value));\n }\n catch (err) {\n this.logger_.error('write', 'write_stringify_error', {err});\n }\n }\n\n /**\n * Removes the item from localStorage.\n */\n public remove(): void {\n localStorage.removeItem(this.key__);\n }\n\n /**\n * Manages data migration by removing all previous versions of the item.\n */\n private migrate__(): void {\n if (this.config_.schemaVersion <= 1) return;\n\n // Iterate from v1 up to the version just before the current one and remove them.\n for (let i = 1; i < this.config_.schemaVersion; i++) {\n const oldKey = LocalStorageProvider.getKey({name: this.config_.name, schemaVersion: i});\n localStorage.removeItem(oldKey);\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";;qqBAAA,+LCAA,kBAA2B,0BAwBpB,IAAM,qBAAN,MAAM,qBAA0C,CAMrD,YAA+B,QAAwC,CAAxC,qBAF/B,KAAmB,WAAU,4BAAa,2BAA2B,KAAK,QAAQ,IAAI,QAAQ,KAAK,QAAQ,aAAa,EAAE,EAGxH,KAAK,QAAQ,gBAAgB,cAAe,CAAC,OAAQ,KAAK,OAAO,CAAC,EAClE,KAAK,MAAQ,sBAAqB,OAAO,KAAK,OAAO,EACrD,KAAK,UAAU,CACjB,CATA,YAAuB,QAAU,QAgBjC,OAAc,OAAO,KAA2B,CAC9C,MAAO,GAAG,KAAK,IAAI,KAAK,KAAK,aAAa,EAC5C,CAmBA,OAAc,IAAI,KAA4B,CAC5C,MAAM,IAAM,sBAAqB,OAAO,IAAI,EAC5C,OAAO,aAAa,QAAQ,GAAG,IAAM,IACvC,CAKQ,gBAAoB,CAC1B,KAAK,QAAQ,gBAAgB,iBAAkB,KAAK,QAAQ,YAAY,EACxE,KAAK,MAAM,KAAK,QAAQ,YAAY,EACpC,OAAO,KAAK,QAAQ,YACtB,CAOO,MAAU,CACf,GAAI,CACF,MAAM,MAAQ,aAAa,QAAQ,KAAK,KAAK,EAE7C,GAAI,QAAU,KAAM,CAClB,KAAK,QAAQ,YAAY,gBAAgB,EACzC,OAAO,KAAK,eAAe,CAC7B,CAEA,MAAM,YAAc,KAAK,MAAM,KAAK,EACpC,KAAK,QAAQ,gBAAgB,cAAe,OAAW,CAAC,WAAW,CAAC,EACpE,OAAO,WACT,OACO,IAAK,CACV,KAAK,QAAQ,MAAM,OAAQ,mBAAoB,CAAC,GAAG,CAAC,EACpD,OAAO,KAAK,eAAe,CAC7B,CACF,CAKO,MAAM,MAAgB,CAC3B,KAAK,QAAQ,gBAAgB,QAAS,CAAC,KAAK,CAAC,EAC7C,GAAI,CACF,aAAa,QAAQ,KAAK,MAAO,KAAK,UAAU,KAAK,CAAC,CACxD,OACO,IAAK,CACV,KAAK,QAAQ,MAAM,QAAS,wBAAyB,CAAC,GAAG,CAAC,CAC5D,CACF,CAKO,QAAe,CACpB,aAAa,WAAW,KAAK,KAAK,CACpC,CAKQ,WAAkB,CACxB,GAAI,KAAK,QAAQ,eAAiB,EAAG,OAGrC,QAAS,EAAI,EAAG,EAAI,KAAK,QAAQ,cAAe,IAAK,CACnD,MAAM,OAAS,sBAAqB,OAAO,CAAC,KAAM,KAAK,QAAQ,KAAM,cAAe,CAAC,CAAC,EACtF,aAAa,WAAW,MAAM,CAChC,CACF,CACF,EDvGO,SAAS,2BAAgD,OAAgE,CAC9H,OAAO,IAAI,qBAAwB,MAAM,CAC3C",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/main.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
/** 📦 @alwatr/local-storage v6.1.
|
|
2
|
-
|
|
1
|
+
/** 📦 @alwatr/local-storage v6.1.8 */
|
|
2
|
+
__dev_mode__: console.debug("📦 @alwatr/local-storage v6.1.8");
|
|
3
|
+
import{createLogger}from"@alwatr/logger";var LocalStorageProvider=class _LocalStorageProvider{constructor(config_){this.config_=config_;this.logger_=createLogger(`local-storage-provider: ${this.config_.name}, v: ${this.config_.schemaVersion}`);this.logger_.logMethodArgs?.("constructor",{config:this.config_});this.key__=_LocalStorageProvider.getKey(this.config_);this.migrate__()}static{this.version="6.1.8"}static getKey(meta){return`${meta.name}.v${meta.schemaVersion}`}static has(meta){const key=_LocalStorageProvider.getKey(meta);return localStorage.getItem(key)!==null}writeDefault__(){this.logger_.logMethodArgs?.("writeDefaultــ",this.config_.defaultValue);this.write(this.config_.defaultValue);return this.config_.defaultValue}read(){try{const value=localStorage.getItem(this.key__);if(value===null){this.logger_.logMethod?.("read//no_value");return this.writeDefault__()}const parsedValue=JSON.parse(value);this.logger_.logMethodFull?.("read//value",void 0,{parsedValue});return parsedValue}catch(err){this.logger_.error("read","read_parse_error",{err});return this.writeDefault__()}}write(value){this.logger_.logMethodArgs?.("write",{value});try{localStorage.setItem(this.key__,JSON.stringify(value))}catch(err){this.logger_.error("write","write_stringify_error",{err})}}remove(){localStorage.removeItem(this.key__)}migrate__(){if(this.config_.schemaVersion<=1)return;for(let i=1;i<this.config_.schemaVersion;i++){const oldKey=_LocalStorageProvider.getKey({name:this.config_.name,schemaVersion:i});localStorage.removeItem(oldKey)}}};function createLocalStorageProvider(config){return new LocalStorageProvider(config)}export{LocalStorageProvider,createLocalStorageProvider};
|
|
3
4
|
//# sourceMappingURL=main.mjs.map
|
package/dist/main.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/local-storage.provider.ts", "../src/main.ts"],
|
|
4
|
-
"sourcesContent": ["import {createLogger} from '@alwatr/logger';\n\nimport type {LocalStorageProviderConfig, StorageMeta} from './type.js';\n\n/**\n * A provider class for managing a specific, versioned item in localStorage.\n * It encapsulates the logic for key generation, serialization, and migration.\n *\n * @example\n * ```typescript\n * const userSettings = new LocalStorageProvider({\n * name: 'user-settings',\n * version: 1,\n * defaultValue: { theme: 'light', notifications: true }\n * });\n *\n * // Write new settings\n * userSettings.write({ theme: 'dark', notifications: false });\n *\n * // Read the current settings\n * const currentSettings = userSettings.read();\n * console.log(currentSettings); // { theme: 'dark', notifications: false }\n * ```\n */\nexport class LocalStorageProvider<T extends JsonValue> {\n public static readonly version = __package_version__;\n\n private readonly key__: string;\n protected readonly logger_ = createLogger(`local-storage-provider: ${this.config_.name}, v: ${this.config_.
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import {createLogger} from '@alwatr/logger';\n\nimport type {LocalStorageProviderConfig, StorageMeta} from './type.js';\n\n/**\n * A provider class for managing a specific, versioned item in localStorage.\n * It encapsulates the logic for key generation, serialization, and migration.\n *\n * @example\n * ```typescript\n * const userSettings = new LocalStorageProvider({\n * name: 'user-settings',\n * version: 1,\n * defaultValue: { theme: 'light', notifications: true }\n * });\n *\n * // Write new settings\n * userSettings.write({ theme: 'dark', notifications: false });\n *\n * // Read the current settings\n * const currentSettings = userSettings.read();\n * console.log(currentSettings); // { theme: 'dark', notifications: false }\n * ```\n */\nexport class LocalStorageProvider<T extends JsonValue> {\n public static readonly version = __package_version__;\n\n private readonly key__: string;\n protected readonly logger_ = createLogger(`local-storage-provider: ${this.config_.name}, v: ${this.config_.schemaVersion}`);\n\n constructor(protected readonly config_: LocalStorageProviderConfig<T>) {\n this.logger_.logMethodArgs?.('constructor', {config: this.config_});\n this.key__ = LocalStorageProvider.getKey(this.config_);\n this.migrate__();\n }\n\n /**\n * Generates the versioned storage key.\n * @param meta - An object containing the name and schemaVersion.\n * @returns The versioned key string.\n */\n public static getKey(meta: StorageMeta): string {\n return `${meta.name}.v${meta.schemaVersion}`;\n }\n\n /**\n * Statically checks if a versioned item exists in localStorage.\n * This method provides a high-performance way to check for data existence without the overhead of creating a full provider instance.\n *\n * @param meta - An object containing the name and version of the item to check.\n * @returns `true` if the item exists, otherwise `false`.\n *\n * @example\n * ```typescript\n * const formExists = LocalStorageProvider.has({ name: 'user-form', schemaVersion: 1 });\n * if (formExists) {\n * // Show the \"Thank you\" message\n * } else {\n * // Show the form\n * }\n * ```\n */\n public static has(meta: StorageMeta): boolean {\n const key = LocalStorageProvider.getKey(meta);\n return localStorage.getItem(key) !== null;\n }\n\n /**\n * Writes the default value to localStorage and returns it.\n */\n private writeDefault__(): T {\n this.logger_.logMethodArgs?.('writeDefaultــ', this.config_.defaultValue);\n this.write(this.config_.defaultValue);\n return this.config_.defaultValue;\n }\n\n /**\n * Reads and parses the value from localStorage.\n * If the item doesn't exist, is invalid JSON, or doesn't match the expected type,\n * it writes and returns the default value.\n */\n public read(): T {\n try {\n const value = localStorage.getItem(this.key__);\n\n if (value === null) {\n this.logger_.logMethod?.('read//no_value');\n return this.writeDefault__();\n }\n\n const parsedValue = JSON.parse(value) as T;\n this.logger_.logMethodFull?.('read//value', undefined, {parsedValue});\n return parsedValue;\n }\n catch (err) {\n this.logger_.error('read', 'read_parse_error', {err});\n return this.writeDefault__();\n }\n }\n\n /**\n * Serializes and writes a value to localStorage.\n */\n public write(value: T): void {\n this.logger_.logMethodArgs?.('write', {value});\n try {\n localStorage.setItem(this.key__, JSON.stringify(value));\n }\n catch (err) {\n this.logger_.error('write', 'write_stringify_error', {err});\n }\n }\n\n /**\n * Removes the item from localStorage.\n */\n public remove(): void {\n localStorage.removeItem(this.key__);\n }\n\n /**\n * Manages data migration by removing all previous versions of the item.\n */\n private migrate__(): void {\n if (this.config_.schemaVersion <= 1) return;\n\n // Iterate from v1 up to the version just before the current one and remove them.\n for (let i = 1; i < this.config_.schemaVersion; i++) {\n const oldKey = LocalStorageProvider.getKey({name: this.config_.name, schemaVersion: i});\n localStorage.removeItem(oldKey);\n }\n }\n}\n", "import {LocalStorageProvider} from './local-storage.provider.js';\n\nimport type {LocalStorageProviderConfig} from './type.js';\n\nexport * from './local-storage.provider.js';\nexport type * from './type.js';\n\n/**\n * Factory function to create a new LocalStorageProvider.\n *\n * @param config - The configuration for the provider.\n * @returns An instance of LocalStorageProvider.\n *\n * @example\n * ```typescript\n * const userSettings = createLocalStorageProvider({\n * name: 'user-settings',\n * version: 1,\n * defaultValue: { theme: 'light', notifications: true }\n * });\n *\n * // Write new settings\n * userSettings.write({ theme: 'dark', notifications: false });\n *\n * // Read the current settings\n * const currentSettings = userSettings.read();\n * console.log(currentSettings); // { theme: 'dark', notifications: false }\n * ```\n */\nexport function createLocalStorageProvider<T extends JsonValue>(config: LocalStorageProviderConfig<T>): LocalStorageProvider<T> {\n return new LocalStorageProvider<T>(config);\n}\n"],
|
|
5
|
+
"mappings": ";;AAAA,OAAQ,iBAAmB,iBAwBpB,IAAM,qBAAN,MAAM,qBAA0C,CAMrD,YAA+B,QAAwC,CAAxC,qBAF/B,KAAmB,QAAU,aAAa,2BAA2B,KAAK,QAAQ,IAAI,QAAQ,KAAK,QAAQ,aAAa,EAAE,EAGxH,KAAK,QAAQ,gBAAgB,cAAe,CAAC,OAAQ,KAAK,OAAO,CAAC,EAClE,KAAK,MAAQ,sBAAqB,OAAO,KAAK,OAAO,EACrD,KAAK,UAAU,CACjB,CATA,YAAuB,QAAU,QAgBjC,OAAc,OAAO,KAA2B,CAC9C,MAAO,GAAG,KAAK,IAAI,KAAK,KAAK,aAAa,EAC5C,CAmBA,OAAc,IAAI,KAA4B,CAC5C,MAAM,IAAM,sBAAqB,OAAO,IAAI,EAC5C,OAAO,aAAa,QAAQ,GAAG,IAAM,IACvC,CAKQ,gBAAoB,CAC1B,KAAK,QAAQ,gBAAgB,iBAAkB,KAAK,QAAQ,YAAY,EACxE,KAAK,MAAM,KAAK,QAAQ,YAAY,EACpC,OAAO,KAAK,QAAQ,YACtB,CAOO,MAAU,CACf,GAAI,CACF,MAAM,MAAQ,aAAa,QAAQ,KAAK,KAAK,EAE7C,GAAI,QAAU,KAAM,CAClB,KAAK,QAAQ,YAAY,gBAAgB,EACzC,OAAO,KAAK,eAAe,CAC7B,CAEA,MAAM,YAAc,KAAK,MAAM,KAAK,EACpC,KAAK,QAAQ,gBAAgB,cAAe,OAAW,CAAC,WAAW,CAAC,EACpE,OAAO,WACT,OACO,IAAK,CACV,KAAK,QAAQ,MAAM,OAAQ,mBAAoB,CAAC,GAAG,CAAC,EACpD,OAAO,KAAK,eAAe,CAC7B,CACF,CAKO,MAAM,MAAgB,CAC3B,KAAK,QAAQ,gBAAgB,QAAS,CAAC,KAAK,CAAC,EAC7C,GAAI,CACF,aAAa,QAAQ,KAAK,MAAO,KAAK,UAAU,KAAK,CAAC,CACxD,OACO,IAAK,CACV,KAAK,QAAQ,MAAM,QAAS,wBAAyB,CAAC,GAAG,CAAC,CAC5D,CACF,CAKO,QAAe,CACpB,aAAa,WAAW,KAAK,KAAK,CACpC,CAKQ,WAAkB,CACxB,GAAI,KAAK,QAAQ,eAAiB,EAAG,OAGrC,QAAS,EAAI,EAAG,EAAI,KAAK,QAAQ,cAAe,IAAK,CACnD,MAAM,OAAS,sBAAqB,OAAO,CAAC,KAAM,KAAK,QAAQ,KAAM,cAAe,CAAC,CAAC,EACtF,aAAa,WAAW,MAAM,CAChC,CACF,CACF,ECvGO,SAAS,2BAAgD,OAAgE,CAC9H,OAAO,IAAI,qBAAwB,MAAM,CAC3C",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/type.d.ts
CHANGED
package/dist/type.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,WAAW;IAC1B;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,0BAA0B,CAAC,CAAC,CAAE,SAAQ,WAAW;IAChE;;OAEG;IACH,YAAY,EAAE,CAAC,CAAC;CACjB"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/local-storage",
|
|
3
3
|
"description": "A modern, simple, and robust solution for managing versioned JSON objects in the browser's `localStorage`. This package provides a clean, class-based API with a factory function to ensure your application's data persistence is safe, maintainable, and future-proof.",
|
|
4
|
-
"version": "6.1.
|
|
4
|
+
"version": "6.1.8",
|
|
5
5
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
6
|
"bugs": "https://github.com/Alwatr/nanolib/issues",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@alwatr/logger": "6.0.
|
|
8
|
+
"@alwatr/logger": "6.0.5"
|
|
9
9
|
},
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@alwatr/nano-build": "6.3.
|
|
11
|
+
"@alwatr/nano-build": "6.3.1",
|
|
12
12
|
"@alwatr/prettier-config": "5.0.4",
|
|
13
13
|
"@alwatr/tsconfig-base": "6.0.2",
|
|
14
14
|
"@alwatr/type-helper": "6.1.1",
|
|
@@ -75,5 +75,5 @@
|
|
|
75
75
|
"sideEffects": false,
|
|
76
76
|
"type": "module",
|
|
77
77
|
"types": "./dist/main.d.ts",
|
|
78
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "ff51e9fd23af6ffcf83efac6de0b02d9b06f7861"
|
|
79
79
|
}
|
package/src/main.test.js
CHANGED
|
@@ -28,14 +28,14 @@ describe('LocalStorageProvider', () => {
|
|
|
28
28
|
|
|
29
29
|
describe('static getKey', () => {
|
|
30
30
|
it('should generate the correct versioned key', () => {
|
|
31
|
-
const key = LocalStorageProvider.getKey({name: 'test',
|
|
31
|
+
const key = LocalStorageProvider.getKey({name: 'test', schemaVersion: 1});
|
|
32
32
|
expect(key).toBe('test.v1');
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
it('should handle different names and versions', () => {
|
|
36
|
-
const key1 = LocalStorageProvider.getKey({name: 'user-settings',
|
|
36
|
+
const key1 = LocalStorageProvider.getKey({name: 'user-settings', schemaVersion: 2});
|
|
37
37
|
expect(key1).toBe('user-settings.v2');
|
|
38
|
-
const key2 = LocalStorageProvider.getKey({name: 'form-data',
|
|
38
|
+
const key2 = LocalStorageProvider.getKey({name: 'form-data', schemaVersion: 5});
|
|
39
39
|
expect(key2).toBe('form-data.v5');
|
|
40
40
|
});
|
|
41
41
|
});
|
|
@@ -43,20 +43,20 @@ describe('LocalStorageProvider', () => {
|
|
|
43
43
|
describe('static has', () => {
|
|
44
44
|
it('should return true if item exists', () => {
|
|
45
45
|
mockLocalStorage.getItem.mockReturnValue('{"value": "test"}');
|
|
46
|
-
const exists = LocalStorageProvider.has({name: 'test',
|
|
46
|
+
const exists = LocalStorageProvider.has({name: 'test', schemaVersion: 1});
|
|
47
47
|
expect(exists).toBe(true);
|
|
48
48
|
expect(mockLocalStorage.getItem).toHaveBeenCalledWith('test.v1');
|
|
49
49
|
});
|
|
50
50
|
|
|
51
51
|
it('should return false if item does not exist', () => {
|
|
52
52
|
mockLocalStorage.getItem.mockReturnValue(null);
|
|
53
|
-
const exists = LocalStorageProvider.has({name: 'test',
|
|
53
|
+
const exists = LocalStorageProvider.has({name: 'test', schemaVersion: 1});
|
|
54
54
|
expect(exists).toBe(false);
|
|
55
55
|
});
|
|
56
56
|
|
|
57
57
|
it('should return false for null value', () => {
|
|
58
58
|
mockLocalStorage.getItem.mockReturnValue(null);
|
|
59
|
-
const exists = LocalStorageProvider.has({name: 'test',
|
|
59
|
+
const exists = LocalStorageProvider.has({name: 'test', schemaVersion: 1});
|
|
60
60
|
expect(exists).toBe(false);
|
|
61
61
|
});
|
|
62
62
|
});
|
|
@@ -65,26 +65,26 @@ describe('LocalStorageProvider', () => {
|
|
|
65
65
|
it('should initialize with config and migrate old versions', () => {
|
|
66
66
|
const provider = createLocalStorageProvider({
|
|
67
67
|
name: 'test',
|
|
68
|
-
|
|
68
|
+
schemaVersion: 3,
|
|
69
69
|
defaultValue: {key: 'value'},
|
|
70
70
|
});
|
|
71
71
|
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('test.v1');
|
|
72
72
|
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('test.v2');
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
-
it('should not migrate if
|
|
75
|
+
it('should not migrate if schemaVersion is 1', () => {
|
|
76
76
|
const provider = createLocalStorageProvider({
|
|
77
77
|
name: 'test',
|
|
78
|
-
|
|
78
|
+
schemaVersion: 1,
|
|
79
79
|
defaultValue: {key: 'value'},
|
|
80
80
|
});
|
|
81
81
|
expect(mockLocalStorage.removeItem).not.toHaveBeenCalled();
|
|
82
82
|
});
|
|
83
83
|
|
|
84
|
-
it('should migrate multiple old versions for higher
|
|
84
|
+
it('should migrate multiple old versions for higher schemaVersion', () => {
|
|
85
85
|
const provider = createLocalStorageProvider({
|
|
86
86
|
name: 'test',
|
|
87
|
-
|
|
87
|
+
schemaVersion: 5,
|
|
88
88
|
defaultValue: {key: 'value'},
|
|
89
89
|
});
|
|
90
90
|
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('test.v1');
|
|
@@ -99,7 +99,7 @@ describe('LocalStorageProvider', () => {
|
|
|
99
99
|
mockLocalStorage.getItem.mockReturnValue(null);
|
|
100
100
|
const provider = createLocalStorageProvider({
|
|
101
101
|
name: 'test',
|
|
102
|
-
|
|
102
|
+
schemaVersion: 1,
|
|
103
103
|
defaultValue: {key: 'default'},
|
|
104
104
|
});
|
|
105
105
|
const result = provider.read();
|
|
@@ -111,7 +111,7 @@ describe('LocalStorageProvider', () => {
|
|
|
111
111
|
mockLocalStorage.getItem.mockReturnValue('{"key":"stored"}');
|
|
112
112
|
const provider = createLocalStorageProvider({
|
|
113
113
|
name: 'test',
|
|
114
|
-
|
|
114
|
+
schemaVersion: 1,
|
|
115
115
|
defaultValue: {key: 'default'},
|
|
116
116
|
});
|
|
117
117
|
const result = provider.read();
|
|
@@ -122,7 +122,7 @@ describe('LocalStorageProvider', () => {
|
|
|
122
122
|
mockLocalStorage.getItem.mockReturnValue('invalid json');
|
|
123
123
|
const provider = createLocalStorageProvider({
|
|
124
124
|
name: 'test',
|
|
125
|
-
|
|
125
|
+
schemaVersion: 1,
|
|
126
126
|
defaultValue: {key: 'default'},
|
|
127
127
|
});
|
|
128
128
|
const result = provider.read();
|
|
@@ -133,7 +133,7 @@ describe('LocalStorageProvider', () => {
|
|
|
133
133
|
mockLocalStorage.getItem.mockReturnValue(null);
|
|
134
134
|
const provider = createLocalStorageProvider({
|
|
135
135
|
name: 'test',
|
|
136
|
-
|
|
136
|
+
schemaVersion: 1,
|
|
137
137
|
defaultValue: {theme: 'dark', lastLogin: Date.now(), settings: [1, 2, 3]},
|
|
138
138
|
});
|
|
139
139
|
const result = provider.read();
|
|
@@ -146,7 +146,7 @@ describe('LocalStorageProvider', () => {
|
|
|
146
146
|
it('should serialize and store the value', () => {
|
|
147
147
|
const provider = createLocalStorageProvider({
|
|
148
148
|
name: 'test',
|
|
149
|
-
|
|
149
|
+
schemaVersion: 1,
|
|
150
150
|
defaultValue: {key: 'default'},
|
|
151
151
|
});
|
|
152
152
|
provider.write({key: 'newValue'});
|
|
@@ -159,7 +159,7 @@ describe('LocalStorageProvider', () => {
|
|
|
159
159
|
});
|
|
160
160
|
const provider = createLocalStorageProvider({
|
|
161
161
|
name: 'test',
|
|
162
|
-
|
|
162
|
+
schemaVersion: 1,
|
|
163
163
|
defaultValue: {key: 'default'},
|
|
164
164
|
});
|
|
165
165
|
expect(() => provider.write({key: 'value'})).not.toThrow();
|
|
@@ -168,7 +168,7 @@ describe('LocalStorageProvider', () => {
|
|
|
168
168
|
it('should write different data types', () => {
|
|
169
169
|
const provider = createLocalStorageProvider({
|
|
170
170
|
name: 'test',
|
|
171
|
-
|
|
171
|
+
schemaVersion: 1,
|
|
172
172
|
/**
|
|
173
173
|
* @type {string|number|Array<number>}
|
|
174
174
|
*/
|
|
@@ -187,7 +187,7 @@ describe('LocalStorageProvider', () => {
|
|
|
187
187
|
it('should remove the item from storage', () => {
|
|
188
188
|
const provider = createLocalStorageProvider({
|
|
189
189
|
name: 'test',
|
|
190
|
-
|
|
190
|
+
schemaVersion: 1,
|
|
191
191
|
defaultValue: {key: 'default'},
|
|
192
192
|
});
|
|
193
193
|
provider.remove();
|
|
@@ -197,7 +197,7 @@ describe('LocalStorageProvider', () => {
|
|
|
197
197
|
it('should not throw if item does not exist', () => {
|
|
198
198
|
const provider = createLocalStorageProvider({
|
|
199
199
|
name: 'test',
|
|
200
|
-
|
|
200
|
+
schemaVersion: 1,
|
|
201
201
|
defaultValue: {key: 'default'},
|
|
202
202
|
});
|
|
203
203
|
expect(() => provider.remove()).not.toThrow();
|
|
@@ -208,7 +208,7 @@ describe('LocalStorageProvider', () => {
|
|
|
208
208
|
it('should create a provider instance', () => {
|
|
209
209
|
const provider = createLocalStorageProvider({
|
|
210
210
|
name: 'factory-test',
|
|
211
|
-
|
|
211
|
+
schemaVersion: 1,
|
|
212
212
|
defaultValue: {created: true},
|
|
213
213
|
});
|
|
214
214
|
expect(provider).toBeInstanceOf(LocalStorageProvider);
|
|
@@ -219,12 +219,12 @@ describe('LocalStorageProvider', () => {
|
|
|
219
219
|
it('should handle different configurations', () => {
|
|
220
220
|
const provider1 = createLocalStorageProvider({
|
|
221
221
|
name: 'config1',
|
|
222
|
-
|
|
222
|
+
schemaVersion: 2,
|
|
223
223
|
defaultValue: 'default1',
|
|
224
224
|
});
|
|
225
225
|
const provider2 = createLocalStorageProvider({
|
|
226
226
|
name: 'config2',
|
|
227
|
-
|
|
227
|
+
schemaVersion: 1,
|
|
228
228
|
defaultValue: {key: 'default2'},
|
|
229
229
|
});
|
|
230
230
|
expect(provider1.read()).toBe('default1');
|