Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Commit dacc35c

Browse files
author
tunnckoCore
committed
docs(api): docs
1 parent ee18991 commit dacc35c

File tree

3 files changed

+179
-1
lines changed

3 files changed

+179
-1
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Install](#install)
1414
- [Usage](#usage)
1515
- [API](#api)
16+
* [parseFunction](#parsefunction)
1617
- [Related](#related)
1718
- [Contributing](#contributing)
1819
- [Building docs](#building-docs)
@@ -44,6 +45,52 @@ const parseFunction = require('parse-function')
4445

4546
## API
4647

48+
### [parseFunction](index.js#L64)
49+
> Parse a function or string that contains a function, using [babylon][] or [acorn][] parsers. By default it uses `babylon`, but you can pass custom one through `options.parser` option - for example pass `.parse: acorn.parse` to force use the `acorn` parser instead.
50+
51+
**Params**
52+
53+
* `code` **{Function|String}**: function to be parsed, it can be string too
54+
* `options` **{Object}**: optional, passed directly to [babylon][] or [acorn][]; you can also pass custom `options.parser` parser
55+
* `returns` **{Object}**: always returns an object, check `result.valid`
56+
57+
**Example**
58+
59+
```js
60+
const acorn = require('acorn')
61+
const acornLoose = require('acorn/dist/acorn_loose')
62+
const parseFunction = require('parse-function')
63+
64+
const myFn = function abc (e, f, ...rest) { return 1 + e + 2 + f }
65+
const parsed = parseFunction(myFn)
66+
67+
console.log(parsed.name) // => 'abc'
68+
console.log(parsed.body) // => ' return 1 + e + 2 + f '
69+
console.log(parsed.args) // => [ 'e', 'f', 'rest' ]
70+
console.log(parsed.params) // => 'e, f, rest'
71+
72+
// some useful `is*` properties
73+
console.log(parsed.isNamed) // => true
74+
console.log(parsed.isArrow) // => false
75+
console.log(parsed.isAnonymous) // => false
76+
console.log(parsed.isGenerator) // => false
77+
78+
// use `acorn` parser
79+
const someArrow = (foo, bar) => 1 * foo + bar
80+
const result = parseFunction(someArrow, {
81+
parser: acorn.parse,
82+
ecmaVersion: 2017
83+
})
84+
85+
console.log(result.name) // => 'anonymous'
86+
console.log(result.body) // => '1 * foo + bar'
87+
console.log(result.args) // => [ 'foo', 'bar' ]
88+
89+
console.log(result.isArrow) // => true
90+
console.log(result.isNamed) // => false
91+
console.log(result.isAnonymous) // => true
92+
```
93+
4794
## Related
4895
- [always-done](https://www.npmjs.com/package/always-done): Handle completion and errors with elegance! Support for streams, callbacks, promises, child processes, async/await and sync functions. A drop-in replacement… [more](https://github.com/hybridables/always-done#readme) | [homepage](https://github.com/hybridables/always-done#readme "Handle completion and errors with elegance! Support for streams, callbacks, promises, child processes, async/await and sync functions. A drop-in replacement for [async-done][] - pass 100% of its tests plus more")
4996
- [minibase](https://www.npmjs.com/package/minibase): Minimalist alternative for Base. Build complex APIs with small units called plugins. Works well with most of the already existing… [more](https://github.com/node-minibase/minibase#readme) | [homepage](https://github.com/node-minibase/minibase#readme "Minimalist alternative for Base. Build complex APIs with small units called plugins. Works well with most of the already existing [base][] plugins.")
@@ -124,3 +171,5 @@ _This file was generated by [verb-generate-readme](https://github.com/verbose/ve
124171
[standard-url]: https://github.com/feross/standard
125172
[standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
126173

174+
[acorn]: https://github.com/ternjs/acorn
175+
[babylon]: https://babeljs.io/

index.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,57 @@
1010
const babylon = require('babylon')
1111
const define = require('define-property')
1212

13+
/**
14+
* > Parse a function or string that contains a function,
15+
* using [babylon][] or [acorn][] parsers.
16+
* By default it uses `babylon`, but you can pass custom one
17+
* through `options.parser` option - for example pass `.parse: acorn.parse`
18+
* to force use the `acorn` parser instead.
19+
*
20+
* **Example**
21+
*
22+
* ```js
23+
* const acorn = require('acorn')
24+
* const acornLoose = require('acorn/dist/acorn_loose')
25+
* const parseFunction = require('parse-function')
26+
*
27+
* const myFn = function abc (e, f, ...rest) { return 1 + e + 2 + f }
28+
* const parsed = parseFunction(myFn)
29+
*
30+
* console.log(parsed.name) // => 'abc'
31+
* console.log(parsed.body) // => ' return 1 + e + 2 + f '
32+
* console.log(parsed.args) // => [ 'e', 'f', 'rest' ]
33+
* console.log(parsed.params) // => 'e, f, rest'
34+
*
35+
* // some useful `is*` properties
36+
* console.log(parsed.isNamed) // => true
37+
* console.log(parsed.isArrow) // => false
38+
* console.log(parsed.isAnonymous) // => false
39+
* console.log(parsed.isGenerator) // => false
40+
*
41+
* // use `acorn` parser
42+
* const someArrow = (foo, bar) => 1 * foo + bar
43+
* const result = parseFunction(someArrow, {
44+
* parser: acorn.parse,
45+
* ecmaVersion: 2017
46+
* })
47+
*
48+
* console.log(result.name) // => 'anonymous'
49+
* console.log(result.body) // => '1 * foo + bar'
50+
* console.log(result.args) // => [ 'foo', 'bar' ]
51+
*
52+
* console.log(result.isArrow) // => true
53+
* console.log(result.isNamed) // => false
54+
* console.log(result.isAnonymous) // => true
55+
* ```
56+
*
57+
* @param {Function|String} `code` function to be parsed, it can be string too
58+
* @param {Object} `options` optional, passed directly to [babylon][] or [acorn][];
59+
* you can also pass custom `options.parser` parser
60+
* @return {Object} always returns an object, check `result.valid`
61+
* @api public
62+
*/
63+
1364
module.exports = function parseFunction (code, options) {
1465
let result = getDefaults(code)
1566

@@ -46,6 +97,15 @@ module.exports = function parseFunction (code, options) {
4697
return result
4798
}
4899

100+
/**
101+
* > Create default result object,
102+
* and normalize incoming arguments.
103+
*
104+
* @param {Function|String} `code`
105+
* @return {Object} result
106+
* @api private
107+
*/
108+
49109
function getDefaults (code) {
50110
const result = {
51111
name: 'anonymous',
@@ -62,6 +122,16 @@ function getDefaults (code) {
62122
return hiddens(result, code)
63123
}
64124

125+
/**
126+
* > Create hidden properties into
127+
* the result object.
128+
*
129+
* @param {Object} `result`
130+
* @param {Function|String} code
131+
* @return {Object} result
132+
* @api private
133+
*/
134+
65135
function hiddens (result, code) {
66136
define(result, 'defaults', {})
67137
define(result, 'orig', code || '')
@@ -76,6 +146,17 @@ function hiddens (result, code) {
76146
return result
77147
}
78148

149+
/**
150+
* > Update the result object with some
151+
* useful information like is given function
152+
* is async, generator or it is a FunctionExpression.
153+
*
154+
* @param {Object} `node`
155+
* @param {Object} `result`
156+
* @return {Object} `node`
157+
* @api private
158+
*/
159+
79160
function update (node, result) {
80161
const asyncExpr = node.expression && node.expression.async
81162
const genExpr = node.expression && node.expression.generator
@@ -87,6 +168,14 @@ function update (node, result) {
87168
return node
88169
}
89170

171+
/**
172+
* > Visit each arrow expression.
173+
*
174+
* @param {Object} `node`
175+
* @param {Object} `result`
176+
* @return {Object} `node`
177+
* @api private
178+
*/
90179
function visitArrows (node, result) {
91180
const isArrow = node.expression.type === 'ArrowFunctionExpression'
92181

@@ -98,6 +187,14 @@ function visitArrows (node, result) {
98187
return node
99188
}
100189

190+
/**
191+
* > Visit each function declaration.
192+
*
193+
* @param {Object} `node`
194+
* @param {Object} `result`
195+
* @return {Object} `node`
196+
* @api private
197+
*/
101198
function visitFunctions (node, result) {
102199
if (node.type === 'FunctionDeclaration') {
103200
result.name = node.id.name
@@ -106,6 +203,15 @@ function visitFunctions (node, result) {
106203
return node
107204
}
108205

206+
/**
207+
* > Visit each of the params in the
208+
* given function.
209+
*
210+
* @param {Object} `node`
211+
* @param {Object} `result`
212+
* @return {Object} `result`
213+
* @api private
214+
*/
109215
function visitParams (node, result) {
110216
if (node.params.length) {
111217
node.params.forEach(function (param) {
@@ -133,6 +239,17 @@ function visitParams (node, result) {
133239
return result
134240
}
135241

242+
/**
243+
* > Gets the raw body, without the
244+
* surrounding curly braces. It also preserves
245+
* the whitespaces and newlines - they are original.
246+
*
247+
* @param {Object} `node`
248+
* @param {Object} `result`
249+
* @return {Object} `result`
250+
* @api private
251+
*/
252+
136253
function fixBody (node, result) {
137254
result.body = result.orig.slice(node.body.start, node.body.end)
138255

@@ -146,6 +263,16 @@ function fixBody (node, result) {
146263
return result
147264
}
148265

266+
/**
267+
* > Tweak the names. Each anonymous function
268+
* initially are renamed to some unique name
269+
* and it is restore to be `anonymous`.
270+
*
271+
* @param {Object} `result`
272+
* @return {Object} `result`
273+
* @api private
274+
*/
275+
149276
function fixName (result) {
150277
// tweak throwing if anonymous function
151278
const isAnon = result.name === '____foo$1o__i3n8v$al4i1d____'

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@
106106
"once",
107107
"standard-version",
108108
"verb",
109-
"verb-generate-readme"
109+
"verb-generate-readme",
110+
"acorn",
111+
"babylon"
110112
]
111113
}
112114
}

0 commit comments

Comments
 (0)