JavaScriptの整形

CotEditorのスクリプト

FireBugを使っていて,Consoleから"console.toSource()"と実行するとFireBugスクリプトを表示できることに気がつきました.でもぐっちゃりしていて読みにくいんす.最初は手で編集して読もうとしていたんだけど,こんなことは自動でやるべきだと強く思ったので,CotEditorのスクリプトを作ってみました.こういうファイルを

#!/bin/sh
# %%%{CotEditorXInput=Selection}%%%
# %%%{CotEditorXOutput=ReplaceSelection}%%%
/usr/local/bin/perl -MIO::All -MJavaScript::Swell -e 'print JavaScript::Swell->swell(io->stdin->all)'

~/Library/Application Support/CotEditor/ScriptMenuに例えば"jstidy.pl"という名前で保存します.詳しいことは404 Blog Not Found:Mac - CotEditorがはげしくイカしている件を参照してくださいませ.
んで,実際に変換してみたJavaScriptは以下のようになります.

({
  firebug : "1.05", log : (function () {
    logFormatted(arguments, "log"); 
  }
  ), debug : (function () {
    logFormatted(arguments, "debug", true); 
  }
  ), info : (function () {
    logFormatted(arguments, "info", true); 
  }
  ), warn : (function () {
    logFormatted(arguments, "warn", true); 
  }
  ), error : (function () {
    Firebug.Errors.increaseCount(context); logFormatted(arguments, "error", true); 
  }
  ), assert : (function (x) {
    if (! x) {
      logAssert(FBL.sliceArray(arguments, 1), ["undefined", x]); 
    }
  }
  ), dir : (function (o) {
    Firebug.Console.log(o, context, "dir", Firebug.DOMPanel.DirTable); 
  }
  ), dirxml : (function (o) {
    if (o instanceof Window) {
      o = o.document.documentElement; 
    }
    else if (o instanceof Document) {
      o = o.documentElement; 
    }
    Firebug.Console.log(o, context, "dirxml", Firebug.HTMLPanel.SoloElement); 
  }
  ), trace : (function () {
    var trace = FBL.getCurrentStackTrace(context); Firebug.Console.log(trace, context, "stackTrace"); 
  }
  ), group : (function () {
    var sourceLink = FBL.getStackSourceLink(Components.stack); Firebug.Console.openGroup(arguments, null, "group", null, false, sourceLink); 
  }
  ), groupEnd : (function () {
    Firebug.Console.closeGroup(context); 
  }
  ), time : (function (name, reset) {
    if (! name) {
      return; 
    }
    var time = (new Date).getTime(); if (! context.timeCounters) {
      context.timeCounters = {
      };
      
    }
    if (! reset && name in context.timeCounters) {
      return; 
    }
    context.timeCounters[name] = time; 
  }
  ), timeEnd : (function (name) {
    var time = (new Date).getTime(); if (! context.timeCounters) {
      return; 
    }
    var timeCounter = context.timeCounters[name]; if (timeCounter) {
      var diff = time - timeCounter; var label = name + ": " + diff + "ms"; logFormatted([label], null, true); delete context.timeCounters[name]; 
    }
  }
  ), profile : (function (title) {
    Firebug.Profiler.startProfiling(context, title); 
  }
  ), profileEnd : (function () {
    Firebug.Profiler.stopProfiling(context); 
  }
  ), count : (function (key) {
    var frameId = FBL.getStackFrameId(); if (frameId) {
      if (! context.frameCounters) {
        context.frameCounters = {
        };
        
      }
      if (key != undefined) {
        frameId += key; 
      }
      var frameCounter = context.frameCounters[frameId]; if (! frameCounter) {
        var logRow = logFormatted(["0"], null, true, true); frameCounter = {
          logRow : logRow, count : 1
        };
        context.frameCounters[frameId] = frameCounter; 
      }
      else {
        ++frameCounter.count; 
      }
      var label = key == undefined ? frameCounter.count : key + " " + frameCounter.count; frameCounter.logRow.firstChild.firstChild.nodeValue = label; 
    }
  }
  )
}
)

改行するように作り直すとより便利かな?