Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ee-arguments

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ee-arguments - npm Package Compare versions

Comparing version 0.1.4 to 1.0.0

test/index.js

52

lib/arguments.js

@@ -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);
}();

21

package.json
{
"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
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc