Class HList<Value>

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...


// If you want the list to hold any value then use `new HList<HVal>(...)` instead.
const list = new HList<HStr>([ HStr.make('alpha'), HStr.make('beta') ])

// Write 'gamma'
list.set(0, HStr.make('gamma'))

// Read 'gamma'
console.log(list.get(0))

A list is also iterable...

// Iterate a list
for (let value of list) {
console.log(value)
}

// Get the list as an array...
const array = list.toArray()

Type Parameters

Implements

Indexable

  • [prop: number]: Value

    Numerical index access.

Constructors

Properties

$store: ListStore<Value>

The list values.

Accessors

  • get avg(): number
  • 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

    Returns number

    The average of all the numbers.

  • get max(): number
  • 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.

    Returns number

    A maximum number in a list.

  • get min(): number
  • 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.

    Returns number

    A minimum number in a list.

  • get sum(): number
  • Convience method for summing a list of numbers in a list.

    // Sums to 6
    const sum = new HList<HNum>(1, 2, 3).sum

    Returns number

    A sum of all the numbers in the list.

Methods

  • Iterate over a list.

    This enables a 'for ... of' loop to be used directly on an iterator.

    Returns Iterator<Value>

    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'])

    Parameters

    • ...values: (HaysonVal | Value | (HaysonVal | Value)[])[]

      The values to add to the list.

    Returns this

    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
    }

    Parameters

    • val: string | Node | Value

      The value to test, a haystack filter or AST node.

    • Optionalcx: Partial<EvalContext>

      Optional haystack filter evaluation context.

    Returns boolean

    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
    }

    Parameters

    • val: string | Node | Value

      The value to test, a haystack filter or AST node.

    • Optionalcx: Partial<EvalContext>

      Optional haystack filter evaluation context.

    Returns boolean

    True if the list contains the value.

  • Clear all entries from the list.

    // Makes a list completely empty.
    list.clear()

    Returns void

  • Compares two values.

    Parameters

    • value: unknown

      The value to compare against.

    Returns number

    The sort order as negative, 0, or positive.

  • Value equality check.

    Parameters

    • value: unknown

      The value to test.

    Returns boolean

    True if the value is the same.

  • 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')
    

    Parameters

    • filter: string | Node | FilterCallback<Value>

      A haystack filter or AST node.

    • Optionalcx: Partial<EvalContext>

      Optional haystack filter evaluation context.

    Returns HList<Value>

    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))
    

    Parameters

    • callback: FindCallback<Value>

      The find callback.

    • OptionalthisArg: any

      Optional this argument.

    Returns undefined | Value

    The item or undefined.

  • Loop through a list's values.

    Parameters

    • callback: (value: Value, index: number, array: Value[]) => void

      The function to execute on each haystack value.

    • OptionalthisArg: any

      Optional value to use as this when executing callback.

    Returns void

  • Returns a haystack value from the list or undefined if it can't be found.

    const val = list.get(0)
    if (val) {
    // Do something
    }

    Parameters

    • index: number

      The index number of the value to get.

    Returns undefined | Value

    The value, null or undefined if it can't be found.

  • Find a value to search for and returns true if it's found.

    if (list.includes(HNum.make(24, 'm'))) {
    // Do something...
    }

    Parameters

    • val: HaysonVal | Value

      The value to search for.

    • OptionalfromIndex: number

      Optional index number to search from.

    Returns boolean

    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'])

    Parameters

    • index: number
    • ...values: (HaysonVal | Value | (HaysonVal | Value)[])[]

      The values to insert into the list.

    Returns this

    The list instance.

  • Dump the value to the local console output.

    Parameters

    • Optionalmessage: string

      An optional message to display before the value.

    Returns this

    The value instance.

  • if (list.isEmpty()) {
    // List is empty.
    }

    Returns boolean

    True when there are no entries in the list.

  • 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>)

    Type Parameters

    • NewValue

    Parameters

    Returns NewValue[]

    A new list with the mapped value.

  • Returns true if the haystack filter matches the value.

    This method is the same as any.

    Parameters

    • filter: string | Node

      The filter to test.

    • Optionalcx: Partial<EvalContext>

      Optional haystack filter evaluation context.

    Returns boolean

    True if the filter matches ok.

  • Pops removes the last element of the array and returns it. If the array is empty then undefined is returned.

    const lastValue = list.pop()

    if (lastValue) {
    // Do something
    }

    Returns undefined | Value

    The popped value or undefined.

  • Pushes a value onto the end of the list.

    list.push(HStr.make('A new string'))

    // Same but using Hayson...
    list.push('A new string')

    Parameters

    Returns this

    The list instance.

  • 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
    )

    Type Parameters

    Parameters

    • callback: ReduceCallback<NewValue, Value>

      the reduce callback.

    • OptionalinitialValue: NewValue

      The initial value for the reduce operation.

    Returns NewValue

    The reduced value.

  • Removes a value from the list.

    // Removes the first element of the list.
    list.remove(0)

    Parameters

    • index: number

      The index of the item in the list.

    Returns void

  • Reverse the order of all the values in the list.

    const listInDescendingOrder = list.sort().reverse()
    

    Returns this

    The list instance.

  • Set a haystack value in the list.

    list.set(0, HStr.make('A new string'))

    // Or use Hayson...
    list.set(0, 'A new string')

    Parameters

    • index: number

      The index of the item in the list.

    • value: HaysonVal | Value

      The haystack value or hayson value to set.

    Returns this

    The list instance.

  • Sort the list in ascending order.

    const sortedList = list.sort()
    

    Returns this

    The list instance.

  • Encodes to an encoded zinc value that can be used in a haystack filter string.

    A list isn't supported in filter so throw an error.

    Returns string

    The encoded value that can be used in a haystack filter.