项目原始demo,不改动
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # reduce-function-call [![Build Status](https://travis-ci.org/MoOx/reduce-function-call.png)](https://travis-ci.org/MoOx/reduce-function-call)
  2. > Reduce function calls in a string, using a callback
  3. ## Installation
  4. ```bash
  5. npm install reduce-function-call
  6. ```
  7. ## Usage
  8. ```js
  9. var reduceFunctionCall = require("reduce-function-call")
  10. reduceFunctionCall("foo(1)", "foo", function(body) {
  11. // body === "1"
  12. return parseInt(body, 10) + 1
  13. })
  14. // "2"
  15. var nothingOrUpper = function(body, functionIdentifier) {
  16. // ignore empty value
  17. if (body === "") {
  18. return functionIdentifier + "()"
  19. }
  20. return body.toUpperCase()
  21. }
  22. reduceFunctionCall("bar()", "bar", nothingOrUpper)
  23. // "bar()"
  24. reduceFunctionCall("upper(baz)", "upper", nothingOrUpper)
  25. // "BAZ"
  26. reduceFunctionCall("math(math(2 + 2) * 4 + math(2 + 2)) and other things", "math", function(body, functionIdentifier, call) {
  27. try {
  28. return eval(body)
  29. }
  30. catch (e) {
  31. return call
  32. }
  33. })
  34. // "20 and other things"
  35. reduceFunctionCall("sha bla blah() blaa bla() abla() aabla() blaaa()", /\b([a-z]?bla[a-z]?)\(/, function(body, functionIdentifier) {
  36. if (functionIdentifier === "bla") {
  37. return "ABRACADABRA"
  38. }
  39. return functionIdentifier.replace("bla", "!")
  40. }
  41. // "sha bla !h blaa ABRACADABRA a! aabla() blaaa()"
  42. ```
  43. See [unit tests](test/index.js) for others examples.
  44. ## Contributing
  45. Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
  46. ```bash
  47. git clone https://github.com/MoOx/reduce-function-call.git
  48. git checkout -b patch-1
  49. npm install
  50. npm test
  51. ```
  52. ## [Changelog](CHANGELOG.md)
  53. ## [License](LICENSE-MIT)