-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
256 lines (226 loc) · 4.92 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const fs = require('fs');
const path = require('path');
const countries = JSON.parse(
fs.readFileSync(path.join(__dirname, 'data', 'countries.json'), 'utf8')
);
const manufacturers = JSON.parse(
fs.readFileSync(path.join(__dirname, 'data', 'manufacturers.json'), 'utf8')
);
const validate = (vin, checksumParam) => {
const splitVIN = vin.toLowerCase().split('');
// use 9th character when checksumParam is not set
const checksum = checksumParam || splitVIN[8];
let total = 0;
for (let i = 0; i < splitVIN.length; i++) {
let numValue = 0;
let weight = 0;
switch (splitVIN[i]) {
case '0':
numValue = 0;
break;
case '1':
case 'a':
case 'j':
numValue = 1;
break;
case '2':
case 'b':
case 'k':
case 's':
numValue = 2;
break;
case '3':
case 'c':
case 'l':
case 't':
numValue = 3;
break;
case '4':
case 'd':
case 'm':
case 'u':
numValue = 4;
break;
case '5':
case 'e':
case 'n':
case 'v':
numValue = 5;
break;
case '6':
case 'f':
case 'w':
numValue = 6;
break;
case '7':
case 'g':
case 'p':
case 'x':
numValue = 7;
break;
case '8':
case 'h':
case 'y':
numValue = 8;
break;
case '9':
case 'r':
case 'z':
numValue = 9;
break;
default:
return false;
}
switch (i) {
case 0:
weight = 8;
break;
case 1:
weight = 7;
break;
case 2:
weight = 6;
break;
case 3:
weight = 5;
break;
case 4:
weight = 4;
break;
case 5:
weight = 3;
break;
case 6:
weight = 2;
break;
case 7:
weight = 10;
break;
case 8:
weight = 0;
break;
case 9:
weight = 9;
break;
case 10:
weight = 8;
break;
case 11:
weight = 7;
break;
case 12:
weight = 6;
break;
case 13:
weight = 5;
break;
case 14:
weight = 4;
break;
case 15:
weight = 3;
break;
case 16:
weight = 2;
break;
default:
return false;
}
splitVIN.splice(i, 1, numValue * weight);
}
for (const element of splitVIN) {
total += element;
}
const lastFiveChars = splitVIN.splice(0, 12);
for (const element of lastFiveChars) {
if (!Number.isInteger(parseInt(element))) {
return false;
}
}
if (
total % 11 === parseInt(checksum) ||
(total % 11 === 10 && checksum === 'x')
) {
return true;
} else {
return false;
}
};
const split = (vin) => {
const INDEXES = {
MADE_IN_START: 0,
MADE_IN_END: 2,
MANUFACTURER_START: 0,
MANUFACTURER_END: 3,
DETAILS_START: 3,
DETAILS_END: 8,
SECURITY_CODE: 8,
YEAR: 9,
ASSEMBLY_PLANT: 10,
SERIAL_NUMBER_START: 11
};
const rawInfo = {
madeIn: vin.substring(INDEXES.MADE_IN_START, INDEXES.MADE_IN_END),
manufacturer: vin.substring(
INDEXES.MANUFACTURER_START,
INDEXES.MANUFACTURER_END
),
details: vin.substring(INDEXES.DETAILS_START, INDEXES.DETAILS_END),
securityCode: vin.charAt(INDEXES.SECURITY_CODE),
year: vin.charAt(INDEXES.YEAR),
assemblyPlant: vin.charAt(INDEXES.ASSEMBLY_PLANT),
serialNumber: vin.substring(INDEXES.SERIAL_NUMBER_START)
};
return rawInfo;
};
const lookup = (keyName, key, elements) => {
for (const element of elements) {
if (element[keyName] == key) return element;
}
return '';
};
const getVinYear = (vin) => {
const letters = 'ABCDEFGHJKLMNPRSTVWXY123456789';
const yearStr = vin[10];
const currentYear = new Date().getFullYear();
const result = [];
let yearCounter = 1980;
let lettersCounter = 0;
while (yearCounter !== currentYear) {
const letter = letters[lettersCounter];
if (letter == yearStr) {
result.push(yearCounter);
}
if (lettersCounter == letters.length - 1) {
lettersCounter = 0;
} else {
lettersCounter += 1;
}
yearCounter += 1;
}
result.sort().reverse();
return result;
};
const getCountry = (countryCode) => {
const country = lookup('code', countryCode, countries);
return country.name;
};
const getManufacturer = (manufacturerCode) => {
const manufacturer = lookup('code', manufacturerCode, manufacturers);
return manufacturer.name;
};
const decode = (vin) => {
const codeValues = split(vin);
const carInfo = {
country: getCountry(codeValues.madeIn),
serialNumber: codeValues.serialNumber,
manufacturer: getManufacturer(codeValues.manufacturer),
modelYear: getVinYear(vin)
};
return carInfo;
};
module.exports = {
validate: validate,
split: split,
decode: decode
};