HPhone 0.0.1.0 → 0.0.1.1
raw patch · 3 files changed
+22/−20 lines, 3 filesnew-uploaderPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- ChangeLog.md +4/−0
- HPhone.cabal +1/−1
- src/HPhone.hs +17/−19
ChangeLog.md view
@@ -1,5 +1,9 @@ # Revision history for HPhone +## 0.0.1.1 -- 2016-06-23++* Support for phone-numbers matching multiple types.+ ## 0.0.1.0 -- 2016-06-22 * First version.
HPhone.cabal view
@@ -6,7 +6,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.0.1.0+version: 0.0.1.1 -- A short (one-line) description of the package. synopsis: Phone number parser and validator
src/HPhone.hs view
@@ -5,6 +5,7 @@ import Data.Maybe (isNothing, catMaybes, fromJust) import Data.List import Text.Regex.PCRE+import Data.Char (toUpper) type CountryCodeAbbr = String type TerritoryMap = Map.Map CountryCodeAbbr Territory@@ -16,10 +17,10 @@ , numberType :: String } deriving (Eq, Read, Show) -ts:: [Territory]+ts :: [Territory] ts = territories phoneNumberMetadata -territoryMap:: TerritoryMap+territoryMap :: TerritoryMap territoryMap = Map.fromList [(abbreviation t, t) | t <- ts] {------------------- APIs follow ------------------}@@ -31,7 +32,7 @@ | otherwise = True {-| Phone number must not include country code. Leading zeroes are allowed -}-lookup:: PhoneNumber -> CountryCodeAbbr -> Maybe Phone+lookup :: PhoneNumber -> CountryCodeAbbr -> Maybe Phone lookup phone abbr = lookupPhone (ltrim phone) abbr territoryMap where ltrim = dropWhile (`elem` "0") @@ -40,30 +41,27 @@ lookupPhone :: PhoneNumber -> CountryCodeAbbr -> TerritoryMap -> Maybe Phone lookupPhone phoneNumber abbr territoryMap =- Map.lookup abbr territoryMap >>= \t ->- find (isMatch phoneNumber) (catMaybes $ phoneNumberPatterns t) >>= \numberPatterns ->- return $ constructResponse phoneNumber t $ phoneNumberType numberPatterns+ Map.lookup abbr territoryMap >>= \t ->+ HPhone.find phoneNumber t +find :: PhoneNumber -> Territory -> Maybe Phone+find p t+ | null matches = Nothing+ | otherwise = Just $ constructResponse p t phonetype+ where phonetype = fmap toUpper $ intercalate "_or_" $ fmap phoneNumberType matches+ matches = filter (isMatch p) (catMaybes $ phoneNumberPatterns t)++ isMatch :: PhoneNumber -> PhoneNumberPatterns -> Bool isMatch phone (PhoneNumberPatterns _ (Just np) _ _) = phone =~ ("^(" ++ np ++ ")$")::Bool isMatch phone (PhoneNumberPatterns _ _ (Just pp) _) = phone =~ ("^(" ++ pp ++ ")$")::Bool isMatch phone PhoneNumberPatterns {} = False -phoneNumberPatterns:: Territory -> [Maybe PhoneNumberPatterns]+phoneNumberPatterns :: Territory -> [Maybe PhoneNumberPatterns] phoneNumberPatterns t =- [ mobile t- , fixedLine t- , pager t- , tollFree t- , premiumRate t- , sharedCost t- , personalNumber t- , voip t- , uan t- , voicemail t- ]+ [ mobile, fixedLine, pager, tollFree, premiumRate, sharedCost, personalNumber, voip, uan, voicemail] <*> pure t -constructResponse:: PhoneNumber -> Territory -> String -> Phone+constructResponse :: PhoneNumber -> Territory -> String -> Phone constructResponse number territory phoneType = Phone { number = number , code = countryCode territory