Created
February 4, 2009 13:47
-
-
Save samleb/58106 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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