Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
YUI.add('dataschema-text', function (Y, NAME) {
2
 
3
/**
4
 * Provides a DataSchema implementation which can be used to work with
5
 * delimited text data.
6
 *
7
 * @module dataschema
8
 * @submodule dataschema-text
9
 */
10
 
11
/**
12
Provides a DataSchema implementation which can be used to work with
13
delimited text data.
14
 
15
See the `apply` method for usage.
16
 
17
@class DataSchema.Text
18
@extends DataSchema.Base
19
@static
20
**/
21
 
22
var Lang = Y.Lang,
23
    isString = Lang.isString,
24
    isUndef  = Lang.isUndefined,
25
 
26
    SchemaText = {
27
 
28
        ////////////////////////////////////////////////////////////////////////
29
        //
30
        // DataSchema.Text static methods
31
        //
32
        ////////////////////////////////////////////////////////////////////////
33
        /**
34
        Applies a schema to a string of delimited data, returning a normalized
35
        object with results in the `results` property. The `meta` property of
36
        the response object is present for consistency, but is assigned an
37
        empty object.  If the input data is absent or not a string, an `error`
38
        property will be added.
39
 
40
        Use _schema.resultDelimiter_ and _schema.fieldDelimiter_ to instruct
41
        `apply` how to split up the string into an array of data arrays for
42
        processing.
43
 
44
        Use _schema.resultFields_ to specify the keys in the generated result
45
        objects in `response.results`. The key:value pairs will be assigned
46
        in the order of the _schema.resultFields_ array, assuming the values
47
        in the data records are defined in the same order.
48
 
49
        _schema.resultFields_ field identifiers are objects with the following
50
        properties:
51
 
52
          * `key`   : <strong>(required)</strong> The property name you want
53
                the data value assigned to in the result object (String)
54
          * `parser`: A function or the name of a function on `Y.Parsers` used
55
                to convert the input value into a normalized type.  Parser
56
                functions are passed the value as input and are expected to
57
                return a value.
58
 
59
        If no value parsing is needed, you can use just the desired property
60
        name string as the field identifier instead of an object (see example
61
        below).
62
 
63
        @example
64
            // Process simple csv
65
            var schema = {
66
                    resultDelimiter: "\n",
67
                    fieldDelimiter: ",",
68
                    resultFields: [ 'fruit', 'color' ]
69
                },
70
                data = "Banana,yellow\nOrange,orange\nEggplant,purple";
71
 
72
            var response = Y.DataSchema.Text.apply(schema, data);
73
 
74
            // response.results[0] is { fruit: "Banana", color: "yellow" }
75
 
76
 
77
            // Use parsers
78
            schema.resultFields = [
79
                {
80
                    key: 'fruit',
81
                    parser: function (val) { return val.toUpperCase(); }
82
                },
83
                'color' // mix and match objects and strings
84
            ];
85
 
86
            response = Y.DataSchema.Text.apply(schema, data);
87
 
88
            // response.results[0] is { fruit: "BANANA", color: "yellow" }
89
 
90
        @method apply
91
        @param {Object} schema Schema to apply.  Supported configuration
92
            properties are:
93
          @param {String} schema.resultDelimiter Character or character
94
              sequence that marks the end of one record and the start of
95
              another.
96
          @param {String} [schema.fieldDelimiter] Character or character
97
              sequence that marks the end of a field and the start of
98
              another within the same record.
99
          @param {Array} [schema.resultFields] Field identifiers to
100
              assign values in the response records. See above for details.
101
        @param {String} data Text data.
102
        @return {Object} An Object with properties `results` and `meta`
103
        @static
104
        **/
105
        apply: function(schema, data) {
106
            var data_in = data,
107
                data_out = { results: [], meta: {} };
108
 
109
            if (isString(data) && schema && isString(schema.resultDelimiter)) {
110
                // Parse results data
111
                data_out = SchemaText._parseResults.call(this, schema, data_in, data_out);
112
            } else {
113
                data_out.error = new Error("Text schema parse failure");
114
            }
115
 
116
            return data_out;
117
        },
118
 
119
        /**
120
         * Schema-parsed list of results from full data
121
         *
122
         * @method _parseResults
123
         * @param schema {Array} Schema to parse against.
124
         * @param text_in {String} Text to parse.
125
         * @param data_out {Object} In-progress parsed data to update.
126
         * @return {Object} Parsed data object.
127
         * @static
128
         * @protected
129
         */
130
        _parseResults: function(schema, text_in, data_out) {
131
            var resultDelim = schema.resultDelimiter,
132
                fieldDelim  = isString(schema.fieldDelimiter) &&
133
                                schema.fieldDelimiter,
134
                fields      = schema.resultFields || [],
135
                results     = [],
136
                parse       = Y.DataSchema.Base.parse,
137
                results_in, fields_in, result, item,
138
                field, key, value, i, j;
139
 
140
            // Delete final delimiter at end of string if there
141
            if (text_in.slice(-resultDelim.length) === resultDelim) {
142
                text_in = text_in.slice(0, -resultDelim.length);
143
            }
144
 
145
            // Split into results
146
            results_in = text_in.split(schema.resultDelimiter);
147
 
148
            if (fieldDelim) {
149
                for (i = results_in.length - 1; i >= 0; --i) {
150
                    result = {};
151
                    item = results_in[i];
152
 
153
                    fields_in = item.split(schema.fieldDelimiter);
154
 
155
                    for (j = fields.length - 1; j >= 0; --j) {
156
                        field = fields[j];
157
                        key = (!isUndef(field.key)) ? field.key : field;
158
                        // FIXME: unless the key is an array index, this test
159
                        // for fields_in[key] is useless.
160
                        value = (!isUndef(fields_in[key])) ?
161
                                    fields_in[key] :
162
                                    fields_in[j];
163
 
164
                        result[key] = parse.call(this, value, field);
165
                    }
166
 
167
                    results[i] = result;
168
                }
169
            } else {
170
                results = results_in;
171
            }
172
 
173
            data_out.results = results;
174
 
175
            return data_out;
176
        }
177
    };
178
 
179
Y.DataSchema.Text = Y.mix(SchemaText, Y.DataSchema.Base);
180
 
181
 
182
}, '3.18.1', {"requires": ["dataschema-base"]});