forked from pbakondy/mcc-mnc-list
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
108 lines (89 loc) · 2.71 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
'use strict';
const records = require( './mcc-mnc-list.json' );
const statusCodeList = require( './status-codes.json' );
const regionList = require('./regions.json');
const regionNames = regionList.map(function(x) { return x.name });
function all () {
return records;
}
function statusCodes () {
return statusCodeList;
}
function regions () {
return regionList;
}
function filter ( filters ) {
if (filters === undefined || filters === null) {
return records;
}
if (typeof filters !== 'object') {
throw new TypeError('Invalid parameter (object expected)');
}
let statusCode, mcc, mnc, countryCode, plmn, nibbledPlmn, region;
if (filters.statusCode) {
statusCode = filters.statusCode;
if (statusCodeList.indexOf(statusCode) === -1) {
throw new TypeError('Invalid statusCode parameter (not found in statusCode list)');
}
}
if (filters.plmn) {
if (typeof filters.plmn === 'string') {
plmn = String(filters.plmn);
} else {
throw new TypeError('Invalid plmn parameter (string expected)');
}
mcc = plmn.substr(0, 3);
mnc = plmn.substr(3);
}
if (filters.mcc && mcc) {
throw new TypeError('Don\'t use mccmnc and mcc parameter at once');
}
if (filters.mnc && mnc) {
throw new TypeError('Don\'t use mccmnc and mnc parameter at once');
}
if (filters.mcc) {
if (typeof filters.mcc === 'string' || typeof filters.mcc === 'number') {
mcc = String(filters.mcc);
} else {
throw new TypeError('Invalid mcc parameter (string expected)');
}
}
if (filters.mnc) {
if (typeof filters.mnc === 'string' || typeof filters.mnc === 'number') {
mnc = String(filters.mnc);
} else {
throw new TypeError('Invalid mnc parameter (string expected)');
}
}
if (filters.countryCode != undefined) {
if (typeof filters.countryCode === 'string') {
countryCode = filters.countryCode;
} else {
throw new TypeError('Invalid countryCode parameter (string expected)');
}
}
if (filters.region) {
region = filters.region;
if (regionNames.indexOf(region) === -1) {
throw new TypeError('Invalid region parameter (not found in region list)');
}
}
let result = records;
if (statusCode) {
result = result.filter( record => record['status'] === statusCode );
}
if (countryCode) {
result = result.filter( record => record['countryCode'] === countryCode );
}
if (region) {
result = result.filter( record => record['region'] === region );
}
if (mcc) {
result = result.filter( record => record['mcc'] === mcc );
}
if (mnc) {
result = result.filter( record => record['mnc'] === mnc );
}
return result;
}
module.exports = { all, statusCodes, regions, filter };