|
0
|
1 |
/* |
|
|
2 |
Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
|
3 |
Code licensed under the BSD License: |
|
|
4 |
http://developer.yahoo.net/yui/license.txt |
|
|
5 |
version: 3.0.0b1 |
|
|
6 |
build: 1163 |
|
|
7 |
*/ |
|
|
8 |
YUI.add('dataschema-text', function(Y) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Provides a DataSchema implementation which can be used to work with delimited text data. |
|
|
12 |
* |
|
|
13 |
* @module dataschema |
|
|
14 |
* @submodule dataschema-text |
|
|
15 |
*/ |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Text subclass for the DataSchema Utility. |
|
|
19 |
* @class DataSchema.Text |
|
|
20 |
* @extends DataSchema.Base |
|
|
21 |
* @static |
|
|
22 |
*/ |
|
|
23 |
|
|
|
24 |
var LANG = Y.Lang, |
|
|
25 |
|
|
|
26 |
SchemaText = { |
|
|
27 |
|
|
|
28 |
///////////////////////////////////////////////////////////////////////////// |
|
|
29 |
// |
|
|
30 |
// DataSchema.Text static methods |
|
|
31 |
// |
|
|
32 |
///////////////////////////////////////////////////////////////////////////// |
|
|
33 |
/** |
|
|
34 |
* Applies a given schema to given delimited text data. |
|
|
35 |
* |
|
|
36 |
* @method apply |
|
|
37 |
* @param schema {Object} Schema to apply. |
|
|
38 |
* @param data {Object} Text data. |
|
|
39 |
* @return {Object} Schema-parsed data. |
|
|
40 |
* @static |
|
|
41 |
*/ |
|
|
42 |
apply: function(schema, data) { |
|
|
43 |
var data_in = data, |
|
|
44 |
data_out = {results:[],meta:{}}; |
|
|
45 |
|
|
|
46 |
if(LANG.isString(data_in) && LANG.isString(schema.resultDelimiter)) { |
|
|
47 |
// Parse results data |
|
|
48 |
data_out = SchemaText._parseResults(schema, data_in, data_out); |
|
|
49 |
} |
|
|
50 |
else { |
|
|
51 |
data_out.error = new Error("Text schema parse failure"); |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
return data_out; |
|
|
55 |
}, |
|
|
56 |
|
|
|
57 |
/** |
|
|
58 |
* Schema-parsed list of results from full data |
|
|
59 |
* |
|
|
60 |
* @method _parseResults |
|
|
61 |
* @param schema {Array} Schema to parse against. |
|
|
62 |
* @param text_in {String} Text to parse. |
|
|
63 |
* @param data_out {Object} In-progress parsed data to update. |
|
|
64 |
* @return {Object} Parsed data object. |
|
|
65 |
* @static |
|
|
66 |
* @protected |
|
|
67 |
*/ |
|
|
68 |
_parseResults: function(schema, text_in, data_out) { |
|
|
69 |
var resultDelim = schema.resultDelimiter, |
|
|
70 |
results = [], |
|
|
71 |
results_in, fields_in, result, item, fields, field, key, value, i, j, |
|
|
72 |
|
|
|
73 |
// Delete final delimiter at end of string if there |
|
|
74 |
tmpLength = text_in.length-resultDelim.length; |
|
|
75 |
if(text_in.substr(tmpLength) == resultDelim) { |
|
|
76 |
text_in = text_in.substr(0, tmpLength); |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
// Split into results |
|
|
80 |
results_in = text_in.split(schema.resultDelimiter); |
|
|
81 |
|
|
|
82 |
for(i=results_in.length-1; i>-1; i--) { |
|
|
83 |
result = {}; |
|
|
84 |
item = results_in[i]; |
|
|
85 |
|
|
|
86 |
if(LANG.isString(schema.fieldDelimiter)) { |
|
|
87 |
fields_in = item.split(schema.fieldDelimiter); |
|
|
88 |
|
|
|
89 |
if(LANG.isArray(schema.resultFields)) { |
|
|
90 |
fields = schema.resultFields; |
|
|
91 |
for(j=fields.length-1; j>-1; j--) { |
|
|
92 |
field = fields[j]; |
|
|
93 |
key = (!LANG.isUndefined(field.key)) ? field.key : field; |
|
|
94 |
value = (!LANG.isUndefined(fields_in[key])) ? fields_in[key] : fields_in[j]; |
|
|
95 |
result[key] = Y.DataSchema.Base.parse(value, field); |
|
|
96 |
} |
|
|
97 |
} |
|
|
98 |
|
|
|
99 |
} |
|
|
100 |
else { |
|
|
101 |
result = item; |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
results[i] = result; |
|
|
105 |
} |
|
|
106 |
data_out.results = results; |
|
|
107 |
|
|
|
108 |
return data_out; |
|
|
109 |
} |
|
|
110 |
}; |
|
|
111 |
|
|
|
112 |
Y.DataSchema.Text = Y.mix(SchemaText, Y.DataSchema.Base); |
|
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
|
116 |
}, '3.0.0b1' ,{requires:['dataschema-base']}); |