Classes

There are several classes defined in this library:

Linq

This class implements the IEnumerable<T> interface which is the following:

interface IEnumerable<TItem> {

    //
    //  Returns one element which is fitting the expression. 
    //  If no expression was defined, it takes the only argument in list. In this case there can be only one element in the list, otherwise an error is thrown.
    //  If nothing is found or not item is in the list, or the expression matches to more than one element, the method throws an error.
    //
    single(expr?: (item: TItem, index: number) => boolean): TItem

    //
    //  Returns the first element which is fitting the expression. If no expression was defined, it takes the first argument. If nothing is found or not item is in the list, the method throws an error.
    //
    first(expr?: (item: TItem, index: number) => boolean): TItem;

    //
    //  Returns the first item, or when a expression is given it returns the first item which is matching. If no items are existing
    //  or no item is matching, the method returns null.
    //
    firstOrDefault(expr?: (item: TItem, index: number) => boolean): TItem;

    //
    //  Returns the last element which is fitting the expression. If no expression was defined, it takes the last item in the current list. 
    //  If nothing is found or not item is in the list, the method throws an error.
    //
    last(expr?: (item: TItem, index: number) => boolean): TItem;

    //
    //  Returns the last element which is fitting the expression. If no expression was defined, it takes the last item in the current list. 
    //  If nothing is found or not item is in the list, the method returns null.
    //
    lastOrDefault(expr?: (item: TItem, index: number) => boolean): TItem;

    //
    //  Returns an value which indicates wherever the given value does exists in the collection. When a comparer is specified,
    //  it is used for comparing the elements while looping.
    //
    contains(value: TItem, comparer?: IComparer<TItem>): boolean;

    //
    //  Counts all elements. If an expression is given, the system counts all elements which are fitting this expression.
    //
    count(expr?: (item: TItem, index: number) => boolean): number;

    //
    //  Iterates over all items and runs the given function. If the function returns false, the enumeration is stopped. 
    //  The method returns true if the enumeration was not stopped, if the enumeration was stopped, the method returns false.
    //
    forEach(toDo: (item: TItem, index: number) => any): boolean;

    //
    //  Returns a list of values which are produced by the mapper function for every containing item.
    //
    select<TResult>(mapper: (item: TItem, index: number) => TResult): IEnumerable<TResult>;

    //
    //  Returns a value wherever the enumeration contains a item which matches the given expression. If no expression is given,
    //  the method returns true when there is any item the collection.
    //
    any(expr?: (item: TItem, index: number) => boolean): boolean;

    //
    //  Returns the current items as an array.
    //
    toArray(): TItem[]

    //
    //  Returns the index of the given item. If an comparer was specified, it is used for finding the item. 
    //  The method returns the 0 based index of the item. If it does not exists, the method returns -1.
    //
    indexOf(item: TItem, comparer?: IComparer<TItem>): number;

    //
    //  Returns all elements which are fitting the specified condition.
    //
    where(expr: (item: TItem, index: number) => boolean): IEnumerable<TItem>;

    //  
    //  Casts all elements in the list and returns a new one. If an mapper function is specified, this is used for generating the output.
    //
    cast<TResult>(mapMethod?: (item: TItem) => TResult): Linq<TResult>;

    //
    //  Aggregates the current values with the given aggregate method.
    //  If seed is undefined, the first entry of the current list is the first seeding value and will not enumerated, otherwise the aggregate method
    //  starts with the first entry in the current list by using the value of seed as the first aggregate value.
    //
    aggregate<TResult>(func: (item1: TResult, item2: TItem) => TItem, seed?: TResult): TResult;

    //  
    //  Returns an error without the elements on the index until count is reached. 
    //
    skip(count: number): Linq<TItem>;

    //
    //  Returns the element at the given position.
    //
    elementAt(position: number): TItem;

    //
    //  Returns the element at the given position. If the position is not valid, it returns null.
    //
    elementAtOrDefault(position: number, defaultValue?: TItem): TItem;

    //
    //  Produces the set intersection of two sequences
    //
    intersect(intersectList: TItem[], comparer?: IComparer<TItem>): IEnumerable<TItem>;

    //
    //  Joins the two lists by adding the elements. When a zipper was specified, this method is used to join two elements.
    //
    zip<TResult>(zipWith: TItem[], zipper?: (item1: TItem, item2: TItem) => TResult): Linq<TItem>;

    //
    //  Returns the minimal value of all items. You can specify a comparer for doing the comparision.
    //
    min(comparer?: IComparer<TItem>): TItem;

    //
    //  Returns the maximal value of all items. You can specify a comparer for doing the comparision.
    //
    max(comparer?: IComparer<TItem>): TItem;

    //
    //  Conats the two lists.
    //
    concat(withSource: TItem[]): IEnumerable<TItem>;

    //
    //  Returns a distinct list of all elements.
    //
    distinct(comparer?: IComparer<TItem>): Linq<TItem>;

    //
    //  Returns the first count elements of the list.
    //
    take(count: number): IEnumerable<TItem>;

    //
    //  Returns a distinct list of the join of both lists, so every element is containing only one times.
    //
    union(
        listToAdd: any,
        comparer?: IComparer<TItem>): Linq<TItem>;

    // 
    //  Returns a grouped list of the given items. The groupMapper parameter contains a method where the grouped output
    //  is generated. The optional key mapper and key comparer helps generating an comparing the key which are grouped. If this
    //  is empty, the key is a string of json which represents the item. If the keyMapper does not return a string (an object or array instead)
    //  the key is mapped to a json string and then it gets compared as the key.
    //  The groupJoiner methods gets called after every call to the groupMapper with the value from the previous call for this method. This method
    //  gets only called when there are more than one entry. This method helps joining the grouped data for aggregation purposes.
    //
    groupBy<TKey, TGrouped>(groupMapper: (item: TItem, index: number) => TGrouped,
        groupAggregateJoiner: (groupItem1: TGrouped, groupItem2: TGrouped) => TGrouped,
        keyMapper?: (item: TItem, index: number) => TKey,
        keyComparer?: IComparer<TKey>): IEnumerable<TGrouped>;

    //
    //  Groups the elements into a dictionary. The indexer method creates the key for the dictionary.
    //
    toDictionary<TKey>(
        indexer: (item: TItem, index: number) => TKey,
        keyComparer?: IComparer<TKey>): Dictionary<TKey, TItem[]>;

    //
    //  Orders the items by the given expression. You can specify a comparer for comparing the resulting expression values.
    //
    orderBy<TKey>(
        orderByKeyMethod: (item: TItem, index: number) => TKey,
        comparer?: IComparer<TKey>): IEnumerable<TItem>;

    //
    //  Orders the items descending by the given expression. You can specify a comparer for comparing the resulting expression values.
    //
    orderByDescending<TKey>(
        orderByKeyMethod: (item: TItem, index: number) => TKey,
        comparer?: IComparer<TKey>): IEnumerable<TItem>;

    //
    //  Returns a flatten list of values which are produced by the mapper function for every containing item.
    //
    selectMany<TResult>(mapper: (item: TItem, index: number) => TResult): IEnumerable<TResult>;

    //
    //  Returns an value which indicates wherever all elements are matching the given expression.
    //
    all(expression: (item: TItem, index: number) => boolean): boolean;

    //
    //  Returns the given default IEnumerable when the current collection is empty.
    //
    defaultIfEmpty(defaultValue: IEnumerable<TItem>): IEnumerable<TItem>;
}

List

This is a implementation of the List from C#. Here is the interface definition:

interface IList<TItem> extends IEnumerable<TItem> {
    //  
    //  Clears all items in the dictionary.
    //
    clear(): void;

    // 
    // Adds a new item to the list.
    push(item: TItem);

    //
    //  Adds all elements to the current list
    //
    addRange(items: TItem[]);

    //
    //  Inserts an item on the given position. If no position is given, the item is added as the first item in the collection.
    //
    insert(item: TItem, position?: number);

    //
    //  Removes the first occurrence of a specific object. If the object was not found, an error is thrown.
    //
    remove(item: TItem, comp?: IComparer<TItem>): TItem;

    //  
    //  Removes the element on the specified position.
    //
    removeAt(position: number): TItem;
}


As you can see, List implements IEnumerable so it derrives from Linq.

KeyValuePair

This is used for a pair of keys and values and is the base for the Dictinary implementation. Here is the interface definition for this class:

interface IKeyValuePair<TKey, TValue> {
    key: TKey;
    value: TValue;
}

KeyValueComparerForKey<TKey, TValue> and KeyValueComparerForValue<TKey, TValue>

This are helper comparers for comparing keys / values of a key value pair. Here is the interface definition for IComparer interface:

interface IComparer<TItem> {
    //  
    //  Returns a value which indicates wherever item1 is bigger, smaller or equal with item2. If -1 is returned, item1 is smaller, if 0 is returned, both are equal and if 1 is returned, item1 is bigger than item2.
    //
    compare(item1: TItem, item2: TItem): number;
}

Both of them taking as first argument in constructor another IComparer intstance which then helps you to compare the key / value byitself. This is optional.

Dictionary<TKey, TValue>

An implementation similar to the C# Dictionary. Here is the interface for this class:

interface IDictionary<TKey, TValue> extends IList<IKeyValuePair<TKey, TValue>> {

    //
    //  Adds a new entry. 
    //
    add(key: TKey, value: TValue): void;
    //
    //  Returns whenever the key was added to the collection. Optionally you can add a comparer to find the correct key.
    // 
    containsKey(key: TKey, comparer?: IComparer<TKey>): boolean;

    //
    //  Removes the entry with the specified key.
    //
    removeKey(key: TKey, comp?: IComparer<TKey>): TValue;

    //
    //  Returns the first element index of the entry with the matching key.
    //
    indexOfKey(key: TKey, comp?: IComparer<TKey>): number;

    //
    //  tries returning a value with a specific key. If the value does not exists, the method returns null.
    //
    tryGetValue(key: TKey, comp?: IComparer<TKey>): TValue;

    //
    //  Returns an array of all values in the dictionary.
    //
    getValues(): TValue[];

    //  
    //  Returns an array of all keys.
    //
    getKeys(): TKey[];
}


As you can see, Dictionary is an IList implementation so it derrives from List.

JsonStringComparer

The JsonStringComparer helps comparing object of type any. It converts the objects into a JSON string and then it compares the resulting string. Using this comparer helps only when you are sure, that the objects to compare has the same structure and initializing sequence.

So this will compare the objects to 0 (so they are equal):
Object1:
{ name: 'Michael', lastname: 'Baarz' }
Object2
{ name: 'Michael', lastname: 'Baarz' }


But these objects are not equal by using the comparer:

Object1:
{ name: 'Michael', lastname: 'Baarz' }
Object2
{ lastname: 'Baarz', name: 'Michael' }


This happens because the fields are not initialized in the same order.

Last edited May 12, 2014 at 2:58 PM by mbaarz, version 7