~ubuntu-branches/ubuntu/precise/whoopsie-daisy/precise-updates

« back to all changes in this revision

Viewing changes to backend/stats/static/js/yui/3.4.1/build/dataschema-json/dataschema-json-debug.js

  • Committer: Package Import Robot
  • Author(s): Evan Dandrea
  • Date: 2012-04-18 13:04:36 UTC
  • Revision ID: package-import@ubuntu.com-20120418130436-vmt93p8fds516lws
Tags: 0.1.32
Fix failing tests on powerpc and ARM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
YUI 3.4.1 (build 4118)
3
 
Copyright 2011 Yahoo! Inc. All rights reserved.
4
 
Licensed under the BSD License.
5
 
http://yuilibrary.com/license/
6
 
*/
7
 
YUI.add('dataschema-json', function(Y) {
8
 
 
9
 
/**
10
 
Provides a DataSchema implementation which can be used to work with JSON data.
11
 
 
12
 
@module dataschema
13
 
@submodule dataschema-json
14
 
**/
15
 
 
16
 
/**
17
 
Provides a DataSchema implementation which can be used to work with JSON data.
18
 
 
19
 
See the `apply` method for usage.
20
 
 
21
 
@class DataSchema.JSON
22
 
@extends DataSchema.Base
23
 
@static
24
 
**/
25
 
var LANG = Y.Lang,
26
 
    isFunction = LANG.isFunction,
27
 
    isObject   = LANG.isObject,
28
 
    isArray    = LANG.isArray,
29
 
    // TODO: I don't think the calls to Base.* need to be done via Base since
30
 
    // Base is mixed into SchemaJSON.  Investigate for later.
31
 
    Base       = Y.DataSchema.Base,
32
 
 
33
 
    SchemaJSON;
34
 
    
35
 
SchemaJSON = {
36
 
 
37
 
/////////////////////////////////////////////////////////////////////////////
38
 
//
39
 
// DataSchema.JSON static methods
40
 
//
41
 
/////////////////////////////////////////////////////////////////////////////
42
 
    /**
43
 
     * Utility function converts JSON locator strings into walkable paths
44
 
     *
45
 
     * @method getPath
46
 
     * @param locator {String} JSON value locator.
47
 
     * @return {String[]} Walkable path to data value.
48
 
     * @static
49
 
     */
50
 
    getPath: function(locator) {
51
 
        var path = null,
52
 
            keys = [],
53
 
            i = 0;
54
 
 
55
 
        if (locator) {
56
 
            // Strip the ["string keys"] and [1] array indexes
57
 
            // TODO: the first two steps can probably be reduced to one with
58
 
            // /\[\s*(['"])?(.*?)\1\s*\]/g, but the array indices would be
59
 
            // stored as strings.  This is not likely an issue.
60
 
            locator = locator.
61
 
                replace(/\[\s*(['"])(.*?)\1\s*\]/g,
62
 
                function (x,$1,$2) {keys[i]=$2;return '.@'+(i++);}).
63
 
                replace(/\[(\d+)\]/g,
64
 
                function (x,$1) {keys[i]=parseInt($1,10)|0;return '.@'+(i++);}).
65
 
                replace(/^\./,''); // remove leading dot
66
 
 
67
 
            // Validate against problematic characters.
68
 
            // commented out because the path isn't sent to eval, so it
69
 
            // should be safe. I'm not sure what makes a locator invalid.
70
 
            //if (!/[^\w\.\$@]/.test(locator)) {
71
 
            path = locator.split('.');
72
 
            for (i=path.length-1; i >= 0; --i) {
73
 
                if (path[i].charAt(0) === '@') {
74
 
                    path[i] = keys[parseInt(path[i].substr(1),10)];
75
 
                }
76
 
            }
77
 
            /*}
78
 
            else {
79
 
                Y.log("Invalid locator: " + locator, "error", "dataschema-json");
80
 
            }
81
 
            */
82
 
        }
83
 
        return path;
84
 
    },
85
 
 
86
 
    /**
87
 
     * Utility function to walk a path and return the value located there.
88
 
     *
89
 
     * @method getLocationValue
90
 
     * @param path {String[]} Locator path.
91
 
     * @param data {String} Data to traverse.
92
 
     * @return {Object} Data value at location.
93
 
     * @static
94
 
     */
95
 
    getLocationValue: function (path, data) {
96
 
        var i = 0,
97
 
            len = path.length;
98
 
        for (;i<len;i++) {
99
 
            if (isObject(data) && (path[i] in data)) {
100
 
                data = data[path[i]];
101
 
            } else {
102
 
                data = undefined;
103
 
                break;
104
 
            }
105
 
        }
106
 
        return data;
107
 
    },
108
 
 
109
 
    /**
110
 
    Applies a schema to an array of data located in a JSON structure, returning
111
 
    a normalized object with results in the `results` property. Additional
112
 
    information can be parsed out of the JSON for inclusion in the `meta`
113
 
    property of the response object.  If an error is encountered during
114
 
    processing, an `error` property will be added.
115
 
 
116
 
    The input _data_ is expected to be an object or array.  If it is a string,
117
 
    it will be passed through `Y.JSON.parse()`.
118
 
 
119
 
    If _data_ contains an array of data records to normalize, specify the
120
 
    _schema.resultListLocator_ as a dot separated path string just as you would
121
 
    reference it in JavaScript.  So if your _data_ object has a record array at
122
 
    _data.response.results_, use _schema.resultListLocator_ =
123
 
    "response.results". Bracket notation can also be used for array indices or
124
 
    object properties (e.g. "response['results']");  This is called a "path
125
 
    locator"
126
 
 
127
 
    Field data in the result list is extracted with field identifiers in
128
 
    _schema.resultFields_.  Field identifiers are objects with the following
129
 
    properties:
130
 
 
131
 
      * `key`   : <strong>(required)</strong> The path locator (String)
132
 
      * `parser`: A function or the name of a function on `Y.Parsers` used
133
 
            to convert the input value into a normalized type.  Parser
134
 
            functions are passed the value as input and are expected to
135
 
            return a value.
136
 
 
137
 
    If no value parsing is needed, you can use path locators (strings) 
138
 
    instead of field identifiers (objects) -- see example below.
139
 
 
140
 
    If no processing of the result list array is needed, _schema.resultFields_
141
 
    can be omitted; the `response.results` will point directly to the array.
142
 
 
143
 
    If the result list contains arrays, `response.results` will contain an
144
 
    array of objects with key:value pairs assuming the fields in
145
 
    _schema.resultFields_ are ordered in accordance with the data array
146
 
    values.
147
 
 
148
 
    If the result list contains objects, the identified _schema.resultFields_
149
 
    will be used to extract a value from those objects for the output result.
150
 
 
151
 
    To extract additional information from the JSON, include an array of
152
 
    path locators in _schema.metaFields_.  The collected values will be
153
 
    stored in `response.meta`.
154
 
 
155
 
 
156
 
    @example
157
 
        // Process array of arrays
158
 
        var schema = {
159
 
                resultListLocator: 'produce.fruit',
160
 
                resultFields: [ 'name', 'color' ]
161
 
            },
162
 
            data = {
163
 
                produce: {
164
 
                    fruit: [
165
 
                        [ 'Banana', 'yellow' ],
166
 
                        [ 'Orange', 'orange' ],
167
 
                        [ 'Eggplant', 'purple' ]
168
 
                    ]
169
 
                }
170
 
            };
171
 
 
172
 
        var response = Y.DataSchema.JSON.apply(schema, data);
173
 
 
174
 
        // response.results[0] is { name: "Banana", color: "yellow" }
175
 
 
176
 
        
177
 
        // Process array of objects + some metadata
178
 
        schema.metaFields = [ 'lastInventory' ];
179
 
 
180
 
        data = {
181
 
            produce: {
182
 
                fruit: [
183
 
                    { name: 'Banana', color: 'yellow', price: '1.96' },
184
 
                    { name: 'Orange', color: 'orange', price: '2.04' },
185
 
                    { name: 'Eggplant', color: 'purple', price: '4.31' }
186
 
                ]
187
 
            },
188
 
            lastInventory: '2011-07-19'
189
 
        };
190
 
 
191
 
        response = Y.DataSchema.JSON.apply(schema, data);
192
 
 
193
 
        // response.results[0] is { name: "Banana", color: "yellow" }
194
 
        // response.meta.lastInventory is '2001-07-19'
195
 
 
196
 
 
197
 
        // Use parsers
198
 
        schema.resultFields = [
199
 
            {
200
 
                key: 'name',
201
 
                parser: function (val) { return val.toUpperCase(); }
202
 
            },
203
 
            {
204
 
                key: 'price',
205
 
                parser: 'number' // Uses Y.Parsers.number
206
 
            }
207
 
        ];
208
 
 
209
 
        response = Y.DataSchema.JSON.apply(schema, data);
210
 
 
211
 
        // Note price was converted from a numeric string to a number
212
 
        // response.results[0] looks like { fruit: "BANANA", price: 1.96 }
213
 
     
214
 
    @method apply
215
 
    @param {Object} [schema] Schema to apply.  Supported configuration
216
 
        properties are:
217
 
      @param {String} [schema.resultListLocator] Path locator for the
218
 
          location of the array of records to flatten into `response.results`
219
 
      @param {Array} [schema.resultFields] Field identifiers to
220
 
          locate/assign values in the response records. See above for
221
 
          details.
222
 
      @param {Array} [schema.metaFields] Path locators to extract extra
223
 
          non-record related information from the data object.
224
 
    @param {Object|Array|String} data JSON data or its string serialization.
225
 
    @return {Object} An Object with properties `results` and `meta`
226
 
    @static
227
 
    **/
228
 
    apply: function(schema, data) {
229
 
        var data_in = data,
230
 
            data_out = { results: [], meta: {} };
231
 
 
232
 
        // Convert incoming JSON strings
233
 
        if (!isObject(data)) {
234
 
            try {
235
 
                data_in = Y.JSON.parse(data);
236
 
            }
237
 
            catch(e) {
238
 
                data_out.error = e;
239
 
                return data_out;
240
 
            }
241
 
        }
242
 
 
243
 
        if (isObject(data_in) && schema) {
244
 
            // Parse results data
245
 
            data_out = SchemaJSON._parseResults.call(this, schema, data_in, data_out);
246
 
 
247
 
            // Parse meta data
248
 
            if (schema.metaFields !== undefined) {
249
 
                data_out = SchemaJSON._parseMeta(schema.metaFields, data_in, data_out);
250
 
            }
251
 
        }
252
 
        else {
253
 
            Y.log("JSON data could not be schema-parsed: " + Y.dump(data) + " " + Y.dump(data), "error", "dataschema-json");
254
 
            data_out.error = new Error("JSON schema parse failure");
255
 
        }
256
 
 
257
 
        return data_out;
258
 
    },
259
 
 
260
 
    /**
261
 
     * Schema-parsed list of results from full data
262
 
     *
263
 
     * @method _parseResults
264
 
     * @param schema {Object} Schema to parse against.
265
 
     * @param json_in {Object} JSON to parse.
266
 
     * @param data_out {Object} In-progress parsed data to update.
267
 
     * @return {Object} Parsed data object.
268
 
     * @static
269
 
     * @protected
270
 
     */
271
 
    _parseResults: function(schema, json_in, data_out) {
272
 
        var getPath  = SchemaJSON.getPath,
273
 
            getValue = SchemaJSON.getLocationValue,
274
 
            path     = getPath(schema.resultListLocator),
275
 
            results  = path ?
276
 
                        (getValue(path, json_in) ||
277
 
                         // Fall back to treat resultListLocator as a simple key
278
 
                            json_in[schema.resultListLocator]) :
279
 
                        // Or if no resultListLocator is supplied, use the input
280
 
                        json_in;
281
 
 
282
 
        if (isArray(results)) {
283
 
            // if no result fields are passed in, then just take
284
 
            // the results array whole-hog Sometimes you're getting
285
 
            // an array of strings, or want the whole object, so
286
 
            // resultFields don't make sense.
287
 
            if (isArray(schema.resultFields)) {
288
 
                data_out = SchemaJSON._getFieldValues.call(this, schema.resultFields, results, data_out);
289
 
            } else {
290
 
                data_out.results = results;
291
 
            }
292
 
        } else if (schema.resultListLocator) {
293
 
            data_out.results = [];
294
 
            data_out.error = new Error("JSON results retrieval failure");
295
 
            Y.log("JSON data could not be parsed: " + Y.dump(json_in), "error", "dataschema-json");
296
 
        }
297
 
 
298
 
        return data_out;
299
 
    },
300
 
 
301
 
    /**
302
 
     * Get field data values out of list of full results
303
 
     *
304
 
     * @method _getFieldValues
305
 
     * @param fields {Array} Fields to find.
306
 
     * @param array_in {Array} Results to parse.
307
 
     * @param data_out {Object} In-progress parsed data to update.
308
 
     * @return {Object} Parsed data object.
309
 
     * @static
310
 
     * @protected
311
 
     */
312
 
    _getFieldValues: function(fields, array_in, data_out) {
313
 
        var results = [],
314
 
            len = fields.length,
315
 
            i, j,
316
 
            field, key, locator, path, parser, val,
317
 
            simplePaths = [], complexPaths = [], fieldParsers = [],
318
 
            result, record;
319
 
 
320
 
        // First collect hashes of simple paths, complex paths, and parsers
321
 
        for (i=0; i<len; i++) {
322
 
            field = fields[i]; // A field can be a simple string or a hash
323
 
            key = field.key || field; // Find the key
324
 
            locator = field.locator || key; // Find the locator
325
 
 
326
 
            // Validate and store locators for later
327
 
            path = SchemaJSON.getPath(locator);
328
 
            if (path) {
329
 
                if (path.length === 1) {
330
 
                    simplePaths.push({
331
 
                        key : key,
332
 
                        path: path[0]
333
 
                    });
334
 
                } else {
335
 
                    complexPaths.push({
336
 
                        key    : key,
337
 
                        path   : path,
338
 
                        locator: locator
339
 
                    });
340
 
                }
341
 
            } else {
342
 
                Y.log("Invalid key syntax: " + key, "warn", "dataschema-json");
343
 
            }
344
 
 
345
 
            // Validate and store parsers for later
346
 
            //TODO: use Y.DataSchema.parse?
347
 
            parser = (isFunction(field.parser)) ?
348
 
                        field.parser :
349
 
                        Y.Parsers[field.parser + ''];
350
 
 
351
 
            if (parser) {
352
 
                fieldParsers.push({
353
 
                    key   : key,
354
 
                    parser: parser
355
 
                });
356
 
            }
357
 
        }
358
 
 
359
 
        // Traverse list of array_in, creating records of simple fields,
360
 
        // complex fields, and applying parsers as necessary
361
 
        for (i=array_in.length-1; i>=0; --i) {
362
 
            record = {};
363
 
            result = array_in[i];
364
 
            if(result) {
365
 
                // Cycle through complexLocators
366
 
                for (j=complexPaths.length - 1; j>=0; --j) {
367
 
                    path = complexPaths[j];
368
 
                    val = SchemaJSON.getLocationValue(path.path, result);
369
 
                    if (val === undefined) {
370
 
                        val = SchemaJSON.getLocationValue([path.locator], result);
371
 
                        // Fail over keys like "foo.bar" from nested parsing
372
 
                        // to single token parsing if a value is found in
373
 
                        // results["foo.bar"]
374
 
                        if (val !== undefined) {
375
 
                            simplePaths.push({
376
 
                                key:  path.key,
377
 
                                path: path.locator
378
 
                            });
379
 
                            // Don't try to process the path as complex
380
 
                            // for further results
381
 
                            complexPaths.splice(i,1);
382
 
                            continue;
383
 
                        }
384
 
                    }
385
 
 
386
 
                    record[path.key] = Base.parse.call(this,
387
 
                        (SchemaJSON.getLocationValue(path.path, result)), path);
388
 
                }
389
 
 
390
 
                // Cycle through simpleLocators
391
 
                for (j = simplePaths.length - 1; j >= 0; --j) {
392
 
                    path = simplePaths[j];
393
 
                    // Bug 1777850: The result might be an array instead of object
394
 
                    record[path.key] = Base.parse.call(this,
395
 
                            ((result[path.path] === undefined) ?
396
 
                            result[j] : result[path.path]), path);
397
 
                }
398
 
 
399
 
                // Cycle through fieldParsers
400
 
                for (j=fieldParsers.length-1; j>=0; --j) {
401
 
                    key = fieldParsers[j].key;
402
 
                    record[key] = fieldParsers[j].parser.call(this, record[key]);
403
 
                    // Safety net
404
 
                    if (record[key] === undefined) {
405
 
                        record[key] = null;
406
 
                    }
407
 
                }
408
 
                results[i] = record;
409
 
            }
410
 
        }
411
 
        data_out.results = results;
412
 
        return data_out;
413
 
    },
414
 
 
415
 
    /**
416
 
     * Parses results data according to schema
417
 
     *
418
 
     * @method _parseMeta
419
 
     * @param metaFields {Object} Metafields definitions.
420
 
     * @param json_in {Object} JSON to parse.
421
 
     * @param data_out {Object} In-progress parsed data to update.
422
 
     * @return {Object} Schema-parsed meta data.
423
 
     * @static
424
 
     * @protected
425
 
     */
426
 
    _parseMeta: function(metaFields, json_in, data_out) {
427
 
        if (isObject(metaFields)) {
428
 
            var key, path;
429
 
            for(key in metaFields) {
430
 
                if (metaFields.hasOwnProperty(key)) {
431
 
                    path = SchemaJSON.getPath(metaFields[key]);
432
 
                    if (path && json_in) {
433
 
                        data_out.meta[key] = SchemaJSON.getLocationValue(path, json_in);
434
 
                    }
435
 
                }
436
 
            }
437
 
        }
438
 
        else {
439
 
            data_out.error = new Error("JSON meta data retrieval failure");
440
 
        }
441
 
        return data_out;
442
 
    }
443
 
};
444
 
 
445
 
// TODO: Y.Object + mix() might be better here
446
 
Y.DataSchema.JSON = Y.mix(SchemaJSON, Base);
447
 
 
448
 
 
449
 
}, '3.4.1' ,{requires:['dataschema-base','json']});