AS3 Dumper

| Comments

i just found at libspark this handy class it’s just like the ObjectDumper Class from AS2 =D and has the ability to throw the message to the firebug console =D

package org.libspark.utils 
{
    import flash.external.ExternalInterface;
    import flash.utils.getQualifiedClassName;

    /**
     * <p>Data????????????perl?Data::Dumper???????
     * ???????????Object, Array, Number, String, Boolean???
     * XML?????????????????
     * ??????FireBug?console???????????????????????
     * debug, info, warn, error ???????????????</p>
     * <listing>
     * package {
     *    import org.libspark.utils.Dumper;
     *    public Class Hoge {
     *        var test:Object = { a:'hoge', b:'fuga'};
     *        Dumper.debug(test);
     *        trace(Dumper.toString(test));
     *    }
     *    // output
     *         $var0 = {
     *                     'a' => 'hoge',
     *                     'b' => 'fuga',
     *                 }
     *     
     * }</listing>
     * @author dealforest
     * @version 0.102
     */

    public class Dumper {

        private static const INDENT:String = "    ";
        /*
         * @default 4tab
        */
        private static var _dumpString:String = "";

        /**
         * ????????????????????
         *
         * @param args ???????(????)
         * @return _txt ??????????????????
         */
        public static function toString(... args):String {
            var _txt:String = _dumpString = '';
            for (var a:String in args)
                _txt += parse(args[a]);
            return _txt;
        }

        /**
         * FireBug???????'debug'?????
         *
         * @param args ???????(????)
         */
        public static function debug(... args):void {
            if (!ExternalInterface.available) return;

            var _txt:String = _dumpString = '';
            for (var a:String in args)
                _txt += parse(args[a]);
            //interim action for IE
            ExternalInterface.call('function (txt) { try { console.log(txt); } catch (e) {}; }', _txt);
        }

        /**
         * FireBug???????'info'?????
         *
         * @param args ???????(????)
         */
        public static function info(... args):void {
            if (!ExternalInterface.available) return;

            var _txt:String = _dumpString = '';
            for (var a:String in args)
                _txt += parse(args[a]);
            //interim action for IE
            ExternalInterface.call('function (txt) { try { console.info(txt); } catch (e) {}; }', _txt);
        }

        /**
         * FireBug???????'warn'?????
         *
         * @param args ???????(????)
         */
        public static function warn(... args):void {
            if (!ExternalInterface.available) return;

            var _txt:String = _dumpString = '';
            for (var a:String in args)
                _txt += parse(args[a]);
            //interim action for IE
            ExternalInterface.call('function (txt) { try { console.warn(txt); } catch (e) {}; }', _txt);
        }

        /**
         * FireBug???????'error'?????
         *
         * @param arg ???????(????)
         */
        public static function error(... args):void {
            if (!ExternalInterface.available) return;

            var _txt:String = _dumpString = '';
            for (var a:String in args)
                _txt += parse(args[a]);
            //interim action for IE
            ExternalInterface.call('function (txt) { try { console.error(txt); } catch (e) {}; }', _txt);
        }

        /**
         * ????????????????????
         *
         * @param arg ???????(??????1?)
         * @return ?????????????
         * @private
         */
        public static function parse(arg:*):String {
            var argIndent:String = ('$var'+ ' = ');
            _dumpString += argIndent;
            inspect(arg, argIndent.replace(/./g, ' '));
            _dumpString += 'n';
           return _dumpString;
        }

        /**
         * ?????????????????????
         *
         * @param arg ???????(??????1?)
         * @param indent ????????
         * @private
         */
        private static function inspect(arg:*, indent:String):void {
            var bracket:Object;

            switch (getQualifiedClassName(arg)) {
                case 'Object':
                    bracket = {start: '{', end: '}'};
                case 'Array':
                    bracket ||= {start: '[', end: ']'};
                    _dumpString += bracket.start + 'n';
                    recursion(arg, indent + INDENT);
                    _dumpString += (indent +bracket.end);
                    break;
                default:
                    _dumpString += format(arg, indent, true);
            }
        }

        /**
         * ?????????????????
         *
         * @param arg ???????(??????1?)
         * @param indent ????????
         * @private
         */
        private static function recursion(arg:*, indent:String):void {
            for (var index:String in arg) {
                var tmp:* = arg[index];
                var className:String = getQualifiedClassName(tmp);
                var bracket:Object;

                switch(className) {
                    case 'Object':
                        bracket = {start: '{', end: '}'};
                    case 'Array':
                        bracket ||= {start: '[', end: ']'};
                        var str:String = (getQualifiedClassName(arg) == 'Object')
                            ? ''' + index + '' => ' : '';
                        var indent_str:String = str.replace(/./g, ' ');

                        _dumpString += (indent + str + bracket.start + "n");
                        recursion(tmp, indent + indent_str + INDENT);
                        _dumpString += (indent + indent_str + bracket.end + ",");
                        break;
                    default:
                        _dumpString += format(tmp, indent, false, arg, index);
                }
                _dumpString += "n";
            }
        }

        /**
         * ??????????????????
         *
         * @param target ?????Object
         * @param indent ????????
         * @param braket ,??????????
         * @param parent ?????Object
         * @param index Object?key
         * @return ???????????????
         * @private
         */
        private static function format(target:Object, indent:String, bracket:Boolean, parent:* = null, index:String = null):String {
            var className:String = getQualifiedClassName(target);
            var str:String = "";

            if (getQualifiedClassName(parent) == 'Object')
                return indent +  "'" + index + "'" + " => " + target + (bracket ? '' : ',');

            switch (className) {
                case 'String':
                    return (bracket)
                        ? "'" + target + "'"
                        : indent + "'" + target + "',";

                case 'Array':
                case 'int':
                case 'uint':
                case 'Number':
                case 'Boolean':
                    return (bracket)
                        ? target.toString()
                        : indent + target.toString() + ",";
                default:
                    return 'don't analyze';
            }
        }
    }
}

from Dumper Class

check it out libspark.org they have very interesting things in their repository

by the way, the japanese characters doesn’t display in my browser =(

Comments