Created
December 2, 2019 11:35
-
-
Save greenlikeorange/6cd8361123df84ce12be51bb0e572925 to your computer and use it in GitHub Desktop.
Myanmar Phone Number Util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const RULES_BY_OPRATORS = { | |
MPT_GSM: [ | |
'20{5}', | |
'21{5}', | |
'22{5}', | |
'23{5}', | |
'24{5}', | |
'40{7}', | |
'41{6}', | |
'42{7}', | |
'43{6}', | |
'47{6}', | |
'48{7}', | |
'49{6}', | |
'50{5}', | |
'51{5}', | |
'52{5}', | |
'53{5}', | |
'54{5}', | |
'55{5}', | |
'56{5}', | |
'73{6}', | |
'83{5}', | |
'85{5}', | |
'86{5}', | |
'87{5}', | |
'89{7}', | |
'91{6}', | |
], | |
MPT_WCDMA: [ | |
'25{7}', | |
'26{7}', | |
'44{7}', | |
'45{7}', | |
'46{7}', // Need to confirm | |
], | |
MPT_CDMA450: [ | |
], | |
MPT_CDMA800: [ | |
], | |
MEC_CDMA800: [ | |
'30{6}', | |
'31{6}', | |
'32{6}', | |
'33{6}', | |
'34{7}', | |
'35{7}', | |
'36{6}', | |
], | |
TELENOR_GSM: [ | |
'75{7}', | |
'76{7}', | |
'77{7}', | |
'78{7}', | |
'79{7}', | |
], | |
OOREDOO_GSM: [ | |
'95{7}', | |
'96{7}', | |
'97{7}', | |
], | |
MYTEL_GSM: [ | |
'68{7}', | |
'69{7}', | |
], | |
}; | |
const OPERATOR_INFOS = { | |
MPT_GSM: { name: 'MPT', network: 'GSM' }, | |
MPT_WCDMA: { name: 'MPT', network: 'WCDMA' }, | |
MPT_CDMA450: { name: 'MPT', network: 'CDMA450' }, | |
MPT_CDMA800: { name: 'MPT', network: 'CDMA800' }, | |
MEC_CDMA800: { name: 'MEC', network: 'CDMA800' }, | |
TELENOR_GSM: { name: 'Telenor', network: 'GSM' }, | |
OOREDOO_GSM: { name: 'Ooredoo', network: 'GSM' }, | |
MYTEL_GSM: { name: 'MyTel', network: 'GSM' }, | |
}; | |
function transformRegExp(rules) { | |
return new RegExp(`(${rules.join('|')})$`); | |
} | |
function transformStrictRegExp(rules) { | |
return new RegExp(`^(09${rules.join('|')})$`); | |
} | |
const REGEXP_RULES_BY_OPRATORS = Object.keys(RULES_BY_OPRATORS) | |
.map((operator) => ({ | |
key: operator, | |
regex: transformRegExp(RULES_BY_OPRATORS[operator]), | |
})) | |
.reduce((a, b) => ({ | |
...a, | |
[b.key]: b.regex, | |
}), {}); | |
const REGEXP_ALL_OPERATOR_RULE = transformRegExp( | |
Object.keys(RULES_BY_OPRATORS) | |
.map((operator) => RULES_BY_OPRATORS[operator]) | |
.reduce((a, b) => a.concat(b), []), | |
); | |
const REGEXP_ALL_OPERATOR_STRICT_RULE = transformStrictRegExp( | |
Object.keys(RULES_BY_OPRATORS) | |
.map((operator) => RULES_BY_OPRATORS[operator]) | |
.reduce((a, b) => a.concat(b), []), | |
); | |
function _preCheck(input) { | |
return REGEXP_ALL_OPERATOR_RULE.test(input.trim()); | |
} | |
function _normalize(input) { | |
return input.trim().replace(REGEXP_ALL_OPERATOR_RULE, '09$1'); | |
} | |
function isValid(input) { | |
if (!_preCheck(input)) { | |
return false; | |
} | |
const normalizedInput = _normalize(input); | |
return REGEXP_ALL_OPERATOR_STRICT_RULE.test(normalizedInput); | |
} | |
function normalize(input) { | |
if (!isValid(input)) { | |
return 'invalid'; | |
} | |
return _normalize(input); | |
} | |
function getOperatorInfo(input) { | |
const normalizedInput = normalize(input); | |
if (normalizedInput === 'invalid') { | |
return { | |
name: 'invalid', | |
network: 'invalid', | |
}; | |
} | |
const matchOperator = Object.keys(REGEXP_RULES_BY_OPRATORS).find((operator) => { | |
return REGEXP_RULES_BY_OPRATORS[operator].text(normalizedInput); | |
}); | |
return OPERATOR_INFOS[matchOperator]; | |
} | |
function getOperatorName(input) { | |
return getOperatorInfo(input).name; | |
} | |
function getNetwork(input) { | |
return getNetwork(input).network; | |
} | |
exports = { | |
isValid, | |
normalize, | |
getOperatorInfo, | |
getOperatorName, | |
getNetwork, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment