opendb-store 1.1.1 → 1.2.0
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 +31 -0
- package/dist/opendb-esm.js +103 -4
- package/dist/opendb-esm.min.js +1 -1
- package/dist/opendb-umd.js +103 -4
- package/dist/opendb-umd.min.js +1 -1
- package/package.json +1 -1
- package/src/config/version.js +1 -1
- package/src/modules/capacity.js +14 -0
- package/src/modules/free.js +17 -0
- package/src/modules/index.js +8 -0
- package/src/modules/size.js +18 -0
- package/src/modules/used.js +17 -0
- package/src/utils/index.js +2 -0
- package/src/utils/units.js +31 -0
package/README.md
CHANGED
|
@@ -142,3 +142,34 @@ Empty the entire storage.
|
|
|
142
142
|
// Clears all data from local storage
|
|
143
143
|
db.local.clear();
|
|
144
144
|
```
|
|
145
|
+
|
|
146
|
+
### size(key: string, options={ format?: "B" | "KB" | "MB"; unit?: boolean }): string | number
|
|
147
|
+
Get the size of the value for a specific key.
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
const sizeInBytes = 2 * 1024 * 1024; // 2 MB
|
|
151
|
+
const largeString = "A".repeat(sizeInBytes); // Create a 2MB string
|
|
152
|
+
const largeObject = { data: largeString };
|
|
153
|
+
|
|
154
|
+
db.local.set('largeObj', largeObject);
|
|
155
|
+
console.log(db.local.size('largeObj')); // 2097163 (raw bytes)
|
|
156
|
+
console.log(db.local.size('largeObj', { format: 'KB', unit: true })); // 2.00 MB
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### free(options={ format?: "B" | "KB" | "MB"; unit?: boolean }): string | number
|
|
160
|
+
Get the remaining free space in local or session storage.
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
|
|
164
|
+
console.log(db.local.free()); // raw bytes
|
|
165
|
+
console.log(db.local.free({ format: 'mb', unit: true })); // e.g., "3.00 MB"
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### capacity(options={ format?: "B" | "KB" | "MB"; unit?: boolean }): string | number
|
|
169
|
+
Get the total capacityof local or session storage.
|
|
170
|
+
|
|
171
|
+
```javascript
|
|
172
|
+
|
|
173
|
+
console.log(db.local.capacity()); // raw bytes
|
|
174
|
+
console.log(db.local.capacity({ format: 'mb', unit: true })); // e.g., "5.00 MB"
|
|
175
|
+
```
|
package/dist/opendb-esm.js
CHANGED
|
@@ -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.
|
|
36
|
+
const version = '1.2.0';
|
|
37
37
|
|
|
38
38
|
var config = {
|
|
39
39
|
version,
|
|
@@ -89,10 +89,43 @@ function isNull(obj) {
|
|
|
89
89
|
return obj === null || obj === 'null';
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
function auto(bytes, unit) {
|
|
93
|
+
if (bytes === 0) return '0 B';
|
|
94
|
+
const k = 1024;
|
|
95
|
+
const sizes = ['B', 'KB', 'MB'];
|
|
96
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
97
|
+
return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + (unit ? sizes[i] : '');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function units(bytes, format, unit) {
|
|
101
|
+
format = format.toUpperCase();
|
|
102
|
+
let formatted = '';
|
|
103
|
+
|
|
104
|
+
switch (format) {
|
|
105
|
+
case 'B':
|
|
106
|
+
formatted = bytes.toFixed(2);
|
|
107
|
+
break;
|
|
108
|
+
case 'KB':
|
|
109
|
+
formatted = (bytes / 1024).toFixed(2);
|
|
110
|
+
break;
|
|
111
|
+
case 'MB':
|
|
112
|
+
formatted = (bytes / (1024 * 1024)).toFixed(2);
|
|
113
|
+
break;
|
|
114
|
+
default:
|
|
115
|
+
console.warn(
|
|
116
|
+
`Unsupported format "${format}" provided. Falling back to auto-formatting.`
|
|
117
|
+
);
|
|
118
|
+
return auto(bytes, unit);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return unit ? `${formatted} ${format}` : formatted;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
var utils = {
|
|
93
125
|
parse,
|
|
94
126
|
isUndefined,
|
|
95
127
|
isNull,
|
|
128
|
+
units,
|
|
96
129
|
};
|
|
97
130
|
|
|
98
131
|
function isInvalidArg(key) {
|
|
@@ -115,12 +148,12 @@ function get(key, defaultValue = null) {
|
|
|
115
148
|
const namespacedKey = config.generateKey(key);
|
|
116
149
|
const value = this.storage.getItem(namespacedKey);
|
|
117
150
|
|
|
118
|
-
if (
|
|
151
|
+
if (utils.isNull(value)) {
|
|
119
152
|
return defaultValue;
|
|
120
153
|
}
|
|
121
154
|
|
|
122
155
|
try {
|
|
123
|
-
let parsedData =
|
|
156
|
+
let parsedData = utils.parse(value);
|
|
124
157
|
|
|
125
158
|
if (parsedData.expire && Date.now() > parsedData.expire) {
|
|
126
159
|
this.remove(key);
|
|
@@ -220,6 +253,23 @@ function trim(key) {
|
|
|
220
253
|
return this.storage.get(key).trim();
|
|
221
254
|
}
|
|
222
255
|
|
|
256
|
+
function size(key, options = {}) {
|
|
257
|
+
const value = this.get(key);
|
|
258
|
+
|
|
259
|
+
if (value === null) {
|
|
260
|
+
return 0;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const bytes = new Blob([JSON.stringify(value)]).size;
|
|
264
|
+
|
|
265
|
+
if (options.format) {
|
|
266
|
+
let unit = 'unit' in options ? options.unit : true;
|
|
267
|
+
return utils.units(bytes, options.format, unit);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return bytes;
|
|
271
|
+
}
|
|
272
|
+
|
|
223
273
|
function setFormattedData(key, obj) {
|
|
224
274
|
const seprator = getSeparator();
|
|
225
275
|
|
|
@@ -246,7 +296,56 @@ function getFormattedData(key) {
|
|
|
246
296
|
return result;
|
|
247
297
|
}
|
|
248
298
|
|
|
299
|
+
const DEFAULT_CAPACITY$1 = 5 * 1024 * 1024; // 5 MB
|
|
300
|
+
|
|
301
|
+
function free(options = {}) {
|
|
302
|
+
const capacity = options.capacity || DEFAULT_CAPACITY$1;
|
|
303
|
+
const total = this.used();
|
|
304
|
+
const remaining = capacity - total;
|
|
305
|
+
const bytes = remaining > 0 ? remaining : 0;
|
|
306
|
+
|
|
307
|
+
if (options.format) {
|
|
308
|
+
let unit = 'unit' in options ? options.unit : true;
|
|
309
|
+
return utils.units(bytes, options.format, unit);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return bytes;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function used() {
|
|
316
|
+
let totalBytes = 0;
|
|
317
|
+
const seprator = getSeparator();
|
|
318
|
+
|
|
319
|
+
for (let i = 0; i < this.count; i++) {
|
|
320
|
+
const completkey = this.key(i);
|
|
321
|
+
const [, currentkey] = completkey.split(`${seprator}`, 2);
|
|
322
|
+
|
|
323
|
+
const value = this.get(currentkey);
|
|
324
|
+
totalBytes += new Blob([JSON.stringify(completkey)]).size;
|
|
325
|
+
totalBytes += new Blob([JSON.stringify(value)]).size;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return totalBytes;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const DEFAULT_CAPACITY = 5 * 1024 * 1024; // 5 MB
|
|
332
|
+
|
|
333
|
+
function capacity(options = {}) {
|
|
334
|
+
const bytes = options.capacity || DEFAULT_CAPACITY;
|
|
335
|
+
|
|
336
|
+
if (options.format) {
|
|
337
|
+
let unit = 'unit' in options ? options.unit : true;
|
|
338
|
+
return utils.units(bytes, options.format, unit);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return bytes;
|
|
342
|
+
}
|
|
343
|
+
|
|
249
344
|
var storageMethods = {
|
|
345
|
+
size,
|
|
346
|
+
free,
|
|
347
|
+
used,
|
|
348
|
+
capacity,
|
|
250
349
|
from,
|
|
251
350
|
get,
|
|
252
351
|
set,
|
package/dist/opendb-esm.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const config$1={namespace:"app",separator:".",trimKeys:!0,expiry:!0};function createNamespace(
|
|
1
|
+
const config$1={namespace:"app",separator:".",trimKeys:!0,expiry:!0};function createNamespace(t){config$1.namespace=t}function switchNamespace(t){config$1.namespace=t}function getCurrentNamespace(){return config$1.namespace}function get$1(){return config$1}function setSeparator(t){config$1.separator=t}function getSeparator(){return config$1.separator}function generateKey(t){return`${config$1.namespace}${config$1.separator}${t}`}const version="1.2.0";var config={version:"1.2.0",createNamespace:createNamespace,getCurrentNamespace:getCurrentNamespace,switchNamespace:switchNamespace,get:get$1,setSeparator:setSeparator,getSeparator:getSeparator,generateKey:generateKey};function from(t){t&&switchNamespace(t)}function parse(t){try{return JSON.parse(t)}catch{return t}}function isUndefined(t){return void 0===t}function isNull(t){return null===t||"null"===t}function auto(t,e){if(0===t)return"0 B";const r=Math.floor(Math.log(t)/Math.log(1024));return(t/Math.pow(1024,r)).toFixed(2)+" "+(e?["B","KB","MB"][r]:"")}function units(t,e,r){let n="";switch(e=e.toUpperCase()){case"B":n=t.toFixed(2);break;case"KB":n=(t/1024).toFixed(2);break;case"MB":n=(t/1048576).toFixed(2);break;default:return console.warn(`Unsupported format "${e}" provided. Falling back to auto-formatting.`),auto(t,r)}return r?`${n} ${e}`:n}var utils={parse:parse,isUndefined:isUndefined,isNull:isNull,units:units};function isInvalidArg(t){return isUndefined(t)||isNull(t)}function get(t,e=null){if(isInvalidArg(t))return null;const r=config.generateKey(t),n=this.storage.getItem(r);if(utils.isNull(n))return e;try{let r=utils.parse(n);return r.expire&&Date.now()>r.expire?(this.remove(t),e):r.value}catch{return e}}function set(t,e,r={}){if(isInvalidArg(t))return null;const n=config.generateKey(t),{expire:i}=r;let o={value:e,...i?{expire:Date.now()+i}:{}};this.storage.setItem(n,JSON.stringify(o))}function has(t){return!!this.get(t)}function remove(t){const e=this.get(t),r=config.generateKey(t);return this.storage.removeItem(r),e}function clear(){return this.storage.clear()}function key(t){return this.storage.key(t)}function keys(){const t=[];for(let e=0;e<this.storage.length;e++)t.push(this.storage.key(e));return t}function trim(t){return this.storage.get(t).trim()}function size(t,e={}){const r=this.get(t);if(null===r)return 0;const n=new Blob([JSON.stringify(r)]).size;if(e.format){let t=!("unit"in e)||e.unit;return utils.units(n,e.format,t)}return n}function setFormattedData(t,e){const r=getSeparator();for(let n in e)n in e&&this.set(`${t}${r}${n}`,e[n])}function getFormattedData(t){const e={},r=getSeparator();for(let n=0,i=this.storage.length;n<i;n++){const i=this.key(n),[,o,a]=i.split(`${r}`,3);o===t&&a&&(e[a]=this.get(`${o}${r}${a}`))}return e}const DEFAULT_CAPACITY$1=5242880;function free(t={}){const e=(t.capacity||5242880)-this.used(),r=e>0?e:0;if(t.format){let e=!("unit"in t)||t.unit;return utils.units(r,t.format,e)}return r}function used(){let t=0;const e=getSeparator();for(let r=0;r<this.count;r++){const n=this.key(r),[,i]=n.split(`${e}`,2),o=this.get(i);t+=new Blob([JSON.stringify(n)]).size,t+=new Blob([JSON.stringify(o)]).size}return t}const DEFAULT_CAPACITY=5242880;function capacity(t={}){const e=t.capacity||5242880;if(t.format){let r=!("unit"in t)||t.unit;return utils.units(e,t.format,r)}return e}var storageMethods={size:size,free:free,used:used,capacity:capacity,from:from,get:get,set:set,has:has,remove:remove,clear:clear,key:key,keys:keys,trim:trim,getFormattedData:getFormattedData,setFormattedData:setFormattedData};function storageoperations(t){return{storage:t,get count(){return t.length},...storageMethods}}function ensureSupport(t){if(!t)throw new Error("Storage is not supported in this environment.");return t}const db={config:config,local:storageoperations(ensureSupport(window.localStorage)),session:storageoperations(ensureSupport(window.sessionStorage))};export{db as default};
|
package/dist/opendb-umd.js
CHANGED
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
return `${config$1.namespace}${config$1.separator}${key}`;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
const version = '1.
|
|
45
|
+
const version = '1.2.0';
|
|
46
46
|
|
|
47
47
|
var config = {
|
|
48
48
|
version,
|
|
@@ -98,10 +98,43 @@
|
|
|
98
98
|
return obj === null || obj === 'null';
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
|
|
101
|
+
function auto(bytes, unit) {
|
|
102
|
+
if (bytes === 0) return '0 B';
|
|
103
|
+
const k = 1024;
|
|
104
|
+
const sizes = ['B', 'KB', 'MB'];
|
|
105
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
106
|
+
return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + (unit ? sizes[i] : '');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function units(bytes, format, unit) {
|
|
110
|
+
format = format.toUpperCase();
|
|
111
|
+
let formatted = '';
|
|
112
|
+
|
|
113
|
+
switch (format) {
|
|
114
|
+
case 'B':
|
|
115
|
+
formatted = bytes.toFixed(2);
|
|
116
|
+
break;
|
|
117
|
+
case 'KB':
|
|
118
|
+
formatted = (bytes / 1024).toFixed(2);
|
|
119
|
+
break;
|
|
120
|
+
case 'MB':
|
|
121
|
+
formatted = (bytes / (1024 * 1024)).toFixed(2);
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
console.warn(
|
|
125
|
+
`Unsupported format "${format}" provided. Falling back to auto-formatting.`
|
|
126
|
+
);
|
|
127
|
+
return auto(bytes, unit);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return unit ? `${formatted} ${format}` : formatted;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
var utils = {
|
|
102
134
|
parse,
|
|
103
135
|
isUndefined,
|
|
104
136
|
isNull,
|
|
137
|
+
units,
|
|
105
138
|
};
|
|
106
139
|
|
|
107
140
|
function isInvalidArg(key) {
|
|
@@ -124,12 +157,12 @@
|
|
|
124
157
|
const namespacedKey = config.generateKey(key);
|
|
125
158
|
const value = this.storage.getItem(namespacedKey);
|
|
126
159
|
|
|
127
|
-
if (
|
|
160
|
+
if (utils.isNull(value)) {
|
|
128
161
|
return defaultValue;
|
|
129
162
|
}
|
|
130
163
|
|
|
131
164
|
try {
|
|
132
|
-
let parsedData =
|
|
165
|
+
let parsedData = utils.parse(value);
|
|
133
166
|
|
|
134
167
|
if (parsedData.expire && Date.now() > parsedData.expire) {
|
|
135
168
|
this.remove(key);
|
|
@@ -229,6 +262,23 @@
|
|
|
229
262
|
return this.storage.get(key).trim();
|
|
230
263
|
}
|
|
231
264
|
|
|
265
|
+
function size(key, options = {}) {
|
|
266
|
+
const value = this.get(key);
|
|
267
|
+
|
|
268
|
+
if (value === null) {
|
|
269
|
+
return 0;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const bytes = new Blob([JSON.stringify(value)]).size;
|
|
273
|
+
|
|
274
|
+
if (options.format) {
|
|
275
|
+
let unit = 'unit' in options ? options.unit : true;
|
|
276
|
+
return utils.units(bytes, options.format, unit);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return bytes;
|
|
280
|
+
}
|
|
281
|
+
|
|
232
282
|
function setFormattedData(key, obj) {
|
|
233
283
|
const seprator = getSeparator();
|
|
234
284
|
|
|
@@ -255,7 +305,56 @@
|
|
|
255
305
|
return result;
|
|
256
306
|
}
|
|
257
307
|
|
|
308
|
+
const DEFAULT_CAPACITY$1 = 5 * 1024 * 1024; // 5 MB
|
|
309
|
+
|
|
310
|
+
function free(options = {}) {
|
|
311
|
+
const capacity = options.capacity || DEFAULT_CAPACITY$1;
|
|
312
|
+
const total = this.used();
|
|
313
|
+
const remaining = capacity - total;
|
|
314
|
+
const bytes = remaining > 0 ? remaining : 0;
|
|
315
|
+
|
|
316
|
+
if (options.format) {
|
|
317
|
+
let unit = 'unit' in options ? options.unit : true;
|
|
318
|
+
return utils.units(bytes, options.format, unit);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return bytes;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function used() {
|
|
325
|
+
let totalBytes = 0;
|
|
326
|
+
const seprator = getSeparator();
|
|
327
|
+
|
|
328
|
+
for (let i = 0; i < this.count; i++) {
|
|
329
|
+
const completkey = this.key(i);
|
|
330
|
+
const [, currentkey] = completkey.split(`${seprator}`, 2);
|
|
331
|
+
|
|
332
|
+
const value = this.get(currentkey);
|
|
333
|
+
totalBytes += new Blob([JSON.stringify(completkey)]).size;
|
|
334
|
+
totalBytes += new Blob([JSON.stringify(value)]).size;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return totalBytes;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const DEFAULT_CAPACITY = 5 * 1024 * 1024; // 5 MB
|
|
341
|
+
|
|
342
|
+
function capacity(options = {}) {
|
|
343
|
+
const bytes = options.capacity || DEFAULT_CAPACITY;
|
|
344
|
+
|
|
345
|
+
if (options.format) {
|
|
346
|
+
let unit = 'unit' in options ? options.unit : true;
|
|
347
|
+
return utils.units(bytes, options.format, unit);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return bytes;
|
|
351
|
+
}
|
|
352
|
+
|
|
258
353
|
var storageMethods = {
|
|
354
|
+
size,
|
|
355
|
+
free,
|
|
356
|
+
used,
|
|
357
|
+
capacity,
|
|
259
358
|
from,
|
|
260
359
|
get,
|
|
261
360
|
set,
|
package/dist/opendb-umd.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define("db",e):(t="undefined"!=typeof globalThis?globalThis:t||self,function(){var n=t.db,r=t.db=e();r.noConflict=function(){return t.db=n,r}}())}(this,(function(){const t={namespace:"app",separator:".",trimKeys:!0,expiry:!0};function e(e){t.namespace=e}function n(){return t.separator}var r={version:"1.2.0",createNamespace:function(e){t.namespace=e},getCurrentNamespace:function(){return t.namespace},switchNamespace:e,get:function(){return t},setSeparator:function(e){t.separator=e},getSeparator:n,generateKey:function(e){return`${t.namespace}${t.separator}${e}`}};function i(t){return void 0===t}function o(t){return null===t||"null"===t}var s={parse:function(t){try{return JSON.parse(t)}catch{return t}},isUndefined:i,isNull:o,units:function(t,e,n){let r="";switch(e=e.toUpperCase()){case"B":r=t.toFixed(2);break;case"KB":r=(t/1024).toFixed(2);break;case"MB":r=(t/1048576).toFixed(2);break;default:return console.warn(`Unsupported format "${e}" provided. Falling back to auto-formatting.`),function(t,e){if(0===t)return"0 B";const n=Math.floor(Math.log(t)/Math.log(1024));return(t/Math.pow(1024,n)).toFixed(2)+" "+(e?["B","KB","MB"][n]:"")}(t,n)}return n?`${r} ${e}`:r}};function u(t){return i(t)||o(t)}var a={size:function(t,e={}){const n=this.get(t);if(null===n)return 0;const r=new Blob([JSON.stringify(n)]).size;if(e.format){let t=!("unit"in e)||e.unit;return s.units(r,e.format,t)}return r},free:function(t={}){const e=(t.capacity||5242880)-this.used(),n=e>0?e:0;if(t.format){let e=!("unit"in t)||t.unit;return s.units(n,t.format,e)}return n},used:function(){let t=0;const e=n();for(let n=0;n<this.count;n++){const r=this.key(n),[,i]=r.split(`${e}`,2),o=this.get(i);t+=new Blob([JSON.stringify(r)]).size,t+=new Blob([JSON.stringify(o)]).size}return t},capacity:function(t={}){const e=t.capacity||5242880;if(t.format){let n=!("unit"in t)||t.unit;return s.units(e,t.format,n)}return e},from:function(t){t&&e(t)},get:function(t,e=null){if(u(t))return null;const n=r.generateKey(t),i=this.storage.getItem(n);if(s.isNull(i))return e;try{let n=s.parse(i);return n.expire&&Date.now()>n.expire?(this.remove(t),e):n.value}catch{return e}},set:function(t,e,n={}){if(u(t))return null;const i=r.generateKey(t),{expire:o}=n;let s={value:e,...o?{expire:Date.now()+o}:{}};this.storage.setItem(i,JSON.stringify(s))},has:function(t){return!!this.get(t)},remove:function(t){const e=this.get(t),n=r.generateKey(t);return this.storage.removeItem(n),e},clear:function(){return this.storage.clear()},key:function(t){return this.storage.key(t)},keys:function(){const t=[];for(let e=0;e<this.storage.length;e++)t.push(this.storage.key(e));return t},trim:function(t){return this.storage.get(t).trim()},getFormattedData:function(t){const e={},r=n();for(let n=0,i=this.storage.length;n<i;n++){const i=this.key(n),[,o,s]=i.split(`${r}`,3);o===t&&s&&(e[s]=this.get(`${o}${r}${s}`))}return e},setFormattedData:function(t,e){const r=n();for(let n in e)n in e&&this.set(`${t}${r}${n}`,e[n])}};function c(t){return{storage:t,get count(){return t.length},...a}}function f(t){if(!t)throw new Error("Storage is not supported in this environment.");return t}return{config:r,local:c(f(window.localStorage)),session:c(f(window.sessionStorage))}}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opendb-store",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
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-umd.js",
|
|
6
6
|
"module": "./dist/opendb-esm.js",
|
package/src/config/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = '1.
|
|
1
|
+
export const version = '1.2.0';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import utils from '../utils/index.js';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_CAPACITY = 5 * 1024 * 1024; // 5 MB
|
|
4
|
+
|
|
5
|
+
export default function capacity(options = {}) {
|
|
6
|
+
const bytes = options.capacity || DEFAULT_CAPACITY;
|
|
7
|
+
|
|
8
|
+
if (options.format) {
|
|
9
|
+
let unit = 'unit' in options ? options.unit : true;
|
|
10
|
+
return utils.units(bytes, options.format, unit);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return bytes;
|
|
14
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import util from '../utils/index.js';
|
|
2
|
+
|
|
3
|
+
const DEFAULT_CAPACITY = 5 * 1024 * 1024; // 5 MB
|
|
4
|
+
|
|
5
|
+
export default function free(options = {}) {
|
|
6
|
+
const capacity = options.capacity || DEFAULT_CAPACITY;
|
|
7
|
+
const total = this.used();
|
|
8
|
+
const remaining = capacity - total;
|
|
9
|
+
const bytes = remaining > 0 ? remaining : 0;
|
|
10
|
+
|
|
11
|
+
if (options.format) {
|
|
12
|
+
let unit = 'unit' in options ? options.unit : true;
|
|
13
|
+
return util.units(bytes, options.format, unit);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return bytes;
|
|
17
|
+
}
|
package/src/modules/index.js
CHANGED
|
@@ -7,10 +7,18 @@ import clear from './clear.js';
|
|
|
7
7
|
import key from './key.js';
|
|
8
8
|
import keys from './keys.js';
|
|
9
9
|
import trim from './trim.js';
|
|
10
|
+
import size from './size.js';
|
|
10
11
|
import setFormattedData from './setFormattedData.js';
|
|
11
12
|
import getFormattedData from './getFormattedData.js';
|
|
13
|
+
import free from './free.js';
|
|
14
|
+
import used from './used.js';
|
|
15
|
+
import capacity from './capacity.js';
|
|
12
16
|
|
|
13
17
|
export default {
|
|
18
|
+
size,
|
|
19
|
+
free,
|
|
20
|
+
used,
|
|
21
|
+
capacity,
|
|
14
22
|
from,
|
|
15
23
|
get,
|
|
16
24
|
set,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import util from '../utils/index.js';
|
|
2
|
+
|
|
3
|
+
export default function size(key, options = {}) {
|
|
4
|
+
const value = this.get(key);
|
|
5
|
+
|
|
6
|
+
if (value === null) {
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const bytes = new Blob([JSON.stringify(value)]).size;
|
|
11
|
+
|
|
12
|
+
if (options.format) {
|
|
13
|
+
let unit = 'unit' in options ? options.unit : true;
|
|
14
|
+
return util.units(bytes, options.format, unit);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return bytes;
|
|
18
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getSeparator } from '../config/config.js';
|
|
2
|
+
|
|
3
|
+
export default function used() {
|
|
4
|
+
let totalBytes = 0;
|
|
5
|
+
const seprator = getSeparator();
|
|
6
|
+
|
|
7
|
+
for (let i = 0; i < this.count; i++) {
|
|
8
|
+
const completkey = this.key(i);
|
|
9
|
+
const [, currentkey] = completkey.split(`${seprator}`, 2);
|
|
10
|
+
|
|
11
|
+
const value = this.get(currentkey);
|
|
12
|
+
totalBytes += new Blob([JSON.stringify(completkey)]).size;
|
|
13
|
+
totalBytes += new Blob([JSON.stringify(value)]).size;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return totalBytes;
|
|
17
|
+
}
|
package/src/utils/index.js
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function auto(bytes, unit) {
|
|
2
|
+
if (bytes === 0) return '0 B';
|
|
3
|
+
const k = 1024;
|
|
4
|
+
const sizes = ['B', 'KB', 'MB'];
|
|
5
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
6
|
+
return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + (unit ? sizes[i] : '');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default function units(bytes, format, unit) {
|
|
10
|
+
format = format.toUpperCase();
|
|
11
|
+
let formatted = '';
|
|
12
|
+
|
|
13
|
+
switch (format) {
|
|
14
|
+
case 'B':
|
|
15
|
+
formatted = bytes.toFixed(2);
|
|
16
|
+
break;
|
|
17
|
+
case 'KB':
|
|
18
|
+
formatted = (bytes / 1024).toFixed(2);
|
|
19
|
+
break;
|
|
20
|
+
case 'MB':
|
|
21
|
+
formatted = (bytes / (1024 * 1024)).toFixed(2);
|
|
22
|
+
break;
|
|
23
|
+
default:
|
|
24
|
+
console.warn(
|
|
25
|
+
`Unsupported format "${format}" provided. Falling back to auto-formatting.`
|
|
26
|
+
);
|
|
27
|
+
return auto(bytes, unit);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return unit ? `${formatted} ${format}` : formatted;
|
|
31
|
+
}
|