storage-sync-lite
Advanced tools
@@ -1,11 +0,75 @@ | ||
declare function getLocal(name: string): any; | ||
declare function setLocal(name: string, value: any): any; | ||
declare function deleteLocal(name: string): void; | ||
declare function changeLocal(name: string, value: any): void; | ||
declare function clearLocal(): void; | ||
declare function getSession(name: string): any; | ||
declare function setSession(name: string, value: any): void; | ||
declare function changeSession(name: string, value: any): void; | ||
declare function deleteSession(name: string): void; | ||
declare function clearSession(): void; | ||
/** | ||
* Retrieves a value from local storage. | ||
* @param {string} name - The name of the item to retrieve. | ||
* @returns {*} The value stored in local storage, or undefined if the item does not exist or has expired. | ||
*/ | ||
declare const getLocal: (name: string) => any; | ||
/** | ||
* Stores a value in local storage. | ||
* @param {string} name - The name of the item to store. | ||
* @param {*} value - The value to store. | ||
* @param {object} options - Optional configuration object. | ||
* @param {string | number | Date} options.expiration - The expiration date/time for the item. | ||
* @param {number} options.timeToLive - The time to live in milliseconds. | ||
* @returns {*} The stored value. | ||
*/ | ||
declare const setLocal: (name: string, value: any, { expiration, timeToLive }?: { | ||
expiration?: string | number | Date | undefined; | ||
timeToLive?: number | undefined; | ||
}) => any; | ||
/** | ||
* Deletes an item from local storage. | ||
* @param {string} name - The name of the item to delete. | ||
*/ | ||
declare const deleteLocal: (name: string) => void; | ||
/** | ||
* Updates a value in local storage with the provided changes. | ||
* @param {string} name - The name of the item to update. | ||
* @param {object} changes - An object containing the changes to apply to the stored value. | ||
* @returns {*} The updated value. | ||
*/ | ||
declare const changeLocal: (name: string, changes: { | ||
[key: string]: any; | ||
}) => any; | ||
/** | ||
* Clears all items from local storage. | ||
*/ | ||
declare const clearLocal: () => void; | ||
/** | ||
* Retrieves a value from session storage. | ||
* @param {string} name - The name of the item to retrieve. | ||
* @returns {*} The value stored in session storage, or undefined if the item does not exist or has expired. | ||
*/ | ||
declare const getSession: (name: string) => any; | ||
/** | ||
* Stores a value in session storage. | ||
* @param {string} name - The name of the item to store. | ||
* @param {*} value - The value to store. | ||
* @param {object} options - Optional configuration object. | ||
* @param {string | number | Date} options.expiration - The expiration date/time for the item. | ||
* @param {number} options.timeToLive - The time to live in milliseconds. | ||
* @returns {*} The stored value. | ||
*/ | ||
declare const setSession: (name: string, value: any, { expiration, timeToLive }?: { | ||
expiration?: string | number | Date | undefined; | ||
timeToLive?: number | undefined; | ||
}) => any; | ||
/** | ||
* Updates a value in session storage with the provided changes. | ||
* @param {string} name - The name of the item to update. | ||
* @param {object} changes - An object containing the changes to apply to the stored value. | ||
* @returns {*} The updated value. | ||
*/ | ||
declare const changeSession: (name: string, changes: { | ||
[key: string]: any; | ||
}) => any; | ||
/** | ||
* Deletes an item from session storage. | ||
* @param {string} name - The name of the item to delete. | ||
*/ | ||
declare const deleteSession: (name: string) => void; | ||
/** | ||
* Clears all items from session storage. | ||
*/ | ||
declare const clearSession: () => void; | ||
export { getLocal, setLocal, deleteLocal, changeLocal, clearLocal, getSession, setSession, changeSession, deleteSession, clearSession }; |
131
lib/index.js
@@ -1,34 +0,109 @@ | ||
// Local | ||
function getLocal(name) { | ||
return JSON.parse(localStorage[name] || '[]')[0]; | ||
} | ||
function setLocal(name, value) { | ||
localStorage[name] = JSON.stringify([value]); | ||
/** | ||
* Retrieves a value from local storage. | ||
* @param {string} name - The name of the item to retrieve. | ||
* @returns {*} The value stored in local storage, or undefined if the item does not exist or has expired. | ||
*/ | ||
const getLocal = (name) => { | ||
const [value, expiration] = JSON.parse(localStorage[name] || '[]'); | ||
if (expiration && expiration < Date.now()) | ||
deleteLocal(name); | ||
else | ||
return value; | ||
}; | ||
/** | ||
* Stores a value in local storage. | ||
* @param {string} name - The name of the item to store. | ||
* @param {*} value - The value to store. | ||
* @param {object} options - Optional configuration object. | ||
* @param {string | number | Date} options.expiration - The expiration date/time for the item. | ||
* @param {number} options.timeToLive - The time to live in milliseconds. | ||
* @returns {*} The stored value. | ||
*/ | ||
const setLocal = (name, value, { expiration, timeToLive } = {}) => { | ||
if (timeToLive) | ||
expiration = Date.now() + timeToLive; | ||
localStorage[name] = JSON.stringify(expiration ? [value, expiration] : [value]); | ||
return value; | ||
} | ||
function deleteLocal(name) { | ||
}; | ||
/** | ||
* Deletes an item from local storage. | ||
* @param {string} name - The name of the item to delete. | ||
*/ | ||
const deleteLocal = (name) => { | ||
localStorage.removeItem(name); | ||
} | ||
function changeLocal(name, value) { | ||
localStorage[name] = JSON.stringify([Object.assign(getLocal(name) || {}, value)]); | ||
} | ||
function clearLocal() { | ||
}; | ||
/** | ||
* Updates a value in local storage with the provided changes. | ||
* @param {string} name - The name of the item to update. | ||
* @param {object} changes - An object containing the changes to apply to the stored value. | ||
* @returns {*} The updated value. | ||
*/ | ||
const changeLocal = (name, changes) => { | ||
const [value, expiration] = JSON.parse(localStorage[name] || '[]'); | ||
if (value) { | ||
Object.assign(value, changes); | ||
localStorage[name] = JSON.stringify(expiration ? [value, expiration] : [value]); | ||
return value; | ||
} | ||
}; | ||
/** | ||
* Clears all items from local storage. | ||
*/ | ||
const clearLocal = () => { | ||
localStorage.clear(); | ||
} | ||
// Session | ||
function getSession(name) { | ||
return JSON.parse(sessionStorage[name] || '[]')[0]; | ||
} | ||
function setSession(name, value) { | ||
sessionStorage[name] = JSON.stringify([value]); | ||
} | ||
function changeSession(name, value) { | ||
sessionStorage[name] = JSON.stringify([Object.assign(getSession(name) || {}, value)]); | ||
} | ||
function deleteSession(name) { | ||
}; | ||
/** | ||
* Retrieves a value from session storage. | ||
* @param {string} name - The name of the item to retrieve. | ||
* @returns {*} The value stored in session storage, or undefined if the item does not exist or has expired. | ||
*/ | ||
const getSession = (name) => { | ||
const [value, expiration] = JSON.parse(sessionStorage[name] || '[]'); | ||
if (expiration && expiration < Date.now()) | ||
deleteSession(name); | ||
else | ||
return value; | ||
}; | ||
/** | ||
* Stores a value in session storage. | ||
* @param {string} name - The name of the item to store. | ||
* @param {*} value - The value to store. | ||
* @param {object} options - Optional configuration object. | ||
* @param {string | number | Date} options.expiration - The expiration date/time for the item. | ||
* @param {number} options.timeToLive - The time to live in milliseconds. | ||
* @returns {*} The stored value. | ||
*/ | ||
const setSession = (name, value, { expiration, timeToLive } = {}) => { | ||
if (timeToLive) | ||
expiration = Date.now() + timeToLive; | ||
sessionStorage[name] = JSON.stringify(expiration ? [value, expiration] : [value]); | ||
return value; | ||
}; | ||
/** | ||
* Updates a value in session storage with the provided changes. | ||
* @param {string} name - The name of the item to update. | ||
* @param {object} changes - An object containing the changes to apply to the stored value. | ||
* @returns {*} The updated value. | ||
*/ | ||
const changeSession = (name, changes) => { | ||
const [value, expiration] = JSON.parse(sessionStorage[name] || '[]'); | ||
if (value) { | ||
Object.assign(value, changes); | ||
sessionStorage[name] = JSON.stringify(expiration ? [value, expiration] : [value]); | ||
return value; | ||
} | ||
}; | ||
/** | ||
* Deletes an item from session storage. | ||
* @param {string} name - The name of the item to delete. | ||
*/ | ||
const deleteSession = (name) => { | ||
sessionStorage.removeItem(name); | ||
} | ||
function clearSession() { | ||
}; | ||
/** | ||
* Clears all items from session storage. | ||
*/ | ||
const clearSession = () => { | ||
sessionStorage.clear(); | ||
} | ||
}; | ||
export { getLocal, setLocal, deleteLocal, changeLocal, clearLocal, getSession, setSession, changeSession, deleteSession, clearSession }; |
{ | ||
"name": "storage-sync-lite", | ||
"version": "1.0.19", | ||
"version": "1.1.19", | ||
"description": "Easily store objects or any type of data to localStorage or sessionStorage.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -17,3 +17,4 @@ storage-sync-lite<br> | ||
- [Usage](#usage) | ||
* [Contributing](#contributing) | ||
- [API Reference](#API-Reference) | ||
- [Contributing](#contributing) | ||
- [License](#license) | ||
@@ -114,3 +115,83 @@ - [Author](#author) | ||
## API Reference | ||
### `setLocal(name, value, options)` | ||
Stores a value in local storage. | ||
- `name` (string): The name of the item to store. | ||
- `value` (*): The value to store. | ||
- `options` (object, optional): Optional configuration object. | ||
- `options.expiration` (string | number | Date): The expiration date/time for the item. | ||
- `options.timeToLive` (number): The time to live in milliseconds. | ||
Returns the stored value. | ||
### `getLocal(name)` | ||
Retrieves a value from local storage. | ||
- `name` (string): The name of the item to retrieve. | ||
Returns the value stored in local storage, or undefined if the item does not exist or has expired. | ||
### `changeLocal(name, changes)` | ||
Updates a value in local storage with the provided changes. | ||
- `name` (string): The name of the item to update. | ||
- `changes` (object): An object containing the changes to apply to the stored value. | ||
Returns the updated value. | ||
### `deleteLocal(name)` | ||
Deletes an item from local storage. | ||
- `name` (string): The name of the item to delete. | ||
### `clearLocal()` | ||
Clears all items from local storage. | ||
### `setSession(name, value, options)` | ||
Stores a value in session storage. | ||
- `name` (string): The name of the item to store. | ||
- `value` (*): The value to store. | ||
- `options` (object, optional): Optional configuration object. | ||
- `options.expiration` (string | number | Date): The expiration date/time for the item. | ||
- `options.timeToLive` (number): The time to live in milliseconds. | ||
Returns the stored value. | ||
### `getSession(name)` | ||
Retrieves a value from session storage. | ||
- `name` (string): The name of the item to retrieve. | ||
Returns the value stored in session storage, or undefined if the item does not exist or has expired. | ||
### `changeSession(name, changes)` | ||
Updates a value in session storage with the provided changes. | ||
- `name` (string): The name of the item to update. | ||
- `changes` (object): An object containing the changes to apply to the stored value. | ||
Returns the updated value. | ||
### `deleteSession(name)` | ||
Deletes an item from session storage. | ||
- `name` (string): The name of the item to delete. | ||
### `clearSession()` | ||
Clears all items from session storage. | ||
## Contributing | ||
@@ -117,0 +198,0 @@ |
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
15082
105.23%184
308.89%210
62.79%1
Infinity%