Toolkit

ArrayExtensions

The ArrayExtensions are included in the framework and offer some extensions to the Array object in javascript. The ArrayExtensions are inspired by Linq.

Sorting: OrderBy, OrderByDesc

{
    Init: function() {
        var items = [{ Id: 1, Name: "Number one" }, { Id: 3, Name: "Number three" }, { Id: 2, Name: "Number two" }];

        items = items.OrderBy(function(a){ return a.Id; });

        for (var i = 0; i < items.length; i++) {
            this.Add({
                innerHTML: items[i].Name
            });
        }
    }
}

Where

Filter array items

{
    Init: function() {
        var items = [{ Id: 1, Name: "A" }, { Id: 2, Name: "B" }, { Id: 3, Name: "C" }];

        items = items.Where(function(a){ return a.Id >= 2; });

        for (var i = 0; i < items.length; i++) {
            this.Add({
                innerHTML: items[i].Name
            });
        }
    }
}

Skip, Take

{
    Init: function() {
        var items = [{ Id: 1, Name: "A" }, { Id: 2, Name: "B" }, { Id: 3, Name: "C" }];

        items = items.Skip(2).Take(1);

        for (var i = 0; i < items.length; i++) {
            this.Add({
                innerHTML: items[i].Name
            });
        }
    }
}

Select

Transform existing array items into new items

{
    Init: function() {
        var items = [{ Id: 1, Name: "A" }, { Id: 2, Name: "B" }, { Id: 3, Name: "C" }];

        items = items.Select(function(a) { return a.Id + 10; });

        for (var i = 0; i < items.length; i++) {
            this.Add({
                innerHTML: items[i]
            });
        }
    }
}

Min, Max, Average

{
    Init: function() {
        var items = [{ Id: 1, Name: "A" }, { Id: 2, Name: "B" }, { Id: 3, Name: "C" }];        
        
        this.Add({
            innerHTML: "Min: "+ items.Min(function(a) { return a.Id })
        });
        this.Add({
            innerHTML: "Max: "+ items.Max(function(a) { return a.Id })
        });
        this.Add({
            innerHTML: "Average: "+ items.Average(function(a) { return a.Id })
        });
    }
}

Unique

{
    Init: function() {
        var items = [{ Name: "Double" }, { Name: "Double" }, { Name: "Single" }];        
        
        items = items.Unique(function(a) { return a.Name; });

        for (var i = 0; i < items.length; i++) {
            this.Add({
                innerHTML: items[i].Name
            });
        }
    }
}

Randomize

{
    Init: function() {
        var items = ["A","B","C","D","E","F","G","H","I","J"];        
        
        items = items.Randomize();

        for (var i = 0; i < items.length; i++) {
            this.Add({
                _: "span",
                innerHTML: items[i]
            });
        }
    }
}