parms
Decode a function's parameters and parameter comments.
Last updated 6 years ago by bluejeansandrain .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install parms 
SYNC missed versions from official npm registry.

#parms Build Status

Decode a Javascript function's parameters and parameter comments.

This utility was written for use in the radish dependency injection container.

All engines in which Function.prototype.toString returns the function source (IE8+, Node.js, etc.) should be supported.

##Installation

Use NPM to install it (recommended).

> npm install parms

Clone it from GitHub.

> git clone git@github.com:ChrisAckerman/parms.git
> cd parms
> npm install

Require it in Node.js

var parms = require('parms');

Include it in the browser. The utility will be available at window.parms

<script src="path/to/parms/dist/parms.min.js"></script>

Usage

function myFunc(/* Foo.matic */ foo, /* Bar.none */ bar /* Or trailing comments if you like */,
	// Supports line-breaks and single-line comments too.
	baz
) {
	...
}

var parameters = parms(myFunc);

The parameters variable will contain something like the following:

[
	{
		"start": 0,
		"end": 19,
		"text": "/* Foo.matic */ foo",
		"name": {
			"text": "foo",
			"start": 16,
			"end": 19
		},
		"comments": [
			{
				"text": " Foo.matic ",
				"start": 2,
				"end": 13,
				"style": "block",
				"placement": "before"
			}
		]
	},
	{
		"start": 20,
		"end": 78,
		"text": " /* Bar.none */ bar /* Or trailing comments if you like */",
		"name": {
			"text": "bar",
			"start": 36,
			"end": 39
		},
		"comments": [
			{
				"text": " Bar.none ",
				"start": 23,
				"end": 33,
				"style": "block",
				"placement": "before"
			},
			{
				"text": " Or trailing comments if you like ",
				"start": 42,
				"end": 76,
				"style": "block",
				"placement": "after"
			}
		]
	},
	{
		"start": 79,
		"end": 124,
		"text": "\n\t// Supports line-breaks and single-line comments too.\n\tbaz\n",
		"name": {
			"text": "baz",
			"start": 120,
			"end": 123
		},
		"comments": [
			{
				"text": " Supports line-breaks and single-line comments too.",
				"start": 83,
				"end": 118,
				"style": "line",
				"placement": "before"
			}
		]
	}
]
  • Text values are not trimmed.
  • Start and end indexes are from the opening parenthesis ( of the argument list.
  • Comment style may be "block" or "line".
  • Comment placement may be "before" or "after" indicating that the comment came before or after the parameter name.

The following would give you a simple array of parameter names.

for (var i = 0, max = parameters.length; i < max; ++i) {
	parameters[i] = parameters[i].name.text;
}

The parameters variable now contains:

[
	"foo",
	"bar",
	"baz"
]

Source String

A source string can also be used instead of a source function. Passing the source text of a function should produce exactly the same result as passing the function itself, though technically only the function(...) part is necessary because the body of the function is not used.

Accepting a string source allows for preprocessing of the function source.

var parameters = parms(myFunc.toString());

License

The MIT License (MIT)

Copyright (c) 2014 Chris Ackerman

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.

Current Tags

  • 0.1.2                                ...           latest (6 years ago)

12 Versions

  • 0.1.2                                ...           6 years ago
  • 0.1.1                                ...           6 years ago
  • 0.1.0                                ...           6 years ago
  • 0.0.11                                ...           6 years ago
  • 0.0.9                                ...           6 years ago
  • 0.0.8                                ...           6 years ago
  • 0.0.7                                ...           6 years ago
  • 0.0.5                                ...           6 years ago
  • 0.0.4                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 2
Last Month 0
Dependencies (0)
None
Dev Dependencies (9)
Dependents (1)

Copyright 2014 - 2017 © taobao.org |