aboutsummaryrefslogtreecommitdiff
path: root/node_modules/reduce-function-call/index.js
diff options
context:
space:
mode:
authorruki <waruqi@gmail.com>2018-11-08 00:38:48 +0800
committerruki <waruqi@gmail.com>2018-11-07 21:53:09 +0800
commit26105034da4fcce7ac883c899d781f016559310d (patch)
treec459a5dc4e3aa0972d9919033ece511ce76dd129 /node_modules/reduce-function-call/index.js
parent2c77f00f1a7ecb6c8192f9c16d3b2001b254a107 (diff)
downloadxmake-docs-26105034da4fcce7ac883c899d781f016559310d.tar.gz
xmake-docs-26105034da4fcce7ac883c899d781f016559310d.zip
switch to vuepress
Diffstat (limited to 'node_modules/reduce-function-call/index.js')
-rwxr-xr-xnode_modules/reduce-function-call/index.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/node_modules/reduce-function-call/index.js b/node_modules/reduce-function-call/index.js
new file mode 100755
index 00000000..c20effaa
--- /dev/null
+++ b/node_modules/reduce-function-call/index.js
@@ -0,0 +1,74 @@
+/*
+ * Module dependencies
+ */
+var balanced = require("balanced-match")
+
+/**
+ * Expose `reduceFunctionCall`
+ *
+ * @type {Function}
+ */
+module.exports = reduceFunctionCall
+
+/**
+ * Walkthrough all expressions, evaluate them and insert them into the declaration
+ *
+ * @param {Array} expressions
+ * @param {Object} declaration
+ */
+
+function reduceFunctionCall(string, functionRE, callback) {
+ var call = string
+ return getFunctionCalls(string, functionRE).reduce(function(string, obj) {
+ return string.replace(obj.functionIdentifier + "(" + obj.matches.body + ")", evalFunctionCall(obj.matches.body, obj.functionIdentifier, callback, call, functionRE))
+ }, string)
+}
+
+/**
+ * Parses expressions in a value
+ *
+ * @param {String} value
+ * @returns {Array}
+ * @api private
+ */
+
+function getFunctionCalls(call, functionRE) {
+ var expressions = []
+
+ var fnRE = typeof functionRE === "string" ? new RegExp("\\b(" + functionRE + ")\\(") : functionRE
+ do {
+ var searchMatch = fnRE.exec(call)
+ if (!searchMatch) {
+ return expressions
+ }
+ if (searchMatch[1] === undefined) {
+ throw new Error("Missing the first couple of parenthesis to get the function identifier in " + functionRE)
+ }
+ var fn = searchMatch[1]
+ var startIndex = searchMatch.index
+ var matches = balanced("(", ")", call.substring(startIndex))
+
+ if (!matches || matches.start !== searchMatch[0].length - 1) {
+ throw new SyntaxError(fn + "(): missing closing ')' in the value '" + call + "'")
+ }
+
+ expressions.push({matches: matches, functionIdentifier: fn})
+ call = matches.post
+ }
+ while (fnRE.test(call))
+
+ return expressions
+}
+
+/**
+ * Evaluates an expression
+ *
+ * @param {String} expression
+ * @returns {String}
+ * @api private
+ */
+
+function evalFunctionCall (string, functionIdentifier, callback, call, functionRE) {
+ // allow recursivity
+ return callback(reduceFunctionCall(string, functionRE, callback), functionIdentifier, call)
+}