[][src]Function minifier::js::replace_token_with

pub fn replace_token_with<'a, 'b: 'a, F: Fn(&Token<'a>) -> Option<Token<'b>>>(
    tokens: Tokens<'a>,
    callback: F
) -> Tokens<'a>

Minifies a given JS source code and to replace keywords.

Example

extern crate minifier;
use minifier::js::{Keyword, Token, replace_token_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_token_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;