<commit_before>var parser = require('./query-parser'),
    iterator = require('./iterator');

var selector = (function() {
    "use strict";
    
    var operands = {
        'match-class' : function(actual, expected) {
            return (actual && (new RegExp('(^| )' + expected + '($| )')).test(actual));
        },
        'match-tag' : function(actual, expected) {
            actual = actual.substr(actual.lastIndexOf('.') + 1);
            
            return actual.toLowerCase() == expected.toLowerCase();
        },
        '=' : function(actual, expected) {
            return actual === expected;
        }
    };
    
    var getMatchingFunction = function(query) {
        var type = typeof query,
            matchingFunction;
        
        if(type === 'function') {
            matchingFunction = query;
        } else if(type === 'string') {
            var ruleSetList = parser(query);
            
            matchingFunction = function(element) {
                for(var i = 0; i < ruleSetList.length; i++) {
                    var ruleSet = ruleSetList[i];
                    
                    var matching = true;
                    
                    for(var j = 0; j < ruleSet.length && matching; j++) {
                        var property = ruleSet[j].property,
                            value = ruleSet[j].value,
                            operand = ruleSet[j].operand; //willfully ignored so far
                        
                        if(property === 'class') {
                            property = 'className';
                        } else if(property === 'tagname') {
                            property = 'apiName';
                        }
                        
                        matching = operands[operand] && element[property] && operands[operand](element[property], value);
                    }
                    
                    if(matching) {
                        return true;
                    }
                }
                
                return false;
            };
        } else {
            throw new Error('Unexpected type of query : ' + type);
        }
        
        return matchingFunction;
    };
    
    var getElements = function(root, query, limit) {
        throw new Error('To implement');
    };
    
    var getElement = function(root, query) {
        return getElements(root, query, 1);
    };
    
    var getParents = function(root, query, limit) {
        var queryFunction = getMatchingFunction(query),
            elements = [root],
            results = [];
        
        while(elements = iterator.parents(elements[0])) {
            if(elements.length < 1) {
                break;
            }
            
            if(limit && results.length >= limit) {
                break;
            }
            
            if(queryFunction(elements[0])) {<commit_msg>prototyping the getElements function<commit_after>var parser = require('./query-parser'),
    iterator = require('./iterator');

var selector = (function() {
    "use strict";
    
    var operands = {
        'match-class' : function(actual, expected) {
            return (actual && (new RegExp('(^| )' + expected + '($| )')).test(actual));
        },
        'match-tag' : function(actual, expected) {
            actual = actual.substr(actual.lastIndexOf('.') + 1);
            
            return actual.toLowerCase() == expected.toLowerCase();
        },
        '=' : function(actual, expected) {
            return actual === expected;
        }
    };
    
    var getMatchingFunction = function(query) {
        var type = typeof query,
            matchingFunction;
        
        if(type === 'function') {
            matchingFunction = query;
        } else if(type === 'string') {
            var ruleSetList = parser(query);
            
            matchingFunction = function(element) {
                for(var i = 0; i < ruleSetList.length; i++) {
                    var ruleSet = ruleSetList[i];
                    
                    var matching = true;
                    
                    for(var j = 0; j < ruleSet.length && matching; j++) {
                        var property = ruleSet[j].property,
                            value = ruleSet[j].value,
                            operand = ruleSet[j].operand; //willfully ignored so far
                        
                        if(property === 'class') {
                            property = 'className';
                        } else if(property === 'tagname') {
                            property = 'apiName';
                        }
                        
                        matching = operands[operand] && element[property] && operands[operand](element[property], value);
                    }
                    
                    if(matching) {
                        return true;
                    }
                }
                
                return false;
            };
        } else {
            throw new Error('Unexpected type of query : ' + type);
        }
        
        return matchingFunction;
    };
    
    var getElements = function(root, query, limit) {
        var queryFunction = getMatchingFunction(query),
            results = [];
            
        var iterate = function(root) {
            var elements = iterator.children(root);
            
            for(var i = 0; i < elements.length; i++) {
                if(limit && results.length >= limit) {
                    return;
                }
                
                var element = elements[i];
                
                if(queryFunction(element)) {
                    results.push(element);
                }
                
                iterate(element);
            }
        };
        
        iterate(root);
            
        return results;
    };
    
    var getElement = function(root, query) {
        return getElements(root, query, 1);
    };
    
    var getParents = function(root, query, limit) {
        var queryFunction = getMatchingFunction(query),
            elements = [root],
            results = [];
        
        while(elements = iterator.parents(elements[0])) {
            if(elements.length < 1) {
                break;
            }
            
            if(limit && results.length >= limit) {
                break;
            }
            
            if(queryFunction(elements[0])) {
{'fraction_non_alphanumeric': 0.11171282686059106, 'fraction_numerical': 0.002320903605136933, 'mean_word_length': 1.1277156023699801, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 6, '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': '615772', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1504, 'n_words': 332}