localstorage-ttl
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -1,2 +0,3 @@ | ||
declare function set<T>(variable: string, value: T, ttl_ms: number): void; | ||
declare function set<T>(variable: string, value: T, ttl_ms?: number): void; | ||
declare function remove(variable: string): void; | ||
declare function get<T>(variable: string): T | null; | ||
@@ -6,3 +7,4 @@ export declare const ls: { | ||
get: typeof get; | ||
remove: typeof remove; | ||
}; | ||
export default ls; |
@@ -5,8 +5,13 @@ "use strict"; | ||
function set(variable, value, ttl_ms) { | ||
const data = { | ||
let data = { | ||
value: value, | ||
expires_at: Date.now() + ttl_ms, | ||
}; | ||
if (ttl_ms) { | ||
data.expires_at = Date.now() + ttl_ms; | ||
} | ||
localStorage.setItem(variable, JSON.stringify(data)); | ||
} | ||
function remove(variable) { | ||
localStorage.removeItem(variable); | ||
} | ||
function get(variable) { | ||
@@ -31,3 +36,3 @@ const item = localStorage.getItem(variable); | ||
} | ||
exports.ls = { set, get }; | ||
exports.ls = { set, get, remove }; | ||
exports.default = exports.ls; |
16
index.ts
interface StoredData<T> { | ||
value: T; | ||
expires_at: number; | ||
expires_at?: number; | ||
} | ||
function set<T>(variable: string, value: T, ttl_ms: number): void { | ||
const data: StoredData<T> = { | ||
function set<T>(variable: string, value: T, ttl_ms?: number): void { | ||
let data: StoredData<T> = { | ||
value: value, | ||
expires_at: Date.now() + ttl_ms, | ||
}; | ||
if (ttl_ms) { | ||
data.expires_at = Date.now() + ttl_ms; | ||
} | ||
localStorage.setItem(variable, JSON.stringify(data)); | ||
} | ||
function remove(variable: string): void { | ||
localStorage.removeItem(variable); | ||
} | ||
function get<T>(variable: string): T | null { | ||
@@ -32,3 +38,3 @@ const item = localStorage.getItem(variable); | ||
export const ls = { set, get }; | ||
export const ls = { set, get, remove }; | ||
export default ls; |
{ | ||
"name": "localstorage-ttl", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "TS module to save data to localstorage with time to live. Data will expire after set time.", | ||
@@ -5,0 +5,0 @@ "main": "index.ts", |
@@ -6,3 +6,3 @@ # localstorage-ttl | ||
>Version 1.x is JavaScript, version 2.x is Typescript | ||
Updated to TS (v2) after 9yr of JS (v1) 🎉 | ||
@@ -22,11 +22,10 @@ # Install: | ||
ls.get(variable); | ||
ls.remove(variable); | ||
``` | ||
to remove, just use plain js. It works in all usable browsers. http://caniuse.com/#search=localstorage | ||
``` | ||
localStorage.removeItem(variable); | ||
``` | ||
* ttl_ms = time to live time in ms. | ||
if **ttl_ms** is not set it will be **without expire date** and will be there till user deletes or your other js deletes it. | ||
if **ttl_ms** is not set it will stay **without expire date** and will be there till deleted. | ||
You may contact me here: https://lukasliesis.com/ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9515
97
0
30