Skip to content

Commit 05c8897

Browse files
committed
Regex start of string and end of string; lstrip/rstrip
1 parent ed438c6 commit 05c8897

File tree

6 files changed

+38
-7
lines changed

6 files changed

+38
-7
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ the script.
236236
* `.keys` becomes `Object.keys()`
237237
* `.last` becomes `[*.length-1]`
238238
* `.last(n)` becomes `.slice(*.length-1, *.length)`
239+
* `.lstrip` becomes `.replace(/^\s+/, "")`
239240
* `.max` becomes `Math.max.apply(Math)`
240241
* `.merge` becomes `Object.assign({}, ...)`
241242
* `.merge!` becomes `Object.assign()`
@@ -245,6 +246,7 @@ the script.
245246
* `puts` becomes `console.log`
246247
* `.replace` becomes `.length = 0; ...push.apply(*)`
247248
* `.respond_to?` becomes `right in left`
249+
* `.rstrip` becomes `.replace(/s+$/, "")`
248250
* `.scan` becomes `.match(//g)`
249251
* `.start_with?` becomes `.substring(0, arg.length) == arg`
250252
* `.upto(lim)` becomes `for (var i=num; i<=lim; i+=1)`

lib/ruby2js/converter/regexp.rb

+11
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,23 @@ class Converter
2727
end
2828
end
2929

30+
# in Ruby regular expressions, ^ and $ apply to each line
3031
if parts.first.type == :str and parts.first.children[0].start_with?('^')
3132
opts = opts + [:m] unless opts.include? :m or opts.include? 'm'
3233
elsif parts.last.type == :str and parts.last.children[0].end_with?('$')
3334
opts = opts + [:m] unless opts.include? :m or opts.include? 'm'
3435
end
3536

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+
end
46+
3647
# use slash syntax if there are few embedded slashes in the regexp
3748
if parts.all? {|part| part.type == :str}
3849
str = parts.map {|part| part.children.first}.join

lib/ruby2js/filter/functions.rb

+14-4
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,21 @@ def on_send(node)
417417
process node.updated(nil, [s(:const, nil, :Object), :fromEntries,
418418
target])
419419

420-
elsif es2019 and method==:rstrip
421-
process node.updated(nil, [target, :trimStart, *args])
420+
elsif method==:rstrip
421+
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+
end
422427

423-
elsif es2019 and method==:lstrip
424-
process node.updated(nil, [target, :trimEnd, *args])
428+
elsif method==:lstrip and args.length == 0
429+
if es2019
430+
process s(:send, target, :trimStart)
431+
else
432+
node.updated(nil, [process(target), :replace,
433+
s(:regexp, s(:str, '\A\s+') , s(:regopt)), s(:str, '')])
434+
end
425435

426436
elsif method == :class and args.length==0 and not node.is_method?
427437
process node.updated(:attr, [target, :constructor])

spec/es2019_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def to_js_fn(string)
3333

3434
describe :String do
3535
it "should handle lstrip" do
36-
to_js_fn( 'a.lstrip()' ).must_equal 'a.trimEnd()'
36+
to_js_fn( 'a.lstrip()' ).must_equal 'a.trimStart()'
3737
end
3838

3939
it "should handle rstrip" do
40-
to_js_fn( 'a.rstrip()' ).must_equal 'a.trimStart()'
40+
to_js_fn( 'a.rstrip()' ).must_equal 'a.trimEnd()'
4141
end
4242
end
4343
end

spec/functions_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,11 @@ def to_js(string)
145145
to_js( 'x.end_with?("z")' ).must_equal 'x.slice(-1) == "z"'
146146
end
147147

148-
it 'should handle strip' do
148+
it 'should handle strip/lstrip/rstrip' do
149149
to_js( 'x.strip()' ).must_equal 'x.trim()'
150150
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+$/, "")'
151153
end
152154

153155
it 'should handle string multiply' do

spec/transliteration_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,12 @@ def to_js( string, opts={} )
963963
to_js( "/^./m" ).must_equal '/^./m'
964964
to_js( "Regexp.new('^$', 'm')" ).must_equal '/^$/m'
965965
end
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+
end
966972
end
967973

968974
describe "exceptions" do

0 commit comments

Comments
 (0)