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-array', function(Y) {
11 * Provides a DataSchema implementation which can be used to work with data stored in arrays.
14 * @submodule dataschema-array
18 * Array subclass for the DataSchema Utility.
19 * @class DataSchema.Array
20 * @extends DataSchema.Base
27 /////////////////////////////////////////////////////////////////////////////
29 // DataSchema.Array static methods
31 /////////////////////////////////////////////////////////////////////////////
33 * Applies a given schema to given Array data.
36 * @param schema {Object} Schema to apply.
37 * @param data {Object} Array data.
38 * @return {Object} Schema-parsed data.
41 apply: function(schema, data) {
43 data_out = {results:[],meta:{}};
45 if(LANG.isArray(data_in)) {
46 if(LANG.isArray(schema.resultFields)) {
48 data_out = SchemaArray._parseResults.call(this, schema.resultFields, data_in, data_out);
51 data_out.results = data_in;
55 data_out.error = new Error("Array schema parse failure");
62 * Schema-parsed list of results from full data
64 * @method _parseResults
65 * @param fields {Array} Schema to parse against.
66 * @param array_in {Array} Array to parse.
67 * @param data_out {Object} In-progress parsed data to update.
68 * @return {Object} Parsed data object.
72 _parseResults: function(fields, array_in, data_out) {
74 result, item, type, field, key, value, i, j;
76 for(i=array_in.length-1; i>-1; i--) {
79 type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
81 for(j=fields.length-1; j>-1; j--) {
83 key = (!LANG.isUndefined(field.key)) ? field.key : field;
84 value = (!LANG.isUndefined(item[key])) ? item[key] : item[j];
85 result[key] = Y.DataSchema.Base.parse.call(this, value, field);
97 data_out.results = results;
103 Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base);
107 }, '3.3.0' ,{requires:['dataschema-base']});