@schibsted/niche-utils
Advanced tools
Comparing version 0.1.2 to 0.2.0
export declare function getFromLocalStorage(item: string): string | null | undefined; | ||
export declare function saveToLocalStorage(item: string, value: any): void; | ||
export declare function removeFromLocalStorage(item: string): void; | ||
export declare function getFromLocalStorageWithExpiration(key: string, expiration: number): any; | ||
export declare function saveToLocalStorageWithExpiration(key: string, value: any): void; |
@@ -25,1 +25,27 @@ export function getFromLocalStorage(item) { | ||
} | ||
export function getFromLocalStorageWithExpiration(key, expiration) { | ||
try { | ||
const data = JSON.parse(localStorage.getItem(key) || ''); | ||
if ((data === null || data === void 0 ? void 0 : data.timestamp) && data.timestamp + expiration > new Date().getTime()) { | ||
return data.data; | ||
} | ||
} | ||
catch (e) { | ||
// ignore | ||
} | ||
return undefined; | ||
} | ||
export function saveToLocalStorageWithExpiration(key, value) { | ||
try { | ||
if (value) { | ||
const data = { | ||
data: value, | ||
timestamp: new Date().getTime(), | ||
}; | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
} | ||
} | ||
catch (e) { | ||
// ignore | ||
} | ||
} |
{ | ||
"name": "@schibsted/niche-utils", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"license": "Apache-2.0", | ||
@@ -22,3 +22,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "9facbdde0c84693cfe066edab199fbe062b49461" | ||
"gitHead": "cac17de15fd80ae74eb553d7a29ddb8f819b7963" | ||
} |
@@ -24,1 +24,29 @@ export function getFromLocalStorage(item: string) { | ||
} | ||
export function getFromLocalStorageWithExpiration(key: string, expiration: number) { | ||
try { | ||
const data = JSON.parse(localStorage.getItem(key) || ''); | ||
if (data?.timestamp && data.timestamp + expiration > new Date().getTime()) { | ||
return data.data; | ||
} | ||
} catch (e) { | ||
// ignore | ||
} | ||
return undefined; | ||
} | ||
export function saveToLocalStorageWithExpiration(key: string, value: any) { | ||
try { | ||
if (value) { | ||
const data = { | ||
data: value, | ||
timestamp: new Date().getTime(), | ||
}; | ||
localStorage.setItem(key, JSON.stringify(data)); | ||
} | ||
} catch (e) { | ||
// ignore | ||
} | ||
} |
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
5994
166