localstorage-ttl
Advanced tools
Comparing version 2.0.9 to 2.0.10
function set(variable, value, ttl_ms) { | ||
let data = { | ||
value: value, | ||
}; | ||
if (ttl_ms) { | ||
data.expires_at = Date.now() + ttl_ms; | ||
if (localStorage) { | ||
let data = { | ||
value: value, | ||
}; | ||
if (ttl_ms) { | ||
data.expires_at = Date.now() + ttl_ms; | ||
} | ||
localStorage.setItem(variable, JSON.stringify(data)); | ||
} | ||
localStorage.setItem(variable, JSON.stringify(data)); | ||
} | ||
function remove(variable) { | ||
localStorage.removeItem(variable); | ||
if (localStorage) { | ||
localStorage.removeItem(variable); | ||
} | ||
} | ||
function get(variable) { | ||
const item = localStorage.getItem(variable); | ||
if (item) { | ||
try { | ||
const data = JSON.parse(item); | ||
if (data.expires_at && data.expires_at < Date.now()) { | ||
if (localStorage) { | ||
const item = localStorage.getItem(variable); | ||
if (item) { | ||
try { | ||
const data = JSON.parse(item); | ||
if (data.expires_at && data.expires_at < Date.now()) { | ||
localStorage.removeItem(variable); | ||
return null; | ||
} else { | ||
return data.value; | ||
} | ||
} catch (e) { | ||
localStorage.removeItem(variable); | ||
return null; | ||
} else { | ||
return data.value; | ||
} | ||
} catch (e) { | ||
localStorage.removeItem(variable); | ||
} | ||
@@ -27,0 +33,0 @@ } |
{ | ||
"name": "localstorage-ttl", | ||
"version": "2.0.9", | ||
"version": "2.0.10", | ||
"description": "TS module to save data to localstorage with time to live. Data will expire after set time.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
5441
48