Storage

These functions have dependency on jQuery



Sometimes web pages are more than just static sources of information; sometimes they want to interact with and know something about their users. In order to do this, websites need to store data locally on the client-side.

There are two simple ways for you to store data: Cookies and Local Storage

Cookies

If you would like to store something less than 4KB of data, then cookies will do the job for you.

Below are the awesome cookie functions for you to store simple data

Simplest way to add a cookie is as follow:

Usage
//Syntax
c.Add(cookiename,value,daysexpiredin );

//Without days expired in
c.Add('codewithmark','I love Awesome Functions' );

//With days expired in
c.Add('codewithmark','I love Awesome Functions',2 );

Note if daysexpiredin is not provided, then it will default to 1 day

Simplest way to get a cookie is as follow:

Usage
//Syntax
c.Get(cookiename);

c.Get('codewithmark' ); //out put ---> I love Awesome Functions

If you want to add more than just a string of data, you can use object array method as follow:

Usage
//syntax
c.AddObjArr(cookiename,ObjArr);

//Method 1
var ObjArr = {test1: 'test1',test2:'test2'};
c.AddObjArr('awesomefunctions1',ObjArr);

//Method 2
var people = 
[
 { 'name' : 'Abel', 'age' : 1 },
 { 'name' : 'Bella', 'age' : 2 },
 { 'name' : 'Chad', 'age' : 3 },
]
c.AddObjArr('awesomefunctions2',people);

Simplest way to get object array a cookie is as follow:

Usage
//Syntax
c.GetObjArr(cookiename);

//Example
c.GetObjArr('awesomefunctions1') 

c.GetObjArr('awesomefunctions2') 

Deleting a cookie is easy

Usage
//Syntax
c.Delete(cookiename);

//Example
c.Delete('codewithmark');

Local Storage

What is Local Storage?

It allows you to add data(string, objects and json arrays) on your client's computer. The data stored in localStorage has no expiration time.

You can open a new tab/window or close the browser completely and come back local storage data will be there.

Note: This only allow you to add up to 5 MB of data

Easiest way to add a storage is as follow:

Usage
 
//Syntax for local storage
ls.Add(localstoragename,value);

 
ls.Add('codewithmark','I love Awesome Functions' ); 

Simplest way to get a storage is as follow:

Usage
//Syntax for local storage
ls.Get(localstoragename);

ls.Get('codewithmark' ); //out put ---> I love Awesome Functions

If you want to add multiple strings, you can use object method to store stuff as follow:

Usage
//Syntax for local storage
ls.AddObj(localstoragename,ObjArr);
 
var ObjArr = {username: 'test1',useremail:'[email protected]', useraccesstype:"level1"};
ls.AddObj('awesomefunctions',ObjArr);

Note: This shoule be used for a single array only with multiple objects. If you to add multiple arrays with objects, use "Add Array" method (see below)

Simplest way to get a storage object is as follow:

Usage
 
//Syntax for local storage
ls.GetObj(localstoragename);

ls.GetObj('awesomefunctions')  

To update your objects

Usage
 
//Syntax for local storage
ls.UpdateObj (localstoragename,FieldObjArrToUpdatValue,WhereObjArr) 

var people =  { 'name' : 'Bella', 'age' : 3 ,'age1' : 1 ,'code':'test' }

//Add data
ls.AddObj('awesomefunctions',people); 

var FieldObjArrToUpdatValue = { 'code':'my name is', 'age1':'mark 007'}

var WhereObjArr = { 'name' : 'Bella', 'age' : 3   }

//Update value
ls.UpdateObj ('awesomefunctions',FieldObjArrToUpdatValue,WhereObjArr) 


//--->New data will be
{ 'name' : 'Bella', 'age' : 3 ,'code':'my name is', 'age1':'mark 007'}

If you want to add multiple object arrays one at time, you can use array method as follow:

Usage
//Syntax for local storage
ls.AddArr(localstoragename,ObjArr);
 
var people = { 'name' : 'Abel', 'age' : 1 };
ls.AddArr('awesomefunctions',people);

var people1 = { 'name' : 'Chad', 'age' : 4 };
ls.AddArr('awesomefunctions',people1); 

This works similar to Add Array the only difference is that this doesn't append to your old data.

When you add an array data with this function, this will delete the old array data first and then add the new array data in the storage.

Usage
//Syntax for local storage
ls.AddTempArr(localstoragename,ObjArr);
 
var people = { 'name' : 'Abel', 'age' : 1 };
ls.AddTempArr('awesomefunctions',people);

var people1 = { 'name' : 'Chad', 'age' : 4 };
ls.AddTempArr('awesomefunctions',people1); 

If you want to add multiple object arrays at once, you can do so as follow:

Usage
//Syntax for local storage
ls.AddBulkArr(localstoragename,Arr);
 
var people = 
[
 { 'name' : 'Abel', 'age' : 1 },
 { 'name' : 'Bella', 'age' : 2 },
 { 'name' : 'Bella', 'age' : 3 },
 { 'name' : 'Chad', 'age' : 4 },
]

ls.AddBulkArr('awesomefunctions',people);

This works similar toAdd Bulk Array the only difference is that this doesn't append to your old data.

When you add an array data with this function, this will delete the old array data first and then add the new array data in the storage.

Usage
//Syntax for local storage
ls.AddTempBulkArr(localstoragename,Arr);
 
var people = 
[
 { 'name' : 'Abel', 'age' : 1 },
 { 'name' : 'Bella', 'age' : 2 },
 { 'name' : 'Bella', 'age' : 3 },
 { 'name' : 'Chad', 'age' : 4 },
]

ls.AddTempBulkArr('awesomefunctions',people);

If you just want to filter for a search criteria (Let's assume you want to get all of object arrays where name equal "Bella" and age equal "2") in your object array, you can do it as follow:

Usage
//Syntax for local storage
ls.GetArr(localstoragename,WhereValueEquals)

var people = 
[
 { 'name' : 'Abel', 'age' : 1 },
 { 'name' : 'Bella', 'age' : 2 },
 { 'name' : 'Bella', 'age' : 3 },
 { 'name' : 'Chad', 'age' : 4 },
]
//Add
ls.AddBulkArr('awesomefunctions',people);


//lookup and get
var WhereValueEquals = {name:'Bella',age:2}

var data = ls.GetArr('awesomefunctions',WhereValueEquals)
 
console.log(data)

//Out put will be:
{ 'name' : 'Bella', 'age' : 2 }
   

If you just want to get all of the object arrays, you can do it as follow:

Usage
//Syntax for local storage
ls.GetAllArr(localstoragename);


//Add array objects
var people = 
[
 { 'name' : 'Abel', 'age' : 1 },
 { 'name' : 'Bella', 'age' : 2 },
 { 'name' : 'Bella', 'age' : 3 },
 { 'name' : 'Chad', 'age' : 4 },
]

ls.AddBulkArr('awesomefunctions',people);


//Get all of them back
var d = ls.GetAllArr('awesomefunctions');

console.log(d);
//outputput will be:
[
 { 'name' : 'Abel', 'age' : 1 },
 { 'name' : 'Bella', 'age' : 2 },
 { 'name' : 'Bella', 'age' : 3 },
 { 'name' : 'Chad', 'age' : 4 },
]  
   

If you just want to get total number of object arrays, you can do it as follow:

Usage
//Syntax for local storage
ls.CountArr(localstoragename)

var people = 
[
 { 'name' : 'Abel', 'age' : 1 },
 { 'name' : 'Bella', 'age' : 2 },
 { 'name' : 'Bella', 'age' : 3 },
 { 'name' : 'Chad', 'age' : 4 },
]

ls.AddBulkArr('awesomefunctions',people);

ls.CountArr('awesomefunctions') //--->Out put will be 4

To update your object array:

Example 1
//Syntax for local storage
ls.UpdateArr (localstoragename,FieldObjArrToUpdatValue,WhereObjArr) 

var people = 
[
 { name : 'Abel', age : 1 },
 { name : 'Bella', age : 2 },
 { name : 'Bella', age : 3 },
 { name : 'Chad', age : 4 },
]

//Add data
ls.AddBulkArr('awesomefunctions',people); 

var FieldObjArrToUpdatValue = { 'age' : 'NewBella'}

var WhereObjArr = { 'name' : 'Bella'}

//Update value
ls.UpdateArr ('awesomefunctions',FieldObjArrToUpdatValue,WhereObjArr) 


//--->New data will be
[
 { name : 'Abel',  age : 1 },
 { name : 'Bella', age : 'NewBella' },
 { name : 'Bella', age : 'NewBella' },
 { name : 'Chad',  age : 4 },
]
Example 2
//Syntax for local storage 
ls.UpdateArr (localstoragename,FieldObjArrToUpdatValue,WhereObjArr) 

var people = 
[
 { name : 'Abel',  age : 1, food: "Pizza", level : 1 },
 { name : 'Bella', age : 2, food: "Chocolate", level : 2 },
 { name : 'Bella', age : 2, food: "Chocolate", level : 22 },
 { name : 'Bella', age : 3, food: "Ice Cream", level : 3 },
 { name : 'Chad',  age : 4, food: "Bacon", level : 4 },
]

//Add data
ls.AddBulkArr('awesomefunctions',people);


var UpdatValue = { age : 'NewBella'}

var Where = { name : 'Bella', food : 'Chocolate',  level : 22}

//Update value
ls.UpdateArr ('awesomefunctions',UpdatValue,Where) 


//--->New data will be
[
 { name : 'Abel',  age : 1, food: "Pizza", level : 1 },
 { name : 'Bella', age : 2, food: "Chocolate", level : 2 },
 { name : 'Bella', age : 'NewBella', food: "Chocolate", level : 22 },
 { name : 'Bella', age : 3, food: "Ice Cream", level : 3 },
 { name : 'Chad',  age : 4, food: "Bacon", level : 4 },
]
Example 3
//Syntax for local storage
ls.UpdateArr (localstoragename,FieldObjArrToUpdatValue,WhereObjArr) 

var people = 
[
 { name : 'Abel',  age : 1, food: "Pizza", level : 1 },
 { name : 'Bella', age : 2, food: "Chocolate", level : 2 },
 { name : 'Bella', age : 2, food: "Chocolate", level : 22 },
 { name : 'Bella', age : 3, food: "Ice Cream", level : 3 },
 { name : 'Chad',  age : 4, food: "Bacon", level : 4 },
]

//Add data
ls.AddBulkArr('awesomefunctions',people);


var UpdatValue = { age : 'NewBella',food : 'Chickfila',level : 0}

var Where = { name : 'Bella' }

//Update value
ls.UpdateArr ('awesomefunctions',UpdatValue,Where) 


//--->New data will be
[
 { name : 'Abel',  age : 1, food: "Pizza", level : 1 },
 { name : 'Bella', age : 'NewBella',food : 'Chickfila',level : 0 },
 { name : 'Bella', age : 'NewBella',food : 'Chickfila',level : 0 },
 { name : 'Bella', age : 'NewBella',food : 'Chickfila',level : 0 },
 { name : 'Chad',  age : 4, food: "Bacon", level : 4 },
]

To delete your object array(s):

Usage
//Syntax for local storage
ls.DeleteArr (localstoragename,WhereValueEquals)

var people = 
[
 { 'name' : 'Abel', 'age' : 1 },
 { 'name' : 'Bella', 'age' : 2 },
 { 'name' : 'Bella', 'age' : 2 },
 { 'name' : 'Chad', 'age' : 4 },
]

//Add data
ls.AddBulkArr('awesomefunctions',people);
 

//Delete arrays 
var WhereValueEquals = {name:'Bella',age:2}

var data = ls.DeleteArr ('awesomefunctions',WhereValueEquals)
 
 
//Get new data
var data = ls.GetAllArr('awesomefunctions');

console.log(data)

//--->New data will be
[
 { 'name' : 'Abel', 'age' : 1 },
 { 'name' : 'Chad', 'age' : 4 },
]

Check to see if your storage exist

Usage
//Syntax for local storage 
ls.Exist (localstoragename) 

ls.Exist ('awesomefunctions')

//--->return True if it exist...else false

You can easily Empty your storage

Usage
//Syntax for local storage
ls.Empty (localstoragename) 

ls.Empty ('awesomefunctions') 

//--->No out put will return 

If you would completely like to delete your storage, you can easily do like this:

Usage
 //Syntax for local storage
ls.Delete(localstoragename) 

ls.Delete('awesomefunctions') 

//--->No out put will return 

If you would delete all your storages, you can easily do like this:

Usage
//Syntax for local storage
ls.DeleteAll() 

//--->No out put will return