@netilon/local-storage-builder v2.1.1
Local Storage Builder
Synopsis
Generate an object that handle custom localStorage data in an incredibly fast, simple and safe way with Setters, Getters and much more methods.
Your contribution is appreciated (thanks!)
Installation
npm install @netilon/local-storage-builder --save
or download code from:
https://github.com/netilon/local-storage-builder
For HTML web page (no framework dependency):
include the following js file in your web page:
./node_modules/@netilon/local-storage-builder/dist/local-storage-builder.min.js
For AngularJS 2.x or higher:
See our specific module here: https://github.com/netilon/local-storage-builder-angular
Tests
just open /jasmine/SpecRunner.html in your browser
Usage with a Code Example
//remember include ./node_modules/@netilon/local-storage-builder/dist/local-storage-builder.min.js in your HTML.
//storage model, is an array thath define the properties of the final
//generated object.
var personModel = ["name", "age"];
//the builder creates an Object based on the storage model (array of properties)
//"personModel" in this example.
var personStorage = localStorageBuilder().build("MyCustomId", personModel);
//When you call the build method, the object is created and the data is
//loaded from localStorage if exists with the id "MyCustomId".
//now we can do the following actions.
//the SET method, persist the data in localStorage.
personStorage.setName("Fabian");
personStorage.setAge(34);
//we can GET the saved data simply with a GET method
console.log(personStorage.getName(), personStorage.getAge());
//if we need to delete (set to null) specific property data, we can call DELETE methods.
personStorage.deleteName();
personStorage.deleteAge();
//in the other hand, if you want to clear the whole object data, you can
//use the CLEAR method
personStorage.clear();
Observations
- When a property isn't set (it doesn't exist in localstorage), the get method will return null.
- When you pass a storage model (Array of string properties) as a parameter of the build method, the name of each element that contains characters like -_. , will be replaced by an empty string and the next character will be converted to upper case.
Builder Methods
Method: build(string , array);
Description: Return an Custom Storage Model Methods (see bellow).
Params: The first param, is an string that is used as ID for save and retrieve data from localStorage (if you create more than one objects with build() method, you should use different ID's). The second param, is an array of properties (strings), that will be used to generate setters, getters, etc.
Custom Storage Model Methods (Object that build() method returns)
Method: getId();
Description: Return the Object ID (used for localStorage operations).
Method: getKeys();
Description: Return the Object properties as an array.
Method: set{PropertyName}
Description: Save data to localStorage.
Method: get{PropertyName}
Description: Get data from memory.
Method: delete{PropertyName}
Description: Set property to null and save data to localStorage.
Method: clear
Description: delete all the current object data in localStorage.
* "PropertyName" is the name of each property passed as array as second parameter of build() method.
Examples:
name | SET | GET | DELETE |
---|---|---|---|
age | setAge | getAge | deleteAge |
my.name | setMyName | getMyName | deleteMyName |
my-name | setMyName | getMyName | deleteMyName |
my_name | setMyName | getMyName | deleteMyName |
1st.place | set1stPlace | get1stPlace | delete1stPlace |
License
Copyright 2018 Netilon (Fabian Orue) http://netilon.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.