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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdbe70d784541cad9e2009b2306416e72349cfc5e9136cadb998faf811d5ad91
4
- data.tar.gz: fee286185020e60bf68b8c36736993ed75f8056fae28a882d977498a35445b44
3
+ metadata.gz: 4aafea531008742c38c529eee7b2145db1dbffa69b3fb1d4b6ff5d6c8931a0f8
4
+ data.tar.gz: 3c26adad1068ded864447c384fab39b339b8abb12a401617fcaaecde385b19ea
5
5
  SHA512:
6
- metadata.gz: b9290e4565d2635db41c94ffbf865dbe3a085d943c413e5f5c635c74b67584ff73e09e0fe230674f662146cf170e8977e474d9aa0963b47aa84da4a3012a18ce
7
- data.tar.gz: d12f32df94de6834ce18c083e0b0ba9eaadacdf1ed4c56a6e39c236e4308e1337710eb33a1ebe5f34474e52ba75db90af5654c6eadea37ecee8c2e8f90f54cd0
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsRegexToRuby
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
@@ -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('not a regex') #=> nil
23
- # JsRegexToRuby.try_convert('/invalid[/') #=> nil
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
- # @param input [String] Either a JS literal `/.../flags` or a JS pattern source.
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
- result = Converter.convert(input, flags: flags, compile: true)
30
- result.regexp
31
- rescue ArgumentError, RegexpError
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
 
@@ -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
- def self.try_convert: (String input, ?flags: String?) -> Regexp?
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]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_regex_to_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - jasl