| Author: | Oliver Steele |
| Copyright: | Copyright 2006 Oliver Steele. All rights reserved. |
| License: | MIT License (Open Source) |
| Homepage: | http://osteele.com/sources/javascript/ |
| Docs: | http://osteele.com/sources/javascript/docs/readable |
| Download: | http://osteele.com/sources/javascript/readable.js |
| Created: | 2006-03-03 |
| Modified: | 2006-03-20 |
Readable.js file adds readable strings for JavaScript values, and a simple set of logging commands that use them.
A readable string is intended for use by developers, to facilitate command-line and logger-based debugging. Readable strings correspond to the literal representation of a value, except that:
For example, in JavaScript, [1, ’’, null ,[3 ,’4’]].toString() evaluates to "1,,,3,4". This is less than helpful for command-line debugging or logging. With the inclusion of this file, the string representation of this object is the same as its source representation, and similarly for {a: 1, b: 2} (which otherwise displays as [object Object]).
Loading readable.js has the following effects:
Read more or leave a comment here.
Returns a string representation of value.
Returns a string representation of object.
Options is a hash of:
By default, this file replaces object.toString(). and array.toString() with calls to toReadable(). To disable this replacement, define READABLE_TOSTRING to a non-false value before loading this file.
In principle, these replacements could break code. For example, code that depends on [‘one’,’two’,’three’].toString() evaluating to "one,two,three" for serialization or before presenting it to a user will no longer work. In practice, this was what was most convenient for me — it means that I can use the Rhino command line to print values readably, without having to wrap them in an extra function call. So that’s the default.
This file defines the logging functions info, warn, debug, and error. These are designed to work in the browser or in Rhino, and to call fvlogger if it has been loaded. (For this to work, readable.js has to load after fvlogger.)
The functions are defined in one of the following ways:
The advantages of calling info (and the other logging functions) instead of (in DHTML) alert or (in Rhino) print are:
Replace ReadableLogger.log(level, message) or ReadableLog.display(message) to customize this behavior.
Logging uses ReadableLogger.defaults to limit the maximum length and recursion level.
There’s no check for recursive objects. Setting the level option will at least keep the system from recursing infinitely. (level is set by default.)
Unicode characters aren’t quoted. This is simple laziness. JavaScript keywords that are used as Object property names aren’t quoted either. I haven’t decided whether this is a bug or a feature.
The logging functions intentionally use toString instead of toReadable for the arguments themselves. That is, a but not b is quoted in info([a], b). This is usually what you want, for uses such as info(key, ’=>’, value). When it’s not, you can explicitly apply toReadable to the value, e.g. info(value.toReadable()) or, when value might be undefined or null, info(Readable.toReadable(value)).
inline-console and fvlogger both provide user interfaces to log messages to a text area within an HTML page. Readable.js differs from these libraries in that it customizes the string display of objects to these text areas.
Simple logging for OpenLaszlo defines logging functions that are compatible with those defined by this file. This allows libraries that use these functions to be used in both OpenLaszlo programs and in DHTML.
JSON stringifies values for computer consumption. JSON:
Readable stringifies values for human consumption. Readable: