Class: JsRegex

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/js_regex.rb', line 15

def options
  @options
end

#sourceObject (readonly)

Returns the value of attribute source.



15
16
17
# File 'lib/js_regex.rb', line 15

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



15
16
17
# File 'lib/js_regex.rb', line 15

def target
  @target
end

#warningsObject (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

Returns:

  • (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

Raises:

  • JsRegex::ConversionError



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_hObject



21
22
23
# File 'lib/js_regex.rb', line 21

def to_h
  { source: source, options: options }
end

#to_json(options = {}) ⇒ Object



25
26
27
# File 'lib/js_regex.rb', line 25

def to_json(options = {})
  to_h.to_json(options)
end

#to_sObject



29
30
31
# File 'lib/js_regex.rb', line 29

def to_s
  "/#{source.empty? ? '(?:)' : source}/#{options}"
end