Blame view

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