YUI 3: DataType [beta]
The DataType Utility provides type-conversion and string-formatting convenience methods for numbers, dates, and XML documents.
More Information
- Examples: DataType Utility in action.
- API Documentation: View the full API documentation for the DataType Utility.
- Download: DataType Utility as part of the full YUI Library.
- Free Hosting on our fast edge servers with combo-loading.
- License: BSD.
Getting Started
Include Dependencies
The easiest way to include the source files for DataType and its dependencies is to add the YUI seed file to your page, using the following script tag, and allow the YUI instance to download any additional files which may be required:
The YUI instance will automatically pull down DataType's source files and any missing dependencies when the datatype module is used. This helps you avoid having to manually manage the list of files needed on your page to support multiple components while also optimizing your initial page weight by loading files only when they are required.
If you do want to include file dependencies manually on your page, the YUI Dependency Configurator can be used to determine the list of files you need to include in order to use DataType.
The YUI Instance
Once you have the YUI seed file on your page (yui-min.js), you can
create a new YUI instance for your application to use and populate it
with the modules you need, specified as the first set of arguments to the
use method:
The last argument passed to use is a callback function. The callback function will be invoked as soon as the YUI instance is done downloading any required files missing from your page. Once those files are loaded, your local YUI instance will be supplemented with the classes which make up the datatype module and any modules it depends on. A reference to the populated YUI instance (Y) is passed back to your callback function. Within your callback, then, you can start writing your application code based on your own custom instance of YUI.
For more information on creating instances of YUI and the
use method, see the
YUI Global object documentation.
Using the DataType Utility
This section describes how to use the DataType Utility in further detail. It contains these subsections:
Dates
Formatting dates
DataType.Date.format() will output dates as strings formatted using strftime tokens.
Additionally, DataType.Date.Locale supports the registration of custom locale objects.
Parsing dates
DataType.Date.parse() accepts any value supported by the JavaScript Date() constructor and converts it to a Date object. Invalid values will return null.
Numbers
Formatting numbers
JavaScript Number values can be formatted with a variety options into string values using DataType.Number.format().
Parsing numbers
String values can be converted into Number objects with DataType.Number.parse().
XML
Formatting XML
DataType.XML.format() will accept an XML document and return its string representation. Note that browsers may slightly differ in the exact string that is returned.
Parsing XML
DataType.XML.parse() will accept a string representation of XML and return an XML document object. Note that browsers differ in their handling of invalid syntax but will in general return an XML document even under error conditions.
Y.Parsers shortcuts
The functions DataType.Date.parse() and DataType.Number.parse() are registered with the Parsers object for seamless integration with the DataSchema Utility. For dynamic type enforcing when data is being normalized against a schema, simply point to the appropriate function using the built-in shortcut in your schema definition. Parsing your data in this manner is essential if your numerical or date data comes over the wire as JSON, since all the values will be strings.
Support & Community
Forums & Blog
YUI 3 discussion forums are hosted on YUILibrary.com.
In addition, please visit the YUIBlog for updates and articles about the YUI Library written by the library's developers.
Filing Bugs & Feature Requests
The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.

