This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
201flaviosilva edited this page May 27, 2022
·
1 revision
A simple lib to work and organize the localstorage.
npm install guardar
import Guardar from "guardar";
const myData = {
myString: "Hello World",
myNumber: 123,
myBoolean: true,
myArray: [1, false, "Beep"],
myObject: {
name: "Dog",
age: 3,
isHappy: true,
breed: "Dachshund",
}
}
// To start the localstorage
Guardar.init();
// Save the data
Guardar.setData("myData", myData);
// Get you data
console.log(Guardar.getData("myData"));
Import the lib to your project:
import Guardar from "guardar";
For the Guardar to work correctly it is necessary to boot it, and for that it is done with this command:
- Parameter 1 {string} [newSaveDataName] (optional): The name of the localstorage.
Guardar.init("myLocalStorage");
Get all data saved in the local storage
- Returns {any}: A object with all the data saved in the localstorage.
Guardar.getAll();
Get a specific data saved in the local storage
- Parameter 1 {string} [key]: The key of the data.
- Returns {any}: The data saved in the localstorage of the specified key.
Guardar.getData("myKey");
Save/change a specific data by key
- Parameter 1 {string} [key]: The key of the data.
- Parameter 2 {any} [value]: The data to save.
// Add new data
Guardar.setData("myKeyString", "myString");
Guardar.setData("myKeyBool", true);
Guardar.setData("myKeyNumber", 1234567890);
Guardar.setData("myKeyObject", {name: "Dog", age: 3,});
Guardar.setData("myKeyArr", [1, 2, 3, 4, 5]);
// Update
Guardar.setData("myKeyString", "My String Updated");
Guardar.setData("myKeyBool", false);
Guardar.setData("myKeyNumber", 0987654321);
Guardar.setData("myKeyObject", {name: "Fox", age: 1, isHappy: true});
Guardar.setData("myKeyArr", ["A", "B", "C", "D", "E"]);
Save/change all data saved
- Parameter 1 {any} [data]: The data to save.
const newData = { otherNewData: "Hello World" };
Guardar.updateAll(newData);
Delete a specific data by key
- Parameter 1 {string} [key]: The name of the data.
Guardar.removeData("myKey");
Change the name of the local storage
- Parameter 1 {string} [newSaveDataName]: The new name of the localstorage.
Guardar.changeSaveDataName("myNewLocalStorageName");
Delete all data saved in the local storage
Guardar.clear();
Get keys of the stored items
- Returns {string[]}: An array with all the keys saved in the localstorage.
Guardar.keys();
Get number of stored items
- Returns {number}: Number of stored items.
Guardar.size();
Check if the given key is stored
- Parameter 1 {string} [key]: The key to check.
- Returns {boolean}: True if the key is stored, false if not.
Guardar.has("myKey");
Check if the local storage is empty
- Returns {boolean}: True if the local storage is empty.
Guardar.isEmpty("myKey");
Name | Description | param 1 | param 2 | return |
---|---|---|---|---|
init | Start Guardar | newSaveDataName {string} (optional) | - | - |
getAll | Get all data | - | - | any |
getData | Get the items of a key | key {string} | - | any |
setData | Save/change a key | key {string} | value {any} | - |
updateAll | Save/change all data | data {any} | - | - |
removeData | Delete a key | key {string} | - | - |
changeSaveDataName | Change the name of the localstorage | newSaveDataName {string} | - | |
clear | Delete all data | - | - | - |
keys | Get all keys | - | - | string[] |
size | Get number of items | - | - | number |
has | Check if a key is stored | key {string} | - | boolean |
isEmpty | Check if the local storage is empty | - | - | boolean |