js_regex_to_ruby 0.1.1 → 0.1.2
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 +19 -0
- data/lib/js_regex_to_ruby/version.rb +1 -1
- data/lib/js_regex_to_ruby.rb +22 -8
- data/sig/js_regex_to_ruby.rbs +8 -1
- 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: 4aafea531008742c38c529eee7b2145db1dbffa69b3fb1d4b6ff5d6c8931a0f8
|
|
4
|
+
data.tar.gz: 3c26adad1068ded864447c384fab39b339b8abb12a401617fcaaecde385b19ea
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca43d3056ef25e4cf29f630b11dd5bbaf21a210b5e77756d45f572ca5339e9c22941c29eb3131da0c688a56d41e111bdff5643a80097f13385a09b9cc6a84b6f
|
|
7
|
+
data.tar.gz: eca9a62564569a9a8e4413804e85c6c94254cd98fca8e0d9ecd2c91e7301028c2ef2eea884d16db1a0b1f4d9ed99e3efd64c17da2e5dcbc6776e02e1508e4836
|
data/README.md
CHANGED
|
@@ -69,6 +69,25 @@ JsRegexToRuby.try_convert("/(?invalid/") #=> nil
|
|
|
69
69
|
JsRegexToRuby.try_convert(nil) #=> nil
|
|
70
70
|
```
|
|
71
71
|
|
|
72
|
+
#### `literal_only` Option
|
|
73
|
+
|
|
74
|
+
By default, strings without `/` delimiters are treated as raw pattern sources:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
JsRegexToRuby.try_convert("cat") #=> /cat/ (compiled as pattern)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
If you only want to accept JS regex literals (strings starting with `/`), use `literal_only: true`:
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
# Only accept /pattern/flags format
|
|
84
|
+
JsRegexToRuby.try_convert("cat", literal_only: true) #=> nil (not a literal)
|
|
85
|
+
JsRegexToRuby.try_convert("/cat/", literal_only: true) #=> /cat/
|
|
86
|
+
JsRegexToRuby.try_convert("/cat/i", literal_only: true) #=> /cat/i
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
This is useful when you want to distinguish between plain strings (like keywords) and regex patterns in user input.
|
|
90
|
+
|
|
72
91
|
### Parsing JS Literal Without Converting
|
|
73
92
|
|
|
74
93
|
```ruby
|
data/lib/js_regex_to_ruby.rb
CHANGED
|
@@ -17,18 +17,32 @@ module JsRegexToRuby
|
|
|
17
17
|
# Returns the compiled Regexp on success, or nil on failure.
|
|
18
18
|
# Never raises exceptions for invalid input.
|
|
19
19
|
#
|
|
20
|
-
# @example
|
|
20
|
+
# @example With JS regex literal (starts with /)
|
|
21
21
|
# JsRegexToRuby.try_convert('/^foo$/i') #=> /\Afoo\z/i
|
|
22
|
-
# JsRegexToRuby.try_convert('
|
|
23
|
-
#
|
|
22
|
+
# JsRegexToRuby.try_convert('/invalid[/') #=> nil (invalid regex syntax)
|
|
23
|
+
#
|
|
24
|
+
# @example With pattern source (no / delimiter) - treated as raw pattern
|
|
25
|
+
# JsRegexToRuby.try_convert('cat') #=> /cat/
|
|
26
|
+
# JsRegexToRuby.try_convert('[invalid') #=> nil (invalid regex syntax)
|
|
24
27
|
#
|
|
25
|
-
# @
|
|
28
|
+
# @example With literal_only: true - only accept /.../ format
|
|
29
|
+
# JsRegexToRuby.try_convert('cat', literal_only: true) #=> nil (doesn't start with /)
|
|
30
|
+
# JsRegexToRuby.try_convert('/cat/', literal_only: true) #=> /cat/
|
|
31
|
+
#
|
|
32
|
+
# @param input [String, #to_s] Either a JS literal `/.../flags` or a JS pattern source.
|
|
26
33
|
# @param flags [String, nil] JS flags if input is not a literal.
|
|
34
|
+
# @param literal_only [Boolean] If true, returns nil for inputs that don't look like
|
|
35
|
+
# JS regex literals (i.e., don't start with "/"). Useful when you want to treat
|
|
36
|
+
# plain strings like "cat" as non-regex values rather than pattern sources.
|
|
27
37
|
# @return [Regexp, nil] The compiled Ruby Regexp, or nil if conversion/compilation failed.
|
|
28
|
-
def self.try_convert(input, flags: nil)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
38
|
+
def self.try_convert(input, flags: nil, literal_only: false)
|
|
39
|
+
if literal_only
|
|
40
|
+
input = input.to_s
|
|
41
|
+
return nil unless input.start_with?("/")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
Converter.convert(input, flags: flags, compile: true).regexp
|
|
45
|
+
rescue
|
|
32
46
|
nil
|
|
33
47
|
end
|
|
34
48
|
|
data/sig/js_regex_to_ruby.rbs
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
# Interface for objects that respond to #to_s
|
|
2
|
+
interface _ToS
|
|
3
|
+
def to_s: () -> String
|
|
4
|
+
end
|
|
5
|
+
|
|
1
6
|
module JsRegexToRuby
|
|
2
7
|
VERSION: String
|
|
3
8
|
|
|
@@ -5,7 +10,9 @@ module JsRegexToRuby
|
|
|
5
10
|
def self.convert: (String input, ?flags: String?, ?compile: bool) -> Result
|
|
6
11
|
|
|
7
12
|
# Try to convert a JS regex to Ruby Regexp, returns nil on failure
|
|
8
|
-
|
|
13
|
+
# - When literal_only: false (default), only String inputs are processed; non-strings return nil
|
|
14
|
+
# - When literal_only: true, input is converted via #to_s; returns nil if not starting with "/"
|
|
15
|
+
def self.try_convert: (String | _ToS input, ?flags: String?, ?literal_only: bool) -> Regexp?
|
|
9
16
|
|
|
10
17
|
# Parse a JS regex literal into pattern and flags
|
|
11
18
|
def self.parse_literal: (String literal) -> [String, String]
|