[6785] | 1 | // This library is free software; you can redistribute it and/or |
---|
| 2 | // modify it under the terms of the GNU Lesser General Public |
---|
| 3 | // License as published by the Free Software Foundation; either |
---|
| 4 | // version 2.1 of the License, or (at your option) any later version. |
---|
| 5 | // |
---|
| 6 | // This library is distributed in the hope that it will be useful, |
---|
| 7 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 8 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 9 | // Lesser General Public License for more details. |
---|
| 10 | // |
---|
| 11 | // You should have received a copy of the GNU Lesser General Public |
---|
| 12 | // License along with this library; if not, write to the Free Software |
---|
| 13 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 14 | // |
---|
| 15 | // Author: John Leach |
---|
| 16 | // Contact: john.leach@syger.it |
---|
| 17 | |
---|
| 18 | // Create a namespace 'syger' to keep the Global Object clean |
---|
| 19 | var syger = { |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | Introspects an object. |
---|
| 23 | |
---|
| 24 | @param name the object name. |
---|
| 25 | @param obj the object to introspect. |
---|
| 26 | @param indent the indentation (optional, defaults to ""). |
---|
| 27 | @param levels the introspection nesting level (defaults to 1). |
---|
| 28 | @returns a plain text analysis of the object. |
---|
| 29 | */ |
---|
| 30 | introspect : function (name, obj, indent, levels) { |
---|
| 31 | indent = indent || ""; |
---|
| 32 | if (this.typeOf(levels) !== "number") levels = 1; |
---|
| 33 | var objType = this.typeOf(obj); |
---|
| 34 | var result = [indent, name, " ", objType, " :"].join(''); |
---|
| 35 | if (objType === "object") { |
---|
| 36 | if (levels > 0) { |
---|
| 37 | indent = [indent, " "].join(''); |
---|
| 38 | for (prop in obj) { |
---|
| 39 | var prop = this.introspect(prop, obj[prop], indent, levels - 1); |
---|
| 40 | result = [result, "\n", prop].join(''); |
---|
| 41 | } |
---|
| 42 | return result; |
---|
| 43 | } |
---|
| 44 | else { |
---|
| 45 | return [result, " ..."].join(''); |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | else if (objType === "null") { |
---|
| 49 | return [result, " null"].join(''); |
---|
| 50 | } |
---|
| 51 | return [result, " ", obj].join(''); |
---|
| 52 | }, |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | Checks the type of a given object. |
---|
| 56 | |
---|
| 57 | @param obj the object to check. |
---|
| 58 | @returns one of; "boolean", "number", "string", "object", |
---|
| 59 | "function", or "null". |
---|
| 60 | */ |
---|
| 61 | |
---|
| 62 | typeOf : function (obj) { |
---|
| 63 | type = typeof obj; |
---|
| 64 | return type === "object" && !obj ? "null" : type; |
---|
| 65 | }, |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | Checks if a property of a specified object has the given type. |
---|
| 69 | |
---|
| 70 | @param obj the object to check. |
---|
| 71 | @param name the property name. |
---|
| 72 | @param type the property type (optional, default is "function"). |
---|
| 73 | @returns true if the property exists and has the specified type, |
---|
| 74 | otherwise false. |
---|
| 75 | */ |
---|
| 76 | |
---|
| 77 | exists: function (obj, name, type) { |
---|
| 78 | type = type || "function"; |
---|
| 79 | return (obj ? this.typeOf(obj[name]) : "null") === type; |
---|
| 80 | } |
---|
| 81 | }; |
---|