Show:

Provides a DataSchema implementation which can be used to work with delimited text data.

See the apply method for usage.

Index

Methods

Methods

_parseResults

(
  • schema
  • text_in
  • data_out
)
Object protected static

Schema-parsed list of results from full data

Parameters:

  • schema Array

    Schema to parse against.

  • text_in String

    Text to parse.

  • data_out Object

    In-progress parsed data to update.

Returns:

Object:

Parsed data object.

apply

(
  • schema
  • data
)
Object static

Applies a schema to a string of delimited 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 a string, an error property will be added.

Use schema.resultDelimiter and schema.fieldDelimiter to instruct apply how to split up the string into an array of data arrays for processing.

Use schema.resultFields to specify the keys in the generated result objects in response.results. The key:value pairs will be assigned in the order of the schema.resultFields array, assuming the values in the data records are defined in the same order.

schema.resultFields field identifiers are objects with the following properties:

  • key : <strong>(required)</strong> The property name you want the data value assigned to in the result object (String)
  • parser: A function or the name of a function on Y.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 just the desired property name string as the field identifier instead of an object (see example below).

Parameters:

  • schema Object

    Schema to apply. Supported configuration properties are:

    • resultDelimiter String

      Character or character sequence that marks the end of one record and the start of another.

    • [fieldDelimiter] String optional

      Character or character sequence that marks the end of a field and the start of another within the same record.

    • [resultFields] Array optional

      Field identifiers to assign values in the response records. See above for details.

  • data String

    Text data.

Returns:

Object:

An Object with properties results and meta

Example:

// Process simple csv
                                                var schema = {
                                                        resultDelimiter: "\n",
                                                        fieldDelimiter: ",",
                                                        resultFields: [ 'fruit', 'color' ]
                                                    },
                                                    data = "Banana,yellow\nOrange,orange\nEggplant,purple";
                                                
                                                var response = Y.DataSchema.Text.apply(schema, data);
                                                
                                                // response.results[0] is { fruit: "Banana", color: "yellow" }
                                                
                                                
                                                // Use parsers
                                                schema.resultFields = [
                                                    {
                                                        key: 'fruit',
                                                        parser: function (val) { return val.toUpperCase(); }
                                                    },
                                                    'color' // mix and match objects and strings
                                                ];
                                                
                                                response = Y.DataSchema.Text.apply(schema, data);
                                                
                                                // response.results[0] is { fruit: "BANANA", color: "yellow" }

parse

(
  • value
  • field
)
Object

<p>Applies field parser, if defined</p>

Parameters:

  • value Object

    <p><p><p>Original value.</p></p></p>

  • field Object

    <p><p><p>Field.</p></p></p>

Returns:

Object:

<p><p><p>Type-converted value.</p></p></p>