Giter Site home page Giter Site logo

farse's Introduction

Coverage Status

About

Use case—you've got an arrow/generator/normal function and you want: its name, an array of its declared arguments, its body, and/or what kind of function it is.

Why?

There are wonderful Javascript parsers out there (see for example acorn or esprima). They are generalists. This function parser is meant to be universal, small in scope, and small in size.

Getting started

If using it from node, go ahead and:

npm install farse

...then from your Javascript source do:

const farse = require('farse');

When using from the browser, download it, then in your html include a <script> pointing to the correct file. The script will make farse a global variable.

For example you might run:

npm install farse

...and then you might put this in your html (assuming the node_modules directory is statically served to the client):

<script src="/farse/dist/farse.min.js"></script>

...and now farse would be a global variable available to any subsequent client scripts.

Methods

This library's got three functions:

In general, I recommend you default to using farse.inverse.inexact over farse.inverse.exact. The .inexact version, though less precise, is slightly safer because it uses the Function constructor instead of eval. See the documentation below for more details.

farse

About

Input...

<Function>

Output...

{
  name: <String>,
  params: [<String>],
  body: <String>,
  kind: <'StandardFunction','ArrowFunction','GeneratorFunction'>
}

Takes any function and returns a parsed object with the name, parameters, body, and "kind" of that function. The kind designates whether the original function is a standard function, an arrow function, or a generator function.

Examples

An empty function:

farse(function () {});
/*
{
  name: '',
  params: [],
  body: '',
  kind: 'StandardFunction'
}
*/

A more complicated function with some complex es6ish parameter declarations:

farse(function foo (x=(":"+")"), {y}) {
  return x + y + 100;
});
/*
{
  name: 'foo',
  params: ['x=(":"+")")', ' {y}'],
  body: '\n  return x + y + 100;\n',
  kind: 'StandardFunction'
}
*/

An arrow function:

farse(a=>a*1000);
/*
{
  name: '',
  params: ['a'],
  body: 'return a*100;',
  kind: 'ArrowFunction'
}
*/

A generator:

farse(function* doThings (bar) {
  yield bar + 10;
  return bar - 10;
});
/*
{
  name: 'doThings',
  params: ['bar'],
  body: '\n  yield bar + 10;\n  return bar - 10;\n',
  kind: 'GeneratorFunction'
}
*/

farse.inverse.inexact

Note: Employ caution with this method because it utilizes the Function constructor.

Input...

{
  params: [<String>],
  body: <String>,
  kind: <'StandardFunction','ArrowFunction','GeneratorFunction'>
}

Output...

<Function>

Takes an object representing a parsed function (e.g. the result of farseing a function) and spits back a function that is a behavioral copy of the original. That is, it takes the same inputs and returns the same outputs. However, the resulting function will not share the original's name, and if the parsed .kind is 'ArrowFunction' the result will nevertheless come back as an standard/ordinary function. This method uses the Function constructor to do its work, so is somewhat safer than its .exact alternative which uses eval.

farse.inverse.exact

Note: Employ caution with this method because it utilizes eval.

Input...

{
  name: <String>,
  params: [<String>],
  body: <String>,
  kind: <'StandardFunction','ArrowFunction','GeneratorFunction'>
}

Output...

<Function>

Takes an object representing a parsed function (e.g. the result of farseing a function) and returns a function that is not only a behavioral copy of the original, but also shares its .name. Furthermore if the original was an arrow function, the clone will also be an arrow function.

Similar libraries

Contributing

Pull requests / issues / comments / hate mail welcome!

If you'd like to run the tests, download this repo, open a terminal, navigate to it, then run:

npm install

...and once that's done:

npm test

farse's People

Contributors

omriackley avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.