Numerical index access.
Creates a new list.
// A single haystack value array.
const list0 = new HList([HStr.make('foo), HMarker.make()])
// Create a list using Hayson.
const list2 = new HList(['foo', { _kind: Kind.Marker }])
const list2 = new HList('foo', { _kind: Kind.Marker })
Convience method for averaging all the numbers in a list.
If there are no numbers then Number.NaN is returned.
// 2
const avg = new HList<HNum>(1, 2, 3).avg
The average of all the numbers.
Return the length of the list.
Convience method for getting the maximum number in a list.
// 3
const max = new HList<HNum>(1, 2, 3).max
If there are no numbers then Number.MIN_SAFE_INTEGER is returned.
A maximum number in a list.
Convience method for getting the minimum number in a list.
// 1
const min = new HList<HNum>(1, 2, 3).min
If there are no numbers then Number.MAX_SAFE_INTEGER is returned.
A minimum number in a list.
Convience method for summing a list of numbers in a list.
// Sums to 6
const sum = new HList<HNum>(1, 2, 3).sum
A sum of all the numbers in the list.
Adds an element to the list.
This method can add multiple values at once...
list.add(HStr.make('foo'))
list.add(HStr.make('foo'), HStr.make('boo))
// Using an array...
list.add([HStr.make('foo'), HStr.make('boo)])
// Same but using hayson...
list.add('foo')
list.add('foo', 'boo')
list.add(['foo', 'boo'])
The list instance.
Test to see if the list contains all the same value or the filter matches all elements.
const list = new HList([ HStr.make('a'), HStr.make('a'), HStr.make('a') ])
if (list.all(HStr.make('a'))) {
// Do something
}
// Also use a haystack filter...
if (list.all('item == "a"')) {
// Do something
}
// Same again but use the shorter 'it'...
if (list.all('it == "all"')) {
// Do something
}
list.push(HStr.make('b'))
// All will now return false as the values aren't all the same...
if (list.all('it == "a"')) {
// Do something
}
The value to test, a haystack filter or AST node.
Optionalcx: Partial<EvalContext>Optional haystack filter evaluation context.
True if the list contains the value.
Test to see if the list contains the value or matches the filter at least once.
const list = new HList([ HStr.make('a'), HStr.make('b'), HStr.make('c') ])
if (list.any(HStr.make(a))) {
// Do something
}
// Or use a haystack filter...
if (list.any('item == "a"')) {
// Do something
}
// It use the shortened item name in a haystack filter...
if (list.any('it == "a"')) {
// Do something
}
The value to test, a haystack filter or AST node.
Optionalcx: Partial<EvalContext>Optional haystack filter evaluation context.
True if the list contains the value.
Filter the list via a haystack filter or callback function and return a new list.
If a Haystack filter is used, the array list is referenced as either 'it' or 'item.
const list = HList<HStr>.make([ HStr.make('foo'), HStr.make('boo'), HStr.make('foo') ])
const filteredList = list.filter('item == "foo"')
// Or use the shorter it...
const anotherList = list.filter('it == "foo"')
A classic filter function callback can also be used...
const anotherList= list.filter((val: HStr): boolean => val.value === 'foo')
A haystack filter or AST node.
Optionalcx: Partial<EvalContext>Optional haystack filter evaluation context.
A new haystack list.
Find an item in the list and return it or undefiend if not found.
const foundItem = list.find((val) => val.someItem.equals(foo))
The find callback.
OptionalthisArg: anyOptional this argument.
The item or undefined.
// Creates an HList with HNums - [1, 2, 3, 4, 5, 6]
const list = new HList<HNum>([[1, 2, 3], [4, 5], 6]).flat()
A flattened list.
Inserts an element into the list.
This method can insert multiple values at once...
list.insert(1, HStr.make('foo'))
list.insert(1, HStr.make('foo'), HStr.make('boo))
// Using an array...
list.insert(1, [HStr.make('foo'), HStr.make('boo)])
// Same but using hayson...
list.insert(1, 'foo')
list.insert(1, 'foo', 'boo')
list.insert(1, ['foo', 'boo'])
The list instance.
Returns true if the haystack filter matches the value.
This method is the same as any.
The filter to test.
Optionalcx: Partial<EvalContext>Optional haystack filter evaluation context.
True if the filter matches ok.
Use an array style reduce function on a list.
const numList = new HList<HNum>([1, 2, 3])
const total = numList.reduce(
(prev, cur): number => prev + cur.value), 0
)
The reduced value.
Returns the list in a Hayson format.
A JSON reprentation of the object.
A JSON v3 representation of the object.
StaticmakeMake a list.
A list.
A mutable haystack list that uses generics and the JavaScript proxy pattern.
A list implements the JavaScript proxy pattern to make it easy to work with...
A list is also iterable...