File

packages/core/src/lib/collection/collection.ts

Description

A Collection is a more sophisticated Array. It is fundamental for other classes like List.

Index

Properties
Methods

Constructor

constructor(items: Array)

Constructs the collection with the given item Array (optional).

Parameters :
Name Type Optional
items Array<T> No
Example :

```typescript

  • const numbers = new Collection([1, 2, 3]);
  • ```

Properties

Public items
Type : Array<T>

The items must all have the same type T.

Protected update
Type : Subject<Collection<T>>
Default value : new Subject()

Subject that is nexted when the items update

Public update$
Type : Observable<Collection<T>>
Default value : this.update.asObservable()

Subject that is nexted when the items change

Methods

add
add(item: T, unique?: boolean, event: boolean)

Adds the given item to the Collection. If the unique flag is set, the item will only be added if it is not contained.

Parameters :
Name Type Optional Default value
item T No
unique boolean Yes
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.add(4);</li>
<li>```</li>
</ul>
Returns : boolean
addAll
addAll(items: Array, unique: boolean, event: boolean)

Adds the given items to the Collection. If the unique flag is set, only items that are not contained will be added.

Parameters :
Name Type Optional Default value
items Array<T> No []
unique boolean No false
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.addAll([5, 6, 7]);</li>
<li>```</li>
</ul>
Returns : void
has
has(item: T)

Checks if the Collection contains the given item.

Parameters :
Name Type Optional
item T No
Example :
```typescript</p>
<ul>
<li>numbers.has(2); //true</li>
<li>```</li>
</ul>
Returns : boolean
hasAll
hasAll(items: Array)

Checks if the Collection contains all given items.

Parameters :
Name Type Optional Default value
items Array<T> No []
Example :
```typescript</p>
<ul>
<li>numbers.has([1,2]); //true</li>
<li>```</li>
</ul>
Returns : boolean
index
index(item: T)

Returns the index of the given item

Parameters :
Name Type Optional
item T No
Returns : number
isEmpty
isEmpty()

Returns true if the collection is empty

Returns : boolean
move
move(item: T, index: number, event: boolean)

Moves the given item to the given array index.

Parameters :
Name Type Optional Default value
item T No
index number No
event boolean No true
Returns : void
remove
remove(item: T, event: boolean)

Removes the given item from the Collection.

Parameters :
Name Type Optional Default value
item T No
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.remove(4);</li>
<li>```</li>
</ul>
Returns : boolean
removeAll
removeAll(items?: Array, event: boolean)

Removes all items from the Collection.

Parameters :
Name Type Optional Default value
items Array<T> Yes
event boolean No true
Example :
```typescript</p>
<ul>
<li>numbers.removeAll();</li>
<li>```</li>
</ul>
Returns : void
replaceWith
replaceWith(items: Array, event: boolean)

Replaces all current items with the given items.

Parameters :
Name Type Optional Default value
items Array<T> No
event boolean No true
Returns : void
toggle
toggle(item: T, event: boolean)

Toggles the item in and out of collection

Parameters :
Name Type Optional Default value
item T No
event boolean No true
Returns : void

Collection

Collection is a more sophisticated Array:

const collection = new Collection([1, 2, 3]);
collection.has(1); //true
collection.has(4); //false
collection.add(4);
collection.remove(4);
collection.index(1); //0
collection.addAll([10,11,12]); //true
collection.hasAll([1,10,11]); //true
collection.removeAll();

It is the Parent Class of List and Selection.

import { Observable, Subject } from 'rxjs';

/**
 * A Collection is a more sophisticated Array. It is fundamental for other classes like List.
 */
export class Collection<T> {
  /**
   * The items must all have the same type T.
   */
  public items: Array<T>;
  /** Subject that is nexted when the items update */
  protected update: Subject<Collection<T>> = new Subject();
  /** Subject that is nexted when the items change */
  public update$: Observable<Collection<T>> = this.update.asObservable();

  /**
   * Constructs the collection with the given item Array (optional).
   * @example
   * ```typescript
   *  const numbers = new Collection([1, 2, 3]);
   * ```
   */
  constructor(items: Array<T> = []) {
    this.items = [];
    items.forEach((item) => {
      this.items.push(item);
    });
  }

  /** Returns the index of the given item */
  index(item: T): number {
    return this.items.indexOf(item);
  }

  /**
   * Checks if the Collection contains the given item.
   * @example
   * ```typescript
   * numbers.has(2); //true
   * ```
   */
  has(item: T): boolean {
    return this.index(item) !== -1;
  }

  /**
   * Checks if the Collection contains all given items.
   * @example
   * ```typescript
   * numbers.has([1,2]); //true
   * ```
   */
  hasAll(items: Array<T> = []): boolean {
    if (items === null) {
      // console.warn('has all fail', this, items);
      return false;
    }
    return items.reduce((has, item) => {
      return has && this.has(item);
    }, true);
  }

  /**
   * Adds the given item to the Collection. If the unique flag is set, the item will only be added
   * if it is not contained.
   * @example
   * ```typescript
   * numbers.add(4);
   * ```
   */
  add(item: T, unique?: boolean, event: boolean = true) {
    if (unique && this.has(item)) {
      return false;
    }
    this.items.push(item);
    if (event) {
      this.update.next(this);
    }
  }

  /**
   * Adds the given items to the Collection. If the unique flag is set, only items that are not
   * contained will be added.
   * @example
   * ```typescript
   * numbers.addAll([5, 6, 7]);
   * ```
   */
  addAll(items: Array<T> = [], unique: boolean = false, event: boolean = true) {
    const length = this.items.length;
    items.forEach((item) => {
      this.add(item, unique, false);
    });
    if (this.items.length > length && event) {
      this.update.next(this);
    }
  }

  /**
   * Removes the given item from the Collection.
   * @example
   * ```typescript
   * numbers.remove(4);
   * ```
   */
  remove(item: T, event: boolean = true) {
    if (!this.has(item)) {
      return false;
    }
    this.items.splice(this.index(item), 1);
    if (event) {
      this.update.next(this);
    }
  }

  /**
   * Removes all items from the Collection.
   * @example
   * ```typescript
   * numbers.removeAll();
   * ```
   */
  removeAll(items?: Array<T>, event: boolean = true) {
    const length = this.items.length;
    if (items) {
      items.forEach((item) => {
        this.remove(item, false);
      });
    } else {
      this.items.length = 0;
    }
    if (this.items.length < length && event) {
      this.update.next(this);
    }
  }

  /** Toggles the item in and out of collection */
  toggle(item: T, event: boolean = true) {
    if (this.has(item)) {
      this.remove(item, event);
    } else {
      this.add(item, event);
    }
  }

  /** Replaces all current items with the given items. */
  replaceWith(items: Array<T>, event: boolean = true) {
    if (this.items && this.items.length) {
      this.removeAll(undefined, false);
    }
    if (items.length) {
      this.addAll(items, false, false);
    }
    if (event) {
      this.update.next(this);
    }
  }

  /** Returns true if the collection is empty */
  isEmpty() {
    return this.items.length === 0;
  }

  /** Moves the given item to the given array index. */
  move(item: T, index: number, event: boolean = true) {
    if (!this.has(item) || this.items.indexOf(item) === index) {
      return;
    }
    this.items.splice(index, 0, this.items.splice(this.items.indexOf(item), 1)[0]);
    if (event) {
      this.update.next(this);
    }
  }
}

result-matching ""

    No results matching ""