-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiv.js
130 lines (107 loc) · 4.39 KB
/
tiv.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
*
* @author Burak Arslan
* @requires
* @name TIValidation
* @description Turkish Identification Number Validation
* @param {string}
* Usage --
* instance ex : var TCNO = new TIValidation();
* validate ex : TCNO.validate('argument'));
*
*/
TIValidation.prototype = {
/**
* @name validate
* @description Validate Incoming Arguement
* @Usage {$MethodName}.validate(param);
* @param {string, integer} argument
* @return {boolean} true or false
*/
validate: function(argument) {
return (this.checkIdentificationNumber(argument) ? this.CalculateLastDigits(argument) : false);
},
/**
* @name checkIdentificationNumber
* @description Validate Incoming Arguement
* @Usage {$MethodName}.checkIdentificationNumber(param);
* @param {string, integer} argument
* @return {boolean} ex: true or false
*/
checkIdentificationNumber: function(argument) {
var _length = argument.trim().toString().length;
return (_length > 8 && _length < 12 ? this.checkIsNumeric(argument) : false);
},
/**
* @name checkIsNumeric
* @description checks param is numeric
* @Usage {$MethodName}.checkIsNumeric(param);
* @param {string, integer} argument
* @return {boolean} ex: true or false
*/
checkIsNumeric: function(argument) {
return (/^\d+$/.test(argument) ? true : false);
},
/**
* @name CalculateLastDigits
* @description Calculating 10th and 11th digits of number with algorythm
* @Usage {$MethodName}.CalculateLastDigits(param);
* @param {string, integer} argument
* @return {boolean} ex: true or false
*/
CalculateLastDigits: function(argument) {
var _argument = argument.substring(0, 9).split('').map(parseFloat);
var _firstGroup = this.sumArrayValues(this.getIndiceArray(this.algorythm._firstG, _argument));
var _secondGroup = this.sumArrayValues(this.getIndiceArray(this.algorythm._secondG, _argument));
_argument.push(((_firstGroup * 7) - _secondGroup) % 10);
_argument.push(this.sumArrayValues(_argument) % 10);
return (_argument.length === 11 ? this.ValidateResults(_argument, argument.split('').map(parseFloat)) : false);
},
/**
* @name ValidateResults
* @description Validate the results are equal and is it a valid number
* @Usage {$MethodName}.ValidateResults([a, b, c], [a, b, c]);
* @param {array, array} argumentCalculated and argumentBase
* @return {boolean} ex: true or false
*/
ValidateResults: function(_argument, argument) {
return (!![_argument.join(''), argument.join('')].reduce(function(a, b) {
return (a === b) ? a : NaN;
}));
},
/**
* @name sumArrayValues
* @description Validate Incoming Arguement
* @Usage {$MethodName}.sumArrayValues([1,3,4,5]);
* @param {array} argument
* @return {boolean} ex: true or false
*/
sumArrayValues: function(argument) {
return argument.reduceRight(function(a, b) {
return a + b;
})
},
/**
* @name getIndiceArray
* @description Splice array on Multiple Indices multisplice with pattern
* @Usage {$MethodName}.getIndiceArray([1, 4, 8], [1, 2, 3, 4, 5, 6, 7, 8]);
* @param {pattern, array} pattern and argument
* @return {boolean} ex: true or false
*/
getIndiceArray: function(pattern, arguments) {
var indices = [];
for (var i = 0; i < pattern.length; i++) {
var arg = pattern[i];
var index = arguments[arg - 1];
indices.push(index);
}
return indices;
}
};
function TIValidation() {
this.algorythm = {
_firstG: [1, 3, 5, 7, 9],
_secondG: [2, 4, 6, 8]
};
this.debug = false;
}