-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetUniqueListController.js
108 lines (97 loc) · 3.13 KB
/
getUniqueListController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
app.controller("getUniqueListController", function ($scope) {
$scope.types = [
{typeConcat : "new line", symbol : "\n"},
{typeConcat : "tab", symbol : "\t"},
{typeConcat : "semicolon", symbol : ";"},
{typeConcat : "dash", symbol : "-"},
{typeConcat : "underscore", symbol : "_"},
];
$scope.msg = "";
$scope.checks = {
trim : true,
blank : true,
insensitive : true
};
$scope.msgError = undefined;
$scope.duplicatedTableValues = undefined;
$scope.uniqueTableValues = undefined;
$scope.checkList = function(){
$scope.msgError = undefined;
$scope.duplicatedTableValues = undefined;
$scope.uniqueTableValues = undefined;
try{
if($scope.listToCheck.trim() != ""){
var array;
for(var i=0; i < $scope.types.length; i++){
if($scope.types[i].typeConcat == $scope.selectedType.typeConcat){
array = $scope.listToCheck.split($scope.types[i].symbol);
array = arrayOptions(array);
break;
}
}
var result = getUniqueArray(array);
if(result.origin.length < array.length){
$scope.msg = "There are some duplicated values!";
$scope.duplicatedTableValues = result.repeated;
$scope.uniqueTableValues = result.origin.join("\n");
/*console.log($scope.uniqueTableValue.split("\n").length);
$scope.uniqueTableValue = result.origin;*/
/*for(var i = 0; i< result.repeated.length ; i++){
aux = aux + "El elemento '" +result.repeated[i].element + "' aparece '" + (result.repeated[i].count +1) + "' veces \n";
}*/
}else{
$scope.msg = "The list hasn't duplicates values. Total Elements ("+array.length+")";
$scope.uniqueTableValues = result.origin.join("\n");
}
}else{
$scope.msg = "Please insert elements in the textarea box";
}
}catch(err){
$scope.msgError = err.toString();
}
}
function arrayOptions(array){
var newArray = [];
var indexNewArray = 0;
for (var j = 0; j < array.length; j++) {
if($scope.checks.insensitive == true){array[j] = array[j].toLowerCase();}
if($scope.checks.trim == true){array[j] = array[j].trim();}
if($scope.checks.blank == true && array[j] != ""){
newArray[indexNewArray] = array[j];
indexNewArray++;
}
}
if(newArray.length < array.length && newArray.length > 0){
array = newArray;
}
/*false - false*/
return array;
}
function getUniqueArray(array){
var result = {origin : [], repeated : []};
for(var i = 0; i < array.length; i++){
if(result.origin.indexOf(array[i]) == -1){
result.origin.push(array[i]);
}else{
if(result.repeated.length == 0){
result.repeated.push({element : array[i], count : 1});
}else{
/*El elemento ya se encuentra en el original*/
var count = 0;
for(var j = 0; j < result.repeated.length ; j++){
if(result.repeated[j].element == array[i]){
/*Se encuentra en los repetidos*/
result.repeated[j].count++;
count ++;
break;
}
}
if(count == 0){
result.repeated.push({element : array[i], count : 1});
}
}
}
}
return result;
}
});