Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-lin committed Jul 5, 2011
0 parents commit 4b9f211
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
*.sock
node_modules
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.sock
node_modules
support
test
examples
3 changes: 3 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0 / 2011-07-05

- Initial release
77 changes: 77 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Secret

An `in memory key value store module` for node.js



## Description

[Global objects are evil in javascript](http://bit.ly/e6DUOi). They are easily to be overwritten, especially when you mix your code with others. Secret provides a way to store your data or functions that can be used later. It works cross modules and files without creating globals.



## Requires

- node >= 0.4.x



## Installation

npm install secret



## Usage

> Require module.
var secret = require( 'secret' );

> Store data to be called or withdraw later..
secret.set( 'age', 17 );
secret.set( 'name', 'ben' );
secret.set( 'do-some-thing', function( arg1, arg2, arg3 ){
// do something here
});

> Get stored data, it works cross modules and files.
var age = secret.get( 'age' );

> Calling a stored function.
// You can pass as many arguments as you wish
secret.execute( 'do-some-thing', arg1, arg2, arg3 ... );

> Remove stored data.
secret.remove( 'age' );



## License

(The MIT License)

Copyright (c) 2011 dreamerslab <ben@dreamerslab.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/secret');
122 changes: 122 additions & 0 deletions lib/secret.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*!
* Secret
* Copyright(c) 2011 Ben Lin <ben@dreamerslab.com>
* MIT Licensed
*
* @fileoverview
* An`in memory key value store module` for node.js.
* With Secret you can store data in a clousure and use it later without creating globals.
*/



/**
* @private
* @namespace A namespace to store data.
*/
var trunk = {};



/**
* @public
*/
module.exports = {

version : '1.0.0',



/**
* Store data in a closure to be called or withdraw later.
* @this {Secret}
* @param {String} key Data key.
* @param {String|Number|Array|Object|Function} value Data value.
* @return {this} Return `this` to enable chaining.
* @example
*
* var secret = require( 'secret' );
* secret.set( 'age', 17 );
* secret.set( 'name', 'ben' );
* secret.set( 'do-some-thing', function( arg1, arg2, arg3 ){
* // do something here
* });
*/
set : function( key, value ){
// data can be overwritten
trunk[ key ] = value;

return this;
},



/**
* Get stored data.
* @this {Secret}
* @param {String} key Data key.
* @return {data|false} Return the stored data if available.
* @example
*
* var secret = require( 'secret' );
* secret.get( 'age' );
*/
get : function( key ){
// make sure the calling out name exist
if( trunk[ key ] !== undefined ){
return trunk[ key ];
}
return false;
},



/**
* Calling a stored function.
* @this {Secret}
* @param {String} arguments[ 0 ] Data key.
* @param {String|Number|Array|Object|Function} [arguments[ 1 ]] Arguments to be passed to the stored function.
* @return {this} Return `this` to enable chaining.
* @example
*
* var secret = require( 'secret' );
* // You can pass as many arguments as you wish
* secret.execute( 'do-some-thing', arg1, arg2, arg3 ... );
*/
execute : function(){
var key, tmp;

key = [].shift.call( arguments );
// make sure the calling function exist
if( trunk[ key ] !== undefined ){

// execute store function
tmp = trunk[ key ];
if( Object.prototype.toString.call( tmp ) === '[object Function]' ){
tmp.apply( this, arguments );
}else{
throw 'Secret error: on action "execute" - "' + key + '" is not a function';
}
}

return this;
},



/**
* Remove stored data.
* @this {Secret}
* @param {String} key Data key.
* @return {this} Return `this` to enable chaining.
* @example
*
* var secret = require( 'secret' );
* secret.remove( 'age' );
*/
remove : function( key ){
delete trunk[ key ];

return this;
}
};
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "secret"
, "version": "1.0.0"
, "description": "An `in memory key value store module` for node.js"
, "keywords": [ "secret", "key value", "global" ]
, "author": "dreamerslab <ben@dreamerslab.com>"
, "dependencies": {}
, "main": "index"
, "engines": { "node >= 0.4.0" }
}

0 comments on commit 4b9f211

Please sign in to comment.