koloocheh
v4.0.5
Published
**Koloocheh**: A lightweight JavaScript library designed to simplify the management of cookies, localStorage, sessionStorage, and IndexedDB on the frontend. With its easy-to-use API, Koloocheh allows developers to effortlessly handle data storage and re
Downloads
30
Maintainers
Readme
Koloocheh
A simple JavaScript package for managing cookies, localStorage, sessionStorage, and IndexedDB on the frontend. This package provides functions for cookie management (setCookie
, getCookie
, deleteCookie
, clearCookie
), localStorage management (setLocal
, getLocal
, removeLocal
, clearLocal
), sessionStorage management (setSession
, getSession
, removeSession
, clearSession
), and IndexedDB management (IndexedDBManager
).
Installation
You can install the package using npm:
npm install koloocheh
Cookie Functions
setCookie(name, value, days, path = '/', secure = false, sameSite = false)
Sets a cookie with the specified parameters. If days
is set to 0
, the cookie will not have an expiration date.
- name: The name of the cookie (String).
- value: The value of the cookie (String).
- days: The number of days until the cookie expires (Number). Use
0
to create a session cookie with no expiration. - path: The path where the cookie is accessible (String, optional, default:
/
). - secure: Indicates if the cookie should only be transmitted over secure protocols (Boolean, optional, default:
false
). - sameSite: Indicates if the cookie should be restricted to first-party or same-site contexts (Boolean, optional, default:
false
).
Example:
setCookie('username', 'JohnDoe', 7, '/', true, true); // Expires in 7 days
setCookie('session_id', '123456', 0); // Session cookie with no expiration
getCookie(name)
Retrieves the value of the specified cookie.
- name: The name of the cookie to retrieve (String).
Returns: The value of the cookie or null
if the cookie does not exist.
Example:
const username = getCookie('username');
console.log(username); // Outputs: JohnDoe
deleteCookie(name, path = '/')
Deletes the specified cookie.
- name: The name of the cookie to delete (String).
- path: The path where the cookie is accessible (String, optional, default:
/
).
Example:
deleteCookie('username', '/');
clearCookie()
Deletes all cookies.
Example:
clearCookie();
localStorage Functions
setLocal(key, value)
Stores a value in localStorage under the specified key.
- key: The key to store the value under (String).
- value: The value to store (Any type, will be stringified).
Example:
setLocal('userData', { name: 'John', age: 30 });
getLocal(key)
Retrieves the value stored in localStorage under the specified key.
- key: The key of the value to retrieve (String).
Returns: The parsed value from localStorage or null
if the key does not exist.
Example:
const userData = getLocal('userData');
console.log(userData); // Outputs: { name: 'John', age: 30 }
removeLocal(key)
Removes the specified key from localStorage.
- key: The key to remove (String).
Example:
removeLocal('userData');
clearLocal()
Clears all values from localStorage.
Example:
clearLocal();
sessionStorage Functions
setSession(key, value)
Stores a value in sessionStorage under the specified key.
- key: The key to store the value under (String).
- value: The value to store (Any type, will be stringified).
Example:
setSession('sessionData', { token: 'abc123' });
getSession(key)
Retrieves the value stored in sessionStorage under the specified key.
- key: The key of the value to retrieve (String).
Returns: The parsed value from sessionStorage or null
if the key does not exist.
Example:
const sessionData = getSession('sessionData');
console.log(sessionData); // Outputs: { token: 'abc123' }
removeSession(key)
Removes the specified key from sessionStorage.
- key: The key to remove (String).
Example:
removeSession('sessionData');
clearSession()
Clears all values from sessionStorage.
Example:
clearSession();
IndexedDB Manager
IndexedDBManager
A class for managing IndexedDB databases.
Constructor
const dbManager = new IndexedDBManager(dbName, dbVersion = 1, indexes);
- dbName: The name of the database (String).
- dbVersion: The version of the database (Number, optional, default:
1
). - indexes: An array of index objects. Each object should have a
key
(String) andkeyPath
(String), and an optionalunique
property (Boolean).
Methods
- openDatabase(): Opens the database. Returns a promise that resolves with the database instance.
- addData(data): Adds or updates data in the database. Returns a promise that resolves when the data is added or updated.
- getData(index, key): Retrieves data from the database using an index. Returns a promise that resolves with the retrieved data.
- updateData(index, key, newValue): Updates existing data in the database. Returns a promise that resolves when the data is updated.
- deleteData(index, key): Deletes data from the database. Returns a promise that resolves when the data is deleted.
- deleteDataBase(): Deletes the database. Returns a promise that resolves when the database is deleted.
Example:
const dbManager = new IndexedDBManager('myDatabase', 1, [
{ key: 'name', keyPath: 'name', unique: false },
{ key: 'age', keyPath: 'age', unique: false }
]);
dbManager.addData({ name: 'John', age: 30 }).then(console.log).catch(console.error);
Contributing
If you want to contribute to this project, please fork the repository and create a pull request.