1
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
YUI.add('dataschema-array', function (Y, NAME) {
5
* Provides a DataSchema implementation which can be used to work with data
9
* @submodule dataschema-array
13
Provides a DataSchema implementation which can be used to work with data
16
See the `apply` method below for usage.
18
@class DataSchema.Array
19
@extends DataSchema.Base
26
////////////////////////////////////////////////////////////////////////
28
// DataSchema.Array static methods
30
////////////////////////////////////////////////////////////////////////
33
Applies a schema to an array of data, returning a normalized object
34
with results in the `results` property. The `meta` property of the
35
response object is present for consistency, but is assigned an empty
36
object. If the input data is absent or not an array, an `error`
37
property will be added.
39
The input array is expected to contain objects, arrays, or strings.
41
If _schema_ is not specified or _schema.resultFields_ is not an array,
42
`response.results` will be assigned the input array unchanged.
44
When a _schema_ is specified, the following will occur:
46
If the input array contains strings, they will be copied as-is into the
47
`response.results` array.
49
If the input array contains arrays, `response.results` will contain an
50
array of objects with key:value pairs assuming the fields in
51
_schema.resultFields_ are ordered in accordance with the data array
54
If the input array contains objects, the identified
55
_schema.resultFields_ will be used to extract a value from those
56
objects for the output result.
58
_schema.resultFields_ field identifiers are objects with the following properties:
60
* `key` : <strong>(required)</strong> The locator name (String)
61
* `parser`: A function or the name of a function on `Y.Parsers` used
62
to convert the input value into a normalized type. Parser
63
functions are passed the value as input and are expected to
66
If no value parsing is needed, you can use strings as identifiers
67
instead of objects (see example below).
70
// Process array of arrays
71
var schema = { resultFields: [ 'fruit', 'color' ] },
73
[ 'Banana', 'yellow' ],
74
[ 'Orange', 'orange' ],
75
[ 'Eggplant', 'purple' ]
78
var response = Y.DataSchema.Array.apply(schema, data);
80
// response.results[0] is { fruit: "Banana", color: "yellow" }
83
// Process array of objects
85
{ fruit: 'Banana', color: 'yellow', price: '1.96' },
86
{ fruit: 'Orange', color: 'orange', price: '2.04' },
87
{ fruit: 'Eggplant', color: 'purple', price: '4.31' }
90
response = Y.DataSchema.Array.apply(schema, data);
92
// response.results[0] is { fruit: "Banana", color: "yellow" }
96
schema.resultFields = [
99
parser: function (val) { return val.toUpperCase(); }
103
parser: 'number' // Uses Y.Parsers.number
107
response = Y.DataSchema.Array.apply(schema, data);
109
// Note price was converted from a numeric string to a number
110
// response.results[0] looks like { fruit: "BANANA", price: 1.96 }
113
@param {Object} [schema] Schema to apply. Supported configuration
115
@param {Array} [schema.resultFields] Field identifiers to
116
locate/assign values in the response records. See above for
118
@param {Array} data Array data.
119
@return {Object} An Object with properties `results` and `meta`
122
apply: function(schema, data) {
124
data_out = {results:[],meta:{}};
126
if(LANG.isArray(data_in)) {
127
if(schema && LANG.isArray(schema.resultFields)) {
128
// Parse results data
129
data_out = SchemaArray._parseResults.call(this, schema.resultFields, data_in, data_out);
132
data_out.results = data_in;
136
data_out.error = new Error("Array schema parse failure");
143
* Schema-parsed list of results from full data
145
* @method _parseResults
146
* @param fields {Array} Schema to parse against.
147
* @param array_in {Array} Array to parse.
148
* @param data_out {Object} In-progress parsed data to update.
149
* @return {Object} Parsed data object.
153
_parseResults: function(fields, array_in, data_out) {
155
result, item, type, field, key, value, i, j;
157
for(i=array_in.length-1; i>-1; i--) {
160
type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
162
for(j=fields.length-1; j>-1; j--) {
164
key = (!LANG.isUndefined(field.key)) ? field.key : field;
165
value = (!LANG.isUndefined(item[key])) ? item[key] : item[j];
166
result[key] = Y.DataSchema.Base.parse.call(this, value, field);
169
else if(type === 0) {
178
data_out.results = results;
184
Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base);
187
}, '3.9.1', {"requires": ["dataschema-base"]});