Constructs a new grid.
const grid = new HGrid({
columns: [
{
name: 'name'
},
{
name: 'height'
}
],
// rows
rows: [
new HDict({ name: HStr.make('Mall'), height: HNum.make(200, 'm') }),
new HDict({ name: HStr.make('House'), height: HNum.make(30, 'm') })
]
})
// The same grid can be specified without any rows. The columns will be dynamically
// generated based upon the row data...
const grid0 = new HGrid({
rows: [
new HDict({ name: HStr.make('Mall'), height: HNum.make(200, 'm') }),
new HDict({ name: HStr.make('House'), height: HNum.make(30, 'm') })
]
})
// The same grid can be created from a Hayson object.
// Again columns don't have to be specified unless precise order and meta data is required...
const grid1 = new HGrid({
rows: [
{ name: 'Mall', height: { _kind: 'number', val: 200, unit: 'm' } },
{ name: 'House', height: { _kind: 'number', val: 30, unit: 'm' } },
]
})
// Pass in a haystack value to create a grid...
const grid3 = new HGrid(HNum.make(24)) // Creates a grid with one column called 'val' and one row.
// Pass in an array of dicts to create a grid...
const grid4 = new HGrid([
new HDict({ name: HStr.make('Mall'), height: HNum.make(200, 'm') }),
new HDict({ name: HStr.make('House'), height: HNum.make(30, 'm') })
])
// Pass in an array of Hayson dicts to create a grid...
const grid5 = new HGrid([
{ name: 'Mall', height: { _kind: 'number', val: 200, unit: 'm' } },
{ name: 'House', height: { _kind: 'number', val: 30, unit: 'm' } },
])
Optional
arg: HaysonGrid | HVal | GridObj<DictVal> | (HaysonDict | DictVal)[]This flag should be only used internally. If true then any error checking on dicts is skipped. This is useful when we already know the dict being added is valid.
The grid's version number.
Return the first row in the grid or undefined if it can't be found.
const dict = grid.first
if (dict) {
// Do something
}
The dict or undefined if it does not exist.
Return the last row in the grid or undefined if it can't be found.
const dict = grid.last
if (dict) {
// Do something
}
The dict or undefined if it does not exist.
// The number of rows in a grid.
console.log(grid.length)
The total number of rows.
Iterate over a grid using dicts for rows.
This enables a 'for ... of' loop to be used directly on an iterator.
A new iterator for a grid.
// Iterate a grid
for (let dict of grid) {
console.log(dict)
}
// Destructure a grid into an array of dicts...
const fooDict = [...grid].filter((dict): boolean => dict.get('foo') === 'foo')[0]
Add a single or multiple rows using dicts.
This method can be called in different ways to add multiple rows at a time.
// Add a single dict.
grid.add(new HDict({ foo: HStr.make('bar') }))
// Add multiple dicts.
grid.add(new HDict({ foo: HStr.make('bar') }), new HDict({ foo: HStr.make('bar') }))
// Add multiple dicts using an array...
grid.add([new HDict({ foo: HStr.make('bar') }), new HDict({ foo: HStr.make('bar') })])
// Same but using Hayson...
grid.add({ foo: 'bar' }))
grid.add({ foo: 'bar' }), { foo: 'bar' })
grid.add([{ foo: 'bar' }), { foo: 'bar' }])
Rest
...rows: (HaysonDict | HaysonDict[] | DictVal | DictVal[])[]The rows to add.
The grid instance.
If the values being added are not dicts.
Add a column and return its new instance.
If the column is already available then update it.
grid.addColumn('Address', new HDict({ length: 30 }))
The name of the column.
Optional
meta: HDictThe column's meta data.
The new column or the one already found.
Return true if the filter matches at least one row.
const grid = HGrid.make({
rows: [
{ name: 'Fred' },
{ name: 'Fred' },
{ name: 'Fred' },
]
})
if (grid.all('name == "Fred")) {
// All rows in the grid have the name Fred.
}
The haystack filter or AST node.
Optional
cx: Partial<EvalContext>Optional haystack filter evaluation context.
true if there's at least one match
Return true if the filter matches all the values in a particular column in the grid.
This filter runs on the data held in the particular column.
const grid = HGrid.make({
rows: [
{ list: [ 'foo', 'foo', 'foo' ] },
{ list: [ 'foo', 'foo', 'foo' ] },
{ list: [ 'foo', 'foo', 'foo' ] },
]
})
if (grid.allBy('list', 'item == "foo"')) {
// True if all the lists contain all foos.
}
Optional
cx: Partial<EvalContext>Optional haystack filter evaluation context.
true if there's at least one match
Return true if the filter matches at least one row.
if (grid.any('site')) {
// The grid has some sites.
}
The haystack filter or AST node.
Optional
cx: Partial<EvalContext>Optional haystack filter evaluation context.
true if there's at least one match
Return true if the filter matches at least one cell in a particular column in the grid.
This filter runs on the data held in the particular column.
const grid = HGrid.make({
rows: [
{ list: [ 'foo', 'boo', 'goo' ] },
{ list: [ 'foo', 'boo1', 'goo1' ] },
{ list: [ 'doo', 'boo1', 'goo1' ] },
]
})
if (grid.anyBy('list', 'item === "foo"')) {
// One or more of the items in the list contains 'foo'
}
Optional
cx: Partial<EvalContext>Optional haystack filter evaluation context.
true if there's at least one match
Return the sum of values for the specified column.
If there are no numbers then Number.NaN is returned.
const grid = HGrid.make({
rows: [
{ id: 34, num: 1 },
{ id: 35, num: 2 },
{ id: 36, num: 3 },
]
})
// Return average of the num column (2).
const avg = grid.avgOf('num')
The column name, column index or column instance.
The average of all the numeric values.
Filter the grid with the haystack filter and return a new grid with the results.
// Filter a grid with a haystack filter
const newGridWithFoo = grid.filter('foo')
// Filter a grid with a function callback
const newGridWithFooAgain = grid.filter((row: HDict): boolean => row.has('foo'))
The haystack filter, AST node or filter function callback.
Optional
cx: Partial<EvalContext>Optional haystack filter evaluation context.
A new filtered grid.
Filters an individual column in a grid.
For example, if a particular column in a grid holds a list. The inner filter can be run against all of the list values held in that column.
The filter can be run against a list, dict or grid.
const grid = HGrid.make({
rows: [
{ list: [ 'foo', 'boo', 'goo' ] },
{ list: [ 'foo', 'boo1', 'goo1' ] },
{ list: [ 'doo', 'boo1', 'goo1' ] },
]
})
// Returns a grid with only the first two rows.
const newGrid = grid.filterBy('list', 'item == "foo"')
The name of the column that holds the list values.
The haystack filter to run against the list.
Optional
cx: Partial<EvalContext>Optional haystack filter evaluation context.
A filtered grid.
Return a row or undefined if it can't find it via its row number.
// Get a dict at a given index or returned undefined if it can't be found.
const dict = grid.get(0)
if (dict) {
// Do something
}
The index number of the row.
The dict or undefined if it does not exist.
Returns a grid column via its name or index number. If it can't be found then return undefined.
// Get the column at the specified index or return undefined
const col = grid.getColumn('Address')
if (col) {
// Do something
}
// Alternatively use the column index to get the column
const col1 = grid.getColumn(3)
if (col1) {
// Do something
}
The column index number or name.
The column or undefined if not found.
Return an array of column information.
// Return an array of column objects.
const cols = grid.getColumns()
A copy of the grid's columns.
const err = grid.getError()
if (err) {
// Do something with the error.
}
Error information or undefined if not available.
Insert rows as dicts at the specified index.
// Insert a single dict.
grid.insert(1, new HDict({ foo: HStr.make('bar') }))
// Insert multiple dicts.
grid.insert(1, new HDict({ foo: HStr.make('bar') }), new HDict({ foo: HStr.make('bar') }))
// Insert multiple dicts using an array...
grid.insert(1, [new HDict({ foo: HStr.make('bar') }), new HDict({ foo: HStr.make('bar') })])
// Same but using Hayson...
grid.insert(1, { foo: 'bar' }))
grid.insert(1, { foo: 'bar' }), { foo: 'bar' })
grid.insert(1, [{ foo: 'bar' }), { foo: 'bar' }])
The index number to insert the rows at.
Rest
...rows: (HaysonDict | HaysonDict[] | DictVal | DictVal[])[]The rows to insert.
The grid instance.
An error if the index is invalid or the rows are not dicts.
Compares the value's kind.
The kind to compare against.
True if the kind matches.
Limit the grid only to the specified columns.
This will return a new instance of a grid.
grid.filter('site').limitColumns(['id', 'dis']).inspect()
The column names.
A new grid instance with the specified columns.
Return a haystack list for all the values in the specified column.
If the column can't be found then an empty list is returned.
const grid = HGrid.make({
rows: [
{ name: 'Gareth', id: 1 },
{ name: 'Jason', id: 2 },
{ name: 'Radu', id: 3 },
]
})
// Returns an HList<HStr> of the names (Gareth, Jason and Radu).
const listOfNames = grid.listBy<HStr>('name')
The column name, column index or instance to create the list from.
A mapping function that maps from an array of dicts into something else.
// Map each row to a div using React...
grid.map((dict: HDict) => <div>{dict.toZinc()}</div>>)
A mapping callback that takes a row dict, an index number and returns a new value.
Returns true if the haystack filter matches the value.
This is the same as the any
method.
if (grid.matches('site')) {
// The grid has some sites.
}
The filter to test.
Optional
cx: Partial<EvalContext>Optional haystack filter evaluation context.
True if the filter matches ok.
Return the maximum value in the specified column.
If there are no numbers then Number.MIN_SAFE_INTEGER is returned.
const grid = HGrid.make({
rows: [
{ id: 34, num: 1 },
{ id: 35, num: 2 },
{ id: 36, num: 3 },
]
})
// Return the maximum value in the num column (3).
const max = grid.maxOf('num')
The column name, column index or column instance.
The maximum numerical value found.
Return the minimum of value in the specified column.
If there are no numbers then Number.MAX_SAFE_INTEGER is returned.
const grid = HGrid.make({
rows: [
{ id: 34, num: 1 },
{ id: 35, num: 2 },
{ id: 36, num: 3 },
]
})
// Return the maximum value in the num column (1).
const min = grid.minOf('num')
The column name, column index or column instance.
The minimum numerical value found.
Selects a range from the grid.
The start and end can be used to specify a range...
// from [0, 1, 2, 3, 4, 5] to [1, 2, 3, 4]
grid.filter('site').range(1, 4).inspect()
//If only the first argument then a quantity can be used...
// select the first 4 rows - [0, 1, 2, 4]...
grid.filter('site').range(4).inspect()
The start of the range or quantity.
Optional
end: numberOptional end range.
This grid instance.
Reduce the rows in a grid.
// Reduce the grid down to one row...
grid = HGrid.make({
rows: [
{ a: 1, b: 2 },
{ a: 3, b: 4 },
],
})
grid.reduce((prev, cur): HGrid => {
const dict = prev.get(0)
if (dict) {
dict.set('a', Number(cur.get<HNum>('a')?.value) + Number(dict.get<HNum>('a')?.value))
dict.set('b', Number(cur.get<HNum>('b')?.value) + Number(dict.get<HNum>('b')?.value))
}
return prev
}, HGrid.make({ rows: [{ a: 0, b: 0 }] }))
The reducer callback. This method will be called with the previous and current rows (dicts) as well as the index number.
Optional
initialValue: UOptional initial value for the reduce.
Remove the row from the grid via its index number of a haystack filter.
// Remove a row via its index
grid.remove(0)
// Remove multiple rows via a Haystack Filter
grid.remove('foo == "baa"')
A haystack filter, index number or AST node.
Optional
cx: Partial<EvalContext>Optional haystack filter evaluation context.
The rows that were removed. If no rows were removed then the is empty.
Reorder the columns with the specified new order of names.
const grid = HGrid.make({
columns: [
{ name: 'b' },
{ name: 'c' },
{ name: 'a' },
]
})
// Reorder the columns to be a, b and then c.
grid.reorderColumns([ 'a', 'b', 'c' ])
The new order of column names to use.
Set the values or dict for an individual row.
// Set a row in a grid.
grid.set(0, new HDict({ foo: HStr.make('foobar') }))
// Set a row via Hayson.
grid.set(0, { foo: 'foobar' })
The index number of the row.
The dict or Hayson Dict.
The grid instance.
An error if the index is invalid or the number of rows incorrect.
Set the column at the specified index number.
// Set the column at the specified index with the new name and length.
grid.setColumn(3, 'Address', new HDict({ length: 30 }))
The zero based index number of the column.
The name of the column.
Optional
meta: HDictOptional column's meta data.
The updated column.
An error if index does not exist in the columns.
Sort the grid in ascending order via a column name. This also supports sorting via multiple column names.
Precedence is given to the first columns in the table.
// Sorts the grid in ascending order by 'foo'
grid.sortBy('foo')
// Sorts the grid in ascending order by 'foo' and then by 'boo'
grid.sortBy(['foo', 'boo'])
The name of the column to sort by.
The grid instance.
Return the sum of values for the specified column.
const grid = HGrid.make({
rows: [
{ id: 34, num: 1 },
{ id: 35, num: 2 },
{ id: 36, num: 3 },
]
})
// Sum all the values in the num column (6)
const sum = grid.sumOf('num')
The column name, column index or column instance.
The sum of all the numeric values.
A JSON reprentation of the object.
A JSON v3 representation of the object.
Provide a grid with unique values in the specified columns.
const grid = HGrid.make({
rows: [
{ id: 1, name: 'Jason' },
{ id: 2, name: 'Gareth' },
{ id: 3, name: 'Gareth' },
]
})
// Returns a new grid with rows 1 and 2.
const uniqueGrid = grid.unique('name')
The column names.
The filtered grid instance.
Static
makeMakes a new grid.
Optional
arg: HaysonGrid | HVal | GridObj<DictVal> | (HaysonDict | DictVal)[]This flag should be only used internally. If true then any error checking on dicts is skipped. This is useful when we already know the dict being added is valid.
A grid.
Generated using TypeDoc
A haystack grid.