example.js 609 B

123456789101112131415161718192021222324252627
  1. const genfun = require('./')
  2. const { d } = genfun.formats
  3. function multiply (a, b) {
  4. return a * b
  5. }
  6. function addAndMultiplyNumber (val) {
  7. const fn = genfun(`
  8. function (n) {
  9. if (typeof n !== 'number') {
  10. throw new Error('argument should be a number')
  11. }
  12. const result = multiply(${d(val)}, n + ${d(val)})
  13. return result
  14. }
  15. `)
  16. // use fn.toString() if you want to see the generated source
  17. return fn.toFunction({multiply})
  18. }
  19. const addAndMultiply2 = addAndMultiplyNumber(2)
  20. console.log(addAndMultiply2.toString())
  21. console.log('(3 + 2) * 2 =', addAndMultiply2(3))