dataschema-text.js revision 1fad80225c335240e31e0350a65463a0bd7a9b1a
16018N/AYUI.add('dataschema-text', function(Y) {
16018N/A
16018N/A/**
16018N/A * Provides a DataSchema implementation which can be used to work with delimited text data.
16018N/A *
16018N/A * @module dataschema
16018N/A * @submodule dataschema-text
16018N/A */
16018N/A
16018N/A/**
16018N/A * Text subclass for the DataSchema Utility.
16018N/A * @class DataSchema.Text
16018N/A * @extends DataSchema.Base
16018N/A * @static
16018N/A */
16018N/A
16018N/Avar LANG = Y.Lang,
16018N/A
16018N/A SchemaText = {
16018N/A
16018N/A /////////////////////////////////////////////////////////////////////////////
16018N/A //
16018N/A // DataSchema.Text static methods
16018N/A //
16018N/A /////////////////////////////////////////////////////////////////////////////
16018N/A /**
16018N/A * Applies a given schema to given delimited text data.
16018N/A *
16018N/A * @method apply
16018N/A * @param schema {Object} Schema to apply.
16018N/A * @param data {Object} Text data.
16018N/A * @return {Object} Schema-parsed data.
16018N/A * @static
16018N/A */
16018N/A apply: function(schema, data) {
16018N/A var data_in = data,
16018N/A data_out = {results:[],meta:{}};
16018N/A
16018N/A if(LANG.isString(data_in) && LANG.isString(schema.resultDelimiter)) {
16018N/A // Parse results data
16018N/A data_out = SchemaText._parseResults.call(this, schema, data_in, data_out);
16018N/A }
16018N/A else {
16018N/A data_out.error = new Error("Text schema parse failure");
16018N/A }
16018N/A
16018N/A return data_out;
16018N/A },
16018N/A
16018N/A /**
16018N/A * Schema-parsed list of results from full data
16018N/A *
16018N/A * @method _parseResults
16018N/A * @param schema {Array} Schema to parse against.
16018N/A * @param text_in {String} Text to parse.
16018N/A * @param data_out {Object} In-progress parsed data to update.
16018N/A * @return {Object} Parsed data object.
16018N/A * @static
16018N/A * @protected
16018N/A */
16018N/A _parseResults: function(schema, text_in, data_out) {
16018N/A var resultDelim = schema.resultDelimiter,
16018N/A results = [],
16018N/A results_in, fields_in, result, item, fields, field, key, value, i, j,
16018N/A
16018N/A // Delete final delimiter at end of string if there
16018N/A tmpLength = text_in.length-resultDelim.length;
16018N/A if(text_in.substr(tmpLength) == resultDelim) {
16018N/A text_in = text_in.substr(0, tmpLength);
16018N/A }
16018N/A
16018N/A // Split into results
16018N/A results_in = text_in.split(schema.resultDelimiter);
16018N/A
16018N/A for(i=results_in.length-1; i>-1; i--) {
16018N/A result = {};
16018N/A item = results_in[i];
16018N/A
16018N/A if(LANG.isString(schema.fieldDelimiter)) {
16018N/A fields_in = item.split(schema.fieldDelimiter);
16018N/A
16018N/A if(LANG.isArray(schema.resultFields)) {
16018N/A fields = schema.resultFields;
16018N/A for(j=fields.length-1; j>-1; j--) {
16018N/A field = fields[j];
16018N/A key = (!LANG.isUndefined(field.key)) ? field.key : field;
16018N/A value = (!LANG.isUndefined(fields_in[key])) ? fields_in[key] : fields_in[j];
16018N/A result[key] = Y.DataSchema.Base.parse.call(this, value, field);
16018N/A }
16018N/A }
16018N/A
16018N/A }
16018N/A else {
16018N/A result = item;
16018N/A }
results[i] = result;
}
data_out.results = results;
return data_out;
}
};
Y.DataSchema.Text = Y.mix(SchemaText, Y.DataSchema.Base);
}, '@VERSION@' ,{requires:['dataschema-base']});