-
Notifications
You must be signed in to change notification settings - Fork 1
/
country_code.clj
115 lines (95 loc) · 3.17 KB
/
country_code.clj
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
103
104
105
106
107
108
109
110
111
112
113
114
115
(ns
^{:doc "Country calling codes handling for phone-number."
:author "Paweł Wilk"
:added "8.12.4-0"}
phone-number.country-code
(:require [clojure.set]
[phone-number.util :as util])
(:import [com.google.i18n.phonenumbers
PhoneNumberUtil
NumberParseException]))
;;
;; Supported Country Calling Codes
;;
(def ^{:added "8.12.4-0"
:tag clojure.lang.PersistentHashSet}
all
"Set of supported country codes."
(clojure.set/difference
(set (.getSupportedCallingCodes (util/instance)))
(set (.getSupportedGlobalNetworkCallingCodes (util/instance)))))
(def ^{:added "8.12.4-0"
:tag clojure.lang.PersistentHashSet}
all-arg
"Set of supported country codes (to be used as arguments)."
all)
(def ^{:added "8.12.4-0"
:tag clojure.lang.PersistentHashSet}
by-val
"Set of supported values of country codes."
all)
(def ^{:added "8.12.4-0"
:tag clojure.lang.PersistentHashSet}
by-val-arg
"Set of supported values of country codes (to be used as method arguments)."
all-arg)
(def ^{:added "8.12.4-0"
:tag clojure.lang.PersistentVector}
all-vec
"Vector of all supported country codes."
(vec all))
(def ^{:added "8.12.4-0"
:tag clojure.lang.PersistentVector}
by-val-vec
"Vector of all supported country codes."
(vec by-val))
(def ^{:added "8.12.16-1"
:tag clojure.lang.PersistentVector}
all-arg-vec
"Vector of all supported country codes (valid as args)."
(vec all-arg))
(def ^{:added "8.12.16-1"
:tag clojure.lang.PersistentVector}
by-val-arg-vec
"Vector of all supported country codes (valid as args)."
(vec by-val-arg))
(defn valid?
"Returns true if the given region-specification is a valid region code, false
otherwise."
{:added "8.12.4-0" :tag Boolean}
[^Integer calling-code]
(contains? all calling-code))
(defn valid-arg?
"Returns true if the given region-specification is a valid region code (to be used as
an argument), false otherwise."
{:added "8.12.16-1" :tag Boolean}
[^Integer calling-code]
(contains? all-arg calling-code))
(defn parse
"Parses a country calling code and returns a value that can be supplied to
Libphonenumber methods."
{:added "8.12.4-0" :tag Integer}
([^Integer calling-code]
(assert (valid-arg? calling-code)
(str "Country code " calling-code " is not valid"))
calling-code))
(defn generate-sample
"Generates a random country calling code."
{:added "8.12.4-0" :tag Integer}
([] (rand-nth all-vec))
([^java.util.Random rng] (util/get-rand-nth all-vec rng)))
(defn generate-sample-val
"Generates a random country calling code."
{:added "8.12.16-1" :tag Integer}
([] (rand-nth by-val-vec))
([^java.util.Random rng] (util/get-rand-nth by-val-vec rng)))
(defn generate-arg-sample
"Generates a random country calling code."
{:added "8.12.16-1" :tag Integer}
([] (rand-nth all-arg-vec))
([^java.util.Random rng] (util/get-rand-nth all-arg-vec rng)))
(defn generate-arg-sample-val
"Generates a random country calling code."
{:added "8.12.16-1" :tag Integer}
([] (rand-nth by-val-arg-vec))
([^java.util.Random rng] (util/get-rand-nth by-val-arg-vec rng)))