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