localstorage-ttl
Advanced tools
Comparing version 2.0.8 to 2.0.9
@@ -5,7 +5,7 @@ declare function set<T>(variable: string, value: T, ttl_ms?: number): void; | ||
export declare const ls: { | ||
set: typeof set; | ||
get: typeof get; | ||
remove: typeof remove; | ||
set: typeof set; | ||
get: typeof get; | ||
remove: typeof remove; | ||
}; | ||
export default ls; | ||
//# sourceMappingURL=index.d.ts.map | ||
//# sourceMappingURL=index.d.ts.map |
function set(variable, value, ttl_ms) { | ||
let data = { | ||
value: value, | ||
}; | ||
if (ttl_ms) { | ||
data.expires_at = Date.now() + ttl_ms; | ||
} | ||
localStorage.setItem(variable, JSON.stringify(data)); | ||
let data = { | ||
value: value, | ||
}; | ||
if (ttl_ms) { | ||
data.expires_at = Date.now() + ttl_ms; | ||
} | ||
localStorage.setItem(variable, JSON.stringify(data)); | ||
} | ||
function remove(variable) { | ||
localStorage.removeItem(variable); | ||
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()) { | ||
localStorage.removeItem(variable); | ||
return null; | ||
} | ||
else { | ||
return data.value; | ||
} | ||
} | ||
catch (e) { | ||
localStorage.removeItem(variable); | ||
} | ||
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; | ||
} | ||
return null; | ||
} | ||
export const ls = { set, get, remove }; | ||
export default ls; | ||
//# sourceMappingURL=index.js.map | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "localstorage-ttl", | ||
"version": "2.0.8", | ||
"version": "2.0.9", | ||
"description": "TS module to save data to localstorage with time to live. Data will expire after set time.", | ||
@@ -8,3 +8,5 @@ "main": "dist/index.js", | ||
"build": "tsc", | ||
"prepublishOnly": "npm run build" | ||
"check-exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm", | ||
"format": "prettier --write .", | ||
"prepublishOnly": "npm run build && npm run format" | ||
}, | ||
@@ -27,2 +29,4 @@ "repository": { | ||
"devDependencies": { | ||
"@arethetypeswrong/cli": "^0.16.4", | ||
"prettier": "^3.3.3", | ||
"typescript": "^5.6.2" | ||
@@ -29,0 +33,0 @@ }, |
@@ -6,3 +6,3 @@ # localstorage-ttl | ||
Updated to TS (v2) after 9yr of JS (v1) 🎉 | ||
Updated to TS (v2) after 9yr of JS (v1) 🎉 | ||
@@ -25,7 +25,6 @@ # Install: | ||
* ttl_ms = time to live time in ms. | ||
- ttl_ms = time to live time in ms. | ||
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
5205
3
29