Skip to content

Instantly share code, notes, and snippets.

@samleb
Created February 4, 2009 13:47
Show Gist options
  • Save samleb/58106 to your computer and use it in GitHub Desktop.
Save samleb/58106 to your computer and use it in GitHub Desktop.
class JavascriptObfuscator
def self.obfuscate(source)
new(source).obfuscate
end
def initialize(source)
@source = source
@last_identifier = "a"
@translations = { }
end
def obfuscate
@source.gsub(/((?:\{|,|\.)\s*)_(\w+)/m) do
$1 + "_" + translation_for_identifier($2)
end
end
private
def translation_for_identifier(identifier)
@translations[identifier] ||= next_identifier
end
def next_identifier
returning(@last_identifier) do
@last_identifier = @last_identifier.succ
end
end
end
if __FILE__ == $0
print JavascriptObfuscator.obfuscate(File.read(ARGV[0]))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment