2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('dataschema-text', function(Y) {
11 * Provides a DataSchema implementation which can be used to work with delimited text data.
14 * @submodule dataschema-text
18 * Text subclass for the DataSchema Utility.
19 * @class DataSchema.Text
20 * @extends DataSchema.Base
28 /////////////////////////////////////////////////////////////////////////////
30 // DataSchema.Text static methods
32 /////////////////////////////////////////////////////////////////////////////
34 * Applies a given schema to given delimited text data.
37 * @param schema {Object} Schema to apply.
38 * @param data {Object} Text data.
39 * @return {Object} Schema-parsed data.
42 apply: function(schema, data) {
44 data_out = {results:[],meta:{}};
46 if(LANG.isString(data_in) && LANG.isString(schema.resultDelimiter)) {
48 data_out = SchemaText._parseResults.call(this, schema, data_in, data_out);
51 data_out.error = new Error("Text schema parse failure");
58 * Schema-parsed list of results from full data
60 * @method _parseResults
61 * @param schema {Array} Schema to parse against.
62 * @param text_in {String} Text to parse.
63 * @param data_out {Object} In-progress parsed data to update.
64 * @return {Object} Parsed data object.
68 _parseResults: function(schema, text_in, data_out) {
69 var resultDelim = schema.resultDelimiter,
71 results_in, fields_in, result, item, fields, field, key, value, i, j,
73 // Delete final delimiter at end of string if there
74 tmpLength = text_in.length-resultDelim.length;
75 if(text_in.substr(tmpLength) == resultDelim) {
76 text_in = text_in.substr(0, tmpLength);
80 results_in = text_in.split(schema.resultDelimiter);
82 for(i=results_in.length-1; i>-1; i--) {
86 if(LANG.isString(schema.fieldDelimiter)) {
87 fields_in = item.split(schema.fieldDelimiter);
89 if(LANG.isArray(schema.resultFields)) {
90 fields = schema.resultFields;
91 for(j=fields.length-1; j>-1; j--) {
93 key = (!LANG.isUndefined(field.key)) ? field.key : field;
94 value = (!LANG.isUndefined(fields_in[key])) ? fields_in[key] : fields_in[j];
95 result[key] = Y.DataSchema.Base.parse.call(this, value, field);
106 data_out.results = results;
112 Y.DataSchema.Text = Y.mix(SchemaText, Y.DataSchema.Base);
116 }, '3.3.0' ,{requires:['dataschema-base']});