let app = angular.module("myApp", []);
app.directive("fileChange", ["$window",  ($window) => {
    return {
        // both Element(E) names and Attribute(A) names can invoke the directive
        restrict: "EA",
        require: "ngModel",
        link:  (scope, element, attr, control) => {
            // FileReader is used to read the contents of a file 
            const reader = new FileReader;
            // get access to element that triggered an event
            element.bind("change",  (fc) => {    
                let fileContents = fc.target.files[0];  // Retrieves the first File from the FileList object (w3c file api)
                if (fileContents.name.indexOf("json") >= 0) {
                    file = fileContents.name;
                // reading data in the file    
                let check = reader.readAsText(fileContents);
                } 
                else if (!fileContents){
                    $window.alert ("Unable to load the file.Please retry");
                }
                else {
                    $window.alert("Please , strictly select a JSON file.");
                }              
            });
            reader.onload = () => {
                // control change /update the view value
                control.$setViewValue({
                    name: file,
                    files: scope.$eval(reader.result)
                });
                if (attr.selectedFile) {
                    scope.$eval(attr.selectedFile);
                }
            };                    
        }
    };
}]); 
app.controller("myController" ,  ($scope , $timeout) => {
    $scope.beforeIndex = {}; 
    $scope.container = {};
    const obj = new Index();   
    $scope.loadFile =  () => {
        $timeout( () => {
            $scope.beforeIndex[$scope.file.name] = angular.copy($scope.file);
        } , 200); // loads file after 200 microseconds
    };
    $scope.createIndex = (fName) => {
        let fileContents = $scope.beforeIndex[fName];
        let x = JSON.stringify(fileContents);
        try{
            JSON.parse(x);
            let success = obj.createIndex(fileContents);   
            if (success) {
                alert ("The operation was successful.");
                let result = obj.getIndex();
                let fileLength = result[fileContents.name].uploadedFile;
                $timeout( () => {
                    $scope.container[fileContents.name] = {
                        dataAfterIndexed: result[fileContents.name],
                        length: ( () => {
                            let emptyArray = [];
                            for (let x = 0; x < fileLength.length; x++) {
                                emptyArray.push(x);
                            }
                            return emptyArray;
                        })()
                    };
                }, 200);
            } else {
                alert("The operation was not successful.");
            }
        }catch(e){alert("Invalid JSON file");}
    };
    $scope.searchIndex =  () => {
        let fName = $scope.selectedFile;
        let txtSearch = $scope.txtSearch;
        if (!txtSearch) {
            alert("You haven\'t provided anything to be searched.");
        }
        else if (Object.keys($scope.beforeIndex).length <= 0) {
            alert("Unable to find an indexed file");
        }
        else{
            $scope.searchResults = obj.searchIndex(fName , txtSearch);
        }
        for(let i in $scope.searchResults){
            $scope.beforeIndex[i] = {
                name:i,
                index:$scope.searchResults[i],
            };
        }
    };

});


@@ -22,15 +22,15 @@
 ule(
-%22
+'
 myApp
-%22
+'
 , %5B%5D
@@ -46,17 +46,17 @@
 rective(
-%22
+'
 fileChan
@@ -61,21 +61,21 @@
 ange
-%22, %5B%22
+', %5B'
 $window
-%22
+'
 ,  (
@@ -204,12 +204,12 @@
 ct: 
-%22EA%22
+'EA'
 ,%0A  
@@ -227,17 +227,17 @@
 re: 
-%22
+'
 ngModel
-%22
+'
 ,%0A  
@@ -485,16 +485,16 @@
 ind(
-%22
+'
 change
-%22
+'
 ,  (
@@ -680,14 +680,14 @@
 xOf(
-%22
+'
 json
-%22
+'
 ) %3E=
@@ -942,17 +942,17 @@
 .alert (
-%22
+'
 Unable t
@@ -979,17 +979,17 @@
 se retry
-%22
+'
 );%0A     
@@ -1058,17 +1058,17 @@
 w.alert(
-%22
+'
 Please ,
@@ -1096,17 +1096,17 @@
 ON file.
-%22
+'
 );%0A     
@@ -1571,17 +1571,17 @@
 troller(
-%22
+'
 myContro
@@ -1584,17 +1584,17 @@
 ntroller
-%22
+'
  ,  ($sc
@@ -2188,17 +2188,17 @@
  alert (
-%22
+'
 The oper
@@ -2210,33 +2210,33 @@
  was successful.
-%22
+'
 );%0A             
@@ -2917,17 +2917,17 @@
   alert(
-%22
+'
 The oper
@@ -2951,17 +2951,17 @@
 cessful.
-%22
+'
 );%0A     
@@ -2993,17 +2993,17 @@
 )%7Balert(
-%22
+'
 Invalid 
@@ -3011,17 +3011,17 @@
 SON file
-%22
+'
 );%7D%0A    
@@ -3184,17 +3184,17 @@
   alert(
-%22
+'
 You have
@@ -3231,17 +3231,17 @@
 earched.
-%22
+'
 );%0A     
@@ -3327,17 +3327,17 @@
   alert(
-%22
+'
 Unable t
@@ -3362,9 +3362,9 @@
 file
-%22
+'
 );%0A 

remove doubles quotes as per airbnb