Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataschema-array.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('dataschema-array', function(Y) {
  9. /**
  10. * Provides a DataSchema implementation which can be used to work with data
  11. * stored in arrays.
  12. *
  13. * @module dataschema
  14. * @submodule dataschema-array
  15. */
  16. /**
  17. Provides a DataSchema implementation which can be used to work with data
  18. stored in arrays.
  19. See the `apply` method below for usage.
  20. @class DataSchema.Array
  21. @extends DataSchema.Base
  22. @static
  23. **/
  24. var LANG = Y.Lang,
  25. SchemaArray = {
  26. ////////////////////////////////////////////////////////////////////////
  27. //
  28. // DataSchema.Array static methods
  29. //
  30. ////////////////////////////////////////////////////////////////////////
  31. /**
  32. Applies a schema to an array of data, returning a normalized object
  33. with results in the `results` property. The `meta` property of the
  34. response object is present for consistency, but is assigned an empty
  35. object. If the input data is absent or not an array, an `error`
  36. property will be added.
  37. The input array is expected to contain objects, arrays, or strings.
  38. If _schema_ is not specified or _schema.resultFields_ is not an array,
  39. `response.results` will be assigned the input array unchanged.
  40. When a _schema_ is specified, the following will occur:
  41. If the input array contains strings, they will be copied as-is into the
  42. `response.results` array.
  43. If the input array contains arrays, `response.results` will contain an
  44. array of objects with key:value pairs assuming the fields in
  45. _schema.resultFields_ are ordered in accordance with the data array
  46. values.
  47. If the input array contains objects, the identified
  48. _schema.resultFields_ will be used to extract a value from those
  49. objects for the output result.
  50. _schema.resultFields_ field identifiers are objects with the following properties:
  51. * `key` : <strong>(required)</strong> The locator name (String)
  52. * `parser`: A function or the name of a function on `Y.Parsers` used
  53. to convert the input value into a normalized type. Parser
  54. functions are passed the value as input and are expected to
  55. return a value.
  56. If no value parsing is needed, you can use strings as identifiers
  57. instead of objects (see example below).
  58. @example
  59. // Process array of arrays
  60. var schema = { resultFields: [ 'fruit', 'color' ] },
  61. data = [
  62. [ 'Banana', 'yellow' ],
  63. [ 'Orange', 'orange' ],
  64. [ 'Eggplant', 'purple' ]
  65. ];
  66. var response = Y.DataSchema.Array.apply(schema, data);
  67. // response.results[0] is { fruit: "Banana", color: "yellow" }
  68. // Process array of objects
  69. data = [
  70. { fruit: 'Banana', color: 'yellow', price: '1.96' },
  71. { fruit: 'Orange', color: 'orange', price: '2.04' },
  72. { fruit: 'Eggplant', color: 'purple', price: '4.31' }
  73. ];
  74. response = Y.DataSchema.Array.apply(schema, data);
  75. // response.results[0] is { fruit: "Banana", color: "yellow" }
  76. // Use parsers
  77. schema.resultFields = [
  78. {
  79. key: 'fruit',
  80. parser: function (val) { return val.toUpperCase(); }
  81. },
  82. {
  83. key: 'price',
  84. parser: 'number' // Uses Y.Parsers.number
  85. }
  86. ];
  87. response = Y.DataSchema.Array.apply(schema, data);
  88. // Note price was converted from a numeric string to a number
  89. // response.results[0] looks like { fruit: "BANANA", price: 1.96 }
  90. @method apply
  91. @param {Object} [schema] Schema to apply. Supported configuration
  92. properties are:
  93. @param {Array} [schema.resultFields] Field identifiers to
  94. locate/assign values in the response records. See above for
  95. details.
  96. @param {Array} data Array data.
  97. @return {Object} An Object with properties `results` and `meta`
  98. @static
  99. **/
  100. apply: function(schema, data) {
  101. var data_in = data,
  102. data_out = {results:[],meta:{}};
  103. if(LANG.isArray(data_in)) {
  104. if(schema && LANG.isArray(schema.resultFields)) {
  105. // Parse results data
  106. data_out = SchemaArray._parseResults.call(this, schema.resultFields, data_in, data_out);
  107. }
  108. else {
  109. data_out.results = data_in;
  110. }
  111. }
  112. else {
  113. data_out.error = new Error("Array schema parse failure");
  114. }
  115. return data_out;
  116. },
  117. /**
  118. * Schema-parsed list of results from full data
  119. *
  120. * @method _parseResults
  121. * @param fields {Array} Schema to parse against.
  122. * @param array_in {Array} Array to parse.
  123. * @param data_out {Object} In-progress parsed data to update.
  124. * @return {Object} Parsed data object.
  125. * @static
  126. * @protected
  127. */
  128. _parseResults: function(fields, array_in, data_out) {
  129. var results = [],
  130. result, item, type, field, key, value, i, j;
  131. for(i=array_in.length-1; i>-1; i--) {
  132. result = {};
  133. item = array_in[i];
  134. type = (LANG.isObject(item) && !LANG.isFunction(item)) ? 2 : (LANG.isArray(item)) ? 1 : (LANG.isString(item)) ? 0 : -1;
  135. if(type > 0) {
  136. for(j=fields.length-1; j>-1; j--) {
  137. field = fields[j];
  138. key = (!LANG.isUndefined(field.key)) ? field.key : field;
  139. value = (!LANG.isUndefined(item[key])) ? item[key] : item[j];
  140. result[key] = Y.DataSchema.Base.parse.call(this, value, field);
  141. }
  142. }
  143. else if(type === 0) {
  144. result = item;
  145. }
  146. else {
  147. //TODO: null or {}?
  148. result = null;
  149. }
  150. results[i] = result;
  151. }
  152. data_out.results = results;
  153. return data_out;
  154. }
  155. };
  156. Y.DataSchema.Array = Y.mix(SchemaArray, Y.DataSchema.Base);
  157. }, '3.4.0' ,{requires:['dataschema-base']});