func2code
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

Function to Code

func2code's main usage is to get the inner code of a function.

As a by-product, func2code can also get various information of the function, like params, name and name-code.

Import

ESM

import func2code from 'func2code';

CJS

const func2code = require('func2code');

HTML

<script src="func2code/index.js"></script>
<script>
  /// <reference path="func2code/global.d.ts" />
  console.log(func2code);
</script>

Usage

// Input
func2code.getInnerCode(()=>1+1);

// Output
'return 1+1;'
// Input
func2code.split(async function abc(
  a, k,
  {[`${[1,2,3].join('')}`]: b},
  [c, d]
) {
  return 1 + 2;
});

// Output
{
  params: [
    'a',
    'k',
    "{[`${[1,2,3].join('')}`]: b}",
    '[c, d]'
  ],
  innerCode: '\n  return 1 + 2;\n',
  nameCode: '"abc"',
  name: 'abc',
  isArrow: false,
  isAsync: true,
  isGetter: false,
  isSetter: false,
  isGenerator: false
}
// Input
func2code.getNameCode({
  ['n'+`${(()=>1+1)[[
    'to',
    'St',
    'ri',
    'ng',
  ].join('')]()}`+123](n) {
    return n + 1;
  }
}['n()=>1+1123'])

// Output
"'n'+`${(()=>1+1)[[ 'to', 'St', 'ri', 'ng', ].join('')]()}`+123"

See all the test cases

Package Sidebar

Install

npm i func2code

Weekly Downloads

2

Version

1.2.1

License

MIT

Unpacked Size

16.6 kB

Total Files

6

Last publish

Collaborators

  • e0selmy4v