JAVASCRIPT   57

dataschema-array.js

Guest on 22nd July 2021 02:16:57 AM

  1. /*
  2. YUI 3.4.1 (build 4118)
  3. Copyright  Yahoo! Inc. All rights reserved.
  4. Licensed under the BSD License.
  5. http://yuilibrary.com/license/
  6. */
  7. YUI.add('dataschema-array', function(Y) {
  8.  
  9. /**
  10.  * Provides a DataSchema implementation which can be used to work with data
  11.  * stored in arrays.
  12.  *
  13.  * @module dataschema
  14.  * @submodule dataschema-array
  15.  */
  16.  
  17. /**
  18. Provides a DataSchema implementation which can be used to work with data
  19. stored in arrays.
  20.  
  21. See the `apply` method below for usage.
  22.  
  23. @class DataSchema.Array
  24. @extends DataSchema.Base
  25. @static
  26. **/
  27. var LANG = Y.Lang,
  28.  
  29.     SchemaArray = {
  30.  
  31.         ////////////////////////////////////////////////////////////////////////
  32.         //
  33.         // DataSchema.Array static methods
  34.         //
  35.         ////////////////////////////////////////////////////////////////////////
  36.  
  37.         /**
  38.         Applies a schema to an array of data, returning a normalized object
  39.         with results in the `results` property. The `meta` property of the
  40.         response object is present for consistency, but is assigned an empty
  41.         object.  If the input data is absent or not an array, an `error`
  42.         property will be added.
  43.  
  44.         The input array is expected to contain objects, arrays, or strings.
  45.  
  46.         If _schema_ is not specified or _schema.resultFields_ is not an array,
  47.         `response.results` will be assigned the input array unchanged.
  48.  
  49.         When a _schema_ is specified, the following will occur:
  50.  
  51.         If the input array contains strings, they will be copied as-is into the
  52.         `response.results` array.
  53.  
  54.         If the input array contains arrays, `response.results` will contain an
  55.         array of objects with key:value pairs assuming the fields in
  56.         _schema.resultFields_ are ordered in accordance with the data array
  57.         values.
  58.  
  59.         If the input array contains objects, the identified
  60.         _schema.resultFields_ will be used to extract a value from those
  61.         objects for the output result.
  62.  
  63.         _schema.resultFields_ field identifiers are objects with the following properties:
  64.  
  65.           * `key`   : <strong>(required)</strong> The locator name (String)
  66.           * `parser`: A function or the name of a function on `Y.Parsers` used
  67.                 to convert the input value into a normalized type.  Parser
  68.                 functions are passed the value as input and are expected to
  69.                 return a value.
  70.  
  71.         If no value parsing is needed, you can use strings as identifiers
  72.         instead of objects (see example below).
  73.  
  74.         @example
  75.             // Process array of arrays
  76.             var schema = { resultFields: [ 'fruit', 'color' ] },
  77.                 data = [
  78.                     [ 'Banana', 'yellow' ],
  79.                     [ 'Orange', 'orange' ],
  80.                     [ 'Eggplant', 'purple' ]
  81.                 ];
  82.  
  83.             var response = Y.DataSchema.Array.apply(schema, data);
  84.  
  85.             // response.results[0] is { fruit: "Banana", color: "yellow" }
  86.  
  87.            
  88.             // Process array of objects
  89.             data = [
  90.                 { fruit: 'Banana', color: 'yellow', price: '1.96' },
  91.                 { fruit: 'Orange', color: 'orange', price: '2.04' },
  92.                 { fruit: 'Eggplant', color: 'purple', price: '4.31' }
  93.             ];
  94.  
  95.             response = Y.DataSchema.Array.apply(schema, data);
  96.  
  97.             // response.results[0] is { fruit: "Banana", color: "yellow" }
  98.  
  99.  
  100.             // Use parsers
  101.             schema.resultFields = [
  102.                 {
  103.                     key: 'fruit',
  104.                     parser: function (val) { return val.toUpperCase(); }
  105.                 },
  106.                 {
  107.                     key: 'price',
  108.                     parser: 'number' // Uses Y.Parsers.number
  109.                 }
  110.             ];
  111.  
  112.             response = Y.DataSchema.Array.apply(schema, data);
  113.  
  114.             // Note price was converted from a numeric string to a number
  115.             // response.results[0] looks like { fruit: "BANANA", price: 1.96 }
  116.          
  117.         @method apply
  118.         @param {Object} [schema] Schema to apply.  Supported configuration
  119.             properties are:
  120.           @param {Array} [schema.resultFields] Field identifiers to
  121.               locate/assign values in the response records. See above for
  122.               details.
  123.         @param {Array} data Array data.
  124.         @return {Object} An Object with properties `results` and `meta`
  125.         @static
  126.         **/
  127.         apply: function(schema, data) {
  128.             var data_in = data,
  129.                 data_out = {results:[],meta:{}};
  130.  
  131.             if(LANG.isArray(data_in)) {
  132.                 if(schema && LANG.isArray(schema.resultFields)) {
  133.                     // Parse results data
  134.                     data_out = SchemaArray._parseResults.call(this, schema.resultFields, data_in, data_out);
  135.                 }
  136.                 else {
  137.                     data_out.results = data_in;
  138.                 }
  139.             }
  140.             else {
  141.                 data_out.error = new Error("Array schema parse failure");
  142.             }
  143.  
  144.             return data_out;
  145.         },
  146.  
  147.         /**
  148.          * Schema-parsed list of results from full data
  149.          *
  150.          * @method _parseResults
  151.          * @param fields {Array} Schema to parse against.
  152.          * @param array_in {Array} Array to parse.
  153.          * @param data_out {Object} In-progress parsed data to update.
  154.          * @return {Object} Parsed data object.
  155.          * @static
  156.          * @protected
  157.          */
  158.         _parseResults: function(fields, array_in, data_out) {
  159.             var results = [],
  160.                 result, item, type, field, key, value, i, j;
  161.  
  162.             for(i=array_in.length-1; i>-1; i--) {
  163.                 result = {};
  164.                 item = array_in[i];
  165.                 type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
  166.                 if(type > 0) {
  167.                     for(j=fields.length-1; j>-1; j--) {
  168.                         field = fields[j];
  169.                         key = (!LANG.isUndefined(field.key)) ? field.key : field;
  170.                         value = (!LANG.isUndefined(item[key])) ? item[key] : item[j];
  171.                         result[key] = Y.DataSchema.Base.parse.call(this, value, field);
  172.                     }
  173.                 }
  174.                 else if(type === 0) {
  175.                     result = item;
  176.                 }
  177.                 else {
  178.                     //TODO: null or {}?
  179.                     result = null;
  180.                 }
  181.                 results[i] = result;
  182.             }
  183.             data_out.results = results;
  184.  
  185.             return data_out;
  186.         }
  187.     };
  188.  
  189. Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base);
  190.  
  191.  
  192. }, '3.4.1' ,{requires:['dataschema-base']});

Raw Paste


Login or Register to edit or fork this paste. It's free.