Will not reduce objects to '[object Object]' by default. 'strict' option may be used for that.
This commit is contained in:
parent
a58d4566e0
commit
9e7d8354ec
15
README.md
15
README.md
|
@ -12,14 +12,23 @@ Usage
|
||||||
----
|
----
|
||||||
|
|
||||||
var Storage = require('dom-storage')
|
var Storage = require('dom-storage')
|
||||||
, localStorage = new Storage('./db.json') // in-file
|
|
||||||
, sessionStorage = new Storage() // in-memory
|
// in-file, doesn't call `String(val)` on values (default)
|
||||||
|
, localStorage = new Storage('./db.json', { strict: false })
|
||||||
|
|
||||||
|
// in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'`
|
||||||
|
, sessionStorage = new Storage(null, { strict: true })
|
||||||
|
|
||||||
, myValue = { foo: 'bar', baz: 'quux' }
|
, myValue = { foo: 'bar', baz: 'quux' }
|
||||||
;
|
;
|
||||||
|
|
||||||
localStorage.setItem('myKey', JSON.stringify(myValue));
|
localStorage.setItem('myKey', myValue);
|
||||||
myValue = localStorage.getItem('myKey');
|
myValue = localStorage.getItem('myKey');
|
||||||
|
|
||||||
|
// use JSON to stringify / parse when using strict w3c compliance
|
||||||
|
sessionStorage.setItem('myKey', JSON.stringify(myValue));
|
||||||
|
myValue = JSON.parse(localStorage.getItem('myKey'));
|
||||||
|
|
||||||
API
|
API
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
17
lib/index.js
17
lib/index.js
|
@ -12,7 +12,8 @@
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
;
|
;
|
||||||
|
|
||||||
function Storage(path) {
|
function Storage(path, opts) {
|
||||||
|
opts = opts || {};
|
||||||
var db
|
var db
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -24,6 +25,12 @@
|
||||||
, enumerable: false
|
, enumerable: false
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(this, '___priv_strict___', {
|
||||||
|
value: !!opts.strict
|
||||||
|
, writable: false
|
||||||
|
, enumerable: false
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
db = JSON.parse(fs.readFileSync(path));
|
db = JSON.parse(fs.readFileSync(path));
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
@ -37,13 +44,13 @@
|
||||||
|
|
||||||
Storage.prototype.getItem = function (key) {
|
Storage.prototype.getItem = function (key) {
|
||||||
if (this.hasOwnProperty(key)) {
|
if (this.hasOwnProperty(key)) {
|
||||||
return String(this[key]);
|
return this.___priv_strict___ && String(this[key]) || this[key];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
Storage.prototype.setItem = function (key, val) {
|
Storage.prototype.setItem = function (key, val) {
|
||||||
this[key] = String(val);
|
this[key] = this.___priv_strict___ && String(val) || val;
|
||||||
this.___save___();
|
this.___save___();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -97,8 +104,8 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
Object.defineProperty(Storage, 'create', {
|
Object.defineProperty(Storage, 'create', {
|
||||||
value: function (path) {
|
value: function (path, opts) {
|
||||||
return new Storage(path);
|
return new Storage(path, opts);
|
||||||
}
|
}
|
||||||
, writable: false
|
, writable: false
|
||||||
, enumerable: false
|
, enumerable: false
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
|
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
|
||||||
"name": "dom-storage",
|
"name": "dom-storage",
|
||||||
"description": "W3C DOM Storage (localStorage and sessionStorage) for Node.JS",
|
"description": "W3C DOM Storage (localStorage and sessionStorage) for Node.JS",
|
||||||
"version": "1.0.2",
|
"version": "2.0.0",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/coolaj86/node-browser-compat.git"
|
"url": "git://github.com/coolaj86/node-dom-storage.git"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "*"
|
"node": "*"
|
||||||
|
|
Loading…
Reference in New Issue