YUI 3.x Home -

YUI Library Examples: DataSchema Utility: DataSchema.Text

DataSchema Utility: DataSchema.Text

DataSource.Text normalizes delimited text data against a given schema into an object with known properties.
Data
notebooks, 100, spiral-bound
pencils, 300, #2 with erasers
pens, 500, blue ink
    
Schema
{
    resultDelimiter: "\n",
    fieldDelimiter: ",",
    resultFields: [{key:"product"}, {key:"quantity"}, {key:"detail"}]
}
    
Normalized data

In order to use DataSchema.Text, input data must be delimited text. Define as your schema's resultDelimiter property the string that separates each result, and define as your schema's fieldDelimiter property the string that separates each field of each result.

  1. YUI().use("dataschema-text", function(Y) {
  2. var data_in = "notebooks, 100, spiral-bound\npencils, 300, #2 with erasers\npens, 500, blue ink",
  3. schema = {
  4. resultDelimiter: "\n",
  5. fieldDelimiter: ",",
  6. // Or simply: ["product", "quantity", "detail"]
  7. resultFields: [{key:"product"}, {key:"quantity"}, {key:"detail"}]
  8. },
  9. data_out = Y.DataSchema.Text.apply(schema, data_in));
  10.  
  11. alert(data_out);
  12. });
YUI().use("dataschema-text", function(Y) {
    var data_in = "notebooks, 100, spiral-bound\npencils, 300, #2 with erasers\npens, 500, blue ink",
        schema = {
            resultDelimiter: "\n",
            fieldDelimiter: ",",
            // Or simply: ["product", "quantity", "detail"]
            resultFields: [{key:"product"}, {key:"quantity"}, {key:"detail"}]
        },
        data_out = Y.DataSchema.Text.apply(schema, data_in));
 
    alert(data_out);
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings