opendb-store 1.0.5 → 1.1.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.
@@ -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.5';
36
+ const version = '1.1.0';
37
37
 
38
38
  var config = {
39
39
  version,
@@ -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.5";var config={version:"1.0.5",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};
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.1.0";var config={version:"1.1.0",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};
@@ -0,0 +1,226 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
+ typeof define === 'function' && define.amd ? define('db', factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, (function () {
5
+ var current = global.db;
6
+ var exports = global.db = factory();
7
+ exports.noConflict = function () { global.db = current; return exports; };
8
+ })());
9
+ })(this, (function () {
10
+ const config$1 = {
11
+ namespace: 'app',
12
+ separator: '.',
13
+ trimKeys: true,
14
+ expiry: true,
15
+ };
16
+
17
+ function createNamespace(namespace) {
18
+ config$1.namespace = namespace;
19
+ }
20
+
21
+ function switchNamespace(namespace) {
22
+ config$1.namespace = namespace;
23
+ }
24
+
25
+ function getCurrentNamespace() {
26
+ return config$1.namespace;
27
+ }
28
+
29
+ function get$1() {
30
+ return config$1;
31
+ }
32
+
33
+ function setSeparator(separator) {
34
+ config$1.separator = separator;
35
+ }
36
+
37
+ function getSeparator() {
38
+ return config$1.separator;
39
+ }
40
+
41
+ function generateKey(key) {
42
+ return `${config$1.namespace}${config$1.separator}${key}`;
43
+ }
44
+
45
+ const version = '1.1.0';
46
+
47
+ var config = {
48
+ version,
49
+ createNamespace,
50
+ getCurrentNamespace,
51
+ switchNamespace,
52
+ get: get$1,
53
+ setSeparator,
54
+ getSeparator,
55
+ generateKey,
56
+ };
57
+
58
+ function from(namespace) {
59
+ if (namespace) {
60
+ switchNamespace(namespace);
61
+ }
62
+ }
63
+
64
+ function parse(value) {
65
+ try {
66
+ return JSON.parse(value);
67
+ } catch {
68
+ return value;
69
+ }
70
+ }
71
+
72
+ // Is a given variable undefined?
73
+ function isUndefined(obj) {
74
+ return obj === void 0;
75
+ }
76
+
77
+ // Is a given value equal to null?
78
+ function isNull(obj) {
79
+ return obj === null || obj === 'null';
80
+ }
81
+
82
+ var util = {
83
+ parse,
84
+ isUndefined,
85
+ isNull,
86
+ };
87
+
88
+ function isInvalidArg(key) {
89
+ return isUndefined(key) || isNull(key);
90
+ }
91
+
92
+ function get(key, defaultValue = null) {
93
+ if (isInvalidArg(key)) return null;
94
+ const namespacedKey = config.generateKey(key);
95
+ const value = this.storage.getItem(namespacedKey);
96
+
97
+ if (util.isNull(value)) {
98
+ return defaultValue;
99
+ }
100
+
101
+ try {
102
+ let parsedData = util.parse(value);
103
+
104
+ if (parsedData.expire && Date.now() > parsedData.expire) {
105
+ this.remove(key);
106
+ return defaultValue;
107
+ }
108
+
109
+ return parsedData.value;
110
+ } catch {
111
+ return defaultValue;
112
+ }
113
+ }
114
+
115
+ function set(key, value, options = {}) {
116
+ if (isInvalidArg(key)) return null;
117
+ const namespacedKey = config.generateKey(key);
118
+ const { expire } = options;
119
+
120
+ let items = {
121
+ value,
122
+ ...(expire ? { expire: Date.now() + expire } : {}),
123
+ };
124
+
125
+ this.storage.setItem(namespacedKey, JSON.stringify(items));
126
+ }
127
+
128
+ function has(key) {
129
+ return !!this.get(key);
130
+ }
131
+
132
+ function remove(key) {
133
+ const namespacedKey = config.generateKey(key);
134
+
135
+ return this.storage.removeItem(namespacedKey);
136
+ }
137
+
138
+ function clear() {
139
+ return this.storage.clear();
140
+ }
141
+
142
+ function key(index) {
143
+ return this.storage.key(index);
144
+ }
145
+
146
+ function keys() {
147
+ const keys = [];
148
+
149
+ for (let index = 0; index < this.storage.length; index++) {
150
+ keys.push(this.storage.key(index));
151
+ }
152
+
153
+ return keys;
154
+ }
155
+
156
+ function trim(key) {
157
+ return this.storage.get(key).trim();
158
+ }
159
+
160
+ function setFormattedData(key, obj) {
161
+ const seprator = getSeparator();
162
+
163
+ for (let k in obj) {
164
+ if (k in obj) {
165
+ this.set(`${key}${seprator}${k}`, obj[k]);
166
+ }
167
+ }
168
+ }
169
+
170
+ function getFormattedData(key) {
171
+ const result = {};
172
+ const seprator = getSeparator();
173
+
174
+ for (let i = 0, size = this.storage.length; i < size; i++) {
175
+ const completkey = this.key(i);
176
+ const [, objectkey, currentkey] = completkey.split(`${seprator}`, 3);
177
+
178
+ if (objectkey === key && currentkey) {
179
+ result[currentkey] = this.get(`${objectkey}${seprator}${currentkey}`);
180
+ }
181
+ }
182
+
183
+ return result;
184
+ }
185
+
186
+ var storageMethods = {
187
+ from,
188
+ get,
189
+ set,
190
+ has,
191
+ remove,
192
+ clear,
193
+ key,
194
+ keys,
195
+ trim,
196
+ getFormattedData,
197
+ setFormattedData,
198
+ };
199
+
200
+ function storageoperations(storage) {
201
+ return {
202
+ storage,
203
+ get count() {
204
+ return storage.length;
205
+ },
206
+ ...storageMethods,
207
+ };
208
+ }
209
+
210
+ function ensureSupport(storage) {
211
+ if (!storage) {
212
+ throw new Error('Storage is not supported in this environment.');
213
+ }
214
+ return storage;
215
+ }
216
+
217
+ const db = {
218
+ config,
219
+ local: storageoperations(ensureSupport(window.localStorage)),
220
+ session: storageoperations(ensureSupport(window.sessionStorage)),
221
+ };
222
+
223
+ return db;
224
+
225
+ }));
226
+ //# sourceMappingURL=opendb-umd.js.map
@@ -0,0 +1 @@
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define("db",t):(e="undefined"!=typeof globalThis?globalThis:e||self,function(){var n=e.db,r=e.db=t();r.noConflict=function(){return e.db=n,r}}())}(this,(function(){const e={namespace:"app",separator:".",trimKeys:!0,expiry:!0};function t(t){e.namespace=t}function n(){return e.separator}var r={version:"1.1.0",createNamespace:function(t){e.namespace=t},getCurrentNamespace:function(){return e.namespace},switchNamespace:t,get:function(){return e},setSeparator:function(t){e.separator=t},getSeparator:n,generateKey:function(t){return`${e.namespace}${e.separator}${t}`}};function o(e){return void 0===e}function i(e){return null===e||"null"===e}var s={parse:function(e){try{return JSON.parse(e)}catch{return e}},isUndefined:o,isNull:i};function a(e){return o(e)||i(e)}var u={from:function(e){e&&t(e)},get:function(e,t=null){if(a(e))return null;const n=r.generateKey(e),o=this.storage.getItem(n);if(s.isNull(o))return t;try{let n=s.parse(o);return n.expire&&Date.now()>n.expire?(this.remove(e),t):n.value}catch{return t}},set:function(e,t,n={}){if(a(e))return null;const o=r.generateKey(e),{expire:i}=n;let s={value:t,...i?{expire:Date.now()+i}:{}};this.storage.setItem(o,JSON.stringify(s))},has:function(e){return!!this.get(e)},remove:function(e){const t=r.generateKey(e);return this.storage.removeItem(t)},clear:function(){return this.storage.clear()},key:function(e){return this.storage.key(e)},keys:function(){const e=[];for(let t=0;t<this.storage.length;t++)e.push(this.storage.key(t));return e},trim:function(e){return this.storage.get(e).trim()},getFormattedData:function(e){const t={},r=n();for(let n=0,o=this.storage.length;n<o;n++){const o=this.key(n),[,i,s]=o.split(`${r}`,3);i===e&&s&&(t[s]=this.get(`${i}${r}${s}`))}return t},setFormattedData:function(e,t){const r=n();for(let n in t)n in t&&this.set(`${e}${r}${n}`,t[n])}};function c(e){return{storage:e,get count(){return e.length},...u}}function f(e){if(!e)throw new Error("Storage is not supported in this environment.");return e}return{config:r,local:c(f(window.localStorage)),session:c(f(window.sessionStorage))}}));
package/package.json CHANGED
@@ -1,8 +1,13 @@
1
1
  {
2
2
  "name": "opendb-store",
3
- "version": "1.0.5",
3
+ "version": "1.1.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
- "main": "./dist/opendb-esm.js",
5
+ "main": "./dist/opendb-umd.js",
6
+ "module": "./dist/opendb-esm.js",
7
+ "exports": {
8
+ "import": "./dist/opendb-esm.js",
9
+ "require": "./dist/opendb-umd.js"
10
+ },
6
11
  "type": "module",
7
12
  "publishConfig": {
8
13
  "access": "public"
@@ -11,22 +16,26 @@
11
16
  "src",
12
17
  "dist/opendb-esm.js",
13
18
  "dist/opendb-esm.min.js",
19
+ "dist/opendb-umd.js",
20
+ "dist/opendb-umd.min.js",
14
21
  "README.md"
15
22
  ],
16
23
  "scripts": {
17
24
  "prepare": "husky",
18
25
  "lint": "eslint src/**/*.js tests/*.js",
19
26
  "format": "prettier --write .",
20
- "bundle": "npm run module-package-json && rollup -c && eslint dist/opendb-esm.js",
27
+ "bundle": "npm run module-package-json && rollup -c && eslint dist/opendb-esm.js && eslint dist/opendb-umd.js",
21
28
  "module-package-json": "echo \"export const version = '${npm_package_version}';\" > src/config/version.js",
22
29
  "prepare-tests": "npm run bundle",
23
30
  "test-node": "jest",
24
31
  "test": "npm run lint && npm run prepare-tests && npm run test-node",
25
32
  "clean": "rm -rf dist",
26
33
  "lint:fix": "eslint \"src/**/*.js\" \"tests/*.js\" \"public/*.js\" --fix",
34
+ "minify-umd": "npx terser dist/opendb-umd.js -o dist/opendb-umd.min.js --compress --mangle",
27
35
  "minify-esm": "npx terser dist/opendb-esm.js -o dist/opendb-esm.min.js --compress --mangle",
36
+ "build-umd": "npm run minify-umd",
28
37
  "build-esm": "npm run minify-esm",
29
- "build": "npm run bundle && npm run build-esm",
38
+ "build": "npm run bundle && npm run build-esm && npm run build-umd",
30
39
  "semantic-release": "semantic-release",
31
40
  "prepublishOnly": "npm run build"
32
41
  },
@@ -1 +1 @@
1
- export const version = '1.0.5';
1
+ export const version = '1.1.0';