1.0.7 • Published 8 years ago

locstor v1.0.7

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

##locstor.js - A localStorage Helper Library

Supports all modern browsers and IE6+

Contents

  1. Why locstor.js?
  2. API
  3. Running Unit Tests
  4. How to Contribute
  5. Questions / Comments

Why locstor.js?

Let's face it, HTML5 localStorage is awesome. In an era of increasingly complex web applications, localStorage allows developers to persistently store data with ease. Locstor.js extends upon localStorage by providing:

  • Automatic Type Conversions

    	Let's store a number in localStorage.
    	```javascript
    	// add number to localStorage
    	localStorage['year'] = 1984;
    	```
    	When we try to retrieve that number, we instead get back a string.
    	```javascript
    	var num = localStorage['year'];
    	console.log(typeof num); // outputs "string"
    	```
    	We can use locstor.js to avoid this unexpected behavior.
    	```javascript
    	var num = Locstor.get('year');
    	console.log(typeof num); // outputs "number"
    	```
    	Now that we retrieved our value back in the proper type, we can perform numeric 	operations on it without converting it or parsing it or worrying about type 		conversion errors. It's not just numbers: booleans, objects, and arrays are supported too.
  • Easier Storage and Access

    	I know what you're thinking, how is it possible for localStorage to be even easier? All you do is pass a value to a key. One problem to think about is when you store objects. 
Let's take our object here...
```javascript
var person = {
	name: 'Justin',
	location: 'California',
	eyeColor: 'brown'
};
```
…and store it.
```javascript
localStorage['me'] = person;
```
If we tried to access a property of that object in localStorage, we'd have to 		retrieve it and parse it first.
```javascript
var name = localStorage['me'].name; // doesn't work
```
However, if we used locstor.js, we wouldn't need to worry about doing those steps.
```javascript
Locstor.store(person);

var name = Locstor.get('name'); // this works
```
And if you wanted to store the object without separating the properties, we can do 	that too!
```javascript
Locstor.set('me', person);
Locstor.get('me'); // returns an object
```
  • Older Browser Support

    	Older browsers do not natively support HTML5 Local Storage. Locstor.js automatically checks if localStorage is compatible with your current browser. If it detects localStorage is incompatible, the library falls back to cookies. But don't worry, **even with older browsers, locstar.js works exactly the same.**	
    
    	`Note: If you need IE < 8 support, you need to include Douglas Crockford's` [json2.js](https://github.com/douglascrockford/JSON-js)
    
    	####*There's a lot more features than that. Check out the API to read about the rest!*

API

Below you can find the public methods available for use in locstor.js. Please note that this library is a defensive one. Illegal parameters will cause the method to fail instead of attempting to continue and execute. This is by design to avoid unexpected behavior when using localStorage.

###Installation ####Include this line in your .html file

<script src='locstor.min.js'></script>

####Or via bower bower install locstor

###Public Methods

clear()
Removes all key / value pairs from localStorage

Locstor.clear();

contains( string key )
Returns true if localStorage contains the specified key

Locstor.set('name', 'John Smith');
Locstor.contains('name');	// returns true
Locstor.contains('age');	// returns false

get( string key )
Returns the proper-type value of a specified key in localStorage

var person = {
	name: 'Adam',
	age: 28,
	married: true,
	children: ['Frankie', 'Sarah'];
	pet: {name: 'Spike', species: 'Dog'};
};

Locstor.store(person);

Locstor.get('name');		// returns a string
Locstor.get('age');			// returns a number
Locstor.get('married');		// returns a boolean
Locstor.get('children');	// returns an array
Locstor.get('pet');			// returns an object
Locstor.get('occupation');	// returns null because key is not set

get( string key, type defaultValue )
Similar to the get( string key ) method except returns a default value if the value retrieved is null

Locstor.set('notificationsEnabled', true);		// set value of notificationsEnabled to true
Locstor.get('notificationsEnabled', false);		// returns true because key was previously set to true
Locstor.get('locationServicesEnabled', false);	// returns false (the default value) because key was not previously set

getKeys()
Returns an array of keys currently stored in localStorage. The order of the keys is not guaranteed.

Locstor.set('width', 400);
Locstor.set('height', 300);
Locstor.set('color', 'red');

Locstor.getKeys();	// returns ['width', 'height', 'color']

getRemainingSpace()
Returns an approximation of how much space is left in localStorage

Locstor.getRemainingSpace();	// returns a number

getSize()
Returns the size of the total contents in localStorage.

Locstor.getSize();	// returns a number

isEmpty()
Returns true if localStorage has no key/value pairs

Locstor.set('username', 'locstor_monstor');
Locstor.isEmpty();	// returns false

Locstor.clear();
Locstor.isEmpty();	// returns true 

remove( string key )
Removes the specified key/value pair from localStorage given a key

Locstor.set('distance', 10);
Locstor.remove('distance');
Locstor.contains('distance');	// returns false

remove( array[] )
Removes key/value pairs with keys specified in an array

Locstor.set('city', 'New York City');
Locstor.set('state', New York);
Locstor.set('country', 'United States');

Locstor.remove(['city', 'state', 'country']);

set( string key, type value )
Sets the specified key/value pair (allows string, number, boolean, object, array)

Locstor.set('weapon', 'longsword');								// Sets a string
Locstor.set('health', 99);										// Sets a number
Locstor.set('maxLevel', true);									// Sets a boolean
Locstor.set('armor', {metal: 'steel', defense: 99});			// Sets an object
Locstor.set('items', ['Health Potion', 'Wand', 'Lobster']);		// Sets an array

set( object o )
Alias function for method store(object o)

See method below.

store( object o )
Stores a given object in localStorage, allowing access to individual object properties

var person = {
	name: 'Abraham Lincoln',
	occupation: 'President',
	height: 6
};

Locstor.store(person);

Locstor.get('name');		// returns 'Abraham Lincoln'
Locstor.get('occupation');	// returns 'President'
Locstor.get('height');		// returns 6

toObject()
Returns an object representation of the current state of localStorage

Locstor.set('name', 'Justin');
Locstor.set('eyeColor', 'brown');
Locstor.set('gender', 'male');

Locstor.toObject(); // returns {name: 'Justin', eyeColor: 'brown', gender: 'male'}

Running Unit Tests

This library uses QUnit to perform unit tests. Each method from the API has two tests, one to make sure it executes properly and the other to make it fail. The two tests of each method are in the tests.js file, each respectively under the Public Method (Execution) module or the Public Method (Error Checking) module.

To run the tests, open tests/index.html in a browser.

Contributing

Open source software is important, which is why I decided to release this library under the MIT license. If you use this library in your project, please consider giving credit where it is due. This will allow even more developers to know about this library and in turn contribute.

If you found bugs or ways to improve the source code, or extended the library for your own project, please submit a pull request!

The source folder contains:

AUTHORS	(list of contributors)
LICENSE	(MIT)
locstor.js	(uncompressed and full of comments)
locstor.min.js	(minified for production)
bower.json (bower package manager configuration)
README.md
test
	|_	index.html	(launch to view tests)
		tests.js	(unit tests go here)
		resources	(qunit files)
			|_	qunit.css
				qunit.js
TODO.md	(to be done)
VERSION-CHANGES

Questions / Comments

I would love to hear how you've used this library and/or how it's helped you.

Send questions and comments to justin@justindeguzman.net.