opendb-store 1.0.2 → 1.0.4

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/README.md CHANGED
@@ -4,8 +4,22 @@
4
4
 
5
5
  ## Core Database Object Method
6
6
 
7
- local - Contain localStorage methods
8
- session - Contain sessionStorage methods
7
+ > local - Contain localStorage methods
8
+
9
+ > session - Contain sessionStorage methods
10
+
11
+ ## Installation Guide
12
+ ```
13
+ npm i opendb-store
14
+ ```
15
+
16
+ ## Import opendb-store in Your Project
17
+ ```
18
+ import db from 'opendb-store'
19
+
20
+ db.local.set('libname', 'OpenDB Store');
21
+ console.log('Welcome to: ', db.local.get('libname'));
22
+ ```
9
23
 
10
24
  ## Local Storage
11
25
 
@@ -58,14 +72,73 @@ For further details, see the [old-approach](https://github.com/pankajbisht/openD
58
72
 
59
73
  ## List of Local and Session Storage Methods
60
74
 
75
+ ### set(key: string, value: any): void
76
+ Stores data in local or session storage with a key and value.
77
+ Example:
78
+ ```javascript
79
+
80
+ db.local.set('libname', 'OpenDB Store'); // Set simple value
81
+ db.local.set('object', { name: 'OpenDB Store', version: 'x.y.z' }); // Set Object Value
82
+ db.local.set('array', ['OpenDB Store', 'x.y.z']); // Set Array Value
83
+ db.local.set('expiringKey', 'It will expire', { expire: 1 * 1000 }); // Set simple value expire after 1 second
84
+
85
+ // for session storage
86
+ db.session.set('libname', 'OpenDB Store'); // Set simple value inside session storage
61
87
  ```
62
- - set(key, value) :- stores data in local or session storage with a key and value
63
88
 
64
- - get(key, defaultValue) :- retrieve data from local or session storage by key.
89
+ ### get(key: string, defaultValue: any): any
90
+ Retrieves data from local or session storage by key. Returns defaultValue if the key does not exist.
91
+
92
+ ```javascript
93
+
94
+ // Get Simple Value
95
+ console.log('Welcome to: ', db.local.get('libname')); // OpenDB Store
96
+
97
+ // Get Object Value
98
+ console.log('Object: ', db.local.get('object'));
99
+ // Or
100
+ const { name, version } = db.local.get('object', ['name', 'version']);
101
+ console.log(name, version);
102
+
103
+ // Get Array Value
104
+ console.log('Array: ', db.local.get('array'));
105
+
106
+ // Get defaultValue
107
+ db.local.get('missingKey', {}); // {} by default it will null
108
+
109
+ console.log('Before(2 sec) expiringKey', db.local.get('expiringKey'))
110
+
111
+ (async () => {
112
+ await new Promise((resolve) => setTimeout(resolve, 2 * 1000));
113
+ console.log('After(2 sec) expiringKey', db.local.get('expiringKey')); // will get default value
114
+ })();
115
+
116
+ ```
117
+
118
+ ### has(key: string): boolean
119
+ check whether a specified key exists in local or session storage
120
+
121
+ ```javascript
122
+
123
+ // Checks if a key exists in local storage.
124
+ console.log(db.local.has('libname')); // true
125
+ console.log(db.local.has('missingkey')); // false
126
+ ```
127
+
128
+ ### remove(key: string): void
129
+ remove a specific item from local or session storage
130
+
131
+ ```javascript
132
+
133
+ // Removes a value from storage.
134
+ db.local.remove('libname');
135
+ ```
65
136
 
66
- - has(key) :- check whether a specified key exists in local or session storage
137
+ ### clear(): void
138
+ Empty the entire storage.
67
139
 
68
- - remove(key) :- remove a specific item from local or session storage
140
+ ```javascript
69
141
 
70
- - clear() :- Empty the entire storage.
142
+ // Clears all data from local storage
143
+ db.local.clear();
71
144
  ```
@@ -33,7 +33,7 @@ function generateKey(key) {
33
33
  return `${config$1.namespace}${config$1.separator}${key}`;
34
34
  }
35
35
 
36
- const version = '1.0.2';
36
+ const version = '1.0.4';
37
37
 
38
38
  var config = {
39
39
  version,
@@ -86,7 +86,7 @@ function get(key, defaultValue = null) {
86
86
  const value = this.storage.getItem(namespacedKey);
87
87
 
88
88
  if (util.isNull(value)) {
89
- return null;
89
+ return defaultValue;
90
90
  }
91
91
 
92
92
  try {
@@ -1 +1 @@
1
- const config$1={namespace:"app",separator:".",trimKeys:!0,expiry:!0};function createNamespace(e){config$1.namespace=e}function switchNamespace(e){config$1.namespace=e}function getCurrentNamespace(){return config$1.namespace}function get$1(){return config$1}function setSeparator(e){config$1.separator=e}function getSeparator(){return config$1.separator}function generateKey(e){return`${config$1.namespace}${config$1.separator}${e}`}const version="1.0.2";var config={version:"1.0.2",createNamespace:createNamespace,getCurrentNamespace:getCurrentNamespace,switchNamespace:switchNamespace,get:get$1,setSeparator:setSeparator,getSeparator:getSeparator,generateKey:generateKey};function from(e){e&&switchNamespace(e)}function parse(e){try{return JSON.parse(e)}catch{return e}}function isUndefined(e){return void 0===e}function isNull(e){return null===e||"null"===e}var util={parse:parse,isUndefined:isUndefined,isNull:isNull};function isInvalidArg(e){return isUndefined(e)||isNull(e)}function get(e,t=null){if(isInvalidArg(e))return null;const r=config.generateKey(e),n=this.storage.getItem(r);if(util.isNull(n))return null;try{let r=util.parse(n);return r.expire&&Date.now()>r.expire?(this.remove(e),t):r.value}catch{return t}}function set(e,t,r={}){if(isInvalidArg(e))return null;const n=config.generateKey(e),{expire:a}=r;let o={value:t,...a?{expire:Date.now()+a}:{}};this.storage.setItem(n,JSON.stringify(o))}function has(e){return!!this.get(e)}function remove(e){const t=config.generateKey(e);return this.storage.removeItem(t)}function clear(){return this.storage.clear()}function key(e){return this.storage.key(e)}function keys(){const e=[];for(let t=0;t<this.storage.length;t++)e.push(this.storage.key(t));return e}function trim(e){return this.storage.get(e).trim()}function setFormattedData(e,t){const r=getSeparator();for(let n in t)n in t&&this.set(`${e}${r}${n}`,t[n])}function getFormattedData(e){const t={},r=getSeparator();for(let n=0,a=this.storage.length;n<a;n++){const a=this.key(n),[,o,s]=a.split(`${r}`,3);o===e&&s&&(t[s]=this.get(`${o}${r}${s}`))}return t}var storageMethods={from:from,get:get,set:set,has:has,remove:remove,clear:clear,key:key,keys:keys,trim:trim,getFormattedData:getFormattedData,setFormattedData:setFormattedData};function storageoperations(e){return{storage:e,get count(){return e.length},...storageMethods}}function ensureSupport(e){if(!e)throw new Error("Storage is not supported in this environment.");return e}const db={config:config,local:storageoperations(ensureSupport(window.localStorage)),session:storageoperations(ensureSupport(window.sessionStorage))};export{db as default};
1
+ const config$1={namespace:"app",separator:".",trimKeys:!0,expiry:!0};function createNamespace(e){config$1.namespace=e}function switchNamespace(e){config$1.namespace=e}function getCurrentNamespace(){return config$1.namespace}function get$1(){return config$1}function setSeparator(e){config$1.separator=e}function getSeparator(){return config$1.separator}function generateKey(e){return`${config$1.namespace}${config$1.separator}${e}`}const version="1.0.4";var config={version:"1.0.4",createNamespace:createNamespace,getCurrentNamespace:getCurrentNamespace,switchNamespace:switchNamespace,get:get$1,setSeparator:setSeparator,getSeparator:getSeparator,generateKey:generateKey};function from(e){e&&switchNamespace(e)}function parse(e){try{return JSON.parse(e)}catch{return e}}function isUndefined(e){return void 0===e}function isNull(e){return null===e||"null"===e}var util={parse:parse,isUndefined:isUndefined,isNull:isNull};function isInvalidArg(e){return isUndefined(e)||isNull(e)}function get(e,t=null){if(isInvalidArg(e))return null;const r=config.generateKey(e),n=this.storage.getItem(r);if(util.isNull(n))return t;try{let r=util.parse(n);return r.expire&&Date.now()>r.expire?(this.remove(e),t):r.value}catch{return t}}function set(e,t,r={}){if(isInvalidArg(e))return null;const n=config.generateKey(e),{expire:a}=r;let o={value:t,...a?{expire:Date.now()+a}:{}};this.storage.setItem(n,JSON.stringify(o))}function has(e){return!!this.get(e)}function remove(e){const t=config.generateKey(e);return this.storage.removeItem(t)}function clear(){return this.storage.clear()}function key(e){return this.storage.key(e)}function keys(){const e=[];for(let t=0;t<this.storage.length;t++)e.push(this.storage.key(t));return e}function trim(e){return this.storage.get(e).trim()}function setFormattedData(e,t){const r=getSeparator();for(let n in t)n in t&&this.set(`${e}${r}${n}`,t[n])}function getFormattedData(e){const t={},r=getSeparator();for(let n=0,a=this.storage.length;n<a;n++){const a=this.key(n),[,o,s]=a.split(`${r}`,3);o===e&&s&&(t[s]=this.get(`${o}${r}${s}`))}return t}var storageMethods={from:from,get:get,set:set,has:has,remove:remove,clear:clear,key:key,keys:keys,trim:trim,getFormattedData:getFormattedData,setFormattedData:setFormattedData};function storageoperations(e){return{storage:e,get count(){return e.length},...storageMethods}}function ensureSupport(e){if(!e)throw new Error("Storage is not supported in this environment.");return e}const db={config:config,local:storageoperations(ensureSupport(window.localStorage)),session:storageoperations(ensureSupport(window.sessionStorage))};export{db as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opendb-store",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "A lightweight utility to manage browser storage (localStorage, sessionStorage, and cookies) with advanced features. Easily configure namespaces, key trimming, and data expiry.",
5
5
  "main": "./dist/opendb-esm.js",
6
6
  "type": "module",
@@ -1 +1 @@
1
- export const version = '1.0.2';
1
+ export const version = '1.0.4';
@@ -8,7 +8,7 @@ export default function get(key, defaultValue = null) {
8
8
  const value = this.storage.getItem(namespacedKey);
9
9
 
10
10
  if (util.isNull(value)) {
11
- return null;
11
+ return defaultValue;
12
12
  }
13
13
 
14
14
  try {