@netilon/local-storage-builder-angular v1.1.1
Local Storage Builder Angular
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-angular --save
For HTML web page (no framework dependency):
See https://github.com/netilon/local-storage-builder
Tests
ng test
Usage with a Code Example
component.ts
import { Component } from '@angular/core';
//import the library service and model.
import {LocalStorageBuilderAngularService, StorageModel} from '@netilon/local-storage-builder-angular'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
//declare a class attribute that will store our custom model.
public storage : StorageModel;
constructor(private localStorageBuilder : LocalStorageBuilderAngularService){
//assign an ID (remember that the ID is used to retrieve and save data
//in localStorage and must be unique).
//create a storage model ( an array of strings properties).
this.storage = this.localStorageBuilder.build("user1", ['name', 'age']);
//When you call the build method, the object is created and the data is
//loaded from localStorage if exists with the id "user1".
//set the properties if doesnt exists in localStorage and that's all.
if(this.storage.getName() === null){
this.storage.setName('Fabian');
this.storage.setAge(34);
}
}
}
component.html
<div style="text-align:center">
<h1>
LocalStorage test page, my name is {{ storage.getName() }} and my age {{ storage.getAge() }}!
</h1>
</div>
module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
//import Netilon LocalStorageBuilderAngularModule.
import {LocalStorageBuilderAngularModule} from '@netilon/local-storage-builder-angular';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
LocalStorageBuilderAngularModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
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.