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

sanitize-arguments

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sanitize-arguments - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

38

lib/sanitize.js

@@ -16,3 +16,5 @@ // Module dependencies

function sanitize(args, caller, order) {
if(typeOf(caller) == "Array") {
var _args = [];
if (typeOf(caller) == "Array") {
order = caller;

@@ -27,12 +29,34 @@ caller = null;

order.some(function(type, newIndex) {
if(typeOf(type) == "Function" && typeOf(arg) == nameOf(type)) {
args[newIndex] = arg;
if(caller) sanitized[argNames[newIndex]] = arg;
delete order[newIndex];
return true;
var _type = Array.isArray(type) ? type[0] : type
, found = false;
if (typeOf(_type) == "Function" && typeOf(arg) == nameOf(_type)) {
found = true;
} else if (Array.isArray(type) && type.length == 1 && typeOf(type[0]) == typeOf(arg)) {
found = true;
}
if (found) {
_args[newIndex] = arg;
if(caller) sanitized[argNames[newIndex]] = arg;
delete order[newIndex];
}
return found;
});
if (order.length) {
order.forEach(function(type, newIndex) {
var def;
if (Array.isArray(type)) {
def = type.length > 1 ? type[1] : type[0];
_args[newIndex] = def;
if(caller) sanitized[argNames[newIndex]] = def;
}
});
}
});
return caller ? sanitized : args;
return caller ? sanitized : _args;
}

2

lib/typechecks.js

@@ -16,3 +16,3 @@ // Expose typechecks

return typeOf(o) == "Function"
? Function.prototype.toString.call(o).match(/function (\w+)/)[1]
? Function.prototype.toString.call(o).match(/function\s?(\w*)\(/)[1]
: false;

@@ -19,0 +19,0 @@ }

{
"name": "sanitize-arguments",
"description": "Easily check function arguments",
"version": "1.0.0",
"version": "2.0.0",
"author": "Jan Buschtöns <buschtoens@gmail.com>",
"contributors": [
{ "name": "Jan Buschtöns", "email": "buschtoens@gmail.com" }
{ "name": "Jan Buschtöns", "email": "buschtoens@gmail.com", "url": "https://github.com/silvinci" },
{ "name": "Jason Brumwell", "url": "https://github.com/jbrumwell" }
],

@@ -14,3 +15,4 @@ "license": "MIT",

"engines": { "node": "*" },
"devDependencies": { "mocha": "*", "should": "*" },
"scripts": { "prepublish": "npm prune", "test": "make test" }
}

@@ -1,5 +0,4 @@

sanitize-arguments
==================
![sanitize-arguments — Order arguments with ease!](http://i.imgur.com/SAhDs.png)
Sanitize function arguments the easy way!
[![Build Status](https://secure.travis-ci.org/silvinci/node-sanitize-arguments.png?branch=master)](https://travis-ci.org/silvinci/node-sanitize-arguments)

@@ -10,6 +9,12 @@ ```javascript

function Person(name, birthdate, size, pets, ability) {
sanitize(arguments, [String, Date, Number, Array, Function]);
var args = sanitize(arguments, Person, [String, Date, Number, Array, Function]);
// Now we can be sure that all arguments are in the right order
this.properties = { name: name, birthdate: birthdate, size: size, pets: pets, ability: ability };
this.properties = {
name: args.name,
birthdate: args.birthdate,
size: args.size,
pets: args.pets,
ability: args.ability
};

@@ -25,21 +30,42 @@ console.log(this.properties);

Installation
============
Just grab it with the awesome npm.
$ npm install sanitize-arguments
Or clone the repository.
$ git clone git://github.com/silvinci/node-sanitize-arguments
Usage
=====
Very simple. The first argument is your `arguments` object.
The seccond an array of data types showing the desired order.
There are two ways of using `sanitize`. The example above shows the first (and prefered) one.
The first argument is your `arguments` object, which contains all supplied values passed to the function.
The second one is the `Function` itself. The third one is an `Array` of `Types` or `Objects`
indicating the desired order. It's similiar to Java's `function(String name, Date birthdate, ...)`.
We just take our "strong types" out of the warehouse and put them into `sanitize`.
When giving these three `arguments` to `sanitize` it will return an object where all the values are
paired with their correct variable name and undefined values stay undefined, like so:
```javascript
function test(a, b, c, d, e)
sanitize(arguments, [String, Date, Number, Array, Function]);
console.log(a, b, c, d, e);
{
name: "silvinci",
birthdate: undefined,
size: 180,
pets: ["dog", "cat"],
ability: undefined
}
```
It *also* effectively changes the `arguments` object, thus altering the `function`'s variables aswell.
But this doesn't work in every case. First of all: This won't work in strict mode, because the magic link
between `arguments` and its variables is disabled. You also have to make sure that you *always* apply
the full count of arguments when calling your function, since only defined variables can be changed
via `arguments`.
`sanitize` effectively changes the `arguments` object, since objects are passed as references.
This means, that the function arguments `(a, b, c, d, e)` are effectively changed, too!
That works, because they are linked to the `arguments` object. Pretty awesome, huh?
But there's a downside, you'll have to deal with, when using very loose structure. Consider the following.
Only when you can be sure, that you're code won't ever run in strict mode and your function *always*
gets called with the full count of arguments, that may be swapt, then you can use `sanitize` in
another, even more comfortable way.
```javascript

@@ -50,33 +76,30 @@ function test(a, b, c, d, e)

}
test(42);
```
Here we only pass the parameter `c`. But as it turns out, `c` is `undefined`. Why's that?
Only arguments that were provided when calling the function are linked to the `arguments` object.
And here we only provide the first argument `a`, so we can't effect `c`.
Using the magic link the variables are changed and you don't have to use the returned object.
Please note, that `sanitize` will still return an object: the altered `arguments` so you could
acces the arguments by `returned[0]` too.
How can we cope with that? `sanitize` not only changes the `arguments` object, but also returns an object like
`{ a: undefined, b: undefined, c: 42, d: undefined, e: undefined }`. And here we have our precious `c`.
Extras
======
Using `sanitize` this non-magic way isn't that ... magic. I know. But there are only two cases in which you have to.
`sanitize` comes with some extra sugar for you. It exposes the typechecks it uses itself.
- `typeOf(object)` is a `typeof` like you would expect it to be.
For instance `typeOf([])` returns "Array" and not "object", like 'typeof []' does.
- `nameOf(Function)` returns a function's name. Useful for passed in functions.
- `argsOf(Function)` returns an array of the expected arguments.
1. Using a very loose argument structure in which every argument is optional.
2. "use strict"; Yep. Strict mode disables the link of the `arguments` object and the corresponding arguments.
Installation
Contributing
============
Just grab it with the awesome npm.
I happily accept pull requests and work on issues!
$ npm install sanitize-arguments
Contributors
============
Or clone the repository.
- [jbrumwell](https://github.com/jbrumwell)
$ git clone git://github.com/silvinci/sanitize-arguments
Contributing
============
---
I happily accept pull requests and work on issues!
License

@@ -83,0 +106,0 @@ =======

Sorry, the diff of this file is not supported yet

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