Skip to content

Instantly share code, notes, and snippets.

@sbrl
Last active January 30, 2018 23:02
Show Gist options
  • Save sbrl/ad69f6b22c36a1d5bcfe to your computer and use it in GitHub Desktop.
Save sbrl/ad69f6b22c36a1d5bcfe to your computer and use it in GitHub Desktop.
[localStorage 4 node] localStorage for node.js & io.js #nodejs #microlibrary
/******************************************
* localStorage API for Node.js and io.js *
******************************************
* An implementation of the localStorage API for Node.JS and io.js. Currently untested.
******************************
* Written by Starbeamrainbowlabs <https://starbeamrainbowlabs.com/>
* I also wrote (and am upgrading!) Pepperminty Wiki: https://github.com/sbrl/pepperminty-wiki
*
* Gist link: https://gist.github.com/sbrl/ad69f6b22c36a1d5bcfe
* Posted to /r/tinycode here: (not posted yet!)
*/
function localStorage(filename)
{
// Get a reference to the file system
this.fs = require("fs");
// Create an object to hold the localStorage
this.data = {};
// If there is already a file with the name provided to us then we should load & parse it
if(this.fs.existsSync(filename))
this.data = JSON.parse(this.fs.readFileSync(filename, "utf-8"));
// Create a getter that returns the number of items that are currently in storage
this.__defineGetter__("length", function() {
return Object.keys(this.data).length;
});
// Save the filename for future reference
this.filename = filename;
// Whether we should automagically save changes made to the file system or not
this.autoSave = true;
// Gets an item from the localStorage
this.getItem = function(key) {
if(this.data.hasOwnProperty(key))
return this.data[key];
else
return null;
};
// Saves an item to the localStorage
this.setItem = function(key, value) {
this.data[key] = value;
if(this.autoSave) this.saveData();
};
// Removes the item stored at a given key from the localStorage
this.removeItem = function(key) {
var returnStatus = delete this.data[key];
if(this.autoSave) this.saveData();
return returnStatus;
};
// Clears the localStorage and deletes the associated
this.clear = function() {
this.data = {};
this.fs.unlink(this.filename);
};
// Whether we are curently saving to disk
this.isSaving = false;
// Saves the localStorage to disk
this.saveData = function(key) {
return new Promise((function(resolve, reject) {
this.isSaving = true;
this.fs.writeFileSync(this.filename, JSON.stringify(this.data), "utf-8", function(error) {
this.isSaving = false;
if(error) reject(error);
resolve();
});
}).bind(this));
};
}
function localStorage(t){this.fs=require("fs"),this.data={},this.fs.existsSync(t)&&(this.data=JSON.parse(this.fs.readFileSync(t,"utf-8"))),this.__defineGetter__("length",function(){return Object.keys(this.data).length}),this.filename=t,this.autoSave=!0,this.getItem=function(t){return this.data.hasOwnProperty(t)?this.data[t]:null},this.setItem=function(t,i){this.data[t]=i,this.autoSave&&this.saveData()},this.removeItem=function(t){var i=delete this.data[t];return this.autoSave&&this.saveData(),i},this.clear=function(){this.data={},this.fs.unlink(this.filename)},this.isSaving=!1,this.saveData=function(){return new Promise(function(t,i){this.isSaving=!0,this.fs.writeFileSync(this.filename,JSON.stringify(this.data),"utf-8",function(s){this.isSaving=!1,s&&i(s),t()})}.bind(this))}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment