parsi-digits 0.2 → 0.3
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.
- data/.gitignore +0 -2
- data/Gemfile.lock +17 -0
- data/README.markdown +7 -1
- data/lib/parsi-digits.rb +123 -0
- data/lib/version.rb +1 -1
- data/test/test_helper.rb +1 -1
- metadata +4 -3
- data/lib/parsi_digits.rb +0 -117
data/Gemfile.lock
ADDED
data/README.markdown
CHANGED
@@ -5,7 +5,7 @@ Some simple utilities to help working with parsi unicode digits.
|
|
5
5
|
|
6
6
|
Convertion between parsi and western digits:
|
7
7
|
|
8
|
-
require '
|
8
|
+
require 'parsi-digits'
|
9
9
|
"15,000 تومان".with_parsi_digits
|
10
10
|
=> "۱۵,۰۰۰ تومان"
|
11
11
|
123.25.with_parsi_digits
|
@@ -20,4 +20,10 @@ Easy convertion of parsi digits to Float or Integer (which is useful especially
|
|
20
20
|
"۱۹/۸".to_f
|
21
21
|
=> 19.8
|
22
22
|
|
23
|
+
Migrating from 0.2 to 0.3
|
24
|
+
-------------------------
|
25
|
+
|
26
|
+
`parsi_digits` renamed to `parsi-digits` to respect bundlers autorequire.
|
27
|
+
So simple remove `require: 'parsi_digits'` option in `Gemfile` if you have it.
|
28
|
+
|
23
29
|
Copyright (c) 2012 Hassan Zamani, released under the MIT license.
|
data/lib/parsi-digits.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Parsi
|
4
|
+
module Digits
|
5
|
+
PARSI_DIGITS = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def to_parsi digit
|
9
|
+
PARSI_DIGITS[digit.to_i] if digit =~ /[0-9]/
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_western digit
|
13
|
+
PARSI_DIGITS.index digit if digit =~ /[۰-۹]/
|
14
|
+
end
|
15
|
+
|
16
|
+
def locale_separator options={}
|
17
|
+
locale = options[:locale]
|
18
|
+
default = options[:default]
|
19
|
+
default ||= (locale == :fa ? '/' : '.')
|
20
|
+
begin
|
21
|
+
I18n.t(:'number.format.separator', default: default, locale: locale)
|
22
|
+
rescue
|
23
|
+
default
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def fa_separator
|
28
|
+
locale_separator locale: :fa
|
29
|
+
end
|
30
|
+
|
31
|
+
def en_separator
|
32
|
+
locale_separator locale: :en
|
33
|
+
end
|
34
|
+
|
35
|
+
def locale_delimiter options={}
|
36
|
+
locale = options[:locale]
|
37
|
+
default = options[:default]
|
38
|
+
default ||= (locale == :fa ? '٫' : ',')
|
39
|
+
begin
|
40
|
+
I18n.t(:'number.format.delimiter', default: default, locale: locale)
|
41
|
+
rescue
|
42
|
+
default
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def fa_delimiter
|
47
|
+
locale_delimiter locale: :fa
|
48
|
+
end
|
49
|
+
|
50
|
+
def en_delimiter
|
51
|
+
locale_delimiter locale: :en
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class String
|
58
|
+
def with_parsi_digits
|
59
|
+
gsub(/\d/) { |d| Parsi::Digits.to_parsi d }
|
60
|
+
end
|
61
|
+
|
62
|
+
def with_parsi_digits!
|
63
|
+
gsub!(/\d/) { |d| Parsi::Digits.to_parsi d }
|
64
|
+
end
|
65
|
+
|
66
|
+
def with_western_digits
|
67
|
+
gsub(/[۰-۹]/) { |d| Parsi::Digits.to_western d }
|
68
|
+
end
|
69
|
+
|
70
|
+
def with_western_digits!
|
71
|
+
gsub!(/[۰-۹]/) { |d| Parsi::Digits.to_western d }
|
72
|
+
end
|
73
|
+
|
74
|
+
def has_parsi_digits?
|
75
|
+
self =~ /[۰-۹]/
|
76
|
+
end
|
77
|
+
|
78
|
+
def remove_delimiters options={locale: :fa}
|
79
|
+
locale = options[:locale]
|
80
|
+
delimiter = options[:delimiter]
|
81
|
+
delimiter ||= Parsi::Digits.locale_delimiter locale: locale
|
82
|
+
gsub delimiter, ''
|
83
|
+
end
|
84
|
+
|
85
|
+
def remove_delimiters!
|
86
|
+
locale = options[:locale]
|
87
|
+
delimiter = options[:delimiter]
|
88
|
+
delimiter ||= Parsi::Digits.locale_delimiter locale: locale
|
89
|
+
gsub! delimiter, ''
|
90
|
+
end
|
91
|
+
|
92
|
+
alias_method :western_to_i, :to_i
|
93
|
+
def to_i base=10
|
94
|
+
if has_parsi_digits?
|
95
|
+
with_western_digits.western_to_i base
|
96
|
+
else
|
97
|
+
western_to_i base
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
alias_method :western_to_f, :to_f
|
102
|
+
def to_f
|
103
|
+
if has_parsi_digits?
|
104
|
+
separator = Parsi::Digits.fa_separator
|
105
|
+
with_western_digits.sub(separator, '.').western_to_f
|
106
|
+
else
|
107
|
+
western_to_f
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class Integer
|
113
|
+
def with_parsi_digits
|
114
|
+
to_s.with_parsi_digits
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class Float
|
119
|
+
def with_parsi_digits
|
120
|
+
separator = Parsi::Digits.fa_separator
|
121
|
+
to_s.with_parsi_digits.sub '.', separator
|
122
|
+
end
|
123
|
+
end
|
data/lib/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require '
|
2
|
+
require 'parsi-digits'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parsi-digits
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -52,9 +52,10 @@ extra_rdoc_files: []
|
|
52
52
|
files:
|
53
53
|
- .gitignore
|
54
54
|
- Gemfile
|
55
|
+
- Gemfile.lock
|
55
56
|
- README.markdown
|
56
57
|
- Rakefile
|
57
|
-
- lib/
|
58
|
+
- lib/parsi-digits.rb
|
58
59
|
- lib/version.rb
|
59
60
|
- parsi-digits.gemspec
|
60
61
|
- test/parsi_digits_test.rb
|
data/lib/parsi_digits.rb
DELETED
@@ -1,117 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module ParsiDigits
|
4
|
-
PARSI_DIGITS = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
|
5
|
-
|
6
|
-
class << self
|
7
|
-
def to_parsi digit
|
8
|
-
PARSI_DIGITS[digit.to_i] if digit =~ /[0-9]/
|
9
|
-
end
|
10
|
-
|
11
|
-
def to_western digit
|
12
|
-
PARSI_DIGITS.index digit if digit =~ /[۰-۹]/
|
13
|
-
end
|
14
|
-
|
15
|
-
def locale_separator options={}
|
16
|
-
locale = options[:locale]
|
17
|
-
default = options[:default]
|
18
|
-
default ||= (locale == :fa ? '/' : '.')
|
19
|
-
begin
|
20
|
-
I18n.t(:'number.format.separator', default: default, locale: locale)
|
21
|
-
rescue
|
22
|
-
default
|
23
|
-
end
|
24
|
-
end
|
25
|
-
def fa_separator
|
26
|
-
locale_separator locale: :fa
|
27
|
-
end
|
28
|
-
def en_separator
|
29
|
-
locale_separator locale: :en
|
30
|
-
end
|
31
|
-
|
32
|
-
def locale_delimiter options={}
|
33
|
-
locale = options[:locale]
|
34
|
-
default = options[:default]
|
35
|
-
default ||= (locale == :fa ? '٫' : ',')
|
36
|
-
begin
|
37
|
-
I18n.t(:'number.format.delimiter', default: default, locale: locale)
|
38
|
-
rescue
|
39
|
-
default
|
40
|
-
end
|
41
|
-
end
|
42
|
-
def fa_delimiter
|
43
|
-
locale_delimiter locale: :fa
|
44
|
-
end
|
45
|
-
def en_delimiter
|
46
|
-
locale_delimiter locale: :en
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
class String
|
52
|
-
def with_parsi_digits
|
53
|
-
gsub(/\d/) { |d| ParsiDigits.to_parsi d }
|
54
|
-
end
|
55
|
-
|
56
|
-
def with_parsi_digits!
|
57
|
-
gsub!(/\d/) { |d| ParsiDigits.to_parsi d }
|
58
|
-
end
|
59
|
-
|
60
|
-
def with_western_digits
|
61
|
-
gsub(/[۰-۹]/) { |d| ParsiDigits.to_western d }
|
62
|
-
end
|
63
|
-
|
64
|
-
def with_western_digits!
|
65
|
-
gsub!(/[۰-۹]/) { |d| ParsiDigits.to_western d }
|
66
|
-
end
|
67
|
-
|
68
|
-
def has_parsi_digits?
|
69
|
-
self =~ /[۰-۹]/
|
70
|
-
end
|
71
|
-
|
72
|
-
def remove_delimiters options={locale: :fa}
|
73
|
-
locale = options[:locale]
|
74
|
-
delimiter = options[:delimiter]
|
75
|
-
delimiter ||= ParsiDigits.locale_delimiter locale: locale
|
76
|
-
gsub delimiter, ''
|
77
|
-
end
|
78
|
-
|
79
|
-
def remove_delimiters!
|
80
|
-
locale = options[:locale]
|
81
|
-
delimiter = options[:delimiter]
|
82
|
-
delimiter ||= ParsiDigits.locale_delimiter locale: locale
|
83
|
-
gsub! delimiter, ''
|
84
|
-
end
|
85
|
-
|
86
|
-
alias_method :western_to_i, :to_i
|
87
|
-
def to_i base=10
|
88
|
-
if has_parsi_digits?
|
89
|
-
with_western_digits.western_to_i base
|
90
|
-
else
|
91
|
-
western_to_i base
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
alias_method :western_to_f, :to_f
|
96
|
-
def to_f
|
97
|
-
if has_parsi_digits?
|
98
|
-
separator = ParsiDigits.fa_separator
|
99
|
-
with_western_digits.sub(separator, '.').western_to_f
|
100
|
-
else
|
101
|
-
western_to_f
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
class Integer
|
107
|
-
def with_parsi_digits
|
108
|
-
to_s.with_parsi_digits
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
class Float
|
113
|
-
def with_parsi_digits
|
114
|
-
separator = ParsiDigits.fa_separator
|
115
|
-
to_s.with_parsi_digits.sub '.', separator
|
116
|
-
end
|
117
|
-
end
|