Myna. DataSet

A normalized data structure for working with tabular data

Summary
Myna. DataSetA normalized data structure for working with tabular data
Functions
DataSetCreates a new DataSet Object
containsreturns true if any row in the DataSet has a column value that matches compare
findFirstreturns the first row in the DataSet whose column value matches compare, or null if no matches
findAllreturns a new DataSet of all the rows in this DataSet whose column value matches compare
getDsreturns a java DataSource object that can be used as the “ds” option in queries
queryqueries this DataSet as a table named “data”, and returns a Myna.Query object
valueArrayreturns an array of the values of a column.
mapCreates a new DataSet with the results of calling a provided function on every element in this array.
filterPerforms Array.filter, but returns a new DataSet with the same columns as this one
concatPerforms Array.concat, but returns a new DataSet with the same columns as this one
slicePerforms Array.slice, but returns a new DataSet with the same columns as this one
minByColreturns the “smallest” value of a column.
maxByColreturns the “largest” value of a column.
sumByColreturns a sum of the values of a column.
sortByColsorts the DataSet by the supplied column and compare function.
avgByColreturns an average of the column.

Functions

DataSet

Myna.DataSet =function (options)

Creates a new DataSet Object

Parameters

optionsEither an array to be converted to a DataSet, or and object containing detailed options.  If an array, the array must contain at least one record, and the record should have all the non-function properties expected in the DataSet so that <DataSet.columns> can be inferred.  If this is an object it should conform to the Options Object defined below.

Options Object

dataOptional default [] This is an array of initial data.  May be empty.
columnsOptional default [] Either a comma separated list, an array of column names, or an object whose non-function properties represent the column names.  These define the known properties of the objects in a DataSet array.  If columns is not provided, but data contains at least one row, columns will be calculated as all the non-function properties of the first row.
loaderOptional default null If provided, this will be the implementation of <DataSet.load>

Detail

DataSet is an array of objects.  This is treated much like the result set of a query, but does not need to come from a query.  DataSet’s provide a normalized way to represent any tabular data

contains

Myna.DataSet.prototype.contains = function(column,
compare)

returns true if any row in the DataSet has a column value that matches compare

Parameters

columnname of the column to search.
compareRegExp, string regular expresion, or function to compare. if compare is a function, it will be called with the “Compare Function Arguments” below.  The supplied compare function should return true if the current row should be output

Compare Function Arguments

columnValueValue of column in the current row,
dataAn object that represents all the columns in this row
indexThe index of the current row
datasetA reference to this dataset

findFirst

Myna.DataSet.prototype.findFirst = function DataSet_findFirst(column,
compare)

returns the first row in the DataSet whose column value matches compare, or null if no matches

Parameters

columnname of the column to search.
compareRegExp, string regular expresion, or function to compare. if compare is a function, it will be called with the “Compare Function Arguments” below.  The supplied compare function should return true if the current row should be output

Compare Function Arguments

columnValueValue of column in the current row,
dataAn object that represents all the columns in this row
indexThe index of the current row
datasetA reference to this dataset

findAll

Myna.DataSet.prototype.findAll = function DataSet_findAll(column,
compare)

returns a new DataSet of all the rows in this DataSet whose column value matches compare

Parameters

columnname of the column to search.
compareRegExp, string regular expression, or function to compare. if compare is a function, it will be called with the “Compare Function Arguments” below.  The supplied compare function should return true if the current row should be output

Compare Function Arguments

columnValueValue of column in the current row,
dataAn object that represents all the columns in this row
indexThe index of the current row
datasetA reference to this DataSet

getDs

Myna.DataSet.prototype.getDs =function()

returns a java DataSource object that can be used as the “ds” option in queries

Detail

This will create an in-memory H2 database containing a single table “data” which contains the contents of this DataSet.  The return value will be a DataSource that can be used to query this table

query

Myna.DataSet.prototype.query =function(options)

queries this DataSet as a table named “data”, and returns a Myna.Query object

Detail

This will create an in-memory H2 database containing a single table “data” which contains the contents of this DataSet.  This function takes the same options as Myna.Query, except that the ds/dataSource parameter is ignored

valueArray

Myna.DataSet.prototype.valueArray=function(columnName)

returns an array of the values of a column.

Parameters

columnNameString Column name to return

map

Myna.DataSet.prototype.map = function(func)

Creates a new DataSet with the results of calling a provided function on every element in this array.

See

Array.map

filter

Myna.DataSet.prototype.filter = function()

Performs Array.filter, but returns a new DataSet with the same columns as this one

See

Array.filter

concat

Myna.DataSet.prototype.concat = function(otherArray)

Performs Array.concat, but returns a new DataSet with the same columns as this one

See

<Array.concat>

slice

Myna.DataSet.prototype.slice = function()

Performs Array.slice, but returns a new DataSet with the same columns as this one

See

<Array.slice>

minByCol

Myna.DataSet.prototype.minByCol = function(column,
compare)

returns the “smallest” value of a column.

Parameters

columncolumn to compare
compareOptiional, default: function(a,b){return a < b} A compare function like sort() uses to determine the minimum value

maxByCol

Myna.DataSet.prototype.maxByCol = function(column,
compare)

returns the “largest” value of a column.

Parameters

columncolumn to compare
compareOptiional, default: function(a,b){return a > b} A compare function like sort() uses to determaxe the maximum value

sumByCol

Myna.DataSet.prototype.sumByCol = function(column,
accessor)

returns a sum of the values of a column.

Parameters

columncolumn to sum
accessorOptional, default: function(element){return element} A function that takes an element of the column and returns a value to be summed.  This is useful to force integer math or to sum a property of the objects in the column rather than the objects themselves.

sortByCol

Myna.DataSet.prototype.sortByCol = function(column,
compare)

sorts the DataSet by the supplied column and compare function.

Parameters

columncolumn to sort
compareOptiional, default: String.compareAlpha A compare function that takes 2 elements and returns either 1, 0, or -1

Example

var files = new Myna.File("/").listFiles()
files.sortByCol("fileName",String.compareNatural)

avgByCol

Myna.DataSet.prototype.avgByCol = function(column,
accessor)

returns an average of the column.

Parameters

columncolumn to average
accessorOptional, default: function(element){return element} A function that takes an element of the column and returns a value to be averaged.  This is useful to force integer math or to average a property of the objects in the column rather than the objects themselves.

Note

null values are ignored.  If you want to count nulls as 0, use this accessor

function(element){
return element===null?0:element;
}
Myna.DataSet =function (options)
Creates a new DataSet Object
Myna.DataSet.prototype.contains = function(column,
compare)
returns true if any row in the DataSet has a column value that matches compare
Myna.DataSet.prototype.findFirst = function DataSet_findFirst(column,
compare)
returns the first row in the DataSet whose column value matches compare, or null if no matches
Myna.DataSet.prototype.findAll = function DataSet_findAll(column,
compare)
returns a new DataSet of all the rows in this DataSet whose column value matches compare
Myna.DataSet.prototype.getDs =function()
returns a java DataSource object that can be used as the “ds” option in queries
Myna.DataSet.prototype.query =function(options)
queries this DataSet as a table named “data”, and returns a Myna.Query object
Sql query object
Myna.DataSet.prototype.valueArray=function(columnName)
returns an array of the values of a column.
Myna.DataSet.prototype.map = function(func)
Creates a new DataSet with the results of calling a provided function on every element in this array.
Myna.DataSet.prototype.filter = function()
Performs Array.filter, but returns a new DataSet with the same columns as this one
Myna.DataSet.prototype.concat = function(otherArray)
Performs Array.concat, but returns a new DataSet with the same columns as this one
Myna.DataSet.prototype.slice = function()
Performs Array.slice, but returns a new DataSet with the same columns as this one
Myna.DataSet.prototype.minByCol = function(column,
compare)
returns the “smallest” value of a column.
Myna.DataSet.prototype.maxByCol = function(column,
compare)
returns the “largest” value of a column.
Myna.DataSet.prototype.sumByCol = function(column,
accessor)
returns a sum of the values of a column.
Myna.DataSet.prototype.sortByCol = function(column,
compare)
sorts the DataSet by the supplied column and compare function.
Myna.DataSet.prototype.avgByCol = function(column,
accessor)
returns an average of the column.
Array.prototype.map = function(fun /*,
 thisp*/)
Creates a new array with the results of calling a provided function on every element in this array.
Array.prototype.filter = function(fun /*,
 thisp*/)
Creates a new array with all elements that pass the test implemented by the provided function.