Function minifier::js::replace_tokens_with
source · [−]pub fn replace_tokens_with<'a, 'b: 'a, F: Fn(&Token<'a>) -> Option<Token<'b>>>(
tokens: Tokens<'a>,
callback: F
) -> Tokens<'a>
Expand description
Replace given tokens with others.
Example
extern crate minifier;
use minifier::js::{Keyword, Token, replace_tokens_with, simple_minify};
fn main() {
let js = r#"
function replaceByNull(data, func) {
for (var i = 0; i < data.length; ++i) {
if func(data[i]) {
data[i] = null;
}
}
}
}"#.into();
let js_minified = simple_minify(js)
.apply(|f| {
replace_tokens_with(f, |t| {
if *t == Token::Keyword(Keyword::Null) {
Some(Token::Other("N"))
} else {
None
}
})
});
println!("{}", js_minified.to_string());
}
The previous code will have all its null
keywords replaced with N
. In such cases,
don’t forget to include the definition of N
in the returned minified javascript:
var N = null;