Skip to content

Commit

Permalink
fix: options
Browse files Browse the repository at this point in the history
  • Loading branch information
ДимК committed Feb 14, 2023
1 parent 964d260 commit 1435965
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ttl-storage",
"version": "1.0.3",
"version": "1.0.4",
"description": "localStorage/sessionStorage with expiration lifetime",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ let ttlLocalStorage = new useStorage({}, window.localStorage);
//or
let ttlSessionStorage = new useStorage({}, window.sessionStorage);
```
## props

```
{
prefix: 'mycustom_', //prefix to save values in storage, by default is 'pre_'
maxage: 60 // maximum time to live cached data in seconds, by default is '24 * 3600' = 1 day
}
```

you can store any type of items in store. However objects will store as serialized string.
Expand Down
15 changes: 4 additions & 11 deletions src/useStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@ export type StorageOptions = {

export default class useStorage {
private options: StorageOptions;
private storage: Storage;

constructor(options = {}, storage = window.localStorage) {
this.options = {
prefix: 'pre_',
maxage: 24 * 3600, //24 hours (seconds)
...options,
};

this.storage = storage;
constructor(options: StorageOptions, public storage = window.localStorage) {
this.options = { ...{ prefix: 'pre_', maxage: 24 * 3600 }, ...options };
}

private _prepareValue<T>(value: T): StampedValue<T> {
Expand Down Expand Up @@ -112,7 +105,7 @@ export default class useStorage {
const stored = this._get<T>(key);

const callbackResolver = function callbackResolver(): Promise<T> {
let callbackResult: any;
let callbackResult: unknown;
if (typeof callback === 'function') {
callbackResult = callback();
} else callbackResult = callback;
Expand All @@ -129,7 +122,7 @@ export default class useStorage {
}

return new Promise(function (resolve) {
resolve(self.set<T>(key, callbackResult));
resolve(self.set<T>(key, callbackResult as T));
});
};

Expand Down
2 changes: 1 addition & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { expect } from 'chai';
let storage;

beforeEach(function () {
storage = new useStorage({}, new mockStorage());
storage = new useStorage({ prefix: 'mycustom_' }, new mockStorage());
storage.clear();
});

Expand Down

0 comments on commit 1435965

Please sign in to comment.