HPhone (empty) → 0.0.1.0
raw patch · 6 files changed
+187/−0 lines, 6 filesdep +HPhonedep +basedep +containerssetup-changed
Dependencies added: HPhone, base, containers, hspec, phone-metadata, regex-pcre
Files
- ChangeLog.md +5/−0
- HPhone.cabal +86/−0
- LICENSE +20/−0
- Setup.hs +2/−0
- src/HPhone.hs +72/−0
- test/Spec.hs +2/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for HPhone++## 0.0.1.0 -- 2016-06-22++* First version.
+ HPhone.cabal view
@@ -0,0 +1,86 @@+name: HPhone++-- The package version. See the Haskell package versioning policy (PVP) +-- for standards guiding when and how versions should be incremented.+-- https://wiki.haskell.org/Package_versioning_policy+-- PVP summary: +-+------- breaking API changes+-- | | +----- non-breaking API additions+-- | | | +--- code changes with no API change+version: 0.0.1.0++-- A short (one-line) description of the package.+synopsis: Phone number parser and validator++-- A longer description of the package.+description: Phone number parser and validator++homepage: https://github.com/vijayanant/HPhone++bug-reports: https://github.com/vijayanant/HPhone/issues++-- The license under which the package is released.+license: MIT++-- The file containing the license text.+license-file: LICENSE++-- The package author(s).+author: Vijay Anant <vijay.hassan@gmail.com>, + Raghu Ugare <raghu.ugare@gmail.com>++-- An email address to which users can send suggestions, bug reports, and +-- patches.+maintainer: vijay.hassan@gmail.com,+ raghu.ugare@gmail.com++stability: experimental++-- A copyright notice.+-- copyright: ++category: Text++build-type: Simple++-- Extra files to be distributed with the package, such as examples or a +-- README.+extra-source-files: ChangeLog.md++-- Constraint on the version of Cabal needed to build this package.+cabal-version: >=1.10++source-repository head+ type: git+ location: https://github.com/vijayanant/HPhone.git++library+ -- Modules exported by the library.+ exposed-modules: HPhone+ + -- Modules included in this library but not exported.+ -- other-modules: + + -- LANGUAGE extensions used by modules in this package.+ -- other-extensions: + + -- Other library packages from which modules are imported.+ build-depends: base >=4.9 && <4.10+ , phone-metadata+ , containers+ , regex-pcre+ + -- Directories containing source files.+ hs-source-dirs: src+ + -- Base language which the package is written in.+ default-language: Haskell2010+ +Test-Suite tests+ type: exitcode-stdio-1.0+ default-language: Haskell2010+ hs-source-dirs: test+ ghc-options: -Wall+ main-is: Spec.hs+ build-depends: HPhone+ , base+ , hspec
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2016 Vijay Anant++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/HPhone.hs view
@@ -0,0 +1,72 @@+module HPhone (HPhone.lookup, HPhone.validate, CountryCodeAbbr, PhoneNumber, Phone(..)) where++import PhoneNumberMetadata+import qualified Data.Map.Strict as Map+import Data.Maybe (isNothing, catMaybes, fromJust)+import Data.List+import Text.Regex.PCRE++type CountryCodeAbbr = String+type TerritoryMap = Map.Map CountryCodeAbbr Territory++data Phone = Phone+ { number :: String+ , code :: String+ , countryAbbr :: String+ , numberType :: String+ } deriving (Eq, Read, Show)++ts:: [Territory]+ts = territories phoneNumberMetadata++territoryMap:: TerritoryMap+territoryMap = Map.fromList [(abbreviation t, t) | t <- ts]++{------------------- APIs follow ------------------}++{-| Phone number must not include country code. Leading zeroes are allowed -}+validate :: PhoneNumber -> CountryCodeAbbr -> Bool+validate phone abbr+ | isNothing $ HPhone.lookup phone abbr = False+ | otherwise = True++{-| Phone number must not include country code. Leading zeroes are allowed -}+lookup:: PhoneNumber -> CountryCodeAbbr -> Maybe Phone+lookup phone abbr = lookupPhone (ltrim phone) abbr territoryMap+ where ltrim = dropWhile (`elem` "0")++normalize :: PhoneNumber -> PhoneNumber+normalize _ = undefined++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++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 t =+ [ mobile t+ , fixedLine t+ , pager t+ , tollFree t+ , premiumRate t+ , sharedCost t+ , personalNumber t+ , voip t+ , uan t+ , voicemail t+ ]++constructResponse:: PhoneNumber -> Territory -> String -> Phone+constructResponse number territory phoneType =+ Phone { number = number+ , code = countryCode territory+ , countryAbbr = abbreviation territory+ , numberType = phoneType+ }
+ test/Spec.hs view
@@ -0,0 +1,2 @@+-- tests/Spec.hs+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}