Creates a new list.
// A single haystack value array.
const list0 = new HList([HStr.make('foo), HMarker.make()])
// Pass in multiple arguments instead of an array.
const list1 = 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 })
The list values.
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.
Iterate over a list.
This enables a 'for ... of' loop to be used directly on an iterator.
A new iterator for a list.
// Iterate a list
for (let value of list) {
console.log(value)
}
// Destructure a list into a real array
const array = [...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.
Optional
cx: 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.
Optional
cx: 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.
Optional
cx: 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.
Optional
thisArg: 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.
Loop through a list's values.
The function to execute on each haystack value.
Optional
thisArg: anyOptional value to use as this when executing callback.
Find a value to search for and returns true if it's found.
if (list.includes(HNum.make(24, 'm'))) {
// Do something...
}
The value to search for.
Optional
fromIndex: numberOptional index number to search from.
true if the value is found.
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.
Compares the value's kind.
The kind to compare against.
True if the kind matches.
Use an array style map function on a list.
// Using React, map a list to some DOM elements...
list.make((str: HStr): any => <div>{str.value}</div>)
The map callback.
A new list with the mapped value.
Returns true if the haystack filter matches the value.
This method is the same as any
.
The filter to test.
Optional
cx: 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 reduce callback.
Optional
initialValue: NewValueThe initial value for the reduce operation.
The reduced value.
Set a haystack value in the list.
list.set(0, HStr.make('A new string'))
// Or use Hayson...
list.set(0, 'A new string')
The index of the item in the list.
The haystack value or hayson value to set.
The list instance.
Returns the list in a Hayson format.
A JSON reprentation of the object.
A JSON v3 representation of the object.
Static
makeMake a list.
A list.
Generated using TypeDoc
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...