Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

closes #1

  • Loading branch information...
commit dabd45b04b45605039648d83eaa112e7d839a543 1 parent 95cd9e0
@icodeforlove authored
Showing with 19 additions and 10 deletions.
  1. +14 −8 lib/cubby.js
  2. +5 −2 package.json
View
22 lib/cubby.js
@@ -2,7 +2,9 @@
(function () {
'use strict';
- var fs = require('fs');
+ var fs = require('fs'),
+ clone = require('clone'),
+ _ = require('underscore');
function Cubby ($config) {
$config = $config || {};
@@ -13,15 +15,19 @@
Cubby.prototype = {
get: function (key) {
- return key in this._db ? this._db[key] : null;
+ if (key in this._db) {
+ return _.isObject(this._db[key]) ? clone(this._db[key]) : this._db[key];
+ } else {
+ return null;
+ }
},
set: function (key, value) {
if (arguments.length === 1) {
var values = arguments[0];
- for (key in values) this._db[key] = values[key];
+ for (key in values) this._db[key] = _.isObject(values[key]) ? clone(values[key]) : values[key];
} else {
- this._db[key] = value;
+ this._db[key] = _.isObject(value) ? clone(value) : value;
}
this._save();
},
@@ -46,13 +52,13 @@
path = path.split('.');
path.forEach(function (segment, key) {
- if (typeof current !== 'object' && key !== path.length-1) {
+ if (!_.isObject(current) && 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;
+ current[segment] = _.isObject(value) ? clone(value) : value;
} else {
current = current[segment];
}
@@ -68,14 +74,14 @@
path = path.split('.');
for (var segment = 0; segment < path.length; segment++) {
- if (typeof path[segment] !== 'object' && segment !== path.length-1 || !(path[segment] in current)) {
+ if (!_.isObject(current[path[segment]]) && segment !== path.length-1 || !(path[segment] in current)) {
return undefined;
} else {
current = current[path[segment]];
}
}
- return current;
+ return _.isObject(current) ? clone(current) : current;
},
_load: function () {
View
7 package.json
@@ -1,6 +1,6 @@
{
"name": "cubby",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "simple storage",
"main": "index.js",
"directories": {
@@ -17,5 +17,8 @@
"author": "Chad Scira",
"license": "MIT",
"devDependencies": {},
- "dependencies": {}
+ "dependencies": {
+ "clone": "0.1.1",
+ "underscore": "1.4.2"
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.