This project has moved. For the latest updates, please go here.
1
Vote

Improvement suggestions

description

  • We need a ForEach() method:
    ForEach: function(clause) {
        for (var index = 0; index < this.items.length; index++) {
            clause(this.items[index]);
        }
        return this.items;
    }
    
  • Using this.items.length like in the code above might not be the best in terms of performance. A local variable holding the count might be better.
  • I consider this a bug: https://jslinq.codeplex.com/workitem/7820#
  • The init() function would be better like so:
    init: function(dataItems) {
        this.items = dataItems || [];
    },
    
  • First and Last and not optimized enough.
  • Those two should also accept a clause:
    FirstOrDefault: function(defaultValue) {
        return this.First() || defaultValue;
    },
    LastOrDefault: function(defaultValue) {
        return this.Last() || defaultValue;
    },
    
  • Count should be like that:
    Count: function(clause) {
        if (!clause)
            return this.items.length;
        else
            return this.Where(clause).items.length;
    },
    
  • In OrderBy and OrderByDescending you copy the array like so:
        var tempArray = new Array();
        for (var i = 0; i < this.items.length; i++) {
            tempArray[tempArray.length] = this.items[i];
        }
    I think it would be better like so:
        var tempArray =  this.items.slice(0);
    I think slice is well supported cross-browser: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
I think this library should not die.
Can I get inside this project?
I am not a JS expert, but I can't help seeing this like that.
I don't like other libraries like underscore or lazy.js.

file attachments

comments

Konstantinos wrote May 1, 2015 at 4:07 PM

I attach here a fixed version.
Cheers! :)
Let me know if I can get in the project. :)
I always wanted to replace the whole JS mess with .NET-like-style methods.
Next stop the String object... :D

Konstantinos wrote May 1, 2015 at 4:21 PM

But there are more issues that I have not fixed yet:
  • Intersect seems to produce duplicates, although I have not tried it. I have just seem its code and it looks wrong to me.
  • There is no Union method.

Konstantinos wrote May 1, 2015 at 4:24 PM

Cancel that about Intersect.
It is good, just donno if it should remove duplicates from within the initial this.items array...
Probably it should.