<commit_before>var vm = require('vm');
var async = require('async');
var _ = require('lodash');

exports.find = function (records, criteria, callback){
    var boundFunc = _.bind(_tryMatch, {criteria: criteria});
    async.filter( records, boundFunc, function( results ) {
        return callback(results);
    });
}

exports.match = function (record, criteria, callback){
    var boundFunc = _.bind(_tryMatch, { criteria : criteria } );
    boundFunc( record,  function( result ) {
        return callback(result);
    });
}

/* internal functions */

function _tryMatch(record, recordCallback){
    _doEvery( record, this.criteria, recordCallback);
}

function _doEvery(record, criteria, cb){
    var boundFunc = _.bind(_checkTrue, { record : record, criteria : criteria } );

    async.every( _.keys(criteria), boundFunc, function( result ) {
        return cb(result);
    });
}

function _callNor(record, criteria, cb){
    // reverse result of OR
    _callOr(record, criteria, function(result) {
	return cb(!result);		
    });     
}

function _callOr(record, criteria, cb){
    var boundFunc = _.bind(_checkTrue, {record: record, criteria: criteria});
    async.detect( _.keys(criteria), boundFunc, function( innerResult ) {
        return cb(innerResult);
    });
}

function _checkTrue( key, keyCallback ){
    var currCriteria =  this.criteria[key];

    if (
        ( key === '$and' )
            &&
            ( currCriteria instanceof Array )
        )
    {
        return _doEvery( this.record, currCriteria, keyCallback );
    }
    else if (
        ( key === '$or' )
            &&
            ( currCriteria instanceof Array )
        )
    {
        return _callOr( this.record, currCriteria, keyCallback );
    }
    else if (
        ( key === '$nor' )
            &&
            ( currCriteria instanceof Array )
        )
    {
        return _callNor( this.record, currCriteria, keyCallback );
    }
    else if ( key === '$where' )
    {
        var contextCode = '';
        var recordKeys = _.keys( this.record );

        for ( var k = 0; k < recordKeys.length; k++ )
        {
            contextCode += 'var ' + recordKeys[k] + "=" + JSON.stringify(this.record[recordKeys[k]]) + ';';
        }

        var retValue = false;
        try {
            retValue = vm.runInThisContext( contextCode + this.criteria[key] )
        }
        catch (e) {
            console.log( "Error in $where clause: " + e);
        };

        keyCallback( retValue );

    }
    else if ( typeof currCriteria === 'object' )
    {
        if (
            ( _.keys( currCriteria ).length == 1 )
             &&
            ( _.keys( currCriteria )[0].match(/^\$(exists|lt|lte|gt|gte|ne|in|nin)$/))
         )
        {
            var retValue = false;

            switch (_.keys(currCriteria)[0])
            {
                case '$exists' :

                    var isDef = ( typeof _objAccessor(this.record, key) != 'undefined' );

                    retValue = currCriteria['$exists'] ? isDef : (!isDef);
                    break;

                case '$lt' :

                    retValue = ( _objAccessor(this.record, key) < currCriteria['$lt'] );
                    break;

                case '$lte' :

                    retValue = ( _objAccessor(this.record, key) <= currCriteria['$lte'] );
                    break;

                case '$gt' :

                    retValue = ( _objAccessor(this.record, key) > currCriteria['$gt'] );
                    break;

                case '$gte' :

                    retValue = ( _objAccessor(this.record, key) >= currCriteria['$gte'] );
                    break;

                case '$ne' :

                    retValue = ( _objAccessor(this.record, key) != currCriteria['$ne'] );
                    break;

                case '$in' :

                    var toMatch = _objAccessor(this.record, key);
                    for ( var i = 0; i < currCriteria['$in'].length; i++ )
                    {
                        if ( currCriteria['$in'][i] == toMatch )
                        {
                            retValue = true;
                            break;
                        }
                    }
                    break;

                case '$nin' :

                    var toMatch = _objAccessor(this.record, key);
                    for ( var i = 0; i < currCriteria['$in'].length; i++ )
                    {
                        if ( currCriteria['$in'][i] == toMatch )
                        {
                            retValue = true;
                            break;
                        }
                    }

                    // reverse value since "not in"
                    retValue = ! retValue;

                    break;

            }

             keyCallback( retValue );
        }
        else
        {
            return _doEvery( this.record, currCriteria, keyCallback );
        }
    }
    else
    {
        var retValue = ( _objAccessor( this.record, key ) == currCriteria );

         keyCallback( retValue );
    }

}

function _objAccessor(obj, objPath){
    objPath = objPath.split(".");

    for(var i = 0; i < objPath.length; i++)
    {
        if ( typeof obj[ objPath[i] ] != 'undefined' )<commit_msg>No need for REGEX.match before switch statement, just add default<commit_after>var vm = require('vm');
var async = require('async');
var _ = require('lodash');

exports.find = function (records, criteria, callback){
    var boundFunc = _.bind(_tryMatch, {criteria: criteria});
    async.filter( records, boundFunc, function( results ) {
        return callback(results);
    });
}

exports.match = function (record, criteria, callback){
    var boundFunc = _.bind(_tryMatch, { criteria : criteria } );
    boundFunc( record,  function( result ) {
        return callback(result);
    });
}

/* internal functions */

function _tryMatch(record, recordCallback){
    _doEvery( record, this.criteria, recordCallback);
}

function _doEvery(record, criteria, cb){
    var boundFunc = _.bind(_checkTrue, { record : record, criteria : criteria } );

    async.every( _.keys(criteria), boundFunc, function( result ) {
        return cb(result);
    });
}

function _callNor(record, criteria, cb){
    // reverse result of OR
    _callOr(record, criteria, function(result) {
	return cb(!result);		
    });     
}

function _callOr(record, criteria, cb){
    var boundFunc = _.bind(_checkTrue, {record: record, criteria: criteria});
    async.detect( _.keys(criteria), boundFunc, function( innerResult ) {
        return cb(innerResult);
    });
}

function _checkTrue( key, keyCallback ){
    var currCriteria =  this.criteria[key];

    if (
        ( key === '$and' )
            &&
            ( currCriteria instanceof Array )
        )
    {
        return _doEvery( this.record, currCriteria, keyCallback );
    }
    else if (
        ( key === '$or' )
            &&
            ( currCriteria instanceof Array )
        )
    {
        return _callOr( this.record, currCriteria, keyCallback );
    }
    else if (
        ( key === '$nor' )
            &&
            ( currCriteria instanceof Array )
        )
    {
        return _callNor( this.record, currCriteria, keyCallback );
    }
    else if ( key === '$where' )
    {
        var contextCode = '';
        var recordKeys = _.keys( this.record );

        for ( var k = 0; k < recordKeys.length; k++ )
        {
            contextCode += 'var ' + recordKeys[k] + "=" + JSON.stringify(this.record[recordKeys[k]]) + ';';
        }

        var retValue = false;
        try {
            retValue = vm.runInThisContext( contextCode + this.criteria[key] )
        }
        catch (e) {
            console.log( "Error in $where clause: " + e);
        };

        keyCallback( retValue );

    }
    else if ( typeof currCriteria === 'object' )
    {
        if (_.keys(currCriteria).length == 1){
            var retValue = false;

            switch (_.keys(currCriteria)[0])
            {
                case '$exists' :

                    var isDef = ( typeof _objAccessor(this.record, key) != 'undefined' );

                    retValue = currCriteria['$exists'] ? isDef : (!isDef);
                    break;

                case '$lt' :

                    retValue = ( _objAccessor(this.record, key) < currCriteria['$lt'] );
                    break;

                case '$lte' :

                    retValue = ( _objAccessor(this.record, key) <= currCriteria['$lte'] );
                    break;

                case '$gt' :

                    retValue = ( _objAccessor(this.record, key) > currCriteria['$gt'] );
                    break;

                case '$gte' :

                    retValue = ( _objAccessor(this.record, key) >= currCriteria['$gte'] );
                    break;

                case '$ne' :

                    retValue = ( _objAccessor(this.record, key) != currCriteria['$ne'] );
                    break;

                case '$in' :

                    var toMatch = _objAccessor(this.record, key);
                    for ( var i = 0; i < currCriteria['$in'].length; i++ )
                    {
                        if ( currCriteria['$in'][i] == toMatch )
                        {
                            retValue = true;
                            break;
                        }
                    }
                    break;

                case '$nin' :

                    var toMatch = _objAccessor(this.record, key);
                    for ( var i = 0; i < currCriteria['$in'].length; i++ )
                    {
                        if ( currCriteria['$in'][i] == toMatch )
                        {
                            retValue = true;
                            break;
                        }
                    }

                    // reverse value since "not in"
                    retValue = ! retValue;

                    break;

                default:
                    break;

            }

             keyCallback( retValue );
        }
        else
        {
            return _doEvery( this.record, currCriteria, keyCallback );
        }
    }
    else
    {
        var retValue = ( _objAccessor( this.record, key ) == currCriteria );

         keyCallback( retValue );
    }

}

function _objAccessor(obj, objPath){
    objPath = objPath.split(".");

    for(var i = 0; i < objPath.length; i++)
    {
        if ( typeof obj[ objPath[i] ] != 'undefined' )
{'fraction_non_alphanumeric': 0.1268074581430746, 'fraction_numerical': 0.0012366818873668188, 'mean_word_length': 1.4363847045191194, 'pattern_counts': {'":': 0, '<': 15, '<?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': '7114106', 'n_tokens_mistral': 2919, 'n_tokens_neox': 2814, 'n_words': 658}