no_regex 0.1.0 → 0.2.0
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/CHANGELOG.md +8 -0
- data/README.md +237 -2
- data/lib/no_regex/pattern_methods.rb +3 -2
- data/lib/no_regex/string_extensions.rb +1 -1
- data/lib/no_regex/version.rb +1 -1
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14eee48041a9af9e8eb4d1b7fc16ad89ea2655897e5171dd1f7061cb5a982a92
|
4
|
+
data.tar.gz: 45673b07974a3814c9d3db94fac6177ce8ca98f428e38de42dbd1e5ab3132034
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2697530e3cb7641968aae80d95026f5efc990ee327765697fb084517a3aa2de88facb9eb7139c40e6f68d5a0ac1321818337f75cabe7cc1bc95a626285d0a45
|
7
|
+
data.tar.gz: 620a663f1a22948421f04c3f975301e15f0cbc623065c1814fb38f1d003644c5f4acae264365f4ba5e5197b48c914495fc5ef67049be3992aa1a6647764c9bd1
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [0.2.0] - 2025-21-07
|
4
|
+
### Changed
|
5
|
+
- `is_number?` now accepts all numeric formats (integers and decimals, positive and negative)
|
6
|
+
- Previously only accepted positive integers
|
7
|
+
- Use `is_integer` for strictly whole numbers
|
8
|
+
- Added "Transform Your Code" section to README for better first impressions
|
data/README.md
CHANGED
@@ -1,10 +1,245 @@
|
|
1
|
-
|
1
|
+
# NoRegex
|
2
2
|
|
3
3
|
Write Ruby without regex! A gem that provides simple, readable methods to replace complex regular expressions for string validation and manipulation.
|
4
4
|
|
5
|
+
## Transform Your Code
|
6
|
+
|
7
|
+
**BEFORE** - From complex regex patterns:
|
8
|
+
```ruby
|
9
|
+
validates :email, format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i }
|
10
|
+
validates :phone, format: { with: /\A\+?[\d\s\-\(\)]+\z/ }
|
11
|
+
|
12
|
+
if params[:search].match?(/\A\d+\z/)
|
13
|
+
# It's a number
|
14
|
+
end
|
15
|
+
|
16
|
+
user_input.gsub(/[`a-zA-Z0-9]/, '')
|
17
|
+
```
|
18
|
+
|
19
|
+
**AFTER** - To simple, readable methods!
|
20
|
+
```ruby
|
21
|
+
validates :email, format: { with: is_email? }
|
22
|
+
validates :phone, format: { with: is_phone_number? }
|
23
|
+
|
24
|
+
if params[:search].is_number?
|
25
|
+
# It's a number
|
26
|
+
end
|
27
|
+
|
28
|
+
user_input.remove_special_chars
|
29
|
+
```
|
30
|
+
|
31
|
+
No more googling regex patterns or debugging cryptic expressions!
|
32
|
+
|
5
33
|
## Installation
|
6
34
|
|
7
35
|
Add this line to your application's Gemfile:
|
8
36
|
|
9
37
|
```ruby
|
10
|
-
gem 'no_regex'
|
38
|
+
gem 'no_regex'
|
39
|
+
```
|
40
|
+
|
41
|
+
And then execute:
|
42
|
+
|
43
|
+
```bash
|
44
|
+
$ bundle install
|
45
|
+
```
|
46
|
+
|
47
|
+
Or install it yourself as:
|
48
|
+
|
49
|
+
```bash
|
50
|
+
$ gem install no_regex
|
51
|
+
```
|
52
|
+
|
53
|
+
## Rails Integration
|
54
|
+
|
55
|
+
To use the `format: { with: is_number? }` syntax in your Rails models or form objects, you need to extend your class with `NoRegex::PatternMethods`:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class User < ApplicationRecord
|
59
|
+
extend NoRegex::PatternMethods
|
60
|
+
|
61
|
+
validates :email, format: { with: is_email? }
|
62
|
+
validates :phone, format: { with: is_phone_number? }
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
For form objects:
|
67
|
+
```ruby
|
68
|
+
class MyFormObject
|
69
|
+
include ActiveModel::Model
|
70
|
+
extend NoRegex::PatternMethods
|
71
|
+
|
72
|
+
validates :search_term, format: { with: is_number? }
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
This makes all the pattern methods available to your validations. Once extended, you can use any of the validation methods in the Format Validations section.
|
77
|
+
|
78
|
+
|
79
|
+
## Usage
|
80
|
+
|
81
|
+
NoRegex extends Ruby's String, Integer, Float, and other classes with intuitive methods that eliminate the need for regex:
|
82
|
+
|
83
|
+
### String Methods
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
require 'no_regex'
|
87
|
+
|
88
|
+
# Validation methods - return true/false
|
89
|
+
"12345".is_number? # => true
|
90
|
+
"test@example.com".is_email? # => true
|
91
|
+
"https://example.com".is_url? # => true
|
92
|
+
"john_doe".is_username? # => true
|
93
|
+
|
94
|
+
# Manipulation methods - return modified strings
|
95
|
+
"hello-world".remove_dashes # => "helloworld"
|
96
|
+
"abc123xyz".keep_numbers # => "123"
|
97
|
+
"HelloWorld".to_snake_case # => "hello_world"
|
98
|
+
|
99
|
+
# Extraction methods - return arrays
|
100
|
+
"Contact: test@example.com".extract_emails # => ["test@example.com"]
|
101
|
+
"Price: $123".extract_numbers # => ["123"]
|
102
|
+
```
|
103
|
+
|
104
|
+
### Numeric Methods
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
42.is_positive? # => true
|
108
|
+
123.to_currency # => "$123"
|
109
|
+
3.14.to_percentage # => "314.0%"
|
110
|
+
```
|
111
|
+
|
112
|
+
### Validation Methods
|
113
|
+
|
114
|
+
- `is_number?` - Check if string contains a number
|
115
|
+
- `is_letters?` - Check if string contains only letters
|
116
|
+
- `is_integer?` - Check if string is an integer
|
117
|
+
- `is_decimal?` - Check if string is a decimal number
|
118
|
+
- `is_alphanumeric?` - Check if string contains only letters and numbers
|
119
|
+
- `is_email?` - Check if string is a valid email format
|
120
|
+
- `is_blank?` - Check if string is nil or contains only whitespace
|
121
|
+
- `is_url?` - Check if string is a valid URL format
|
122
|
+
- `is_phone_number?` - Check if string is a valid phone number
|
123
|
+
- `is_zip_code?` - Check if string is a valid ZIP code
|
124
|
+
- `is_hex_color?` - Check if string is a valid hex color
|
125
|
+
- `is_username?` - Check if string is a valid username (letters, numbers, _, -)
|
126
|
+
- `is_positive_number?` - Check if string is a positive number
|
127
|
+
- `is_uuid?` - Check if string is a valid UUID
|
128
|
+
- `is_credit_card?` - Check if string is a valid credit card number
|
129
|
+
- `is_ssn?` - Check if string is a valid SSN
|
130
|
+
- `is_ipv4?` - Check if string is a valid IPv4 address
|
131
|
+
- `is_time_24h?` - Check if string is valid 24-hour time format
|
132
|
+
- `is_date_yyyy_mm_dd?` - Check if string is valid YYYY-MM-DD date format
|
133
|
+
|
134
|
+
### Manipulation Methods
|
135
|
+
|
136
|
+
- `remove_dashes` - Remove all dashes from string
|
137
|
+
- `remove_spaces` - Remove all spaces from string
|
138
|
+
- `remove_special_chars` - Remove all special characters, keeping only letters and numbers
|
139
|
+
- `remove_numbers` - Remove all numbers from string
|
140
|
+
- `remove_letters` - Remove all letters from string
|
141
|
+
- `keep_numbers` - Keep only numbers in string
|
142
|
+
- `keep_letters` - Keep only letters in string
|
143
|
+
|
144
|
+
### Conversion Methods
|
145
|
+
|
146
|
+
- `to_snake_case` - Convert string to snake_case
|
147
|
+
- `to_camel_case` - Convert string to camelCase
|
148
|
+
|
149
|
+
### Extraction Methods
|
150
|
+
|
151
|
+
- `extract_numbers` - Extract all numbers from string (returns array)
|
152
|
+
- `extract_emails` - Extract all email addresses from string (returns array)
|
153
|
+
|
154
|
+
### Utility Methods
|
155
|
+
|
156
|
+
- `word_count` - Count words in string
|
157
|
+
- `truncate(length, ellipsis)` - Truncate string to specified length with ellipsis
|
158
|
+
|
159
|
+
|
160
|
+
### Format Validators
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
validates :field, format: { with: is_number? } # Only digits
|
164
|
+
validates :field, format: { with: is_letters? } # Only letters
|
165
|
+
validates :field, format: { with: is_integer? } # Integers
|
166
|
+
validates :field, format: { with: is_decimal? } # Decimals
|
167
|
+
validates :field, format: { with: is_alphanumeric? } # Letters and numbers
|
168
|
+
validates :field, format: { with: is_email? } # Email format
|
169
|
+
validates :field, format: { with: is_phone_number? } # Phone format
|
170
|
+
validates :field, format: { with: is_url? } # URL format
|
171
|
+
validates :field, format: { with: is_zip_code? } # ZIP code
|
172
|
+
validates :field, format: { with: is_hex_color? } # Hex colors
|
173
|
+
validates :field, format: { with: is_username? } # Usernames
|
174
|
+
validates :field, format: { with: is_positive_number? } # Positive numbers
|
175
|
+
validates :field, format: { with: is_uuid? } # UUIDs
|
176
|
+
validates :field, format: { with: is_credit_card? } # Credit cards
|
177
|
+
validates :field, format: { with: is_ssn? } # SSN format
|
178
|
+
validates :field, format: { with: is_ipv4? } # IP addresses
|
179
|
+
validates :field, format: { with: is_time_24h? } # 24-hour time
|
180
|
+
validates :field, format: { with: is_date_yyyy_mm_dd? } # YYYY-MM-DD dates
|
181
|
+
```
|
182
|
+
|
183
|
+
### Custom Validators (Alternative Approach)
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
class User < ApplicationRecord
|
187
|
+
validates :phone, number: true
|
188
|
+
validates :name, letters: { message: "can only contain letters" }
|
189
|
+
validates :username, alphanumeric: true
|
190
|
+
validates :email, email_format: true
|
191
|
+
validates :website, url_format: { allow_blank: true }
|
192
|
+
end
|
193
|
+
```
|
194
|
+
|
195
|
+
## Examples
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
# Validation
|
199
|
+
"12345".is_number? # => true
|
200
|
+
"abc123".is_letters? # => false
|
201
|
+
"test@example.com".is_email? # => true
|
202
|
+
"https://example.com".is_url? # => true
|
203
|
+
|
204
|
+
# Manipulation
|
205
|
+
"hello-world-123".remove_dashes # => "helloworld123"
|
206
|
+
"hello world 123".remove_numbers # => "hello world "
|
207
|
+
"abc123xyz".keep_numbers # => "123"
|
208
|
+
"abc123xyz".keep_letters # => "abcxyz"
|
209
|
+
"Hello@World#123!".remove_special_chars # => "HelloWorld123"
|
210
|
+
|
211
|
+
# Conversion
|
212
|
+
"HelloWorld".to_snake_case # => "hello_world"
|
213
|
+
"hello_world".to_camel_case # => "helloWorld"
|
214
|
+
|
215
|
+
# Extraction
|
216
|
+
"Contact: test@example.com or admin@site.org".extract_emails
|
217
|
+
# => ["test@example.com", "admin@site.org"]
|
218
|
+
|
219
|
+
"Price is $123 or $456".extract_numbers # => ["123", "456"]
|
220
|
+
|
221
|
+
# Utilities
|
222
|
+
"Hello world, this is a test".word_count # => 6
|
223
|
+
"This is a very long string".truncate(15) # => "This is a ve..."
|
224
|
+
|
225
|
+
# Numeric Methods
|
226
|
+
42.is_positive? # => true
|
227
|
+
-10.is_negative? # => true
|
228
|
+
100.to_currency # => "$100"
|
229
|
+
1234.to_formatted # => "1,234"
|
230
|
+
0.75.to_percentage # => "75.0%"
|
231
|
+
```
|
232
|
+
|
233
|
+
## Development
|
234
|
+
|
235
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
236
|
+
|
237
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
238
|
+
|
239
|
+
## Contributing
|
240
|
+
|
241
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Derity/no_regex
|
242
|
+
|
243
|
+
## License
|
244
|
+
|
245
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -3,9 +3,10 @@
|
|
3
3
|
module NoRegex
|
4
4
|
module PatternMethods
|
5
5
|
# Define methods that return regex patterns
|
6
|
+
#
|
6
7
|
def is_number?
|
7
|
-
# Examples: "
|
8
|
-
/\A
|
8
|
+
# Examples: "123", "-456", "12.34", "-0.5", "0", "999.99"
|
9
|
+
/\A-?\d+(\.\d+)?\z/
|
9
10
|
end
|
10
11
|
|
11
12
|
def is_letters?
|
data/lib/no_regex/version.rb
CHANGED
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: no_regex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- David Wright
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
@@ -54,11 +54,12 @@ dependencies:
|
|
54
54
|
description: Replace complex regex patterns with simple, readable methods for string
|
55
55
|
validation and manipulation
|
56
56
|
email:
|
57
|
-
-
|
57
|
+
- derity@derity.com
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
+
- CHANGELOG.md
|
62
63
|
- README.md
|
63
64
|
- lib/no_regex.rb
|
64
65
|
- lib/no_regex/aliases.rb
|
@@ -67,13 +68,13 @@ files:
|
|
67
68
|
- lib/no_regex/rails_validators.rb
|
68
69
|
- lib/no_regex/string_extensions.rb
|
69
70
|
- lib/no_regex/version.rb
|
70
|
-
homepage: https://github.com/
|
71
|
+
homepage: https://github.com/Derity/no_regex
|
71
72
|
licenses:
|
72
73
|
- MIT
|
73
74
|
metadata:
|
74
|
-
homepage_uri: https://github.com/
|
75
|
-
source_code_uri: https://github.com/
|
76
|
-
changelog_uri: https://github.com/
|
75
|
+
homepage_uri: https://github.com/Derity/no_regex
|
76
|
+
source_code_uri: https://github.com/Derity/no_regex
|
77
|
+
changelog_uri: https://github.com/Derity/no_regex/blob/main/CHANGELOG.md
|
77
78
|
rdoc_options: []
|
78
79
|
require_paths:
|
79
80
|
- lib
|