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

Commit

Permalink
fix(docs): update dogs
Browse files Browse the repository at this point in the history
TAG: latest

Resolves #48
  • Loading branch information
tunnckoCore committed Oct 6, 2017
1 parent f70952c commit 2e7eaea
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 26 deletions.
151 changes: 145 additions & 6 deletions .verb.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

> {%= description %}
_You might also be interested in [function-arguments][] library if you need more lightweight solution and need for just getting the names of the function arguments._
<div id="thetop"></div>

{%= include('highlight') %}

## Quality Assurance :100:

Expand Down Expand Up @@ -44,17 +46,154 @@ You may also read the [Contributing Guide](./CONTRIBUTING.md). There, beside _"H

## Install

This project requires [Node.js][nodeversion-url] v{%= engines.node.slice(2) %} and above. Use [npm](https://www.npmjs.com) to install it.
This project requires [**Node.js**][nodeversion-url] **v{%= engines.node.slice(2) %}** and above. Use [**yarn**](https://yarnpkg.com) **v{%= engines.yarn.slice(2) %}** / [**npm**](https://npmjs.com) **v{%= engines.npm.slice(2) %}** or above to install it.

```
$ yarn add {%= name %}
```

## Which version to use?

There's no breaking changes between the `v2.x` version. The only breaking is `v2.1` which also is not
working properly, so no use it.

**Use v2.0.x**

When you don't need support for `arrow functions` and `es6 default params`. This version
uses a RegExp expression to work.

**Use v2.2.x**

Only when you need a _basic_ support for `es6 features` like arrow functions. This version
uses a RegExp expression to work.

**Use v2.3.x**

When you want _full*_ support for `arrow functions` and `es6 default params`. Where this "full",
means "almost full", because it has bugs. This version also uses (`acorn.parse`) real parser
to do the parsing.

**Use v3.x**

When you want to use different parser instead of the default `babylon.parse`, by passing custom
parse function to the `options.parse` option. **From this version we require `node >= 4`**.

**Use v4.x**

When you want full customization and most stable support for old and modern features. This version
uses `babylon.parseExpression` for parsing and provides a [Plugins API](#plugins-architecture).
See the [Features](#features) section for more info.

**Use v5.x**

It is basically the same as `v4`, but requires Node 6 & npm 5. Another is boilerplate stuff.

**[back to top](#thetop)**

## Notes

### Throws in one specific case

> _see: [issue #3](https://github.com/tunnckoCore/parse-function/issues/3) and [test/index.js#L229-L235](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L229-L235)_
It may throw in one specific case, otherwise it won't throw, so you should
relay on the `result.isValid` for sure.

### Function named _"anonymous"_

> _see: [test/index.js#L319-L324](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L319-L324) and [Result](#result) section_
If you pass a function which is named _"anonymous"_ the `result.name` will be `'anonymous'`,
but the `result.isAnonymous` will be `false` and `result.isNamed` will be `true`, because
in fact it's a named function.

### Real anonymous function

> _see: [test/index.js#L326-L331](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L326-L331) and [Result](#result) section_
Only if you pass really an anonymous function you will get `result.name` equal to `null`,
`result.isAnonymous` equal to `true` and `result.isNamed` equal to `false`.

**[back to top](#thetop)**

### Plugins Architecture

> _see: the [.use](#use) method, [test/index.js#L305-L317](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L305-L317) and [test/index.js#L396-L414](https://github.com/tunnckoCore/parse-function/blob/master/test/index.js#L396-L414)_
A more human description of the plugin mechanism. Plugins are **synchronous** - no support
and no need for **async** plugins here, but notice that you can do that manually, because
that exact architecture.

The first function that is passed to the [.use](#use) method is used for extending the core API,
for example adding a new method to the `app` instance. That function is immediately invoked.

```js
const parseFunction = require('parse-function')
const app = parseFunction()

app.use((self) => {
// self is same as `app`
console.log(self.use)
console.log(self.parse)
console.log(self.define)

self.define(self, 'foo', (bar) => bar + 1)
})

console.log(app.foo(2)) // => 3
```
$ npm install {%= name %}

On the other side, if you want to access the AST of the parser, you should return a function
from that plugin, which function is passed with `(node, result)` signature.

This function is lazy plugin, it is called only when the [.parse](#parse) method is called.

```js
const parseFunction = require('parse-function')
const app = parseFunction()

app.use((self) => {
console.log('immediately called')

return (node, result) => {
console.log('called only when .parse is invoked')
console.log(node)
console.log(result)
}
})
```

Where **1)** the `node` argument is an object - actual and real AST Node coming from the parser
and **2)** the `result` is an object too - the end [Result](#result), on which
you can add more properties if you want.

**[back to top](#thetop)**

## API
Review carefully the provided examples and the working [tests](./test.js).
Review carefully the provided examples and the working [tests](./test/index.js).

{%= apidocs('src/index.js') %}

**[back to top](#thetop)**

### Result
> In the result object you have `name`, `args`, `params`, `body` and few hidden properties
that can be useful to determine what the function is - arrow, regular, async/await or generator.

* `name` **{String|null}**: name of the passed function or `null` if anonymous
* `args` **{Array}**: arguments of the function
* `params` **{String}**: comma-separated list representing the `args`
* `defaults` **{Object}**: key/value pairs, useful when use ES2015 default arguments
* `body` **{String}**: actual body of the function, respects trailing newlines and whitespaces
* `isValid` **{Boolean}**: is the given value valid or not, that's because it never throws!
* `isAsync` **{Boolean}**: `true` if function is ES2015 async/await function
* `isArrow` **{Boolean}**: `true` if the function is arrow function
* `isNamed` **{Boolean}**: `true` if function has name, or `false` if is anonymous
* `isGenerator` **{Boolean}**: `true` if the function is ES2015 generator function
* `isAnonymous` **{Boolean}**: `true` if the function don't have name

**[back to top](#thetop)**

{% if (verb.related && verb.related.list && verb.related.list.length) { %}
## Related
{%= related(verb.related.list, { words: 12 }) %}
Expand All @@ -70,12 +209,12 @@ Please read the [Contributing Guide](./CONTRIBUTING.md) and [Code of Conduct](./
- [codementor/tunnckoCore](https://codementor.io/tunnckoCore)

## License
{%= copyright({ start: 2016, linkify: true, prefix: 'Copyright', symbol: '©' }) %} {%= licenseStatement %}
{%= copyright({ start: licenseStart, linkify: true, prefix: 'Copyright', symbol: '©' }) %} {%= licenseStatement %}

***

{%= include('footer') %}
Project scaffolded using [charlike-cli][].
Project scaffolded and managed with [hela][].

{%= reflinks(verb.reflinks) %}

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017-present Charlike Mike Reagent <open.source.charlike@gmail.com>
Copyright (c) 2016-present Charlike Mike Reagent <open.source.charlike@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit 2e7eaea

Please sign in to comment.