2
YUI 3.10.3 (build 2fb5187)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('dataschema-array', function (Y, NAME) {
11
* Provides a DataSchema implementation which can be used to work with data
15
* @submodule dataschema-array
19
Provides a DataSchema implementation which can be used to work with data
22
See the `apply` method below for usage.
24
@class DataSchema.Array
25
@extends DataSchema.Base
32
////////////////////////////////////////////////////////////////////////
34
// DataSchema.Array static methods
36
////////////////////////////////////////////////////////////////////////
39
Applies a schema to an array of data, returning a normalized object
40
with results in the `results` property. The `meta` property of the
41
response object is present for consistency, but is assigned an empty
42
object. If the input data is absent or not an array, an `error`
43
property will be added.
45
The input array is expected to contain objects, arrays, or strings.
47
If _schema_ is not specified or _schema.resultFields_ is not an array,
48
`response.results` will be assigned the input array unchanged.
50
When a _schema_ is specified, the following will occur:
52
If the input array contains strings, they will be copied as-is into the
53
`response.results` array.
55
If the input array contains arrays, `response.results` will contain an
56
array of objects with key:value pairs assuming the fields in
57
_schema.resultFields_ are ordered in accordance with the data array
60
If the input array contains objects, the identified
61
_schema.resultFields_ will be used to extract a value from those
62
objects for the output result.
64
_schema.resultFields_ field identifiers are objects with the following properties:
66
* `key` : <strong>(required)</strong> The locator name (String)
67
* `parser`: A function or the name of a function on `Y.Parsers` used
68
to convert the input value into a normalized type. Parser
69
functions are passed the value as input and are expected to
72
If no value parsing is needed, you can use strings as identifiers
73
instead of objects (see example below).
76
// Process array of arrays
77
var schema = { resultFields: [ 'fruit', 'color' ] },
79
[ 'Banana', 'yellow' ],
80
[ 'Orange', 'orange' ],
81
[ 'Eggplant', 'purple' ]
84
var response = Y.DataSchema.Array.apply(schema, data);
86
// response.results[0] is { fruit: "Banana", color: "yellow" }
89
// Process array of objects
91
{ fruit: 'Banana', color: 'yellow', price: '1.96' },
92
{ fruit: 'Orange', color: 'orange', price: '2.04' },
93
{ fruit: 'Eggplant', color: 'purple', price: '4.31' }
96
response = Y.DataSchema.Array.apply(schema, data);
98
// response.results[0] is { fruit: "Banana", color: "yellow" }
102
schema.resultFields = [
105
parser: function (val) { return val.toUpperCase(); }
109
parser: 'number' // Uses Y.Parsers.number
113
response = Y.DataSchema.Array.apply(schema, data);
115
// Note price was converted from a numeric string to a number
116
// response.results[0] looks like { fruit: "BANANA", price: 1.96 }
119
@param {Object} [schema] Schema to apply. Supported configuration
121
@param {Array} [schema.resultFields] Field identifiers to
122
locate/assign values in the response records. See above for
124
@param {Array} data Array data.
125
@return {Object} An Object with properties `results` and `meta`
128
apply: function(schema, data) {
130
data_out = {results:[],meta:{}};
132
if(LANG.isArray(data_in)) {
133
if(schema && LANG.isArray(schema.resultFields)) {
134
// Parse results data
135
data_out = SchemaArray._parseResults.call(this, schema.resultFields, data_in, data_out);
138
data_out.results = data_in;
142
data_out.error = new Error("Array schema parse failure");
149
* Schema-parsed list of results from full data
151
* @method _parseResults
152
* @param fields {Array} Schema to parse against.
153
* @param array_in {Array} Array to parse.
154
* @param data_out {Object} In-progress parsed data to update.
155
* @return {Object} Parsed data object.
159
_parseResults: function(fields, array_in, data_out) {
161
result, item, type, field, key, value, i, j;
163
for(i=array_in.length-1; i>-1; i--) {
166
type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
168
for(j=fields.length-1; j>-1; j--) {
170
key = (!LANG.isUndefined(field.key)) ? field.key : field;
171
value = (!LANG.isUndefined(item[key])) ? item[key] : item[j];
172
result[key] = Y.DataSchema.Base.parse.call(this, value, field);
175
else if(type === 0) {
184
data_out.results = results;
190
Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base);
193
}, '3.10.3', {"requires": ["dataschema-base"]});