libphonenumber-execjs 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,37 @@
1
+ falsy = (fn) ->
2
+ try
3
+ return fn()
4
+ catch err
5
+ return false
6
+ return
7
+
8
+ this.getE164PhoneNumber = (str, cc, ndc) ->
9
+ cc = "#{cc ? ""}"
10
+ ndc = "#{ndc ? ""}"
11
+ util = i18n.phonenumbers.PhoneNumberUtil
12
+ inst = util.getInstance()
13
+ e164 = i18n.phonenumbers.PhoneNumberFormat.E164
14
+ num = util.extractPossibleNumber(str)
15
+ if cc && /^[\d]+$/.test(cc)
16
+ regionCode = inst.getRegionCodeForCountryCode(+cc)
17
+ else if inst.getMetadataForRegion(cc)
18
+ regionCode = cc
19
+
20
+ fixnumber = (phone_obj) ->
21
+ if phone_obj
22
+ return phone_obj if inst.isValidNumber(phone_obj)
23
+ if ndc && inst.getLengthOfNationalDestinationCode(phone_obj) != ndc.length
24
+ phone_obj.setNationalNumber(+("#{ndc}#{phone_obj.getNationalNumber()}"))
25
+ return phone_obj if inst.isValidNumber(phone_obj)
26
+ return
27
+
28
+ format = (phone_obj) ->
29
+ return inst.format(phone_obj, e164) if phone_obj
30
+ return
31
+
32
+ attempt = (fn) -> format(fixnumber(falsy(fn)))
33
+ phone = attempt () -> inst.parse(num)
34
+ return phone if phone
35
+ phone = attempt () -> inst.parse(num, regionCode)
36
+ return phone if phone
37
+ return
@@ -0,0 +1,152 @@
1
+ require 'bundler'
2
+ Bundler.require(:default, :development)
3
+ require "minitest/autorun"
4
+
5
+ class LibphonenumberTest < MiniTest::Spec
6
+
7
+ SCENARIOS = [
8
+ {
9
+ :given => {
10
+ :number => "9255550100",
11
+ :cc => "US",
12
+ :ndc => "925"
13
+ },
14
+ :expected => {
15
+ :normalized => "9255550100",
16
+ :e164 => "+19255550100"
17
+ }
18
+ },
19
+ {
20
+ :given => {
21
+ :number => "19255550100",
22
+ :cc => 1
23
+ },
24
+ :expected => {
25
+ :normalized => "19255550100",
26
+ :e164 => "+19255550100"
27
+ }
28
+ },
29
+ {
30
+ :given => {
31
+ :number => "5550100",
32
+ :cc => "US",
33
+ :ndc => "925"
34
+ },
35
+ :expected => {
36
+ :normalized => "5550100",
37
+ :e164 => "+19255550100"
38
+ }
39
+ },
40
+ {
41
+ :given => {
42
+ :number => "5550100",
43
+ :ndc => "925"
44
+ },
45
+ :expected => {
46
+ :normalized => "5550100",
47
+ :e164 => nil
48
+ }
49
+ },
50
+ {
51
+ :given => {
52
+ :number => "5550100",
53
+ :cc => "US"
54
+ },
55
+ :expected => {
56
+ :normalized => "5550100",
57
+ :e164 => nil
58
+ }
59
+ },
60
+ {
61
+ :given => {
62
+ :number => " (925) 555 - 0100",
63
+ :cc => "US",
64
+ :ndc => "925"
65
+ },
66
+ :expected => {
67
+ :normalized => "9255550100",
68
+ :e164 => "+19255550100"
69
+ }
70
+ },
71
+ {
72
+ :given => {
73
+ :number => " 1 (925) 555-0100",
74
+ :cc => 1
75
+ },
76
+ :expected => {
77
+ :normalized => "19255550100",
78
+ :e164 => "+19255550100"
79
+ }
80
+ },
81
+ {
82
+ :given => {
83
+ :number => "1.925.555.0100",
84
+ :cc => 1
85
+ },
86
+ :expected => {
87
+ :normalized => "19255550100",
88
+ :e164 => "+19255550100"
89
+ }
90
+ },
91
+ {
92
+ :given => {
93
+ :number => "925.555.0100",
94
+ :cc => 1
95
+ },
96
+ :expected => {
97
+ :normalized => "9255550100",
98
+ :e164 => "+19255550100"
99
+ }
100
+ },
101
+ {
102
+ :given => {
103
+ :number => " 555.0100 ",
104
+ :cc => "US",
105
+ :ndc => "925"
106
+ },
107
+ :expected => {
108
+ :normalized => "5550100",
109
+ :e164 => "+19255550100"
110
+ }
111
+ }
112
+ ]
113
+
114
+ describe "libphonenumber" do
115
+ before do
116
+ @lib = Libphonenumber.new
117
+ end
118
+
119
+ it "does expose goog" do
120
+ @lib.context.exec("return ('goog' in this);").must_equal true
121
+ end
122
+
123
+ it "does expose i18n" do
124
+ @lib.context.exec("return ('i18n' in this);").must_equal true
125
+ end
126
+
127
+ it "does expose getE164PhoneNumber" do
128
+ @lib.context.exec("return ('getE164PhoneNumber' in this);").must_equal true
129
+ end
130
+
131
+ it "does not expose CLOSURE_NO_DEPS" do
132
+ @lib.context.exec("return ('CLOSURE_NO_DEPS' in this);").must_equal false
133
+ end
134
+
135
+ it "does not expose COMPILED" do
136
+ @lib.context.exec("return ('COMPILED' in this);").must_equal false
137
+ end
138
+
139
+ SCENARIOS.each do |scenario|
140
+ d = "given #{scenario[:given].inspect}"
141
+ describe d do
142
+ it "will normalize the number correctly" do
143
+ @lib.normalize(scenario[:given][:number]).must_equal(scenario[:expected][:normalized], d)
144
+ end
145
+ it "will parse the e164 formatted version correctly" do
146
+ @lib.simple.get_e164_phone_number(scenario[:given][:number], scenario[:given][:cc], scenario[:given][:ndc]).must_equal(scenario[:expected][:e164], d)
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libphonenumber-execjs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joe Noon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: execjs
16
+ requirement: &70158186961740 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70158186961740
25
+ description: ExecJS wrapper for Google's libphonenumber library
26
+ email:
27
+ - joenoon@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/libphonenumber-execjs.rb
38
+ - lib/libphonenumber-execjs/libphonenumber.rb
39
+ - lib/libphonenumber-execjs/version.rb
40
+ - libphonenumber-execjs.gemspec
41
+ - support/libphonenumber.js
42
+ - support/simple.coffee
43
+ - test/libphonenumber_test.rb
44
+ homepage: ''
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ segments:
57
+ - 0
58
+ hash: 3747696578054081726
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ segments:
66
+ - 0
67
+ hash: 3747696578054081726
68
+ requirements: []
69
+ rubyforge_project: libphonenumber-execjs
70
+ rubygems_version: 1.8.10
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: ExecJS wrapper for Google's libphonenumber library
74
+ test_files:
75
+ - test/libphonenumber_test.rb