Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

base commit

  • Loading branch information...
commit b75a1e54453a42d507b864778056a819e006a8f0 0 parents
@icodeforlove authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
1  .npmignore
@@ -0,0 +1 @@
+node_modules
35 Readme.md
@@ -0,0 +1,35 @@
+## cubby
+
+a super simple sync way to store javascript information not geared towards performance
+
+## installation
+
+ $ npm install cubby
+
+## usage
+
+```javascript
+var Cubby = require('cubby'),
+ cubby = new Cubby();
+
+cubby.set('foo', 'bar');
+
+console.log(cubby.get('foo'));
+```
+
+## multiple files
+
+by default creating a cubby will result in a cubby.json file, but you can change this by providing a file path
+
+```javascript
+var cubbyOne = new Cubby({file: 'something-one.json'}),
+ cubbyTwo = new Cubby({file: 'something-two.json'});
+```
+
+## paths
+
+```javascript
+cubby.setPath('one.two.three', true);
+cubby.getPath('one.two.three');
+cubby.getPath('one.two.three.four'); // returns undefined when value doesn't exist
+```
1  index.js
@@ -0,0 +1 @@
+module.exports = exports = require('./lib/cubby');
91 lib/cubby.js
@@ -0,0 +1,91 @@
+/*jshint node:true*/
+(function () {
+ 'use strict';
+
+ var fs = require('fs');
+
+ function Cubby ($config) {
+ $config = $config || {};
+
+ this._file = $config.file || 'cubby.json';
+ this._db = this._load();
+ }
+
+ Cubby.prototype = {
+ get: function (key) {
+ return key in this._db ? this._db[key] : null;
+ },
+
+ set: function (key, value) {
+ this._db[key] = value;
+ this._save();
+ },
+
+ remove: function (key) {
+ delete this._db[key];
+ this._save();
+ },
+
+ empty: function () {
+ var self = this;
+ Object.keys(this._db).forEach(function (key) {
+ self._db[key] = undefined;
+ delete self._db[key];
+ });
+ this._save();
+ },
+
+ setPath: function (path, value) {
+ var current = this._db;
+
+ path = path.split('.');
+
+ path.forEach(function (segment, key) {
+ if (typeof current !== 'object' && key !== path.length-1) {
+ throw new Error('trying to store a value into a ' + typeof current);
+ } else if (!(segment in current)) {
+ current = current[segment] = key === path.length-1 ? value : Object.create(null);
+ } else {
+ if (key === path.length-1) {
+ current[segment] = value;
+ } else {
+ current = current[segment];
+ }
+ }
+ }, this);
+
+ this._save();
+ },
+
+ getPath: function (path) {
+ var current = this._db;
+
+ path = path.split('.');
+
+ for (var segment = 0; segment < path.length; segment++) {
+ if (typeof path[segment] !== 'object' && segment !== path.length-1 || !(path[segment] in current)) {
+ return undefined;
+ } else {
+ current = current[path[segment]];
+ }
+ }
+
+ return current;
+ },
+
+ _load: function () {
+ if (fs.existsSync(this._file)) {
+ return JSON.parse(fs.readFileSync(this._file));
+ } else {
+ fs.writeFileSync(this._file, '{}');
+ return Object.create(null);
+ }
+ },
+
+ _save: function () {
+ fs.writeFileSync(this._file, JSON.stringify(this._db));
+ }
+ };
+
+ module.exports = Cubby;
+})();
21 package.json
@@ -0,0 +1,21 @@
+{
+ "name": "cubby",
+ "version": "0.0.0",
+ "description": "simple storage",
+ "main": "index.js",
+ "directories": {
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/icodeforlove/node-cubby.git"
+ },
+ "keywords": [
+ "storage",
+ "node",
+ "json"
+ ],
+ "author": "Chad Scira",
+ "license": "MIT",
+ "devDependencies": {},
+ "dependencies": {}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.