Gathering detailed insights and metrics for parse-function
Gathering detailed insights and metrics for parse-function
Gathering detailed insights and metrics for parse-function
Gathering detailed insights and metrics for parse-function
css-color-function
A parser and converter for Tab Atkins's proposed color function in CSS.
@webassemblyjs/floating-point-hex-parser
A function to parse floating point hexadecimal strings as defined by the WebAssembly specification
fn.name
Extract names from functions
auto-parse
Automatically convert any value to its best matching JavaScript type. Supports numbers, booleans, objects, arrays, BigInt, Symbol, comma-separated numbers, prefix stripping, allowed type enforcement and a plugin API.
Delivering delightful digital solutions. Monorepo of monorepos of Open Source packages with combined ~100M/month downloads, semantically versioned following @conventional-commits. Fully powered ES Modules, @Airbnb @ESLint + @Prettier, independent & fixed versioning. Quality with @Actions, CodeQL, & Dependabot.
npm install parse-function
Typescript
Module System
Min. Node Version
Node Version
NPM Version
96.6
Supply Chain
99.5
Quality
78.2
Maintenance
100
Vulnerability
81.3
License
parse-function@5.6.10
Updated on Mar 28, 2020
to-file-path@2.0.4
Updated on Mar 28, 2020
@tunnckocore/package-json@2.0.4
Updated on Mar 28, 2020
@tunnckocore/jest-runner-eslint@1.2.9
Updated on Mar 28, 2020
stringify-github-short-url@3.3.8
Updated on Mar 28, 2020
prettier-plugin-pkgjson@0.2.8
Updated on Mar 28, 2020
JavaScript (85.76%)
TypeScript (14.24%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
484 Stars
601 Commits
19 Forks
10 Watchers
44 Branches
20 Contributors
Updated on Feb 23, 2025
Latest Version
5.6.10
Package Id
parse-function@5.6.10
Size
16.69 kB
NPM Version
lerna/3.20.2/node@v12.14.0+x64 (linux)
Node Version
12.14.0
Published on
Mar 28, 2020
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
4
Parse a function into an object using espree, acorn or babylon parsers. Extensible through Smart Plugins
Please consider following this project's author, Charlike Mike Reagent, and :star: the project to show your :heart: and support.
If you have any how-to kind of questions, please read the Contributing Guide and Code of Conduct documents. For bugs reports and feature requests, please create an issue or ping @tunnckoCore at Twitter.
Project is semantically versioned & automatically released from GitHub Actions with Lerna.
Topic | Contact |
---|---|
Any legal or licensing questions, like private or commerical use | |
For any critical problems and security reports | |
Consulting, professional support, personal or team training | |
For any questions about Open Source, partnerships and sponsoring |
.parseExpression
method of the Babel v7.x
parseroptions.parse
(TOC generated by verb using markdown-toc)
This project requires Node.js >=8.11 (see
Support & Release Policy).
Install it using yarn or
npm.
We highly recommend to use Yarn when you
think to contribute to this project.
1$ yarn add parse-function
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. See the 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.
see: issue #3 and 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.
see: test/index.js#L319-L324 and 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.
see: test/index.js#L326-L331 and 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
.
see: the .use method, test/index.js#L305-L317 and 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 method is used for
extending the core API, for example adding a new method to the app
instance.
That function is immediately invoked.
1const parseFunction = require('parse-function'); 2const app = parseFunction(); 3 4app.use((self) => { 5 // self is same as `app` 6 console.log(self.use); 7 console.log(self.parse); 8 console.log(self.define); 9 10 self.define(self, 'foo', (bar) => bar + 1); 11}); 12 13console.log(app.foo(2)); // => 3
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 method is called.
1const parseFunction = require('parse-function'); 2const app = parseFunction(); 3 4app.use((self) => { 5 console.log('immediately called'); 6 7 return (node, result) => { 8 console.log('called only when .parse is invoked'); 9 console.log(node); 10 console.log(result); 11 }; 12});
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, on which you can add more properties if you want.
Generated using jest-runner-docs.
Initializes with optional
opts
object which is passed directly to the desired parser and returns an object with.use
and.parse
methods. The default parse which is used is babylon's.parseExpression
method fromv7
.
1function(opts = {})
opts
{object} - optional, merged with options passed to .parse
methodreturns
{object} - app object with .use
and .parse
methods
1const parseFunction = require('parse-function'); 2 3const app = parseFunction({ 4 ecmaVersion: 2017, 5}); 6 7const fixtureFn = (a, b, c) => { 8 a = b + c; 9 return a + 2; 10}; 11 12const result = app.parse(fixtureFn); 13console.log(result); 14 15// see more 16console.log(result.name); // => null 17console.log(result.isNamed); // => false 18console.log(result.isArrow); // => true 19console.log(result.isAnonymous); // => true 20 21// array of names of the arguments 22console.log(result.args); // => ['a', 'b', 'c'] 23 24// comma-separated names of the arguments 25console.log(result.params); // => 'a, b, c'
Parse a given
code
and returns aresult
object with useful properties - such asname
,body
andargs
. By default it uses Babylon parser, but you can switch it by passingoptions.parse
- for exampleoptions.parse: acorn.parse
. In the below example will show how to useacorn
parser, instead of the default one.
code
{Function|string} - any kind of function or string to be parsedoptions
{object} - directly passed to the parser babylon, acorn, espreeoptions.parse
{Function} - by default babylon.parseExpression
, all
options
are passed as second argumentreturns
{object} - result see result section for more info
1const acorn = require('acorn'); 2const parseFn = require('parse-function'); 3const app = parseFn(); 4 5const fn = function foo(bar, baz) { 6 return bar * baz; 7}; 8const result = app.parse(fn, { 9 parse: acorn.parse, 10 ecmaVersion: 2017, 11}); 12 13console.log(result.name); // => 'foo' 14console.log(result.args); // => ['bar', 'baz'] 15console.log(result.body); // => ' return bar * baz ' 16console.log(result.isNamed); // => true 17console.log(result.isArrow); // => false 18console.log(result.isAnonymous); // => false 19console.log(result.isGenerator); // => false
Add a plugin
fn
function for extending the API or working on the AST nodes. Thefn
is immediately invoked and passed withapp
argument which is instance ofparseFunction()
call. Thatfn
may return another function that accepts(node, result)
signature, wherenode
is an AST node andresult
is an object which will be returned result from the.parse
method. This retuned function is called on each node only when.parse
method is called.
fn
{Function} - plugin to be calledreturns
{object} - app instance for chainingSee Plugins Architecture section.
1// plugin extending the `app` 2app.use((app) => { 3 app.define(app, 'hello', (place) => `Hello ${place}!`); 4}); 5 6const hi = app.hello('World'); 7console.log(hi); // => 'Hello World!' 8 9// or plugin that works on AST nodes 10app.use((app) => (node, result) => { 11 if (node.type === 'ArrowFunctionExpression') { 12 result.thatIsArrow = true; 13 } 14 return result; 15}); 16 17const result = app.parse((a, b) => a + b + 123); 18console.log(result.name); // => null 19console.log(result.isArrow); // => true 20console.log(result.thatIsArrow); // => true 21 22const result = app.parse(function foo() { 23 return 123; 24}); 25console.log(result.name); // => 'foo' 26console.log(result.isArrow); // => false 27console.log(result.thatIsArrow); // => undefined
Define a non-enumerable property on an object. Just a convenience mirror of the define-property library, so check out its docs. Useful to be used in plugins.
obj
{object} - the object on which to define the propertyprop
{string} - the name of the property to be defined or modifiedval
{any} - the descriptor for the property being defined or modifiedreturns
{object} - obj the passed object, but modified
1const parseFunction = require('parse-function'); 2const app = parseFunction(); 3 4// use it like `define-property` lib 5const obj = {}; 6app.define(obj, 'hi', 'world'); 7console.log(obj); // => { hi: 'world' } 8 9// or define a custom plugin that adds `.foo` property 10// to the end result, returned from `app.parse` 11app.use((app) => { 12 return (node, result) => { 13 // this function is called 14 // only when `.parse` is called 15 16 app.define(result, 'foo', 123); 17 18 return result; 19 }; 20}); 21 22// fixture function to be parsed 23const asyncFn = async (qux) => { 24 const bar = await Promise.resolve(qux); 25 return bar; 26}; 27 28const result = app.parse(asyncFn); 29 30console.log(result.name); // => null 31console.log(result.foo); // => 123 32console.log(result.args); // => ['qux'] 33 34console.log(result.isAsync); // => true 35console.log(result.isArrow); // => true 36console.log(result.isNamed); // => false 37console.log(result.isAnonymous); // => true
Please read the Contributing Guide and Code of Conduct documents for advices.
For bug reports and feature requests, please join our community forum and open a thread there with prefixing the title of the thread with the name of the project if there's no separate channel for it.
Consider reading the Support and Release Policy guide if you are interested in what are the supported Node.js versions and how we proceed. In short, we support latest two even-numbered Node.js release lines.
Become a Partner or Sponsor? :dollar: Check the OpenSource Commision (tier). :tada: You can get your company logo, link & name on this file. It's also rendered on package's page in npmjs.com and yarnpkg.com sites too! :rocket:
Not financial support? Okey! Pull requests, stars and all kind of contributions are always welcome. :sparkles:
This project follows the all-contributors specification. Contributions of any kind are welcome!
Thanks goes to these wonderful people (emoji key), consider showing your support to them:
Charlike Mike Reagent ???? ???? ???? ???? ???? ⚠️ |
Copyright (c) 2016-present, Charlike Mike Reagent
<opensource@tunnckocore.com>
& contributors.
Released under the MPL-2.0 License.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
Found 0/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More