~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/dataschema-array/dataschema-array.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* YUI 3.9.1 (build 5852) Copyright 2013 Yahoo! Inc. http://yuilibrary.com/license/ */
2
 
YUI.add('dataschema-array', function (Y, NAME) {
3
 
 
4
 
/**
5
 
 * Provides a DataSchema implementation which can be used to work with data
6
 
 * stored in arrays.
7
 
 *
8
 
 * @module dataschema
9
 
 * @submodule dataschema-array
10
 
 */
11
 
 
12
 
/**
13
 
Provides a DataSchema implementation which can be used to work with data
14
 
stored in arrays.
15
 
 
16
 
See the `apply` method below for usage.
17
 
 
18
 
@class DataSchema.Array
19
 
@extends DataSchema.Base
20
 
@static
21
 
**/
22
 
var LANG = Y.Lang,
23
 
 
24
 
    SchemaArray = {
25
 
 
26
 
        ////////////////////////////////////////////////////////////////////////
27
 
        //
28
 
        // DataSchema.Array static methods
29
 
        //
30
 
        ////////////////////////////////////////////////////////////////////////
31
 
 
32
 
        /**
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.
38
 
 
39
 
        The input array is expected to contain objects, arrays, or strings.
40
 
 
41
 
        If _schema_ is not specified or _schema.resultFields_ is not an array,
42
 
        `response.results` will be assigned the input array unchanged.
43
 
 
44
 
        When a _schema_ is specified, the following will occur:
45
 
 
46
 
        If the input array contains strings, they will be copied as-is into the
47
 
        `response.results` array.
48
 
 
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
52
 
        values.
53
 
 
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.
57
 
 
58
 
        _schema.resultFields_ field identifiers are objects with the following properties:
59
 
 
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
64
 
                return a value.
65
 
 
66
 
        If no value parsing is needed, you can use strings as identifiers
67
 
        instead of objects (see example below).
68
 
 
69
 
        @example
70
 
            // Process array of arrays
71
 
            var schema = { resultFields: [ 'fruit', 'color' ] },
72
 
                data = [
73
 
                    [ 'Banana', 'yellow' ],
74
 
                    [ 'Orange', 'orange' ],
75
 
                    [ 'Eggplant', 'purple' ]
76
 
                ];
77
 
 
78
 
            var response = Y.DataSchema.Array.apply(schema, data);
79
 
 
80
 
            // response.results[0] is { fruit: "Banana", color: "yellow" }
81
 
 
82
 
            
83
 
            // Process array of objects
84
 
            data = [
85
 
                { fruit: 'Banana', color: 'yellow', price: '1.96' },
86
 
                { fruit: 'Orange', color: 'orange', price: '2.04' },
87
 
                { fruit: 'Eggplant', color: 'purple', price: '4.31' }
88
 
            ];
89
 
 
90
 
            response = Y.DataSchema.Array.apply(schema, data);
91
 
 
92
 
            // response.results[0] is { fruit: "Banana", color: "yellow" }
93
 
 
94
 
 
95
 
            // Use parsers
96
 
            schema.resultFields = [
97
 
                {
98
 
                    key: 'fruit',
99
 
                    parser: function (val) { return val.toUpperCase(); }
100
 
                },
101
 
                {
102
 
                    key: 'price',
103
 
                    parser: 'number' // Uses Y.Parsers.number
104
 
                }
105
 
            ];
106
 
 
107
 
            response = Y.DataSchema.Array.apply(schema, data);
108
 
 
109
 
            // Note price was converted from a numeric string to a number
110
 
            // response.results[0] looks like { fruit: "BANANA", price: 1.96 }
111
 
         
112
 
        @method apply
113
 
        @param {Object} [schema] Schema to apply.  Supported configuration
114
 
            properties are:
115
 
          @param {Array} [schema.resultFields] Field identifiers to
116
 
              locate/assign values in the response records. See above for
117
 
              details.
118
 
        @param {Array} data Array data.
119
 
        @return {Object} An Object with properties `results` and `meta`
120
 
        @static
121
 
        **/
122
 
        apply: function(schema, data) {
123
 
            var data_in = data,
124
 
                data_out = {results:[],meta:{}};
125
 
 
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);
130
 
                }
131
 
                else {
132
 
                    data_out.results = data_in;
133
 
                }
134
 
            }
135
 
            else {
136
 
                data_out.error = new Error("Array schema parse failure");
137
 
            }
138
 
 
139
 
            return data_out;
140
 
        },
141
 
 
142
 
        /**
143
 
         * Schema-parsed list of results from full data
144
 
         *
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.
150
 
         * @static
151
 
         * @protected
152
 
         */
153
 
        _parseResults: function(fields, array_in, data_out) {
154
 
            var results = [],
155
 
                result, item, type, field, key, value, i, j;
156
 
 
157
 
            for(i=array_in.length-1; i>-1; i--) {
158
 
                result = {};
159
 
                item = array_in[i];
160
 
                type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
161
 
                if(type > 0) {
162
 
                    for(j=fields.length-1; j>-1; j--) {
163
 
                        field = fields[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);
167
 
                    }
168
 
                }
169
 
                else if(type === 0) {
170
 
                    result = item;
171
 
                }
172
 
                else {
173
 
                    //TODO: null or {}?
174
 
                    result = null;
175
 
                }
176
 
                results[i] = result;
177
 
            }
178
 
            data_out.results = results;
179
 
 
180
 
            return data_out;
181
 
        }
182
 
    };
183
 
 
184
 
Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base);
185
 
 
186
 
 
187
 
}, '3.9.1', {"requires": ["dataschema-base"]});