Class HDict

A mutable haystack dict that also implements the JavaScript Proxy pattern.

const dict = new HDict({ foo: HStr.make('alpha') })

// Write 'beta'
dict.set('foo', HStr.make('beta'))

// Read 'beta'
console.log(dict.get('foo'))

A dict is also iterable...

// Iterate a dict
for (let row of dict) {
console.log(row.name)
console.log(row.value)
}

// Use a haystack filter to test for properties...
console.log(dict.matches('foo == "beta"')) // prints true

Hierarchy

Implements

Indexable

[prop: string]: unknown | undefined

Constructors

  • Create a dict using a set of object values.

    There are multiple ways to create a dict...

    // Create an empty dict
    const dict0 = new HDict()

    // Create a dict from a bunch of key haystack value pairs
    const dict1 = new HDict({ foo: HStr.make('foovalue;), boo: HNum.make(123) })

    // Same but with Hayson
    const dict2 = new HDict({ foo: 'foovalue', boo: 123 })

    // Create a grid from any haystack value...
    const dict3 = new HDict(HStr.make('foovalue')) // Create with one row and one column as 'val'.

    Parameters

    Returns HDict

    A dict.

Accessors

  • get defName(): string
  • If this dict is for a def then return its name otherwise return an empty string.

    Returns string

    The def name or an empty string.

  • get keys(): string[]
  • for (let key of dict.keys) {
    console.log(key)
    }

    Returns string[]

    All keys used in the dict.

  • get length(): number
  • console.log('Size: ' + dict.length)
    

    Returns number

    The number of entries in the dict.

Methods

  • Iterate over a dict.

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

    Returns Iterator<HValRow, any, undefined>

    A new iterator for a dict.

    // Iterate a dict
    for (let row of dict) {
    console.log(row.name)
    console.log(row.value)
    }
  • Return true if the dict matches the specified filter or if the value exists in the dict at least once.

    if (dict.any('site and geoCity == "London"')) {
    ...
    }

    Parameters

    • filter: string | Node | OptionalHVal

      The haystack value, haystack filter or AST node.

    • Optional cx: Partial<EvalContext>

      Optional haystack filter evaluation context.

    Returns boolean

    True if the property value exists in the dict.

    Throws

    An error for a invalid haystack filter.

  • Clear all entries from the dict.

    // Clear all the entries from the dict.
    dict.clear()

    Returns void

  • Compares two values.

    Parameters

    • value: unknown

      The value to compare against.

    Returns number

    The sort order as negative, 0, or positive.

  • Create a diff (difference) dict that can be used in an update.

    This will return a new dict with any changed values and removed tags having an HRemove value.

    Parameters

    • dict: HDict

      The newly updated dict that will be checked for differences. These differences will be incorporated into the returned dict.

    Returns HDict

    A diff dict.

  • Value equality check.

    Parameters

    • value: unknown

      The value to test.

    Returns boolean

    True if the value is the same.

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

    // Gets the value as an HVal so cast to an HStr.
    const str = dict.get('foo') as HStr

    if (str) {
    // Do something.
    }

    // Method is generic to make it easier on the eye for casting.
    const str1 = dict.get<HStr>('foo')

    Type Parameters

    Parameters

    • name: string

      The name of the value to find.

    Returns undefined | Value

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

  • Returns true if the dict has the specified key.

    if (dict.has('foo')) {
    // Do something
    }

    Parameters

    • name: string

      The name of the key.

    Returns boolean

    True if the value exists in the dict.

  • Dump the value to the local console output.

    Parameters

    • Optional message: string

      An optional message to display before the value.

    Returns HDict

    The value instance.

  • if (dict.isEmpty()) {
    // There are no entries in the dict.
    }

    Returns boolean

    True when there are no entries in the dict.

  • Compares the value's kind.

    Parameters

    • kind: Kind

      The kind to compare against.

    Returns boolean

    True if the kind matches.

  • Returns true if this dict is newer than the specified dict. The mod timestamp is used to perform the check.

    Parameters

    • dict: HDict

      The other dict to compare to this dict.

    Returns boolean

    True if this dict is newer.

  • Returns true if the haystack filter matches the value.

    This method is the same as any.

    if (dict.matches('site and geoCity == "London"')) {
    // Do something
    }

    Parameters

    • filter: string | Node

      The filter to test.

    • Optional cx: Partial<EvalContext>

      Optional haystack filter evaluation context.

    Returns boolean

    True if the filter matches ok.

  • Return a reflected array of children prototypes.

    If a namespace isn't specified then the default environment namespace will be used.

    Parameters

    • Optional namespace: HNamespace

      An optional namespace to perform the protos call from.

    Returns HDict[]

    An array of dicts.

  • Analyze this dict and return its implemented defs.

    If a namespace isn't specified then the default environment namespace will be used.

    Parameters

    • Optional namespace: HNamespace

      An optional namespace to perform the reflect from.

    Returns Reflection

    An array of dicts.

  • Removes a property from the dict.

    // Removes the tag named foo.
    dict.remove('foo')

    Parameters

    • name: string

      The property name.

    Returns void

  • Set a haystack value.

    dict.set('foo', HStr.make('New value'))

    // Set the value using Hayson
    dict.set('foo', 'New value')

    Parameters

    • name: string

      The name to set.

    • value: HaysonVal | HVal

      The haystack value to set.

    Returns HDict

    The dict instance.

  • Get the display string for the dict or the given tag. If 'name' is undefined, then return display text for the entire dict. If 'name' is non-null then format the tag value. If 'name' is not defined by this dict then return 'def'.

    // Returns the record's dis tag string value...
    myDict.toDis()

    // Returns the record's tag value string value for foo...
    myDict.toDis({ name: 'foo' })

    // Returns a localized string based on `disKey`...
    myDict.toDis({
    i18n: (pod: string, key: string): string | undefined => pods.get(pod)?.key(key)
    })

    Parameters

    • __namedParameters: {
          def?: string;
          i18n?: LocalizedCallback;
          name?: string;
          short?: boolean;
      } = {}
      • Optional def?: string
      • Optional i18n?: LocalizedCallback
      • Optional name?: string
      • Optional short?: boolean

    Returns string

    The display string.

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

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

    Returns string

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

  • // Convert the dict to an HList of haystack strings.
    const hlist = dict.toList<HStr>()

    Type Parameters

    • Value extends HVal<Value>

    Returns HList<Value>

    All the dict's values as a haystack list

  • Returns the underlying object being managed by the store.

    // Gets a JS Object with the keys as strings and the values as HVals.
    const obj = dict.toObj()

    Returns HValObj

    A the underlying object.

  • Update a dict from another dict(s) or Hayson dict(s).

    dict.update(otherDict, anotherDict)

    // Update using a Hayson object
    dict.update({ dis: 'A new display string', curVal: 20 })

    Parameters

    Returns HDict

    The dict instance.

  • Iterates through the dict to ensure we have a valid set of haystack values.

    As the dict's internals are directly exposed calling this method will ensure all the values held in the dict are valid haystack values.

    Returns void

  • Merge multiple dicts into one.

    // The merged dict has all the tags from dict0, dict1 and dict2.
    const mergedDict = HDict.merge(dict0, dict1, dict2)

    Parameters

    • Rest ...dicts: HDict[]

      The dicts to merge.

    Returns HDict

    A merged dict.

Generated using TypeDoc