<commit_before>/**
 * provide data bind ability
 * 
 * @author <NAME>
 * @date 2015/04/13
 * released under terms of MIT lincese
 */

;(function(global, doc, Sizzle, undefined) {
    
    'use strict';

    var Binder = Binder || {
        scope: {},
        version: '0.0.1'
    };

    function findScope(id) {
        var tag = ['html', 'body', 'head', 'title', 'div', 'ul', 'li', 'table',
                    'tr', 'td', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span'],
            i = 0,
            len = tag.length,
            scope;

        for (; i < len; i++) {
            var selector = tag[i] + '[bind-scope="' + id + '"]',
                temp = Sizzle(selector);
            if (temp && temp.length) {
                scope = temp[0];
                break;
            }
        }

        return scope;

    };

    function getHtml(scope) {
        return scope.innerHTML;
    };

    function analyseBinderTag(html) {
        var tagReg = /\{\{\w+\}\}/g,
            match = tagReg.exec(html),
            tags = [];

        while (match) {
            var obj = {},
                key = match[0].replace(/\{\{/, '').replace(/\}\}/, '');
            obj[key] = match.index;
            tags.push(obj);

            match = tagReg.exec(html);
        }

        tags.sort(function(a, b) {
            for (var k1 in a) {
                for (var k2 in b) {
                    if (a[k1] > b[k2]) {
                        return false;
                    }
                }
            }

            return true;
        });

        return tags;
    };

    function replaceBinderTag(html, tags) {
        var i = 0, len = tags.length, generated = html;
        for (; i < len; i++) {
            var ele = tags[i];
            for (var key in ele) {
                if (Binder.scope[key]) {
                    var reg = new RegExp('\{\{' + key + '\}\}');
                    generated = generated.replace(reg, Binder.scope[key]);
                }
            }
        }

        return generated;
    };

    function repaint(scope, generated) {
        scope.innerHTML = generated;
    };

    /**
     * @param id scope id
     */
    function init(id) {
        var html = getHtml(id),
            tags = analyseBinderTag(html),
            generated = replaceBinderTag(html, tags);

        repaint(id, generated);
    };

    /**
     * clear data in scope
     */
    function clear() {
        Binder.scope = {};
    };

    Binder.module = function(id, fn) {
        clear();
        var scope = findScope(id);
        fn(Binder.scope);
        init(scope);
    };

    global.Binder = Binder;

})(window, window.document, Sizzle);

<commit_msg>refined binder<commit_after>/**
 * provide data bind ability
 * 
 * @author <NAME>
 * @date 2015/04/13
 * released under terms of MIT lincese
 */

;(function(global, doc, Sizzle, undefined) {
    
    'use strict';

    var Binder = Binder || {
        scope: {},
        version: '0.0.1'
    };

    // support specify the scope id binding on these tags
    var tags = ['html', 'body', 'head', 'title', 'div', 'ul', 'li', 'table',
                    'tr', 'td', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span'];

    /**
     * find scope in page to execute data binding
     * @param id a scope id
     */
    function findScope(id) {

        var i = 0, len = tag.length, scope;

        for (; i < len; i++) {
            var selector = tags[i] + '[bind-scope="' + id + '"]',
                temp = Sizzle(selector);
            if (temp && temp.length) {
                scope = temp[0];
                break;
            }
        }

        return scope;

    };

    function getHtml(scope) {
        return scope.innerHTML;
    };

    function analyseBinderTag(html) {
        var tagReg = /\{\{\w+\}\}/g,
            match = tagReg.exec(html),
            tags = [];

        while (match) {
            var obj = {},
                key = match[0].replace(/\{\{/, '').replace(/\}\}/, '');
            obj[key] = match.index;
            tags.push(obj);

            match = tagReg.exec(html);
        }

        tags.sort(function(a, b) {
            for (var k1 in a) {
                for (var k2 in b) {
                    if (a[k1] > b[k2]) {
                        return false;
                    }
                }
            }

            return true;
        });

        return tags;
    };

    function replaceBinderTag(html, tags) {
        var i = 0, len = tags.length, generated = html;
        for (; i < len; i++) {
            var ele = tags[i];
            for (var key in ele) {
                if (Binder.scope[key]) {
                    var reg = new RegExp('\{\{' + key + '\}\}');
                    generated = generated.replace(reg, Binder.scope[key]);
                }
            }
        }

        return generated;
    };

    function repaint(scope, generated) {
        scope.innerHTML = generated;
    };

    /**
     * @param id scope id
     */
    function init(id) {
        var html = getHtml(id),
            tags = analyseBinderTag(html),
            generated = replaceBinderTag(html, tags);

        repaint(id, generated);
    };

    /**
     * clear data in scope
     */
    function clear() {
        Binder.scope = {};
    };

    Binder.module = function(id, fn) {
        clear();
        var scope = findScope(id);
        fn(Binder.scope);
        init(scope);
    };

    global.Binder = Binder;

})(window, window.document, Sizzle);


{'fraction_non_alphanumeric': 0.1553574677096598, 'fraction_numerical': 0.00909587047480444, 'mean_word_length': 1.3091138177236454, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '697294', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1634, 'n_words': 412}