Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Converts global chained `var`s into single-line vars.
#
# Example:
#
# >> cat test.js
# var A = require('a'),
# B = require('b'),
# C = B.C;
#
# var D = function() {};
#
# >> ruby unchain_vars.rb test.js
#
# >> cat test.js
# var A = require('a');
# var B = require('b');
# var C = B.C;
#
# var D = function() {};
#
filename = ARGV[0]
active = false
result = ''
File.open(filename).each do |line|
if line =~ /^var .*,$/
active = true
end
if active
result << line.gsub(/^(var | +)(.+)[,;]$/, 'var \2;')
else
result << line
end
if active && line =~ /;$/
active = false
end
end
File.write(filename, result)
@lencioni

This comment has been minimized.

Copy link

@lencioni lencioni commented Nov 13, 2014

I made it able to take multiple filenames: https://gist.github.com/lencioni/b638689132e6c3a665c4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment