f0a1024f230bd393ebc9b13e42341792071cab11fe5de2d100e7f59e95a1eee25cb9aca246a75c44432612906fbcd48378b87325aabf783d5457d2aff530df-exec 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # reduce-function-call [![Build Status](https://travis-ci.org/MoOx/reduce-function-call.svg?branch=master)](https://travis-ci.org/MoOx/reduce-function-call)
  2. > Reduce function calls in a string, using a callback
  3. ---
  4. [Professionally supported reduce-function-call is now available](https://tidelift.com/subscription/pkg/npm-reduce-function-call?utm_source=npm-reduce-function-call&utm_medium=referral&utm_campaign=readme)
  5. ---
  6. ## Installation
  7. ```console
  8. npm install reduce-function-call
  9. ```
  10. ## Usage
  11. ```js
  12. var reduceFunctionCall = require("reduce-function-call")
  13. reduceFunctionCall("foo(1)", "foo", function(body) {
  14. // body === "1"
  15. return parseInt(body, 10) + 1
  16. })
  17. // "2"
  18. var nothingOrUpper = function(body, functionIdentifier) {
  19. // ignore empty value
  20. if (body === "") {
  21. return functionIdentifier + "()"
  22. }
  23. return body.toUpperCase()
  24. }
  25. reduceFunctionCall("bar()", "bar", nothingOrUpper)
  26. // "bar()"
  27. reduceFunctionCall("upper(baz)", "upper", nothingOrUpper)
  28. // "BAZ"
  29. reduceFunctionCall("math(math(2 + 2) * 4 + math(2 + 2)) and other things", "math", function(body, functionIdentifier, call) {
  30. try {
  31. return eval(body)
  32. }
  33. catch (e) {
  34. return call
  35. }
  36. })
  37. // "20 and other things"
  38. reduceFunctionCall("sha bla blah() blaa bla() abla() aabla() blaaa()", /\b([a-z]?bla[a-z]?)\(/, function(body, functionIdentifier) {
  39. if (functionIdentifier === "bla") {
  40. return "ABRACADABRA"
  41. }
  42. return functionIdentifier.replace("bla", "!")
  43. }
  44. // "sha bla !h blaa ABRACADABRA a! aabla() blaaa()"
  45. ```
  46. See [unit tests](test/index.js) for others examples.
  47. ## Contributing
  48. Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
  49. ```console
  50. git clone https://github.com/MoOx/reduce-function-call.git
  51. git checkout -b patch-1
  52. npm install
  53. npm test
  54. ```
  55. ---
  56. ## [Changelog](CHANGELOG.md)
  57. ## [License](LICENSE)
  58. ---
  59. ## Security contact information
  60. To report a security vulnerability, please use the
  61. [Tidelift security contact](https://tidelift.com/security). Tidelift will
  62. coordinate the fix and disclosure.