如何比较和匹配文档名称与2个数据集

时间:2015-03-20 03:49:21

标签: regex angularjs

我正在尝试批量上传PDF。他们的名字结构如下:

mm/yyyy AAA BBBB.pdf
  • AAA是3个字母PipeNameAbreviation
  • BBBB是3个或更多字母/数字LocationAbbreviation

在plunker中,您将看到用户将在视图中选择公司和日期。然后他们将选择PDF。我需要做的是将文件的PipeNameAbr与数据库中的PipeNameAbr进行比较,然后分配正确的PipeId& PipeName到formdata。这将是一致数量的字母,3。我还需要对Location做同样的事情,除了我需要LocationId&表格数据LocationAbr。差异是字母/数字的长度。所以我需要与Abbreviation的最后4个进行比较和匹配。

plunker

 var regex = /^\d+\D\d+\s*?(\S*)\s*(\S*)\..*$/i;
$scope.upload = function () {
    var files = $scope.files;
    if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
            var file = files[i];

            if (regex.test(file.name)) { 
                var matches = file.name.match(regex);

                    $upload.upload({
                    url: '/api/apiBatchPipeLine',
                    fields: {

                        'typeId': 1,
                        'companyId': $scope.companyId.CompanyId,
                        'documentDate': $scope.model.documentDate,
                        'pipeId': $scope.PipeId,
                        'pipeName': $scope.PipeName,
                        'locationId': $scope.LocationId,
                        'locationAb': $scope.LocationAb
                    },
                    file: file
                }).progress(function (evt) {
                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                    console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
                }).success(function (data, status, headers, config) {
                    console.log('file ' + config.file.name + 'uploaded. Response: ' +
                                JSON.stringify(data));
                }).error(function (err, result) {
                    console.log(err, result);
                });

            }

        }
    }
};

JSON

$scope.pipes = [{"$id":"1","PipeId":1,"PipeAb":"ANR","PipeName":"Transcanada ANR Pipeline","Documents":null},{"$id":"2","PipeId":2,"PipeAb":"CGT","PipeName":"Columbia Gulf Transmission","Documents":null},{"$id":"3","PipeId":3,"PipeAb":"CHA","PipeName":"Enterprise Channel Pipeline","Documents":null},{"$id":"4","PipeId":4,"PipeAb":"FGT","PipeName":"Energy Transfer Florida Gas Transmission","Documents":null},{"$id":"5","PipeId":5,"PipeAb":"GAB","PipeName":"Enbridge Garden Banks Pipeline","Documents":null},{"$id":"6","PipeId":6,"PipeAb":"GLO ","PipeName":"American Midstream Gloria  Pipeline","Documents":null},{"$id":"7","PipeId":7,"PipeAb":"GSO","PipeName":"Boardwalk Gulf South Pipeline","Documents":null},{"$id":"8","PipeId":8,"PipeAb":"HIO","PipeName":"Enterprise High Island Offshore System","Documents":null},{"$id":"9","PipeId":9,"PipeAb":"HIP","PipeName":"American Midstream High Point Pipeline","Documents":null},{"$id":"10","PipeId":10,"PipeAb":"HPL","PipeName":"Energy Transfer Houston Pipeline","Documents":null},{"$id":"11","PipeId":11,"PipeAb":"TEJ","PipeName":"Kinder Morgan Tejas Pipeline","Documents":null},{"$id":"12","PipeId":12,"PipeAb":"KIN","PipeName":"Kinetica Partners Pipeline","Documents":null},{"$id":"13","PipeId":13,"PipeAb":"NGP","PipeName":"Kinder Morgan Natural Gas Pipeline","Documents":null},{"$id":"14","PipeId":14,"PipeAb":"SER","PipeName":"Energy Transfer Sea Robin Pipeline Company ","Documents":null},{"$id":"15","PipeId":15,"PipeAb":"SNT","PipeName":"Kinder Morgan Southern Natural Gas Company","Documents":null},{"$id":"16","PipeId":16,"PipeAb":"SRY","PipeName":"MCPO Stingray Pipeline Company","Documents":null},{"$id":"17","PipeId":17,"PipeAb":"TGT","PipeName":"Kinder Morgan Tennessee Gas Pipeline","Documents":null},{"$id":"18","PipeId":18,"PipeAb":"TET","PipeName":"Spectra Energy Texas Eastern Pipeline","Documents":null},{"$id":"19","PipeId":19,"PipeAb":"TXG","PipeName":"Boardwalk Texas Gas Transmission","Documents":null},{"$id":"20","PipeId":20,"PipeAb":"TSC","PipeName":"Williams Transcontinental Gas Pipeline","Documents":null},{"$id":"21","PipeId":21,"PipeAb":"TRK","PipeName":"Energy Transfer Trunkline Gas Company","Documents":null},{"$id":"22","PipeId":22,"PipeAb":"VGS","PipeName":"Targa Venice Gathering System","Documents":null}];

JSON

 $scope.locations = [{"$id":"1","LocationId":1,"LocationAb":"BS32","LocationName":"BravoSam 32","Documents":null},{"$id":"2","LocationId":2,"LocationAb":"MP46","LocationName":"MikePier 46","Documents":null},{"$id":"3","LocationId":3,"LocationAb":"MP140","LocationName":"MikePier 140","Documents":null},{"$id":"4","LocationId":4,"LocationAb":"VR16","LocationName":"VectorRoland 16","Documents":null},{"$id":"5","LocationId":5,"LocationAb":"AP96","LocationName":"AlphaPass 96","Documents":null},{"$id":"6","LocationId":6,"LocationAb":"DZ26","LocationName":"DeltaZulu 26","Documents":null},{"$id":"7","LocationId":7,"LocationAb":"SV963","LocationName":"SandmanViper 963","Documents":null}];

我正在附上我目前正在查看的内容的屏幕截图。 Picture

我用于测试的PDF名称为

  

02-2015 HIP BS32.pdf

     

02-2015 HIP MP46.pdf

     

02-2015 HIP MP140.pdf

     

02-2015 TSC VR16.pdf

我有一个正则表达式,它删除pdf名称的日期并分隔管道和位置。我相信我需要循环遍历文件数组但是什么样的循环?我会使用switch语句进行比较吗?我用数据库中的json创建了一个plunker。

此处更新是工作角度版本

$scope.upload = function () {
    var files = $scope.files;
    if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
            var file = files[i];

            $scope.pipeLookup = {};
            $scope.locationLookup = {};

            $scope.pipes.map(function (pip) {
                $scope.pipeLookup[pip['PipeAb']] = pip;
            });
            $scope.locations.map(function (loc) {
                $scope.locationLookup[loc['LocationAb']] = loc;
            });

            var matches = file.name.match(/^\d+\D\d+\s*?(\S*)\s*(\S*?)\./i);

                $scope.pipe = $scope.pipeLookup[matches[1]];
                $scope.loc = $scope.locationLookup[matches[2]];

                $upload.upload({
                    url: '/api/apiBatchPipeLine',
                    fields: {

                        'typeId': 1,
                        'companyId': $scope.companyId.CompanyId,
                        'documentDate': $scope.model.documentDate,
                        'pipeId': $scope.pipe.PipeId,
                        'pipeName': $scope.pipe['PipeName'],
                        'companyName': $scope.CompanyName,
                        'locationId': $scope.loc['LocationId'],
                        'locationAb': $scope.loc['LocationAb']
                    },
                    file: file
                }).progress(function (evt) {
                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                    console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
                }).success(function (data, status, headers, config) {
                    notificationFactory.success();
                }).error(function (err, result) {
                    notificationFactory.error();
                    console.log(err, result);
                });

        }
    }
};

1 个答案:

答案 0 :(得分:3)

如果您必须匹配的缩写将始终是唯一的,并且您希望进行相当多的查找。您可以选择迭代JSON一次并以缩写为键的方式存储管道/位置:

var pipeLookup = {};
var locationLookup = {};

$scope.pipes.map(function(pip) {
    pipeLookup[pip['PipeAb']] = pip;
});
$scope.locations.map(function(loc) {
    locationLookup[loc['LocationAb']] = loc;
});

所以我在这里说,对于$scope.pipes的每个条目,我希望pipeLookup中的条目为keyPipeAb value并作为Lookup整个管道对象。之后,这些var matches = file.match(/^\d+\D\d+\s*?(\S*)\s*(\S*?)\./i); var pipe = pipeLookup[matches[1]]; var loc = locationLookup[matches[2]]; 变量基本上是哈希映射,因此您可以使用正则表达式匹配捕获组轻松找到正确的管道,如下所示:

pipe.PipeId

然后您拥有pipe.PipeNameloc.LocationIdfields以及您可以使用的任何其他属性,以分配给pipeLookup属性。 (您是否想要locationLookup的{​​{1}} $scope属性取决于您。我对AngularJS并不十分熟悉

在下面的代码段中找到与每个文件名的正确Pipe / Location属性匹配的精简版本。 (为简单起见没有AngularJS)

&#13;
&#13;
var pipes = [{"$id":"1","PipeId":1,"PipeAb":"ANR","PipeName":"Transcanada ANR Pipeline","Documents":null},{"$id":"2","PipeId":2,"PipeAb":"CGT","PipeName":"Columbia Gulf Transmission","Documents":null},{"$id":"3","PipeId":3,"PipeAb":"CHA","PipeName":"Enterprise Channel Pipeline","Documents":null},{"$id":"4","PipeId":4,"PipeAb":"FGT","PipeName":"Energy Transfer Florida Gas Transmission","Documents":null},{"$id":"5","PipeId":5,"PipeAb":"GAB","PipeName":"Enbridge Garden Banks Pipeline","Documents":null},{"$id":"6","PipeId":6,"PipeAb":"GLO ","PipeName":"American Midstream Gloria  Pipeline","Documents":null},{"$id":"7","PipeId":7,"PipeAb":"GSO","PipeName":"Boardwalk Gulf South Pipeline","Documents":null},{"$id":"8","PipeId":8,"PipeAb":"HIO","PipeName":"Enterprise High Island Offshore System","Documents":null},{"$id":"9","PipeId":9,"PipeAb":"HIP","PipeName":"American Midstream High Point Pipeline","Documents":null},{"$id":"10","PipeId":10,"PipeAb":"HPL","PipeName":"Energy Transfer Houston Pipeline","Documents":null},{"$id":"11","PipeId":11,"PipeAb":"TEJ","PipeName":"Kinder Morgan Tejas Pipeline","Documents":null},{"$id":"12","PipeId":12,"PipeAb":"KIN","PipeName":"Kinetica Partners Pipeline","Documents":null},{"$id":"13","PipeId":13,"PipeAb":"NGP","PipeName":"Kinder Morgan Natural Gas Pipeline","Documents":null},{"$id":"14","PipeId":14,"PipeAb":"SER","PipeName":"Energy Transfer Sea Robin Pipeline Company ","Documents":null},{"$id":"15","PipeId":15,"PipeAb":"SNT","PipeName":"Kinder Morgan Southern Natural Gas Company","Documents":null},{"$id":"16","PipeId":16,"PipeAb":"SRY","PipeName":"MCPO Stingray Pipeline Company","Documents":null},{"$id":"17","PipeId":17,"PipeAb":"TGT","PipeName":"Kinder Morgan Tennessee Gas Pipeline","Documents":null},{"$id":"18","PipeId":18,"PipeAb":"TET","PipeName":"Spectra Energy Texas Eastern Pipeline","Documents":null},{"$id":"19","PipeId":19,"PipeAb":"TXG","PipeName":"Boardwalk Texas Gas Transmission","Documents":null},{"$id":"20","PipeId":20,"PipeAb":"TSC","PipeName":"Williams Transcontinental Gas Pipeline","Documents":null},{"$id":"21","PipeId":21,"PipeAb":"TRK","PipeName":"Energy Transfer Trunkline Gas Company","Documents":null},{"$id":"22","PipeId":22,"PipeAb":"VGS","PipeName":"Targa Venice Gathering System","Documents":null}];
var locations = [{"$id":"1","LocationId":1,"LocationAb":"BS32","LocationName":"BravoSam 32","Documents":null},{"$id":"2","LocationId":2,"LocationAb":"MP46","LocationName":"MikePier 46","Documents":null},{"$id":"3","LocationId":3,"LocationAb":"MP140","LocationName":"MikePier 140","Documents":null},{"$id":"4","LocationId":4,"LocationAb":"VR16","LocationName":"VectorRoland 16","Documents":null},{"$id":"5","LocationId":5,"LocationAb":"AP96","LocationName":"AlphaPass 96","Documents":null},{"$id":"6","LocationId":6,"LocationAb":"DZ26","LocationName":"DeltaZulu 26","Documents":null},{"$id":"7","LocationId":7,"LocationAb":"SV963","LocationName":"SandmanViper 963","Documents":null}];

var files = ['02-2015 HIP BS32.pdf', '02-2015 HIP MP46.pdf', '02-2015 HIP MP140.pdf', '02-2015 TSC VR16.pdf'];

var pipeLookup = {};
var locationLookup = {};

pipes.map(function(pip) {
  pipeLookup[pip['PipeAb']] = pip;
});
locations.map(function(loc) {
  locationLookup[loc['LocationAb']] = loc;
});

document.getElementById('out').value = files.map(function(file) {
  var matches = file.match(/^\d+\D\d+\s*?(\S*)\s*(\S*?)\./i);
  var pipe = pipeLookup[matches[1]];
  var loc = locationLookup[matches[2]];
  return file + '\tPipeId: ' + pipe['PipeId'] +
            '\tLocationId: ' + loc['LocationId'] + 
            '\tPipeName: ' + pipe['PipeName'];
}).join('\n');
&#13;
<textarea id="out" rows="10" style="width:100%"></textarea>
&#13;
&#13;
&#13;

另一种方法是为每个文件迭代$scope.pipes$scope.locations。这意味着代码可读性降低,性能下降。