API Docs for: 3.8.0
Show:

File: dataschema/js/dataschema-array.js

  1. /**
  2.  * Provides a DataSchema implementation which can be used to work with data
  3.  * stored in arrays.
  4.  *
  5.  * @module dataschema
  6.  * @submodule dataschema-array
  7.  */

  8. /**
  9. Provides a DataSchema implementation which can be used to work with data
  10. stored in arrays.

  11. See the `apply` method below for usage.

  12. @class DataSchema.Array
  13. @extends DataSchema.Base
  14. @static
  15. **/
  16. var LANG = Y.Lang,

  17.     SchemaArray = {

  18.         ////////////////////////////////////////////////////////////////////////
  19.         //
  20.         // DataSchema.Array static methods
  21.         //
  22.         ////////////////////////////////////////////////////////////////////////

  23.         /**
  24.         Applies a schema to an array of data, returning a normalized object
  25.         with results in the `results` property. The `meta` property of the
  26.         response object is present for consistency, but is assigned an empty
  27.         object.  If the input data is absent or not an array, an `error`
  28.         property will be added.

  29.         The input array is expected to contain objects, arrays, or strings.

  30.         If _schema_ is not specified or _schema.resultFields_ is not an array,
  31.         `response.results` will be assigned the input array unchanged.

  32.         When a _schema_ is specified, the following will occur:

  33.         If the input array contains strings, they will be copied as-is into the
  34.         `response.results` array.

  35.         If the input array contains arrays, `response.results` will contain an
  36.         array of objects with key:value pairs assuming the fields in
  37.         _schema.resultFields_ are ordered in accordance with the data array
  38.         values.

  39.         If the input array contains objects, the identified
  40.         _schema.resultFields_ will be used to extract a value from those
  41.         objects for the output result.

  42.         _schema.resultFields_ field identifiers are objects with the following properties:

  43.           * `key`   : <strong>(required)</strong> The locator name (String)
  44.           * `parser`: A function or the name of a function on `Y.Parsers` used
  45.                 to convert the input value into a normalized type.  Parser
  46.                 functions are passed the value as input and are expected to
  47.                 return a value.

  48.         If no value parsing is needed, you can use strings as identifiers
  49.         instead of objects (see example below).

  50.         @example
  51.             // Process array of arrays
  52.             var schema = { resultFields: [ 'fruit', 'color' ] },
  53.                 data = [
  54.                     [ 'Banana', 'yellow' ],
  55.                     [ 'Orange', 'orange' ],
  56.                     [ 'Eggplant', 'purple' ]
  57.                 ];

  58.             var response = Y.DataSchema.Array.apply(schema, data);

  59.             // response.results[0] is { fruit: "Banana", color: "yellow" }

  60.            
  61.             // Process array of objects
  62.             data = [
  63.                 { fruit: 'Banana', color: 'yellow', price: '1.96' },
  64.                 { fruit: 'Orange', color: 'orange', price: '2.04' },
  65.                 { fruit: 'Eggplant', color: 'purple', price: '4.31' }
  66.             ];

  67.             response = Y.DataSchema.Array.apply(schema, data);

  68.             // response.results[0] is { fruit: "Banana", color: "yellow" }


  69.             // Use parsers
  70.             schema.resultFields = [
  71.                 {
  72.                     key: 'fruit',
  73.                     parser: function (val) { return val.toUpperCase(); }
  74.                 },
  75.                 {
  76.                     key: 'price',
  77.                     parser: 'number' // Uses Y.Parsers.number
  78.                 }
  79.             ];

  80.             response = Y.DataSchema.Array.apply(schema, data);

  81.             // Note price was converted from a numeric string to a number
  82.             // response.results[0] looks like { fruit: "BANANA", price: 1.96 }
  83.          
  84.         @method apply
  85.         @param {Object} [schema] Schema to apply.  Supported configuration
  86.             properties are:
  87.           @param {Array} [schema.resultFields] Field identifiers to
  88.               locate/assign values in the response records. See above for
  89.               details.
  90.         @param {Array} data Array data.
  91.         @return {Object} An Object with properties `results` and `meta`
  92.         @static
  93.         **/
  94.         apply: function(schema, data) {
  95.             var data_in = data,
  96.                 data_out = {results:[],meta:{}};

  97.             if(LANG.isArray(data_in)) {
  98.                 if(schema && LANG.isArray(schema.resultFields)) {
  99.                     // Parse results data
  100.                     data_out = SchemaArray._parseResults.call(this, schema.resultFields, data_in, data_out);
  101.                 }
  102.                 else {
  103.                     data_out.results = data_in;
  104.                     Y.log("Schema resultFields property not found: " + Y.dump(schema), "warn", "dataschema-array");
  105.                 }
  106.             }
  107.             else {
  108.                 Y.log("Array data could not be schema-parsed: " + Y.dump(data) + " " + Y.dump(data), "error", "dataschema-array");
  109.                 data_out.error = new Error("Array schema parse failure");
  110.             }

  111.             return data_out;
  112.         },

  113.         /**
  114.          * Schema-parsed list of results from full data
  115.          *
  116.          * @method _parseResults
  117.          * @param fields {Array} Schema to parse against.
  118.          * @param array_in {Array} Array to parse.
  119.          * @param data_out {Object} In-progress parsed data to update.
  120.          * @return {Object} Parsed data object.
  121.          * @static
  122.          * @protected
  123.          */
  124.         _parseResults: function(fields, array_in, data_out) {
  125.             var results = [],
  126.                 result, item, type, field, key, value, i, j;

  127.             for(i=array_in.length-1; i>-1; i--) {
  128.                 result = {};
  129.                 item = array_in[i];
  130.                 type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
  131.                 if(type > 0) {
  132.                     for(j=fields.length-1; j>-1; j--) {
  133.                         field = fields[j];
  134.                         key = (!LANG.isUndefined(field.key)) ? field.key : field;
  135.                         value = (!LANG.isUndefined(item[key])) ? item[key] : item[j];
  136.                         result[key] = Y.DataSchema.Base.parse.call(this, value, field);
  137.                     }
  138.                 }
  139.                 else if(type === 0) {
  140.                     result = item;
  141.                 }
  142.                 else {
  143.                     //TODO: null or {}?
  144.                     result = null;
  145.                     Y.log("Unexpected type while parsing array: " + Y.dump(item), "warn", "dataschema-array");
  146.                 }
  147.                 results[i] = result;
  148.             }
  149.             data_out.results = results;

  150.             return data_out;
  151.         }
  152.     };

  153. Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base);

  154.