public TelProtocol(string prefix, string defaultInternationalPrefix = _defaultInternationalPrefix) : this(prefix, string.Empty, defaultInternationalPrefix)
        {
            DialingCode code = _dialingCodes.DialingCodes.SingleOrDefault(c => c.Matches(RegionInfo.CurrentRegion.TwoLetterISORegionName));

            if (code != null)
            {
                DefaultCountryCode = code.Code;
            }
        }
        public void MatchDialingCode_match_not_found()
        {
            DialingCode[] dialingCodes = new DialingCode[] { new DialingCode("44", "GB", "GBR") };
            Mock <DialingCodeCollection> mockCollection = new Mock <DialingCodeCollection>();

            mockCollection.SetupGet(m => m.DialingCodes).Returns(dialingCodes);

            Assert.IsNull(mockCollection.Object.MatchDialingCode("551141234567"));
        }
        public void MatchDialingCode_match_found()
        {
            DialingCode dialingCode = new DialingCode("44", "GB", "GBR");

            DialingCode[] dialingCodes = new DialingCode[] { dialingCode };
            Mock <DialingCodeCollection> mockCollection = new Mock <DialingCodeCollection>();

            mockCollection.SetupGet(m => m.DialingCodes).Returns(dialingCodes);

            Assert.AreEqual(dialingCode, mockCollection.Object.MatchDialingCode("441141234567"));
        }
        public void MatchDialingCode_find_longest_match()
        {
            DialingCode ukDialingCode       = new DialingCode("44", "GB", "GBR");
            DialingCode guernseyDialingCode = new DialingCode("44-1481", "GG", "GGY");

            DialingCode[] dialingCodes = new DialingCode[] { ukDialingCode, guernseyDialingCode };
            Mock <DialingCodeCollection> mockCollection = new Mock <DialingCodeCollection>();

            mockCollection.SetupGet(m => m.DialingCodes).Returns(dialingCodes);

            Assert.AreEqual(guernseyDialingCode, mockCollection.Object.MatchDialingCode("44-14811141234567"));
        }
        public string GetNumber(string number)
        {
            if (!_internationalPrefixes.Any(p => number.StartsWith(p)))
            {
                // The number does not start with an international prefix. Prepend the prefixes as needed
                number = _prefix + DefaultInternationalPrefix + DefaultCountryCode + RemoveLeadingZero(number);
            }
            else
            {
                // The number starts with an international prefix. Extract the international country dialing code, and prepend with the given prefix as needed.
                string      internationalPrefix = _internationalPrefixes.Single(p => number.StartsWith(p));
                string      localNumber         = number.Substring(internationalPrefix.Length);
                DialingCode code        = _dialingCodes.MatchDialingCode(localNumber);
                string      dialingCode = string.Empty;
                if (code != null)
                {
                    dialingCode = code.Code;
                }
                number = _prefix + DefaultInternationalPrefix + dialingCode + RemoveLeadingZero(localNumber.Substring(dialingCode.Length));
            }

            return(Regex.Replace(number, @"\s+", ""));
        }
Example #6
0
        public void Matches_alpha_2_true()
        {
            DialingCode code = new DialingCode("00", "AB", "CDE");

            Assert.IsTrue(code.Matches("AB"));
        }
Example #7
0
        public void Matches_alpha_3_false()
        {
            DialingCode code = new DialingCode("00", "AB", "CDE");

            Assert.IsFalse(code.Matches("FGH"));
        }