ee-arguments
Advanced tools
Comparing version
@@ -0,20 +1,52 @@ | ||
!function(){ | ||
var type = require('ee-types') | ||
, Class = require('ee-class') | ||
, log = require('ee-log'); | ||
var type = require( "ee-types" ) | ||
, log = require( "ee-log" ); | ||
var classDefinition = { | ||
init: function(args) { | ||
this._args = args; | ||
} | ||
module.exports = function( theArguments, argumentType, defaultValue, index ){ | ||
if ( type.string( argumentType ) && type.object( theArguments ) ){ | ||
var args = Array.prototype.slice.call( theArguments, 0 ); | ||
, get: function(desiredType, defaultValue) { | ||
return this.getByIndex(desiredType, 0, defaultValue); | ||
} | ||
, getByIndex: function(desiredType, index, defaultValue) { | ||
var i, item; | ||
index = index || 0; | ||
for ( var i = 0, l = args.length; i < l; i++ ){ | ||
if ( type( args[ i ] ) === argumentType ){ | ||
if ( index === 0 ) return args[ i ]; | ||
for(i in this._args) { | ||
item = this._args[i]; | ||
if (type(item) === desiredType) { | ||
if (index === 0) return item; | ||
else index--; | ||
} | ||
} | ||
return defaultValue; | ||
} | ||
else throw new Error( "expecting at least the «arguments» object and the «type» arguments!" ); | ||
} | ||
}; | ||
// generate methods for all types | ||
['string', 'number', 'boolean', 'array', 'object', 'function', 'date', 'regexp', 'error', 'undefined', 'buffer', 'null'].forEach(function(typeName){ | ||
classDefinition['get'+typeName[0].toUpperCase()+typeName.substr(1)] = function(defaultValue){ | ||
return this.getByIndex(typeName, 0, defaultValue); | ||
}; | ||
classDefinition['get'+typeName[0].toUpperCase()+typeName.substr(1)+'ByIndex'] = function(index, defaultValue){ | ||
return this.getByIndex(typeName, index, defaultValue); | ||
}; | ||
}); | ||
module.exports = new Class(classDefinition); | ||
}(); |
{ | ||
"name" : "ee-arguments" | ||
, "description" : "assign values passed to a function to a variable by their type and optional by their index" | ||
, "version" : "0.1.4" | ||
, "description" : "parses the arguments array passed to a function by variable type and their position in the array, returns the requested type & position" | ||
, "version" : "1.0.0" | ||
, "homepage" : "https://github.com/eventEmitter/ee-arguments" | ||
, "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" | ||
, "licence" : "MIT" | ||
, "os" : "linux" | ||
, "repository": { | ||
@@ -20,7 +19,17 @@ "url" : "https://github.com/eventEmitter/ee-arguments.git" | ||
, "dependencies": { | ||
"ee-log" : "*" | ||
, "ee-types" : "*" | ||
"ee-log" : "0.2.*" | ||
, "ee-types" : "0.1.*" | ||
, "ee-class" : "1.0.*" | ||
} | ||
, "devDependencies": {} | ||
, "devDependencies": { | ||
"mocha" : "1.17.x" | ||
} | ||
, "optionalDependencies": {} | ||
, "keywords" : [ "function", "arguments", "parser" ] | ||
, "scripts": { | ||
"test" : "node test.js" | ||
} | ||
, "scripts": { | ||
"test" : "./node_modules/mocha/bin/mocha --reporter spec" | ||
} | ||
} |
# ee-arguments | ||
assign values passed to a function to a variable by their type and optional by their index. you may pass also a default value for each variable | ||
parses the arguments array passed to a function by variable type and their position in the array, returns the requested type & position | ||
@@ -9,30 +9,83 @@ ## installation | ||
## usage | ||
## build status | ||
// syntax | ||
var myVariable = arg( arguments, expectedType, [defaultValue], [index] ); | ||
[](https://travis-ci.org/eventEmitter/ee-arguments) | ||
var arg = require( "ee-arguments" ) | ||
, log = require( "ee-log" ); | ||
## API | ||
var test = function(){ | ||
var callback = arg( arguments, "function", function(){ log.info( "default function" ); } ); | ||
callback(); | ||
} | ||
### Constructor | ||
Creates an instance of the Arguments class bounds to the current arguments object. | ||
// prints «default function» | ||
test(); | ||
var Arguments = require('ee-arguments'); | ||
// prints «custom function» | ||
test( function(){ log.info( "custom function" ); } ); | ||
var myFunction(){ | ||
var args = new Arguments(arguments); | ||
}; | ||
// prints «custom function» | ||
test( 1, null, new Error(), function(){ log.info( "custom function" ); }, "fancy_string" ); | ||
myFunction(); | ||
### get | ||
Get the first occurence of a specific type from the arguments object, return an optional defined default if the type is not found. | ||
var myFunction(){ | ||
var args = new Arguments(arguments); | ||
args.get('string', 'defaultValue'); // do not use strings this way! | ||
args.get('boolean', true); // true | ||
}; | ||
myFunction(1, new String('do not use strings this way!'), 'second string'); | ||
### getByIndex | ||
Returns the item of the defined type and the defined index from the arguments object. return an optional defined default if the type is not found. | ||
var myFunction(){ | ||
var args = new Arguments(arguments); | ||
args.getByIndex('string', 1, function default(){}); // second string | ||
args.getByIndex('string', 5); // undefined | ||
}; | ||
myFunction(1, new String('do not use strings this way!'), 'second string'); | ||
### Type specif methods | ||
For the types defined below there are specific methods available | ||
- string | ||
- number | ||
- boolean | ||
- array | ||
- object | ||
- function | ||
- date | ||
- regexp | ||
- error | ||
- undefined | ||
- buffer | ||
- null | ||
var myFunction(){ | ||
var args = new Arguments(arguments); | ||
args.getString(); // do not use strings this way! | ||
args.getNumberByIndex(1, 'defaultValue'); // second string | ||
}; | ||
myFunction(1, new String('do not use strings this way!'), 'second string'); | ||
## Verions History | ||
- 1.0.0: complete rewrite, made it easier to user, made it faster. Chnaged the API |
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 2 instances in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8059
87.2%79
154.84%0
-100%1
-66.67%90
136.84%3
50%1
Infinity%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated