<commit_before>'use strict';

var ngFileToJson = angular.module('ngFileToJson',[]);

ngFileToJson.directive('ngFileToJson', function(){
    var module,
        createObject;

    module = {
        restrict: 'AE',
        replace: true,
        template: '',
        scope: {
            fileType: "",
            headerField: "true",
            typeField: "false",
            delimiter: ",",
            rowDelimiter: "\n"
        },
        compile: function(){
            var str = '';

            createObject(str, scope);
        }
    };

    // The function that actually converts the string into an object
    createObject = function(str, opts) {
        var obj = [],
            rows,
            row,
            columns,
            header,
            types,
            validTypes = ['String', 'Number', 'Object', 'Boolean', 'Array'],
            items,
            i;

        opts.headerField = opts.headerField && !opts.headerField === 'false' ? true : false;
        opts.typeField = opts.typeField && !opts.typeField === 'false' ? true : false;

        rows = str.split(opts.rowDelimeter);

        if(opts.headerField){
            // set keys
            row = rows.shift();
            header = row.split(opts.delimiter);
        }

        if(opts.typeField){
            // set type
            row = rows.shift();
            types = row.split(opts.delimiter);

            // check for valid types
            for(i = 0; i < types.length; i++){
                if(!(validTypes.indexOf(types[i]) > -1)){
                    types[i] = 'String';
                }
            }
        }

        while(rows.length > 0){
            // set values
            items = {};
            row = rows.shift();
            columns = row.split(opts.delimiter);

            if(header){
                for(i = 0; i < columns.length; i++){
                    items[header[i] || ''] = columns[i];
                }
                obj.push(items);
            } else {
                obj.push(columns);
            }

        }

        return obj;
    };
<commit_msg>fixing file reader and parsing of file<commit_after>'use strict';

var ngFileToJson = angular.module('ngFileToJson',[]);

ngFileToJson.directive('ngFileToJson', function(){
    var module,
        createObject,
        cleanLineBreaks;

    // module to be returned
    module = {
        restrict: 'E',
        transclude: true,
        replace: true,
        scope: {
            result: '='
        },
        //templateUrl: './templates/angular-file-to-json.html',
        template: '<div><input ng-model="uploadFile" type="file"></div>',
        link: function(scope, element){
            element.on('change', function(event){
                var str = '',
                    uploadedFile = new FileReader();

                uploadedFile.readAsBinaryString(event.target.files[0]);

                uploadedFile.onload = function(e){
                    scope.$apply(function(){
                        str = e.target.result;
                        scope.result = createObject(str);
                    });
                };
            });
        }
    };

    // The function that actually converts the string into an object
    createObject = function(str) {
        var options = {
            fileType: '',
            headerField: 'true',
            typeField: 'false',
            delimiter: ','
        };

        var obj = [],
            rows,
            row,
            columns,
            header,
            types,
            validTypes = ['String', 'Number', 'Object', 'Boolean', 'Array'],
            items,
            i;

        options.headerField = options.headerField && !(options.headerField === 'false') ? true : false;
        options.typeField = options.typeField && !options.typeField === 'false' ? true : false;

        rows = cleanLineBreaks(str).split('\n');

        if(options.headerField){
            // set keys
            row = rows.shift();
            header = row.split(options.delimiter);
        }

        if(options.typeField){
            // set type
            row = rows.shift();
            types = row.split(options.delimiter);

            // check for valid types
            for(i = 0; i < types.length; i++){
                if(!(validTypes.indexOf(types[i]) > -1)){
                    types[i] = 'String';
                }
            }
        }

        while(rows.length > 0){
            // set values
            items = {};
            row = rows.shift();
            columns = row.split(options.delimiter);

            if(header){
                for(i = 0; i < columns.length; i++){
                    items[header[i] || ''] = columns[i];
                }
                obj.push(items);
            } else {
                obj.push(columns);
            }

        }

        return obj;
    };

    // helper function to clean up line breaks
    cleanLineBreaks = function(str){
        var arr = str.split(''),
            i;

        for(i = arr.length - 1; i >=0; i--){
            if(arr[i] === '\r'){
                arr.splice(i, 1);
            }
        }

        return arr.join('');
    };

{'fraction_non_alphanumeric': 0.1354066061425536, 'fraction_numerical': 0.0023179447556499902, 'mean_word_length': 1.2871024734982333, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, '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': '4863776', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1354, 'n_words': 299}