ee-arguments
Advanced tools
Comparing version 0.1.4 to 1.0.0
@@ -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] ); | ||
[![Build Status](https://travis-ci.org/eventEmitter/ee-class.png?branch=master)](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 |
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
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
79
0
1
90
3
1
+ Addedee-class@1.0.*
+ Addedee-class@0.4.01.0.9(transitive)
+ Addedee-log@0.2.23(transitive)
+ Addedee-types@0.1.3(transitive)
- Removed@distributed-systems/callsite@1.1.1(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.13.2.1(transitive)
- Removedapp-root-path@2.2.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedchalk@1.1.32.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedee-class@1.4.0(transitive)
- Removedee-log@1.1.03.0.9(transitive)
- Removedee-types@2.2.14.0.4(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedlogd-console-output@1.3.0(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedsection-tests@1.3.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.05.5.0(transitive)
- Removedwrappy@1.0.2(transitive)
Updatedee-log@0.2.*
Updatedee-types@0.1.*