| 
									
										
										
										
											2013-07-27 22:13:26 -07:00
										 |  |  | sessionStorage & localStorage for NodeJS | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | === | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  | An inefficient, but as W3C-compliant as possible using only pure JavaScript, `DOMStorage` implementation. | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-22 14:17:18 -07:00
										 |  |  | Purpose | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This is meant for the purpose of being able to run unit-tests and such for browser-y modules in node. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Usage | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  | ```javascript | 
					
						
							|  |  |  | var Storage = require('dom-storage') | 
					
						
							| 
									
										
										
										
											2014-01-28 18:22:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  |     // in-file, doesn't call `String(val)` on values (default) | 
					
						
							| 
									
										
										
										
											2014-03-08 14:55:21 -07:00
										 |  |  |   , localStorage = new Storage('./db.json', { strict: false, ws: '  ' }) | 
					
						
							| 
									
										
										
										
											2014-01-28 18:22:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  |     // in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'` | 
					
						
							|  |  |  |   , sessionStorage = new Storage(null, { strict: true }) | 
					
						
							| 
									
										
										
										
											2014-01-28 18:22:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  |   , myValue = { foo: 'bar', baz: 'quux' } | 
					
						
							|  |  |  |   ; | 
					
						
							| 
									
										
										
										
											2011-07-22 14:17:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  | localStorage.setItem('myKey', myValue); | 
					
						
							|  |  |  | myValue = localStorage.getItem('myKey'); | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  | // use JSON to stringify / parse when using strict w3c compliance | 
					
						
							|  |  |  | sessionStorage.setItem('myKey', JSON.stringify(myValue)); | 
					
						
							|  |  |  | myValue = JSON.parse(localStorage.getItem('myKey')); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2014-01-28 18:22:05 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | API | 
					
						
							| 
									
										
										
										
											2011-07-22 14:17:18 -07:00
										 |  |  | --- | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |   * getItem(key) | 
					
						
							|  |  |  |   * setItem(key, value) | 
					
						
							|  |  |  |   * removeItem(key) | 
					
						
							|  |  |  |   * clear() | 
					
						
							|  |  |  |   * key(n) | 
					
						
							|  |  |  |   * length | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 14:55:21 -07:00
										 |  |  | ### Options
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   * strict - whether to stringify strictly as text `[Object object]` or as json `{ foo: bar }`. | 
					
						
							|  |  |  |   * ws - the whitespace to use saving json to disk. Defaults to `'  '`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | Tests | 
					
						
							| 
									
										
										
										
											2011-07-22 14:17:18 -07:00
										 |  |  | --- | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  | ```javascript | 
					
						
							|  |  |  | 0 === localStorage.length; | 
					
						
							|  |  |  | null === localStorage.getItem('doesn't exist'); | 
					
						
							|  |  |  | undefined === localStorage['doesn't exist']; | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  | localStorage.setItem('myItem'); | 
					
						
							|  |  |  | "undefined" === localStorage.getItem('myItem'); | 
					
						
							|  |  |  | 1 === localStorage.length; | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  | localStorage.setItem('myItem', 0); | 
					
						
							|  |  |  | "0" === localStorage.getItem('myItem'); | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  | localStorage.removeItem('myItem', 0); | 
					
						
							|  |  |  | 0 === localStorage.length; | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 10:19:38 -06:00
										 |  |  | localStorage.clear(); | 
					
						
							|  |  |  | 0 === localStorage.length; | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  | Notes | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |   * db is read in synchronously | 
					
						
							|  |  |  |   * No callback when db is saved | 
					
						
							|  |  |  |   * Doesn't not emit `Storage` events (not sure how to do) | 
					
						
							| 
									
										
										
										
											2015-02-08 10:30:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | License | 
					
						
							|  |  |  | ------- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | * [Apache2](http://www.apache.org/licenses/LICENSE-2.0) |