Class List<I, P>Abstract

Description

The List extensions is intended for solving problems associated with fast O(1) search for objects by given keys and fast iteration of a collection of objects, multi-index and objects-factory supported.

Features:

Disadvantages:

  • Slow inserting new document (especially on large collections, inserting into Map + Set)
  • Slow delete (especially with sequence number index structures, because reindex all collection needed)

Example

import * as collection from '@biorate/collection';
const { embed } = collection;

class Item extends collection.Item {
@embed(Item.Int) public id: number = null;
@embed(Item.String) public title: string = null;
}

class List extends collection.List<Item> {
protected get _keys() {
return [['id']];
}

protected get _Item() {
return Item;
}
}

const list = new List([{ id: 1, title: 'one' }, { id: 2, title: 'two' }]);

list.set({ id: 3, title: 'three' }, { id: 4, title: 'four' });

console.log(list.find(1)); // Item { id: 1, title: 'one' }
console.log(list.find(2)); // Item { id: 2, title: 'two' }
console.log(list.find(3)); // Item { id: 3, title: 'three' }
console.log(list.find(4)); // Item { id: 4, title: 'four' }

Type Parameters

  • I = any

  • P = {
        parent?: any;
    }

Hierarchy

Constructors

  • Type Parameters

    • I = any

    • P = {
          parent?: any;
      }

    Parameters

    • items: any[] = []

      initialization items

    • parent: P = null

      parent item

    Returns List<I, P>

    Example

    import * as collection from '@biorate/collection';

    class List extends collection.List<{ id: number }> {
    protected get _keys() {
    return [['id']];
    }
    }

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);

Properties

[_map]: Map<string, I> = ...

Description

Key combinations storage

[_processed]: Record<string, any> = null

Description

Current processing item alias, useful for dynamic class substitution in _Item getter

[_set]: Set<I> = ...

Description

Unique items storage

Accessors

  • get _Item(): Ctor<I>
  • Returns Ctor<I>

    Description

    Object instantiation. The constructor from which the instance will be created must be passed here. If null is passed, the object that was passed will be added to the collection.

    Default

    **null**
    

    Example

    class List extends collection.List<{ id: number }> {
    protected get _keys() {
    return [['id']];
    }

    protected get _Item() {
    return null;
    }
    }

    const list = new List([{ id: 1 }, { id: 2 }]);
    console.log(list.find(1)); // { id: 1 }
    console.log(list.find(1)); // { id: 2 }

    Example

    import * as collection from '@biorate/collection';
    const { embed } = collection;

    class Item extends collection.Item {
    @embed(Item.Int) public id: number = null;
    @embed(Item.String) public title: string = null;
    }

    class List extends collection.List<Item> {
    protected get _keys() {
    return [['id']];
    }

    protected get _Item() {
    return Item;
    }
    }

    const list = new List([{ id: 1, title: 'Cat' }, { id: 2, title: 'Dog' }]);
    console.log(list.find(1)); // Item { id: 1, title: 'Cat' }
    console.log(list.find(2)); // Item { id: 2, title: 'Dog' }
  • get _keys(): Keys
  • Returns Keys

    Description

    Indexes map. Designed to declare fields by which indexes will be built.

    Example

    class List extends collection.List<{ a: number; b: number; c: number }> {
    protected get _keys() {
    return [
    ['a', 'b'],
    ['b', 'c'],
    ];
    }
    }

    const list = new List([
    { a: 1, b: 1, c: 3 },
    { a: 2, b: 2, c: 2 },
    { a: 3, b: 1, c: 1 },
    ]);

    console.log(list.get(1, 1)); // [ { a: 1, b: 1, c: 3 }, { a: 3, b: 1, c: 1 } ]
    console.log(list.get(2, 2)); // [ { a: 2, b: 2, c: 2 } ]
  • get _processed(): Record<string, any>
  • Returns Record<string, any>

    Description

    Alias to current processed item, to private #processed property. Useful for dynamic class substitution in _Item getter

    Example

    class Base extends collection.Item {
    id: number = null;
    type: string = null;
    }
    class One extends Base {}
    class Two extends Base {}
    class Three extends Base {}

    const Types = { One, Two, Three }

    class List extends collection.List<Base> {
    protected get _keys() {
    return [['id']];
    }

    protected get _Item() {
    return Types[this.processed.type];
    }
    }

    const list = new List([
    { id: 1, type: 'One' },
    { id: 2, type: 'Two' },
    { id: 3, type: 'Three' }
    ]);

    console.log(list.find(1)); // One { id: 1, type: 'One' }
    console.log(list.find(2)); // Two { id: 2, type: 'Two' }
    console.log(list.find(3)); // Three { id: 3, type: 'Three' }
  • get size(): number
  • Returns number

    Description

    Unique collection items count

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    console.log(list.size); // 3
  • get index(): symbol
  • Returns symbol

    Description

    Alias for index-symbol, if used in _keys getter - enumerate and index items with sequence number.

    Example

    import { List as Base } from '@biorate/collection';

    class List extends Base<{ id: number }> {
    protected get _keys() {
    return [[Base.index]];
    }
    }

    const list = new List([{ id: 3 }, { id: 2 }, { id: 1 }]);

    console.log(list.find(0)); // { id: 3, [Symbol(Props.Index)]: 0 }
    console.log(list.find(1)); // { id: 2, [Symbol(Props.Index)]: 1 }
    console.log(list.find(2)); // { id: 1, [Symbol(Props.Index)]: 2 }

Methods

  • Returns Generator<I, any, unknown>

    Description

    Instance can be iterated by unique items storage

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    for (const item of list)
    console.log(item);
  • Returns void

    Description

    Clear collection

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    console.log(list.size); // 3
    list.clear();
    console.log(list.size); // 0
  • Parameters

    • Rest ...args: any[]

      key values

      Rest

    Returns boolean

    Description

    Delete item from collection by keys

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    list.delete(1); // true
    console.log(list); // [{ id: 2 }, { id: 3 }];
  • Parameters

    • Rest ...args: any[]

      key values

      Rest

    Returns I

    Description

    Find first item in collection by keys

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    list.find(1); // { id: 1 }
  • Parameters

    • Rest ...args: any[]

      key values

      Rest

    Returns I[]

    Description

    Find all items in collection by keys

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    list.get(1); // [{ id: 1 }]
  • Parameters

    • criteria: Record<string | symbol, any>

      query params

    • one: boolean = false

      return only first item?

    Returns I[]

    Description

    Get all items by criteria

    Example

    const list = new List([{ id: 1, a: 1 }, { id: 2, a: 1 }, { id: 3, a: 2 }]);
    list.getBy({ a: 1 }); // [{ id: 1, a: 1 }, { id: 2, a: 1 }]
    list.getBy({ id: 1, a: 1 }); // [{ id: 1, a: 1 }]
  • Parameters

    • Rest ...args: any[]

      key values

      Rest

    Returns boolean

    Description

    Check item exists in collection by keys

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    list.has(1); // true
    list.has(4); // false
  • Parameters

    • items: any[] = []

    Returns void

    Description

    Initialize list

    Example

    const list = new List();
    list.initialize([{ a: 1 }, { a: 2 }])
    console.log([...list]); // [{ a: 1 }, { a: 2 }]
    list.initialize([{ a: 3 }, { a: 4 }])
    console.log([...list]); // [{ a: 3 }, { a: 4 }]
  • Parameters

    • Rest ...args: any[]
      Rest

    Returns I[]

    Description

    Add item into collection

    Example

    const list = new List();
    list.set({ id: 1 }, { id: 2 }, { id: 3 });

Generated using TypeDoc