JSPM

  • Created
  • Published
  • Downloads 270548
  • Score
    100M100P100Q173442F
  • License MIT

Parse a function, arrow function or string to object with name, args, params and body properties.

Package Exports

  • parse-function

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (parse-function) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

parse-function npmjs.com The MIT License

Parse a function, arrow function or string to object with name, args, params and body properties.

code climate standard code style travis build status coverage status dependency status

Please read the CHANGELOG!

Pro Tips (What version to use?)

There's no breaking changes between versions, the only version that have partial breaking change is v2.1.x, so don't use it.

  • use v2.0.x - if you don't need support for arrow functions and es6 default params.
  • use v2.2.x - if you just want basic support for es6 features like ES6 Arrow Functions (faster than v2.3.x)
  • use v2.3.x - if you want full support of es6 arrow functions and es6 default params (this uses acorn)

Install

npm i parse-function --save

Usage

For more use-cases see the tests

const parseFunction = require('parse-function')

parseFunction

Parse function, arrow function or string to object.

  • [val] {Function|ArrowFunction|String} function or string to parse
  • returns {Object} with name, args, params and body properties
    • name {String} name of the function or anonymous
    • body {String} body of the function or ''
    • params {String} arguments names as string (as is) or empty string ''
    • parameters {String} hidden property, alias of params
    • args {Array} arguments names as array, or empty array []
    • arguments {Array} hidden property, alias of args
    • value {String} hidden property, string representation of the given val
    • orig {Function|ArrowFunction|String} hidden property, original given val
    • valid {Boolean} hidden property, true when val is function or string, false otherwise
    • invalid {Boolean} hidden property, opposite of opts.valid
    • defaults {Object} hidden property, empty object or ES6 default params

Example

const parseFunction = require('parse-function')

const fixture = 'function testing (a, b, callback) { callback(null, a + b) }'
const obj = parseFunction(fixture)
// => {
//   name: 'testing',
//   body: ' callback(null, a + b) ',
//   params: 'a, b, callback',
//   args: ['a', 'b', 'callback']
// }

const withoutName = function (x, y) {}
const res = parseFunction(withoutName)
// => {
//   name: 'anonymous',
//   body: '',
//   params: 'x, y',
//   args: ['x', 'y']
// }
  • function-regex: Function regex. Regular expression for matching function parts. Expose match groups for function name, arguments and function body.
  • gulp-di: Dependency injection framework for the Gulp streaming build system
  • is-empty-function: Check that given string, function or arrow function have empty body, using parse-function.
  • is-installed: Checks that given package is installed on the system - globally or locally.
  • is-missing: Check that given name or user/repo exists in npm registry or in github as user repository.

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckocore.tk keybase tunnckocore tunnckoCore npm tunnckoCore twitter tunnckoCore github