Skip to content

Instantly share code, notes, and snippets.

@trotzig
Created November 13, 2014 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trotzig/d3ff1e156701bfcd144a to your computer and use it in GitHub Desktop.
Save trotzig/d3ff1e156701bfcd144a to your computer and use it in GitHub Desktop.
# 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
Copy link

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