parsi-digits 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.markdown +23 -0
- data/Rakefile +12 -0
- data/lib/parsi_digits.rb +67 -0
- data/lib/version.rb +3 -0
- data/parsi-digits.gemspec +27 -0
- data/test/parsi_digits_test.rb +38 -0
- data/test/test_helper.rb +2 -0
- metadata +78 -0
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
ParsiDigits
|
2
|
+
===========
|
3
|
+
|
4
|
+
Some simple utilities to help working with parsi unicode digits.
|
5
|
+
|
6
|
+
Convertion between parsi and western digits:
|
7
|
+
|
8
|
+
require 'parsi_digits'
|
9
|
+
"15,000 تومان".with_parsi_digits
|
10
|
+
=> "۱۵,۰۰۰ تومان"
|
11
|
+
123.25.with_parsi_digits
|
12
|
+
=> "۱۲۳/۲۵"
|
13
|
+
"۱۲۳۴۵".with_western_digits
|
14
|
+
=> "12345"
|
15
|
+
|
16
|
+
Easy convertion of parsi digits to Float or Integer (which is useful especially for input forms):
|
17
|
+
|
18
|
+
"۱۲۳۴۵".to_i
|
19
|
+
=> 12345
|
20
|
+
"۱۹/۸".to_f
|
21
|
+
=> 19.8
|
22
|
+
|
23
|
+
Copyright (c) 2012 Hassan Zamani, released under the MIT license.
|
data/Rakefile
ADDED
data/lib/parsi_digits.rb
ADDED
@@ -0,0 +1,67 @@
|
|
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
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class String
|
18
|
+
def with_parsi_digits
|
19
|
+
gsub(/\d/) { |d| ParsiDigits.to_parsi d }
|
20
|
+
end
|
21
|
+
|
22
|
+
def with_parsi_digits!
|
23
|
+
gsub!(/\d/) { |d| ParsiDigits.to_parsi d }
|
24
|
+
end
|
25
|
+
|
26
|
+
def with_western_digits
|
27
|
+
gsub(/[۰-۹]/) { |d| ParsiDigits.to_western d }
|
28
|
+
end
|
29
|
+
|
30
|
+
def with_western_digits!
|
31
|
+
gsub!(/[۰-۹]/) { |d| ParsiDigits.to_western d }
|
32
|
+
end
|
33
|
+
|
34
|
+
def has_parsi_digits?
|
35
|
+
self =~ /[۰-۹]/
|
36
|
+
end
|
37
|
+
|
38
|
+
alias_method :western_to_i, :to_i
|
39
|
+
def to_i base=10
|
40
|
+
if has_parsi_digits?
|
41
|
+
with_western_digits.western_to_i base
|
42
|
+
else
|
43
|
+
western_to_i base
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
alias_method :western_to_f, :to_f
|
48
|
+
def to_f
|
49
|
+
if has_parsi_digits?
|
50
|
+
with_western_digits.sub("/",".").western_to_f
|
51
|
+
else
|
52
|
+
western_to_f
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class Integer
|
58
|
+
def with_parsi_digits
|
59
|
+
to_s.with_parsi_digits
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class Float
|
64
|
+
def with_parsi_digits
|
65
|
+
to_s.with_parsi_digits.sub '.', '/'
|
66
|
+
end
|
67
|
+
end
|
data/lib/version.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
|
6
|
+
# Description Meta...
|
7
|
+
s.name = 'parsi-digits'
|
8
|
+
s.version = ParsiDigits::VERSION
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.author = 'Hassan Zamani'
|
11
|
+
s.email = 'hsn.zamani@gmail.com'
|
12
|
+
s.homepage = 'http://github.com/hzamani/parsi_digits'
|
13
|
+
s.summary = 'Some utils to support parsi digits'
|
14
|
+
s.description = 'Some utils to support parsi digits including convertion between parsi and western digits and convertion from String to Float or Fixnum'
|
15
|
+
|
16
|
+
|
17
|
+
# Load Paths...
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ['lib']
|
22
|
+
|
23
|
+
|
24
|
+
# Dependencies (installed via 'bundle install')...
|
25
|
+
s.add_development_dependency("bundler")
|
26
|
+
s.add_development_dependency("rake")
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ParsiDigitsTest < Test::Unit::TestCase
|
6
|
+
def test_string_with_parsi_digits
|
7
|
+
assert_equal "۹۸۷۶۵۴۳۲۱۰", "9876543210".with_parsi_digits
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_string_with_western_digits
|
11
|
+
assert_equal "9876543210", "۹۸۷۶۵۴۳۲۱۰".with_western_digits
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_parsi_string_to_i
|
15
|
+
assert_equal 9876543210, "۹۸۷۶۵۴۳۲۱۰".to_i
|
16
|
+
assert_equal 9876543210, "9876543210".to_i
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_parsi_string_to_f
|
20
|
+
assert_equal 0.987654321, "۰/۹۸۷۶۵۴۳۲۱".to_f
|
21
|
+
assert_equal 0.987654321, "0.987654321".to_f
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_integer_with_parsi_digits
|
25
|
+
assert_equal "۹۸۷۶۵۴۳۲۱۰", 9876543210.with_parsi_digits
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_float_with_parsi_digits
|
29
|
+
assert_equal "۹/۰۸۷۶۵۴۳۲۱", 9.087654321.with_parsi_digits
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_has_parsi_digits
|
33
|
+
assert "۱۹۴".has_parsi_digits?
|
34
|
+
assert "test۹۸".has_parsi_digits?
|
35
|
+
assert !"123".has_parsi_digits?
|
36
|
+
assert !"test".has_parsi_digits?
|
37
|
+
end
|
38
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parsi-digits
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Hassan Zamani
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: &19273340 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *19273340
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &19272920 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *19272920
|
36
|
+
description: Some utils to support parsi digits including convertion between parsi
|
37
|
+
and western digits and convertion from String to Float or Fixnum
|
38
|
+
email: hsn.zamani@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- README.markdown
|
46
|
+
- Rakefile
|
47
|
+
- lib/parsi_digits.rb
|
48
|
+
- lib/version.rb
|
49
|
+
- parsi-digits.gemspec
|
50
|
+
- test/parsi_digits_test.rb
|
51
|
+
- test/test_helper.rb
|
52
|
+
homepage: http://github.com/hzamani/parsi_digits
|
53
|
+
licenses: []
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.8.10
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Some utils to support parsi digits
|
76
|
+
test_files:
|
77
|
+
- test/parsi_digits_test.rb
|
78
|
+
- test/test_helper.rb
|