normalize_digits 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/README.md +50 -14
- data/lib/normalize_digits/matchers.rb +2 -2
- data/lib/normalize_digits/version.rb +1 -1
- data/spec/normalize_digits_spec.rb +5 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 778e0a62279930e47ef74cd21d2807d4c7cb720dcb0deb6ea7861d4025ebf43e
|
4
|
+
data.tar.gz: 8fbf501a76462502277d5dc665cf0aae8748fec28a833bc96d23c66f97de23da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9374156d16f29ca671f8341f78f628b8123eda4c8158d6dd1f884e90f9511aa9b6cad616b30cb8666f67655518584c4ed8c7f452a96bd8da78a756dbfa070191
|
7
|
+
data.tar.gz: 217be1503fc313bd359a55a5d59c2f204816335c96b10976f9a68b566504eb99314cc676b50a170aad70c21414d984ebfaf825b902f3700ea025edcd0dab8b4d
|
data/README.md
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
# NormalizeDigits
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/normalize_digits)
|
4
4
|
|
5
|
-
|
5
|
+
NormalizeDigits is a lightweight ActiveModel extension that automatically converts non-English digits (like Arabic,
|
6
|
+
Persian, etc.) to standard English digits (`0-9`) before validation. This is useful for applications where users might
|
7
|
+
input localized numerals, and consistent numeric formatting is required.
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- Automatically normalizes any non-English digits to ASCII numerals.
|
12
|
+
- Integrates seamlessly with ActiveModel or ActiveRecord attributes.
|
13
|
+
- Useful for form inputs, APIs, and user-generated content.
|
6
14
|
|
7
15
|
## Installation
|
8
16
|
|
@@ -14,30 +22,58 @@ gem 'normalize_digits'
|
|
14
22
|
|
15
23
|
And then execute:
|
16
24
|
|
17
|
-
|
25
|
+
```shell
|
26
|
+
bundle install
|
27
|
+
```
|
18
28
|
|
19
29
|
Or install it yourself as:
|
20
30
|
|
21
|
-
|
31
|
+
```shell
|
32
|
+
gem install normalize_digits
|
33
|
+
```
|
22
34
|
|
23
35
|
## Usage
|
24
36
|
|
25
|
-
|
37
|
+
Simply include the concern in your model and specify which attributes you want to normalize:
|
26
38
|
|
27
|
-
|
39
|
+
```ruby
|
28
40
|
|
29
|
-
|
41
|
+
class User < ApplicationRecord
|
42
|
+
include NormalizeDigits
|
30
43
|
|
31
|
-
|
44
|
+
normalize_digits
|
45
|
+
# normalize_digits only: [:username]
|
46
|
+
# normalize_digits except: [:username]
|
47
|
+
end
|
48
|
+
```
|
32
49
|
|
33
|
-
|
50
|
+
Before validation, any non-English digits in the specified fields will be converted to standard 0-9.
|
34
51
|
|
35
|
-
|
52
|
+
### Example
|
36
53
|
|
37
|
-
|
54
|
+
```ruby
|
55
|
+
user = User.new(phone_number: "٠٥٠١٢٣٤٥٦٧")
|
56
|
+
user.valid?
|
57
|
+
user.phone_number
|
58
|
+
# => "0501234567"
|
59
|
+
```
|
38
60
|
|
39
|
-
|
61
|
+
### Supported Digit Sets
|
40
62
|
|
41
|
-
|
63
|
+
The gem currently supports:
|
64
|
+
• Arabic-Indic digits: `٠١٢٣٤٥٦٧٨٩`
|
65
|
+
• Eastern Arabic digits: `۰۱۲۳۴۵۶۷۸۹`
|
66
|
+
• Can be extended easily via the matchers.
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/your-username/normalize_digits.
|
71
|
+
1. Fork the repo
|
72
|
+
2. Create your feature branch (git checkout -b feature/awesome)
|
73
|
+
3. Commit your changes (git commit -am 'Add awesome feature')
|
74
|
+
4. Push to the branch (git push origin feature/awesome)
|
75
|
+
5. Create a new Pull Request
|
76
|
+
|
77
|
+
## License
|
42
78
|
|
43
|
-
|
79
|
+
The gem is available as open source under the terms of the MIT License.
|
@@ -28,7 +28,7 @@ module NormalizeDigits
|
|
28
28
|
|
29
29
|
# RSpec 3.x
|
30
30
|
def failure_message
|
31
|
-
"Expected to be
|
31
|
+
"Expected to be normalized from #{@attribute}, but it was not"
|
32
32
|
end
|
33
33
|
|
34
34
|
# RSpec 1.2, 2.x
|
@@ -36,7 +36,7 @@ module NormalizeDigits
|
|
36
36
|
|
37
37
|
# RSpec 3.x
|
38
38
|
def failure_message_when_negated
|
39
|
-
"Expected to remain on #{@attribute}, but it was
|
39
|
+
"Expected to remain on #{@attribute}, but it was normalized"
|
40
40
|
end
|
41
41
|
|
42
42
|
# RSpec 1.2, 2.x
|
@@ -68,7 +68,7 @@ RSpec.describe NormalizeDigits do
|
|
68
68
|
context "Englishize all attrs" do
|
69
69
|
subject { instance_for(EnglishizeAll) }
|
70
70
|
|
71
|
-
it "should
|
71
|
+
it "should normalize strings" do
|
72
72
|
is_expected.to normalize_digits_for(:a)
|
73
73
|
is_expected.to normalize_digits_for(:b)
|
74
74
|
is_expected.to normalize_digits_for(:d)
|
@@ -90,7 +90,7 @@ RSpec.describe NormalizeDigits do
|
|
90
90
|
context "Englishize only one attr" do
|
91
91
|
subject { instance_for(EnglishizeOnlyOne) }
|
92
92
|
|
93
|
-
it "should
|
93
|
+
it "should normalize included strings" do
|
94
94
|
is_expected.to normalize_digits_for(:a)
|
95
95
|
end
|
96
96
|
|
@@ -102,7 +102,7 @@ RSpec.describe NormalizeDigits do
|
|
102
102
|
context "Englishize only two attrs" do
|
103
103
|
subject { instance_for(EnglishizeOnlyTwo) }
|
104
104
|
|
105
|
-
it "should
|
105
|
+
it "should normalize included strings" do
|
106
106
|
is_expected.to normalize_digits_for(:a)
|
107
107
|
is_expected.to normalize_digits_for(:b)
|
108
108
|
end
|
@@ -115,7 +115,7 @@ RSpec.describe NormalizeDigits do
|
|
115
115
|
context "Englishize except one attr" do
|
116
116
|
subject { instance_for(EnglishizeExceptOne) }
|
117
117
|
|
118
|
-
it "should
|
118
|
+
it "should normalize included strings" do
|
119
119
|
is_expected.to normalize_digits_for(:b)
|
120
120
|
end
|
121
121
|
|
@@ -127,7 +127,7 @@ RSpec.describe NormalizeDigits do
|
|
127
127
|
context "Englishize except two attrs" do
|
128
128
|
subject { instance_for(EnglishizeExceptTwo) }
|
129
129
|
|
130
|
-
it "should
|
130
|
+
it "should normalize included strings" do
|
131
131
|
is_expected.to normalize_digits_for(:d)
|
132
132
|
end
|
133
133
|
|