<commit_before>//
//  match-js
//  A simple yet powerful pattern matching library for Javascript. 
//  
//  Read readme.md for instructions and LICENSE license.
//  
//  Copyright (c) 2010 <NAME> 
//
var Match = (function() {
    var NO_MATCH = { __matchtype__: 'no-match', toString: function() { return 'no-match' }};
    var TYPE_RE = /function\s([A-Za-z_1-9$]+)/;
    var IS_FIELD_OP = /\s(\=|\!|\>|\<|\>\=|\<\=)$/;
    
    function is_prim_type(obj) {
        return obj == String || obj == Number || obj == Date || obj == Boolean
    }
    
    function is_empty(value) {
        return value == null || value == undefined;
    }
    
    function get_op_resolver(op, value_a) {
        var op_resolver = function() { return true };
        switch(op) {
            case '=':
                op_resolver = function(value_b, parent, state) {
                    if(value_a == value_b) return true;
                    return false;
                }
                break;
            case '>':
                op_resolver = function(value_b, parent, state) {
                    if(value_a > value_b) return true;
                    return false;
                }
                break;
            case '<':
                op_resolver = function(value_b, parent, state) {
                    if(value_a < value_b) return true;
                    return false;
                }
                break;
            case '>=':
                op_resolver = function(value_b, parent, state) {
                    if(value_a >= value_b) return true;
                    return false;
                }
                break;
            case '<=':
                op_resolver = function(value_b, parent, state) {
                    if(value_a <= value_b) return true;
                    return false;
                }
                break;
            case '!':
                op_resolver = function(value_b, parent, state) {
                    if(value_a != value_b) return true;
                    return false;
                }
                break;
        }
        op_resolver.is_op = true;
        return op_resolver;
    }
    
    function get_resolver(obj) {
        var ctor;
        
        // Check if we got an instance __compare__ method (most valuable)
        if(obj && obj['__compare__']) return function(value) {
            return obj.__compare__(value);
        }
        
        // Check if we got an instance __equals__ method (most second most 
        // valuable)
        else if(obj && obj['__equals__']) return function(value) {
            if(obj.__equals__(value)) return [value];
            throw NO_MATCH;
        }

        // Check if the instance constructor class has an __compare__ method. 
        else if(obj && obj.constructor && obj.constructor['__compare__']) return function(value) {
            return obj.constructor.__compare__(obj, value);
        }

        // Check if the instance constructor class has an __equals__ method. 
        else if(obj && obj.constructor && obj.constructor['__equals__']) return function(value) {
            var comp = obj.constructor.__equals__;
            if(comp(obj, value)) return [value];
            throw NO_MATCH;
        }
        
        // Check for native type's (Strings, Number and Regexp's)
        else if(is_prim_type(obj)) return function(value) {
            if(is_empty(value) || value.constructor !== obj) throw NO_MATCH;
            return [value];
        }
        
        // Check for null and undefined types
        else if(is_empty(obj)) return function(value) {
            if(obj !== value) throw NO_MATCH;
            return [value];
        }
        
        // Didnt match any of the special case resolvers. Find it in 
        // the TYPE_RESOLVERS list instead.
        var type_name = TYPE_RE(obj.constructor.toString())[1];
        ctor = TYPE_RESOLVERS[type_name];
        if(!ctor) ctor = TYPE_RESOLVERS['Object'];
        return ctor(obj);
    }
    
    var TYPE_RESOLVERS = {

        // Function (class constructor) resolver. 
        Function: function(ctor) {
            return function(value) {
                var c = value !== undefined ? value.constructor : undefined;
                if(c) {
                    if(c === ctor) return [value];
                    while((c = c.super_)) if(c === ctor) return [value];
                }
                throw NO_MATCH;
            }
        },
        
        // Array resolver
        Array: function(arr) {
            if(arr.length == 0) {
                return function(value) {
                    if(value.constructor != Array || value.length != 0)
                        throw NO_MATCH;
                }
            } else {
                var resolvers = []
                for(var i = 0; i < arr.length; i++){
                  resolvers[i] = get_resolver(arr[i]);
                } 
                return function(value) {
                    var result = [];
                    if(!value || value.constructor != Array) throw NO_MATCH;
                    for(var count=0; count < resolvers.length; count++) {
                        result = result.concat(
                             resolvers[count](value[count])<commit_msg>Fixed bug where  the class constructor resolver crashed when matching against ´null´<commit_after>//
//  match-js
//  A simple yet powerful pattern matching library for Javascript. 
//  
//  Read readme.md for instructions and LICENSE license.
//  
//  Copyright (c) 2010 <NAME> 
//
var Match = (function() {
    var NO_MATCH = { __matchtype__: 'no-match', toString: function() { return 'no-match' }};
    var TYPE_RE = /function\s([A-Za-z_1-9$]+)/;
    var IS_FIELD_OP = /\s(\=|\!|\>|\<|\>\=|\<\=)$/;
    
    function is_prim_type(obj) {
        return obj == String || obj == Number || obj == Date || obj == Boolean
    }
    
    function is_empty(value) {
        return value == null || value == undefined;
    }
    
    function get_op_resolver(op, value_a) {
        var op_resolver = function() { return true };
        switch(op) {
            case '=':
                op_resolver = function(value_b, parent, state) {
                    if(value_a == value_b) return true;
                    return false;
                }
                break;
            case '>':
                op_resolver = function(value_b, parent, state) {
                    if(value_a > value_b) return true;
                    return false;
                }
                break;
            case '<':
                op_resolver = function(value_b, parent, state) {
                    if(value_a < value_b) return true;
                    return false;
                }
                break;
            case '>=':
                op_resolver = function(value_b, parent, state) {
                    if(value_a >= value_b) return true;
                    return false;
                }
                break;
            case '<=':
                op_resolver = function(value_b, parent, state) {
                    if(value_a <= value_b) return true;
                    return false;
                }
                break;
            case '!':
                op_resolver = function(value_b, parent, state) {
                    if(value_a != value_b) return true;
                    return false;
                }
                break;
        }
        op_resolver.is_op = true;
        return op_resolver;
    }
    
    function get_resolver(obj) {
        var ctor;
        
        // Check if we got an instance __compare__ method (most valuable)
        if(obj && obj['__compare__']) return function(value) {
            return obj.__compare__(value);
        }
        
        // Check if we got an instance __equals__ method (most second most 
        // valuable)
        else if(obj && obj['__equals__']) return function(value) {
            if(obj.__equals__(value)) return [value];
            throw NO_MATCH;
        }

        // Check if the instance constructor class has an __compare__ method. 
        else if(obj && obj.constructor && obj.constructor['__compare__']) return function(value) {
            return obj.constructor.__compare__(obj, value);
        }

        // Check if the instance constructor class has an __equals__ method. 
        else if(obj && obj.constructor && obj.constructor['__equals__']) return function(value) {
            var comp = obj.constructor.__equals__;
            if(comp(obj, value)) return [value];
            throw NO_MATCH;
        }
        
        // Check for native type's (Strings, Number and Regexp's)
        else if(is_prim_type(obj)) return function(value) {
            if(is_empty(value) || value.constructor !== obj) throw NO_MATCH;
            return [value];
        }
        
        // Check for null and undefined types
        else if(is_empty(obj)) return function(value) {
            if(obj !== value) throw NO_MATCH;
            return [value];
        }
        
        // Didnt match any of the special case resolvers. Find it in 
        // the TYPE_RESOLVERS list instead.
        var type_name = TYPE_RE(obj.constructor.toString())[1];
        ctor = TYPE_RESOLVERS[type_name];
        if(!ctor) ctor = TYPE_RESOLVERS['Object'];
        return ctor(obj);
    }
    
    var TYPE_RESOLVERS = {

        // Function (class constructor) resolver. 
        Function: function(ctor) {
            return function(value) {
                var c = value !== undefined && value !== null ? value.constructor : undefined;
                if(c) {
                    if(c === ctor) return [value];
                    while((c = c.super_)) if(c === ctor) return [value];
                }
                throw NO_MATCH;
            }
        },
        
        // Array resolver
        Array: function(arr) {
            if(arr.length == 0) {
                return function(value) {
                    if(value.constructor != Array || value.length != 0)
                        throw NO_MATCH;
                }
            } else {
                var resolvers = []
                for(var i = 0; i < arr.length; i++){
                  resolvers[i] = get_resolver(arr[i]);
                } 
                return function(value) {
                    var result = [];
                    if(!value || value.constructor != Array) throw NO_MATCH;
                    for(var count=0; count < resolvers.length; count++) {
                        result = result.concat(
                             resolvers[count](value[count])
{'fraction_non_alphanumeric': 0.1093676150864921, 'fraction_numerical': 0.0020795916438226675, 'mean_word_length': 1.3996371059197097, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, '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': '2379844', 'n_tokens_mistral': 2927, 'n_tokens_neox': 2754, 'n_words': 730}