Options
All
  • Public
  • Public/Protected
  • All
Menu

The ItemList class collects items and then arranges them into an array by priority.

Type parameters

  • T

Hierarchy

  • ItemList

Index

Constructors

constructor

Properties

Protected _items

_items: Record<string, Item<T>> = {}

The items in the list.

Accessors

items

Methods

add

  • add(key: string, content: T, priority?: number): ItemList<T>
  • Add an item to the list.

    Parameters

    • key: string

      A unique key for the item.

    • content: T

      The item's content.

    • priority: number = 0

      The priority of the item. Items with a higher priority will be positioned before items with a lower priority.

    Returns ItemList<T>

Private createItemContentProxy

  • createItemContentProxy<C>(content: C, key: string): Readonly<C & { itemName: string }>
  • Proxies an item's content, adding the itemName readonly property to it.

    example

    createItemContentProxy({ foo: 'bar' }, 'myItem'); // { foo: 'bar', itemName: 'myItem' }

    internal

    Type parameters

    • C: object

    Parameters

    • content: C

      The item's content (objects only)

    • key: string

      The item's key

    Returns Readonly<C & { itemName: string }>

    Proxied content

get

  • get(key: string): T

getPriority

  • getPriority(key: string): number

has

  • has(key: string): boolean

isEmpty

  • isEmpty(): boolean

merge

remove

replace

  • replace(key: string, content?: null | T, priority?: null | number): ItemList<T>
  • Replace an item and/or priority in the list, only if it is already present.

    If content or priority are null, these values will not be replaced.

    If the provided key is not present, nothing will happen.

    deprecated

    Please use the ItemList.setContent and ItemList.setPriority methods to replace items and their priorities. This method will be removed in Flarum 2.0.

    example

    Replace priority and not content. items.replace('myItem', null, 10);

    example

    Replace content and not priority. items.replace('myItem',

    My new value.

    );

    example

    Replace content and priority. items.replace('myItem',

    My new value.

    , 10);

    Parameters

    • key: string

      The key of the item in the list

    • content: null | T = null

      The item's new content

    • priority: null | number = null

      The item's new priority

    Returns ItemList<T>

setContent

  • setContent(key: string, content: T): ItemList<T>
  • Replaces an item's content, if the provided item key exists.

    If the provided key is not present, nothing will happen.

    example

    Replace item content. items.setContent('myItem',

    My new value.

    );

    example

    Replace item content and priority. items .setContent('myItem',

    My new value.

    ) .setPriority('myItem', 10);

    throws

    If the provided key is not present in the ItemList.

    Parameters

    • key: string

      The key of the item in the list

    • content: T

      The item's new content

    Returns ItemList<T>

setPriority

  • setPriority(key: string, priority: number): ItemList<T>
  • Replaces an item's priority, if the provided item key exists.

    If the provided key is not present, nothing will happen.

    example

    Replace item priority. items.setPriority('myItem', 10);

    example

    Replace item priority and content. items .setPriority('myItem', 10) .setContent('myItem',

    My new value.

    );

    throws

    If the provided key is not present in the ItemList.

    Parameters

    • key: string

      The key of the item in the list

    • priority: number

      The item's new priority

    Returns ItemList<T>

toArray

  • toArray(keepPrimitives?: false): (T & { itemName: string })[]
  • toArray(keepPrimitives: true): (T extends object ? T & Readonly<{ itemName: string }> : T)[]
  • Convert the list into an array of item content arranged by priority.

    This does not preserve the original types of primitives and proxies all content values to make itemName accessible on them.

    NOTE: If your ItemList holds primitive types (such as numbers, booleans or strings), these will be converted to their object counterparts if you do not provide true to this function.

    NOTE: Modifying any objects in the final array may also update the content of the original ItemList.

    see

    https://github.com/flarum/core/issues/3030

    Parameters

    • Optional keepPrimitives: false

      Converts item content to objects and sets the itemName property on them.

    Returns (T & { itemName: string })[]

  • Convert the list into an array of item content arranged by priority.

    Content values that are already objects will be proxied and have itemName accessible on them. Primitive values will not have the itemName property accessible.

    NOTE: Modifying any objects in the final array may also update the content of the original ItemList.

    Parameters

    • keepPrimitives: true

      Converts item content to objects and sets the itemName property on them.

    Returns (T extends object ? T & Readonly<{ itemName: string }> : T)[]

toObject

  • A read-only map of all keys to their respective items in no particular order.

    We don't allow adding new items to the ItemList via setting new properties, nor do we allow modifying existing items directly. You should use the ItemList.add, ItemList.setContent and ItemList.setPriority methods instead.

    To match the old behaviour of the ItemList.items property, call Object.values(ItemList.toObject()).

    example

    const items = new ItemList(); items.add('b', 'My cool value', 20); items.add('a', 'My value', 10); items.toObject(); // { // a: { content: 'My value', priority: 10, itemName: 'a' }, // b: { content: 'My cool value', priority: 20, itemName: 'b' }, // }

    Returns DeepReadonly<Record<string, IItemObject<T>>>

Generated using TypeDoc version 0.22.10