Comparing version 0.2.1 to 0.3.0
@@ -26,3 +26,3 @@ var tv = require('tv4'); | ||
* | ||
* @param {object=} opts Specify console output and schema | ||
* @param {object=} opts - Specify console output and schema | ||
*/ | ||
@@ -39,3 +39,3 @@ var Bx = function(opts) { | ||
if (opts.debug) { | ||
console.log('CREATE DATA STORE'); | ||
console.log('data store created'); | ||
} | ||
@@ -78,5 +78,5 @@ | ||
* | ||
* @param {string} key Key | ||
* @param {?} val Value | ||
* @param {number=} time Time in ms before auto-deletion | ||
* @param {string} key | ||
* @param {?} val | ||
* @param {number=} time - Time in ms before auto-deletion | ||
*/ | ||
@@ -116,5 +116,22 @@ Bx.prototype.put = function(key, val, time) { | ||
/** | ||
* Checks to see if the data fits the schema | ||
* | ||
* @param {?} data - Data to validate | ||
*/ | ||
Bx.prototype.check = function(data) { | ||
if (this.opts.schema) { | ||
if (tv.validate(data, this.opts.schema)) { | ||
return true; | ||
} | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
}; | ||
/** | ||
* Retrieve data from the data store if the key exists. | ||
* | ||
* @param {string} key Key | ||
* @param {string} key | ||
*/ | ||
@@ -143,3 +160,3 @@ Bx.prototype.get = function(key) { | ||
* | ||
* @param {Array} keys Keys | ||
* @param {Array} keys | ||
*/ | ||
@@ -160,3 +177,3 @@ Bx.prototype.mget = function(keys) { | ||
* | ||
* @param {string} key Key | ||
* @param {string} key | ||
*/ | ||
@@ -173,3 +190,3 @@ Bx.prototype.del = function(key) { | ||
* | ||
* @param {Array} keys Keys | ||
* @param {Array} keys | ||
*/ | ||
@@ -184,7 +201,6 @@ Bx.prototype.mdel = function(keys) { | ||
* Clear the data store. | ||
* | ||
*/ | ||
Bx.prototype.clear = function() { | ||
if (this.opts.debug) { | ||
console.log('DELETE: ALL DATA'); | ||
console.log('DELETE: all data'); | ||
} | ||
@@ -196,3 +212,2 @@ this.data = {}; | ||
* Get entire data store. | ||
* | ||
*/ | ||
@@ -208,3 +223,2 @@ Bx.prototype.all = function() { | ||
* Get an array of the keys in the data store. | ||
* | ||
*/ | ||
@@ -217,3 +231,2 @@ Bx.prototype.keys = function() { | ||
* Get an array of the values in the data store. | ||
* | ||
*/ | ||
@@ -233,3 +246,2 @@ Bx.prototype.vals = function() { | ||
* Get the number of items stored. | ||
* | ||
*/ | ||
@@ -242,3 +254,2 @@ Bx.prototype.len = function() { | ||
* Get the length in bytes of the data store. | ||
* | ||
*/ | ||
@@ -252,3 +263,3 @@ Bx.prototype.size = function() { | ||
* | ||
* @param {string} key Key | ||
* @param {string} key | ||
*/ | ||
@@ -255,0 +266,0 @@ Bx.prototype.exp = function(key) { |
{ | ||
"name": "bx", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "in-memory storage for node", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"pretest": "npm install && ./node_modules/.bin/jshint lib/", | ||
"test": "./node_modules/.bin/mocha test/specs/*.js" | ||
"test": "./node_modules/.bin/mocha test/specs/*.js", | ||
"clean": "rm -rf node_modules && npm install" | ||
}, | ||
@@ -33,8 +34,8 @@ "repository": { | ||
"dependencies": { | ||
"tv4": "^1.1.4" | ||
"tv4": "^1.1.5" | ||
}, | ||
"devDependencies": { | ||
"chai": "^1.9.2", | ||
"jshint": "^2.5.6", | ||
"mocha": "^1.21.4" | ||
"chai": "^1.10.0", | ||
"jshint": "^2.5.11", | ||
"mocha": "^2.0.1" | ||
}, | ||
@@ -41,0 +42,0 @@ "jshintConfig": { |
286
README.md
@@ -41,3 +41,5 @@ # bx | ||
npm install bx | ||
```bash | ||
$ npm install bx | ||
``` | ||
@@ -47,3 +49,3 @@ ## API | ||
The code is pretty simple and well-commented, but here's an overview and example usage for each function. All examples assume that a bx has | ||
been required as 'bx'. For more, feel free to look through the test suite as well. | ||
been required as `bx`. For more, feel free to look through the test suite as well. | ||
@@ -62,25 +64,27 @@ #### *Bx(opts)* | ||
var box = new bx(); //no debug & no schema | ||
```js | ||
var box = new bx(); //no debug & no schema | ||
var people = new bx({ //debug & schema | ||
debug: true, | ||
schema: { | ||
title: "people", | ||
type: "object", | ||
required: ["name", "age", "job"], | ||
properties: { | ||
name: { | ||
type: "string" | ||
}, | ||
age: { | ||
type: "number" | ||
}, | ||
job: { | ||
type: "string" | ||
} | ||
var people = new bx({ //debug & schema | ||
debug: true, | ||
schema: { | ||
title: "people", | ||
type: "object", | ||
required: ["name", "age", "job"], | ||
properties: { | ||
name: { | ||
type: "string" | ||
}, | ||
age: { | ||
type: "number" | ||
}, | ||
job: { | ||
type: "string" | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
var stuff = new bx({ debug: true }); //debug & no schema | ||
var stuff = new bx({ debug: true }); //debug & no schema | ||
``` | ||
@@ -99,22 +103,66 @@ #### *put(key, val, time)* | ||
var box = new bx(); | ||
```js | ||
var box = new bx(); | ||
box.put('bob', { | ||
name: "Bob Johnson", | ||
hometown: "Boston, MA", | ||
age: 24 | ||
}, 2000); //will die in 2 seconds | ||
box.put('bob', { | ||
name: "Bob Johnson", | ||
hometown: "Boston, MA", | ||
age: 24 | ||
}, 2000); //will die in 2 seconds | ||
box.put('color', 'red'); //this will live until manually deleted | ||
box.put('color', 'red'); //this will live until manually deleted | ||
setTimeout(function() { | ||
console.log(box.get('bob')); //object | ||
console.log(box.get('color')); //"red" | ||
}, 1000); | ||
setTimeout(function() { | ||
console.log(box.get('bob')); //object | ||
console.log(box.get('color')); //"red" | ||
}, 1000); | ||
setTimeout(function() { | ||
console.log(box.get('bob')); //undefined | ||
console.log(box.get('color')); //"red" | ||
}, 3000); | ||
setTimeout(function() { | ||
console.log(box.get('bob')); //undefined | ||
console.log(box.get('color')); //"red" | ||
}, 3000); | ||
``` | ||
#### *check(data)* | ||
Check to see if data fits the schema, if there is a schema. | ||
*Arguments* | ||
* data {?} - data to verify | ||
*Example* | ||
```js | ||
var studentSchema = { | ||
'title': 'students', | ||
'type': 'object', | ||
'required': ['name', 'major', 'gpa'], | ||
'properties': { | ||
'name': { | ||
'type': 'string' | ||
}, | ||
'major': { | ||
'type': 'string' | ||
}, | ||
'gpa': { | ||
'type': 'number' | ||
} | ||
} | ||
}; | ||
var me = { | ||
name: "Ty", | ||
major: "Computer Science", | ||
gpa: -500 | ||
}; | ||
var students = new box({ | ||
schema: studentSchema | ||
}); | ||
students.check('bad data'); //false | ||
students.check(me); //true | ||
``` | ||
#### *get(key)* | ||
@@ -130,8 +178,10 @@ | ||
var box = new bx(); | ||
box.put('name', 'Ty'); | ||
```js | ||
var box = new bx(); | ||
box.put('name', 'Ty'); | ||
box.get('name'); //"Ty" | ||
box.get(); //undefined | ||
box.get('oops'); //undefined | ||
box.get('name'); //"Ty" | ||
box.get(); //undefined | ||
box.get('oops'); //undefined | ||
``` | ||
@@ -148,13 +198,15 @@ #### *mget(keys)* | ||
var box = new bx(); | ||
```js | ||
var box = new bx(); | ||
box.put('apple1', 'gala'); | ||
box.put('apple2', 'granny smith'); | ||
box.put('apple1', 'gala'); | ||
box.put('apple2', 'granny smith'); | ||
var food = box.mget([ | ||
'apple1', | ||
'apple2' | ||
]); | ||
var food = box.mget([ | ||
'apple1', | ||
'apple2' | ||
]); | ||
console.log(food); //{ 'apple1': 'gala', 'apple2': 'granny smith' } | ||
console.log(food); //{ 'apple1': 'gala', 'apple2': 'granny smith' } | ||
``` | ||
@@ -171,12 +223,12 @@ #### *del(key)* | ||
var box = new bx(); | ||
```js | ||
var box = new bx(); | ||
box.put('salary', 0); | ||
box.put('salary', 0); | ||
var a = box.get('salary'); //0 | ||
var a = box.get('salary'); //0 | ||
box.del('salary'); | ||
var b = box.get('salary'); //undefined | ||
``` | ||
box.del('salary'); | ||
var b = box.get('salary'); //undefined | ||
#### *mdel(keys)* | ||
@@ -192,12 +244,14 @@ | ||
var box = new bx(); | ||
var keys = ['a', 'b', 'c']; | ||
```js | ||
var box = new bx(); | ||
var keys = ['a', 'b', 'c']; | ||
for (var i in keys) { | ||
box.put(keys[i], i); | ||
} | ||
for (var i in keys) { | ||
box.put(keys[i], i); | ||
} | ||
box.mdel(['a', 'b', 'c']); | ||
box.mdel(['a', 'b', 'c']); | ||
var all = box.mget(['a', 'b', 'c']); //{} | ||
var all = box.mget(['a', 'b', 'c']); //{} | ||
``` | ||
@@ -210,15 +264,17 @@ #### *clear* | ||
var box = new bx({ | ||
schema: { | ||
"title": "fruits", | ||
"type": "array" | ||
} | ||
}); | ||
```js | ||
var box = new bx({ | ||
schema: { | ||
"title": "fruits", | ||
"type": "array" | ||
} | ||
}); | ||
box.put('apples', ['gala', 'granny smith', 'red delicious']); | ||
box.put('berries', ['blueberries', 'strawberries']); | ||
box.put('apples', ['gala', 'granny smith', 'red delicious']); | ||
box.put('berries', ['blueberries', 'strawberries']); | ||
box.clear(); | ||
box.clear(); | ||
var blah = box.get('apples'); //undefined | ||
var blah = box.get('apples'); //undefined | ||
``` | ||
@@ -231,12 +287,14 @@ #### *all* | ||
var box = new bx(); | ||
```js | ||
var box = new bx(); | ||
box.put('a', 'a'.charCodeAt(0)); | ||
box.put('b', 'banana'); | ||
box.put('me', { | ||
'name': 'Ty', | ||
'age': 19 | ||
}); | ||
box.put('a', 'a'.charCodeAt(0)); | ||
box.put('b', 'banana'); | ||
box.put('me', { | ||
'name': 'Ty', | ||
'age': 19 | ||
}); | ||
var all = box.all(); //{object (a, b, me)} | ||
var all = box.all(); //{object (a, b, me)} | ||
``` | ||
@@ -249,9 +307,11 @@ #### *keys* | ||
var box = new bx(); | ||
```js | ||
var box = new bx(); | ||
box.put('snoop', 'dogg'); | ||
box.put('kanye', 'west'); | ||
box.put('dr', 'dre'); | ||
box.put('snoop', 'dogg'); | ||
box.put('kanye', 'west'); | ||
box.put('dr', 'dre'); | ||
var k = box.keys(); //['snoop', 'kanye', 'dr'] | ||
var k = box.keys(); //['snoop', 'kanye', 'dr'] | ||
``` | ||
@@ -264,9 +324,11 @@ #### *vals* | ||
var box = new bx(); | ||
```js | ||
var box = new bx(); | ||
box.put('snoop', 'dogg'); | ||
box.put('kanye', 'west'); | ||
box.put('dr', 'dre'); | ||
box.put('snoop', 'dogg'); | ||
box.put('kanye', 'west'); | ||
box.put('dr', 'dre'); | ||
var v = box.vals(); //['dogg', 'west', 'dre'] | ||
var v = box.vals(); //['dogg', 'west', 'dre'] | ||
``` | ||
@@ -279,11 +341,10 @@ #### *len* | ||
var box = new bx(); | ||
```js | ||
var box = new bx(); | ||
console.log(box.len()); //0 | ||
console.log(box.len()); //0 | ||
box.put('a', 0); | ||
console.log(box.len()); //1 | ||
``` | ||
box.put('a', 0); | ||
console.log(box.len()); //1 | ||
#### *size* | ||
@@ -295,14 +356,13 @@ | ||
var box = new bx(); | ||
```js | ||
var box = new bx(); | ||
console.log(box.size()); //2 | ||
console.log(box.size()); //2 | ||
box.put('a', 'b'); | ||
console.log(box.size()); //19 | ||
box.put('a', 'b'); | ||
box.clear(); | ||
console.log(box.size()); //2 | ||
``` | ||
console.log(box.size()); //19 | ||
box.clear(); | ||
console.log(box.size()); //2 | ||
#### *exp(key)* | ||
@@ -318,8 +378,10 @@ | ||
var box = new bx(); | ||
```js | ||
var box = new bx(); | ||
box.put('a', 0); | ||
box.put('b', 1, 2000); | ||
box.put('a', 0); | ||
box.put('b', 1, 2000); | ||
console.log(box.exp('a')); //undefined | ||
console.log(box.exp('b')); //{2000 + time of creation} | ||
console.log(box.exp('a')); //undefined | ||
console.log(box.exp('b')); //{2000 + time of creation} | ||
``` |
@@ -155,2 +155,7 @@ var expect = require('chai').expect; | ||
it('should return false if "fits" is called using malformed data', function() { | ||
expect(students.check('bad data')).to.equal(false); | ||
expect(students.check(me)).to.equal(true); | ||
}); | ||
it('should fail to insert malformed data', function() { | ||
@@ -157,0 +162,0 @@ expect(function() { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20745
420
373
9
Updatedtv4@^1.1.5