3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('dataschema-array', function(Y) {
10
* Provides a DataSchema implementation which can be used to work with data
14
* @submodule dataschema-array
18
Provides a DataSchema implementation which can be used to work with data
21
See the `apply` method below for usage.
23
@class DataSchema.Array
24
@extends DataSchema.Base
31
////////////////////////////////////////////////////////////////////////
33
// DataSchema.Array static methods
35
////////////////////////////////////////////////////////////////////////
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.
44
The input array is expected to contain objects, arrays, or strings.
46
If _schema_ is not specified or _schema.resultFields_ is not an array,
47
`response.results` will be assigned the input array unchanged.
49
When a _schema_ is specified, the following will occur:
51
If the input array contains strings, they will be copied as-is into the
52
`response.results` array.
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
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.
63
_schema.resultFields_ field identifiers are objects with the following properties:
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
71
If no value parsing is needed, you can use strings as identifiers
72
instead of objects (see example below).
75
// Process array of arrays
76
var schema = { resultFields: [ 'fruit', 'color' ] },
78
[ 'Banana', 'yellow' ],
79
[ 'Orange', 'orange' ],
80
[ 'Eggplant', 'purple' ]
83
var response = Y.DataSchema.Array.apply(schema, data);
85
// response.results[0] is { fruit: "Banana", color: "yellow" }
88
// Process array of objects
90
{ fruit: 'Banana', color: 'yellow', price: '1.96' },
91
{ fruit: 'Orange', color: 'orange', price: '2.04' },
92
{ fruit: 'Eggplant', color: 'purple', price: '4.31' }
95
response = Y.DataSchema.Array.apply(schema, data);
97
// response.results[0] is { fruit: "Banana", color: "yellow" }
101
schema.resultFields = [
104
parser: function (val) { return val.toUpperCase(); }
108
parser: 'number' // Uses Y.Parsers.number
112
response = Y.DataSchema.Array.apply(schema, data);
114
// Note price was converted from a numeric string to a number
115
// response.results[0] looks like { fruit: "BANANA", price: 1.96 }
118
@param {Object} [schema] Schema to apply. Supported configuration
120
@param {Array} [schema.resultFields] Field identifiers to
121
locate/assign values in the response records. See above for
123
@param {Array} data Array data.
124
@return {Object} An Object with properties `results` and `meta`
127
apply: function(schema, data) {
129
data_out = {results:[],meta:{}};
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);
137
data_out.results = data_in;
141
data_out.error = new Error("Array schema parse failure");
148
* Schema-parsed list of results from full data
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.
158
_parseResults: function(fields, array_in, data_out) {
160
result, item, type, field, key, value, i, j;
162
for(i=array_in.length-1; i>-1; i--) {
165
type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
167
for(j=fields.length-1; j>-1; 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);
174
else if(type === 0) {
183
data_out.results = results;
189
Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base);
192
}, '3.4.1' ,{requires:['dataschema-base']});