Trilium Frontend API
    Preparing search index...

    Class Collection<T>

    Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes in the collection (like in an array) or by their ids.

    If an object without an id property is being added to the collection, the id property will be generated automatically. Note that the automatically generated id is unique only within this single collection instance.

    By default an item in the collection is identified by its id property. The name of the identifier can be configured through the constructor of the collection.

    Type Parameters

    • T extends Record<string, any>

      The type of the collection element.

    Hierarchy (View Summary)

    Implements

    Index
    • Creates a new Collection instance.

      You can pass a configuration object as the argument of the constructor:

      const emptyCollection = new Collection<{ name: string }>( { idProperty: 'name' } );
      emptyCollection.add( { name: 'John' } );
      console.log( collection.get( 'John' ) ); // -> { name: 'John' }

      The collection is empty by default. You can add new items using the #add method:

      const collection = new Collection<{ id: string }>();

      collection.add( { id: 'John' } );
      console.log( collection.get( 0 ) ); // -> { id: 'John' }

      Type Parameters

      • T extends Record<string, any>

        The type of the collection element.

      Parameters

      • Optionaloptions: { idProperty?: string }

        The options object.

        • Optional ReadonlyidProperty?: string

          The name of the property which is used to identify an item. Items that do not have such a property will be assigned one when added to the collection.

      Returns Collection<T>

      NO_ITEMS

    • Creates a new Collection instance with specified initial items.

      const collection = new Collection<{ id: string }>( [ { id: 'John' }, { id: 'Mike' } ] );

      console.log( collection.get( 0 ) ); // -> { id: 'John' }
      console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
      console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }

      You can always pass a configuration object as the last argument of the constructor:

      const nonEmptyCollection = new Collection<{ name: string }>( [ { name: 'John' } ], { idProperty: 'name' } );
      nonEmptyCollection.add( { name: 'George' } );
      console.log( collection.get( 'George' ) ); // -> { name: 'George' }
      console.log( collection.get( 'John' ) ); // -> { name: 'John' }

      Type Parameters

      • T extends Record<string, any>

        The type of the collection element.

      Parameters

      • initialItems: Iterable<T>

        The initial items of the collection.

      • Optionaloptions: { idProperty?: string }

        The options object.

        • Optional ReadonlyidProperty?: string

          The name of the property which is used to identify an item. Items that do not have such a property will be assigned one when added to the collection.

      Returns Collection<T>

      INITIAL_ITEMS

    • get first(): T

      Returns the first item from the collection or null when collection is empty.

      Returns T

    • get last(): T

      Returns the last item from the collection or null when collection is empty.

      Returns T

    • get length(): number

      The number of items available in the collection.

      Returns number

    • Iterable interface.

      Returns Iterator<T>

    • Adds an item into the collection.

      If the item does not have an id, then it will be automatically generated and set on the item.

      Parameters

      • item: T
      • Optionalindex: number

        The position of the item in the collection. The item is pushed to the collection when index not specified.

      Returns this

      add

      change

    • Adds multiple items into the collection.

      Any item not containing an id will get an automatically generated one.

      Parameters

      • items: Iterable<T>
      • Optionalindex: number

        The position of the insertion. Items will be appended if no index is specified.

      Returns this

      add

      change

    • Binds and synchronizes the collection with another one.

      The binding can be a simple factory:

      class FactoryClass {
      public label: string;

      constructor( data: { label: string } ) {
      this.label = data.label;
      }
      }

      const source = new Collection<{ label: string }>( { idProperty: 'label' } );
      const target = new Collection<FactoryClass>();

      target.bindTo( source ).as( FactoryClass );

      source.add( { label: 'foo' } );
      source.add( { label: 'bar' } );

      console.log( target.length ); // 2
      console.log( target.get( 1 ).label ); // 'bar'

      source.remove( 0 );
      console.log( target.length ); // 1
      console.log( target.get( 0 ).label ); // 'bar'

      or the factory driven by a custom callback:

      class FooClass {
      public label: string;

      constructor( data: { label: string } ) {
      this.label = data.label;
      }
      }

      class BarClass {
      public label: string;

      constructor( data: { label: string } ) {
      this.label = data.label;
      }
      }

      const source = new Collection<{ label: string }>( { idProperty: 'label' } );
      const target = new Collection<FooClass | BarClass>();

      target.bindTo( source ).using( ( item ) => {
      if ( item.label == 'foo' ) {
      return new FooClass( item );
      } else {
      return new BarClass( item );
      }
      } );

      source.add( { label: 'foo' } );
      source.add( { label: 'bar' } );

      console.log( target.length ); // 2
      console.log( target.get( 0 ) instanceof FooClass ); // true
      console.log( target.get( 1 ) instanceof BarClass ); // true

      or the factory out of property name:

      const source = new Collection<{ nested: { value: string } }>();
      const target = new Collection<{ value: string }>();

      target.bindTo( source ).using( 'nested' );

      source.add( { nested: { value: 'foo' } } );
      source.add( { nested: { value: 'bar' } } );

      console.log( target.length ); // 2
      console.log( target.get( 0 ).value ); // 'foo'
      console.log( target.get( 1 ).value ); // 'bar'

      It's possible to skip specified items by returning null value:

      const source = new Collection<{ hidden: boolean }>();
      const target = new Collection<{ hidden: boolean }>();

      target.bindTo( source ).using( item => {
      if ( item.hidden ) {
      return null;
      }

      return item;
      } );

      source.add( { hidden: true } );
      source.add( { hidden: false } );

      console.log( source.length ); // 2
      console.log( target.length ); // 1

      Note: #clear can be used to break the binding.

      Type Parameters

      • S extends Record<string, any>

        The type of externalCollection element.

      Parameters

      • externalCollection: Collection<S>

        A collection to be bound.

      Returns CollectionBindToChain<S, T>

      The binding chain object.

    • Removes all items from the collection and destroys the binding created using #bindTo.

      Returns void

      remove

      change

    • Returns an array with items for which the callback returned a true value.

      Parameters

      • callback: (item: T, index: number) => boolean
      • Optionalctx: any

        Context in which the callback will be called.

      Returns T[]

      The array with matching items.

    • Finds the first item in the collection for which the callback returns a true value.

      Parameters

      • callback: (item: T, index: number) => boolean
      • Optionalctx: any

        Context in which the callback will be called.

      Returns T

      The item for which callback returned a true value.

    • Performs the specified action for each item in the collection.

      Parameters

      • callback: (item: T, index: number) => unknown
      • Optionalctx: any

        Context in which the callback will be called.

      Returns void

    • Gets an item by its ID or index.

      Parameters

      • idOrIndex: string | number

        The item ID or index in the collection.

      Returns T

      The requested item or null if such item does not exist.

    • Gets an index of an item in the collection. When an item is not defined in the collection, the index will equal -1.

      Parameters

      • itemOrId: string | T

        The item or its ID in the collection.

      Returns number

      The index of a given item.

    • Returns a Boolean indicating whether the collection contains an item.

      Parameters

      • itemOrId: string | T

        The item or its ID in the collection.

      Returns boolean

      true if the collection contains the item, false otherwise.

    • Executes the callback for each item in the collection and composes an array or values returned by this callback.

      Type Parameters

      • U

        The result type of the callback.

      Parameters

      • callback: (item: T, index: number) => U
      • Optionalctx: any

        Context in which the callback will be called.

      Returns U[]

      The result of mapping.

    • Removes an item from the collection.

      Parameters

      • subject: string | number | T

        The item to remove, its ID or index in the collection.

      Returns T

      The removed item.

      remove

      change