DataSchema.Array Class
yui3/src/dataschema/js/dataschema-array.js:9
Parent Module: dataschema
Provides a DataSchema implementation which can be used to work with data stored in arrays.
See the apply
method below for usage.
Index
Methods
- _parseResults static
- apply static
- parse
Methods
_parseResults
-
fields
-
array_in
-
data_out
Schema-parsed list of results from full data
Parameters:
-
fields
ArraySchema to parse against.
-
array_in
ArrayArray to parse.
-
data_out
ObjectIn-progress parsed data to update.
Returns:
Parsed data object.
apply
-
[schema]
-
data
Applies a schema to an array of data, returning a normalized object
with results in the results
property. The meta
property of the
response object is present for consistency, but is assigned an empty
object. If the input data is absent or not an array, an error
property will be added.
The input array is expected to contain objects, arrays, or strings.
If schema is not specified or schema.resultFields is not an array,
response.results
will be assigned the input array unchanged.
When a schema is specified, the following will occur:
If the input array contains strings, they will be copied as-is into the
response.results
array.
If the input array contains arrays, response.results
will contain an
array of objects with key:value pairs assuming the fields in
schema.resultFields are ordered in accordance with the data array
values.
If the input array contains objects, the identified schema.resultFields will be used to extract a value from those objects for the output result.
schema.resultFields field identifiers are objects with the following properties:
key
: <strong>(required)</strong> The locator name (String)parser
: A function or the name of a function onY.Parsers
used to convert the input value into a normalized type. Parser functions are passed the value as input and are expected to return a value.
If no value parsing is needed, you can use strings as identifiers instead of objects (see example below).
Parameters:
-
[schema]
Object optionalSchema to apply. Supported configuration properties are:
-
[resultFields]
Array optionalField identifiers to locate/assign values in the response records. See above for details.
-
-
data
ArrayArray data.
Returns:
An Object with properties results
and meta
Example:
// Process array of arrays
var schema = { resultFields: [ 'fruit', 'color' ] },
data = [
[ 'Banana', 'yellow' ],
[ 'Orange', 'orange' ],
[ 'Eggplant', 'purple' ]
];
var response = Y.DataSchema.Array.apply(schema, data);
// response.results[0] is { fruit: "Banana", color: "yellow" }
// Process array of objects
data = [
{ fruit: 'Banana', color: 'yellow', price: '1.96' },
{ fruit: 'Orange', color: 'orange', price: '2.04' },
{ fruit: 'Eggplant', color: 'purple', price: '4.31' }
];
response = Y.DataSchema.Array.apply(schema, data);
// response.results[0] is { fruit: "Banana", color: "yellow" }
// Use parsers
schema.resultFields = [
{
key: 'fruit',
parser: function (val) { return val.toUpperCase(); }
},
{
key: 'price',
parser: 'number' // Uses Y.Parsers.number
}
];
response = Y.DataSchema.Array.apply(schema, data);
// Note price was converted from a numeric string to a number
// response.results[0] looks like { fruit: "BANANA", price: 1.96 }
parse
-
value
-
field
Applies field parser, if defined
Parameters:
-
value
ObjectOriginal value.
-
field
ObjectField.
Returns:
Type-converted value.