We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ed438c6 commit 05c8897Copy full SHA for 05c8897
README.md
@@ -236,6 +236,7 @@ the script.
236
* `.keys` becomes `Object.keys()`
237
* `.last` becomes `[*.length-1]`
238
* `.last(n)` becomes `.slice(*.length-1, *.length)`
239
+ * `.lstrip` becomes `.replace(/^\s+/, "")`
240
* `.max` becomes `Math.max.apply(Math)`
241
* `.merge` becomes `Object.assign({}, ...)`
242
* `.merge!` becomes `Object.assign()`
@@ -245,6 +246,7 @@ the script.
245
246
* `puts` becomes `console.log`
247
* `.replace` becomes `.length = 0; ...push.apply(*)`
248
* `.respond_to?` becomes `right in left`
249
+ * `.rstrip` becomes `.replace(/s+$/, "")`
250
* `.scan` becomes `.match(//g)`
251
* `.start_with?` becomes `.substring(0, arg.length) == arg`
252
* `.upto(lim)` becomes `for (var i=num; i<=lim; i+=1)`
lib/ruby2js/converter/regexp.rb
@@ -27,12 +27,23 @@ class Converter
27
end
28
29
30
+ # in Ruby regular expressions, ^ and $ apply to each line
31
if parts.first.type == :str and parts.first.children[0].start_with?('^')
32
opts = opts + [:m] unless opts.include? :m or opts.include? 'm'
33
elsif parts.last.type == :str and parts.last.children[0].end_with?('$')
34
35
36
37
+ if parts.first.type == :str and parts.first.children[0].start_with?('\A')
38
+ parts = [s(:str, parts.first.children[0].sub('\A', '^'))] +
39
+ parts[1..-1]
40
+ end
41
+
42
+ if parts.last.type == :str and parts.last.children[0].end_with?('\z')
43
+ parts = parts[0..-2] +
44
+ [s(:str, parts.first.children[0].sub(/\\z\z/, '$'))]
45
46
47
# use slash syntax if there are few embedded slashes in the regexp
48
if parts.all? {|part| part.type == :str}
49
str = parts.map {|part| part.children.first}.join
lib/ruby2js/filter/functions.rb
@@ -417,11 +417,21 @@ def on_send(node)
417
process node.updated(nil, [s(:const, nil, :Object), :fromEntries,
418
target])
419
420
- elsif es2019 and method==:rstrip
421
- process node.updated(nil, [target, :trimStart, *args])
+ elsif method==:rstrip
+ if es2019
422
+ process node.updated(nil, [target, :trimEnd, *args])
423
+ else
424
+ node.updated(nil, [process(target), :replace,
425
+ s(:regexp, s(:str, '\s+\z') , s(:regopt)), s(:str, '')])
426
427
- elsif es2019 and method==:lstrip
- process node.updated(nil, [target, :trimEnd, *args])
428
+ elsif method==:lstrip and args.length == 0
429
430
+ process s(:send, target, :trimStart)
431
432
433
+ s(:regexp, s(:str, '\A\s+') , s(:regopt)), s(:str, '')])
434
435
436
elsif method == :class and args.length==0 and not node.is_method?
437
process node.updated(:attr, [target, :constructor])
spec/es2019_spec.rb
@@ -33,11 +33,11 @@ def to_js_fn(string)
describe :String do
it "should handle lstrip" do
- to_js_fn( 'a.lstrip()' ).must_equal 'a.trimEnd()'
+ to_js_fn( 'a.lstrip()' ).must_equal 'a.trimStart()'
it "should handle rstrip" do
- to_js_fn( 'a.rstrip()' ).must_equal 'a.trimStart()'
+ to_js_fn( 'a.rstrip()' ).must_equal 'a.trimEnd()'
spec/functions_spec.rb
@@ -145,9 +145,11 @@ def to_js(string)
145
to_js( 'x.end_with?("z")' ).must_equal 'x.slice(-1) == "z"'
146
147
148
- it 'should handle strip' do
+ it 'should handle strip/lstrip/rstrip' do
149
to_js( 'x.strip()' ).must_equal 'x.trim()'
150
to_js( 'x.strip' ).must_equal 'x.trim()'
151
+ to_js( 'a.lstrip()' ).must_equal 'a.replace(/^\s+/, "")'
152
+ to_js( 'a.rstrip()' ).must_equal 'a.replace(/\s+$/, "")'
153
154
155
it 'should handle string multiply' do
spec/transliteration_spec.rb
@@ -963,6 +963,12 @@ def to_js( string, opts={} )
963
to_js( "/^./m" ).must_equal '/^./m'
964
to_js( "Regexp.new('^$', 'm')" ).must_equal '/^$/m'
965
966
967
+ it "should treat \A and \z as singleline" do
968
+ to_js( '/\A./' ).must_equal '/^./'
969
+ to_js( '/.\z/' ).must_equal '/.$/'
970
+ to_js( "Regexp.new(#{'\A\z'.inspect})" ).must_equal '/^$/'
971
972
973
974
describe "exceptions" do
0 commit comments