JSLint Cleanup

Apr 13, 2010 at 9:40 PM

Ran the code through JSLint. Also fixed some minor issues with the use of  "new".

//-----------------------------------------------------------------------
// Part of the LINQ to JavaScript (JSLINQ) v2.10 Project - http://jslinq.codeplex.com
// Copyright (C) 2009 Chris Pietschmann (http://pietschsoft.com). All rights reserved.
// This project is licensed under the Microsoft Reciprocal License (Ms-RL)
// This license can be found here: http://jslinq.codeplex.com/license
//-----------------------------------------------------------------------
(function() {
    JSLINQ = window.JSLINQ = function(dataItems) {
        return new JSLINQ.fn.init(dataItems);
    };
    JSLINQ.fn = JSLINQ.prototype = {
        init: function(dataItems) {
            this.items = dataItems;
        },

        // The current version of JSLINQ being used
        jslinq: "2.10",

        ToArray: function() { return this.items; },
        Where: function(clause) {
            var newArray = [];

            // The clause was passed in as a Method that return a Boolean
            for (var index = 0; index < this.items.length; index++) {
                if (clause(this.items[index], index)) {
                    newArray[newArray.length] = this.items[index];
                }
            }
            return JSLINQ(newArray);
        },
        Select: function(clause) {
            var newArray = [];

            // The clause was passed in as a Method that returns a Value
            for (var i = 0; i < this.items.length; i++) {
                var result = clause(this.items[i]);
                if (result) {
                    newArray[newArray.length] = result;
                }
            }
            return JSLINQ(newArray);
        },
        OrderBy: function(clause) {
            var tempArray = [];
            for (var i = 0; i < this.items.length; i++) {
                tempArray[tempArray.length] = this.items[i];
            }
            return JSLINQ(tempArray.sort(function(a, b) {
                var x = clause(a);
                var y = clause(b);
                return ((x < y) ? -1 : ((x > y) ? 1 : 0));
            })
        );
        },
        OrderByDescending: function(clause) {
            var tempArray = [];
            for (var i = 0; i < this.items.length; i++) {
                tempArray[tempArray.length] = this.items[i];
            }
            return JSLINQ(tempArray.sort(function(a, b) {
                var x = clause(b);
                var y = clause(a);
                return ((x < y) ? -1 : ((x > y) ? 1 : 0));
            })
        );
        },
        SelectMany: function(clause) {
            var r = [];
            for (var i = 0; i < this.items.length; i++) {
                r = r.concat(clause(this.items[i]));
            }
            return JSLINQ(r);
        },
        Count: function(clause) {
            return (clause === null || clause === undefined) ? this.items.length : this.Where(clause).items.length;
        },
        Distinct: function(clause) {
            var item;
            var dict = {};
            var retVal = [];
            for (var i = 0; i < this.items.length; i++) {
                item = clause(this.items[i]);
                // TODO - This doens't correctly compare Objects. Need to fix this
                if (dict[item] === null || dict[item] === undefined) {
                    dict[item] = true;
                    retVal[retVal.length] = item;
                }
            }
            dict = null;
            return JSLINQ(retVal);
        },
        Any: function(clause) {
            for (var index = 0; index < this.items.length; index++) {
                if (clause(this.items[index], index)) { return true; }
            }
            return false;
        },
        All: function(clause) {
            for (var index = 0; index < this.items.length; index++) {
                if (!clause(this.items[index], index)) { return false; }
            }
            return true;
        },
        Reverse: function() {
            var retVal = [];
            for (var index = this.items.length - 1; index > -1; index--) {
                retVal[retVal.length] = this.items[index];
            }
            return JSLINQ(retVal);
        },
        First: function(clause) {
            if (clause !== null && clause !== undefined) {
                return this.Where(clause).First();
            }
            // If no clause was specified, then return the First element in the Array
            return (this.items.length > 0) ? this.items[0] : null;
        },
        Last: function(clause) {
            if (clause !== null && clause !== undefined) {
                return this.Where(clause).Last();
            }
            // If no clause was specified, then return the First element in the Array
            return (this.items.length > 0) ? this.items[this.items.length - 1] : null;
        },
        ElementAt: function(index) {
            return this.items[index];
        },
        Concat: function(array) {
            var arr = array.items || array;
            return JSLINQ(this.items.concat(arr));
        },
        Intersect: function(secondArray, clause) {
            var clauseMethod = (clause !== undefined) ? clause
                : function(item, index, item2, index2) { return item === item2; };
                
            var sa = secondArray.items || secondArray;
            var result = [];

            for (var a = 0; a < this.items.length; a++) {
                for (var b = 0; b < sa.length; b++) {
                    if (clauseMethod(this.items[a], a, sa[b], b)) {
                        result[result.length] = this.items[a];
                    }
                }
            }
            return JSLINQ(result);
        },
        DefaultIfEmpty: function(defaultValue) {
            return (this.items.length === 0) ? defaultValue : this;
        },
        ElementAtOrDefault: function(index, defaultValue) {
            return (index >= 0 && index < this.items.length) ? this.items[index] : defaultValue;
        },
        FirstOrDefault: function(defaultValue) {
            return this.First() || defaultValue;
        },
        LastOrDefault: function(defaultValue) {
            return this.Last() || defaultValue;
        }
    };
    JSLINQ.fn.init.prototype = JSLINQ.fn;
})();

Apr 13, 2010 at 9:47 PM

I see you've made some of  these changes already in the recent builds. The main link is still pointing to older code.