forked from pbakondy/mcc-mnc-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (85 loc) · 2.47 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
"use strict";
const records = require("./mcc-mnc-list.json");
const statusCodeList = require("./status-codes.json");
function all() {
return records;
}
function statusCodes() {
return statusCodeList;
}
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;
if (filters.statusCode) {
statusCode = filters.statusCode;
if (statusCodeList.indexOf(statusCode) === -1) {
throw new TypeError(
"Invalid statusCode parameter (not found in statusCode list)"
);
}
}
if (filters.mccmnc) {
let mccmnc;
if (
typeof filters.mccmnc === "string" ||
typeof filters.mccmnc === "number"
) {
mccmnc = String(filters.mccmnc);
} else {
throw new TypeError("Invalid mccmnc parameter (string expected)");
}
mcc = mccmnc.substr(0, 3);
mnc = mccmnc.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)");
}
}
let result = records;
if (statusCode) {
result = result.filter((record) => record["status"] === statusCode);
}
if (countryCode) {
result = result.filter((record) => record["countryCode"] === countryCode);
}
if (mcc) {
result = result.filter((record) => record["mcc"] === mcc);
}
if (mnc) {
result = result.filter((record) => record["mnc"] === mnc);
}
return result;
}
function find(filters) {
// return the first element of undefined, as filter will always return an array
return filter(filters)[0];
}
module.exports = { all, statusCodes, filter, find };