Class: JsRegex
- Inherits:
-
Object
- Object
- JsRegex
- Defined in:
- lib/js_regex.rb,
lib/js_regex/node.rb,
lib/js_regex/error.rb,
lib/js_regex/target.rb,
lib/js_regex/version.rb,
lib/js_regex/converter.rb,
lib/js_regex/conversion.rb,
lib/js_regex/second_pass.rb,
lib/js_regex/converter/base.rb,
lib/js_regex/converter/context.rb,
lib/js_regex/converter/set_converter.rb,
lib/js_regex/converter/keep_converter.rb,
lib/js_regex/converter/meta_converter.rb,
lib/js_regex/converter/type_converter.rb,
lib/js_regex/converter/group_converter.rb,
lib/js_regex/converter/anchor_converter.rb,
lib/js_regex/converter/escape_converter.rb,
lib/js_regex/converter/literal_converter.rb,
lib/js_regex/converter/property_converter.rb,
lib/js_regex/converter/assertion_converter.rb,
lib/js_regex/converter/freespace_converter.rb,
lib/js_regex/converter/conditional_converter.rb,
lib/js_regex/converter/backreference_converter.rb,
lib/js_regex/converter/subexpression_converter.rb,
lib/js_regex/converter/unsupported_token_converter.rb
Overview
JsRegex converts ::Regexp instances to JavaScript.
Usage:
js_regex = JsRegex.new(my_ruby_regex) js_regex.to_h # for use in ‘new RegExp()’ js_regex.to_s # for direct injection into JavaScript
Defined Under Namespace
Modules: Converter, Error, SecondPass, Target Classes: Conversion, Node
Constant Summary collapse
- ConversionError =
Class.new(StandardError).send(:include, JsRegex::Error)
- VERSION =
'3.12.0'
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(ruby_regex, **kwargs) ⇒ JsRegex
constructor
A new instance of JsRegex.
- #to_h ⇒ Object
- #to_json(options = {}) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(ruby_regex, **kwargs) ⇒ JsRegex
Returns a new instance of JsRegex.
17 18 19 |
# File 'lib/js_regex.rb', line 17 def initialize(ruby_regex, **kwargs) @source, @options, @warnings, @target = Conversion.of(ruby_regex, **kwargs) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/js_regex.rb', line 15 def @options end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
15 16 17 |
# File 'lib/js_regex.rb', line 15 def source @source end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
15 16 17 |
# File 'lib/js_regex.rb', line 15 def target @target end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
15 16 17 |
# File 'lib/js_regex.rb', line 15 def warnings @warnings end |
Class Method Details
.compatible?(ruby_regex, **kwargs) ⇒ Boolean
38 39 40 41 42 43 |
# File 'lib/js_regex.rb', line 38 def self.compatible?(ruby_regex, **kwargs) new!(ruby_regex, **kwargs) true rescue ConversionError false end |
.new!(ruby_regex, **kwargs) ⇒ Object
34 35 36 |
# File 'lib/js_regex.rb', line 34 def self.new!(ruby_regex, **kwargs) new(ruby_regex, fail_fast: true, **kwargs) end |
Instance Method Details
#to_h ⇒ Object
21 22 23 |
# File 'lib/js_regex.rb', line 21 def to_h { source: source, options: } end |
#to_json(options = {}) ⇒ Object
25 26 27 |
# File 'lib/js_regex.rb', line 25 def to_json( = {}) to_h.to_json() end |
#to_s ⇒ Object
29 30 31 |
# File 'lib/js_regex.rb', line 29 def to_s "/#{source.empty? ? '(?:)' : source}/#{}" end |