What is reduce-function-call?
The reduce-function-call npm package allows you to reduce the arguments of a function call within a string. This can be particularly useful for processing CSS functions or other string-based function calls.
What are reduce-function-call's main functionalities?
Reduce CSS function calls
This feature allows you to reduce the arguments of CSS function calls. In this example, the `calc` function call is reduced to a simpler form.
const reduceFunctionCall = require('reduce-function-call');
const input = 'calc(100% - 50px)';
const output = reduceFunctionCall(input, 'calc', (body) => {
return '50%';
});
console.log(output); // 'calc(50%)'
Custom function call reduction
You can use this feature to reduce custom function calls within a string. In this example, the `myFunc` function call is reduced to a single argument.
const reduceFunctionCall = require('reduce-function-call');
const input = 'myFunc(1, 2, 3)';
const output = reduceFunctionCall(input, 'myFunc', (body) => {
return '4';
});
console.log(output); // 'myFunc(4)'
Other packages similar to reduce-function-call
postcss-calc
The postcss-calc package is a PostCSS plugin that reduces calc() references in CSS. It is specifically designed for CSS and provides more advanced optimizations for calc() functions compared to reduce-function-call.
cssnano
cssnano is a modular minifier for CSS that includes a variety of optimizations, including reducing calc() functions. It offers a broader range of CSS optimizations beyond just function call reductions.
reduce-function-call
Reduce function calls in a string, using a callback
Professionally supported reduce-function-call is now available
Installation
npm install reduce-function-call
Usage
var reduceFunctionCall = require("reduce-function-call")
reduceFunctionCall("foo(1)", "foo", function(body) {
return parseInt(body, 10) + 1
})
var nothingOrUpper = function(body, functionIdentifier) {
if (body === "") {
return functionIdentifier + "()"
}
return body.toUpperCase()
}
reduceFunctionCall("bar()", "bar", nothingOrUpper)
reduceFunctionCall("upper(baz)", "upper", nothingOrUpper)
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
}
})
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", "!")
}
See unit tests for others examples.
Contributing
Work on a branch, install dev-dependencies, respect coding style & run tests before submitting a bug fix or a feature.
git clone https://github.com/MoOx/reduce-function-call.git
git checkout -b patch-1
npm install
npm test
Security contact information
To report a security vulnerability, please use the
Tidelift security contact. Tidelift will
coordinate the fix and disclosure.