Module: JSMin

### Home Classes Methods

### In Files

- lib/jsmin.rb

### Methods

- ::minify

### Class/Module Index

Quicksearch

- JSMin

No matching classes.

# JSMin

Ruby implementation of Douglas Crockford's JavaScript minifier, JSMin.

Author | Ryan Grove (ryan@wonko.com)
Version | 1.0.1 (2008-11-10)
Copyright | Copyright (c) 2008 Ryan Grove. All rights reserved.
Website | github.com/rgrove/jsmin/

## Example

    require 'rubygems'
    require 'jsmin'

    File.open('example.js', 'r') {|file| puts JSMin.minify(file) }

### Constants

CHR_APOS
CHR_ASTERISK
CHR_BACKSLASH
CHR_CR
CHR_FRONTSLASH
CHR_LF
CHR_QUOTE
CHR_SPACE
ORD_LF
ORD_SPACE
ORD_TILDE

### Public Class Methods

minify(input) click to toggle source

Reads JavaScript from `input` (which can be a String or an IO object) and returns a String containing minified JS.

    # File lib/jsmin.rb, line 72
    def minify(input)
      @js = StringScanner.new(input.is_a?(IO) ? input.read : input.to_s)

      @a         = "\n"
      @b         = nil
      @lookahead = nil
      @output    = ''

      action_get

      while !@a.nil? do
        case @a
        when CHR_SPACE
          if alphanum?(@b)
            action_output
          else
            action_copy
          end

        when CHR_LF
          if @b == CHR_SPACE
            action_get
          elsif @b =~ /[{\[\(+-]/
            action_output
          else
            if alphanum?(@b)
              action_output
            else
              action_copy
            end
          end

        else
          if @b == CHR_SPACE
            if alphanum?(@a)
              action_output
            else
              action_get
            end
          elsif @b == CHR_LF
            if @a =~ /[}\]\)\\"+-]/
              action_output
            else
              if alphanum?(@a)
                action_output
              else
                action_get
              end
            end
          else
            action_output
          end
        end
      end

      @output
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.