diff --git a/.istanbul.yml b/.istanbul.yml new file mode 100644 index 0000000..7fd8cec --- /dev/null +++ b/.istanbul.yml @@ -0,0 +1,2 @@ +instrumentation: + excludes: ['**/spec/**'] \ No newline at end of file diff --git a/build/js/enumerable.js b/build/js/enumerable.js index 894d5ac..b302ede 100644 --- a/build/js/enumerable.js +++ b/build/js/enumerable.js @@ -77,13 +77,6 @@ return /******/ (function(modules) { // webpackBootstrap /* 0 */ /***/ (function(module, exports, __webpack_require__) { -Array.prototype.asEnumerable = function() -{ - var e = new Enumerable(this); - - return e; -}; - function Enumerable(_array) { if(_array) @@ -98,6 +91,18 @@ function Enumerable(_array) Enumerable.fn = Enumerable.prototype; +Enumerable.Range = function(start, count) +{ + var result = []; + + for(var i = 0; i < count; i++) + { + result[i] = start + i; + } + + return result.asEnumerable(); +}; + module.exports = Enumerable; __webpack_require__(1); @@ -128,6 +133,10 @@ __webpack_require__(19); __webpack_require__(20); __webpack_require__(21); __webpack_require__(22); +__webpack_require__(23); +__webpack_require__(24); +__webpack_require__(25); +__webpack_require__(26); /***/ }), /* 2 */ @@ -173,7 +182,11 @@ Object.prototype.getType = function() else if(typeString === '[object Object]') { return 'object'; - } + } + else if (typeof this === "function") + { + return 'function'; + } return typeString; }; @@ -197,7 +210,16 @@ var Enumerable = __webpack_require__(0); Enumerable.fn.any = function(fn) { - var hasFn = (fn !== null && fn !== undefined && typeof fn === 'function'); + var hasFn = (fn !== null && fn !== undefined); + var FnIsFunction = (hasFn && typeof fn === "function"); + + if(!hasFn) + { + if(this !== null && this !== undefined && this.collection.length > 0) + { + return true; + } + } for(var i = 0; i < this.collection.length; i++) { @@ -208,17 +230,11 @@ Enumerable.fn.any = function(fn) continue; } - if(hasFn && fn(i, item, this.collection)) + if(hasFn && FnIsFunction && fn(i, item, this.collection)) { return true; } - else if(hasFn) - { - if(this !== null && this !== undefined && this.collection.length > 0) - { - return true; - } - } + } return false; @@ -309,6 +325,52 @@ Enumerable.fn.firstOrDefault = function(fn) var Enumerable = __webpack_require__(0); +Enumerable.fn.dropFirst = function(count) +{ + if(count <= 0) + { + return new Enumerable(this.collection); + } + + if(this.collection.length > count) + { + return new Enumerable(this.collection.splice(this.collection.length - count, count)); + } + else + { + return new Enumerable(); + } +}; + +/***/ }), +/* 10 */ +/***/ (function(module, exports, __webpack_require__) { + +var Enumerable = __webpack_require__(0); + +Enumerable.fn.dropLast = function(count) +{ + if(count <= 0) + { + return new Enumerable(this.collection); + } + + if(this.collection.length > count) + { + return new Enumerable(this.collection.splice(0, this.collection.length - count)); + } + else + { + return new Enumerable(); + } +}; + +/***/ }), +/* 11 */ +/***/ (function(module, exports, __webpack_require__) { + +var Enumerable = __webpack_require__(0); + Enumerable.fn.last = function() { if(this.collection.length > 0) @@ -322,7 +384,7 @@ Enumerable.fn.last = function() }; /***/ }), -/* 10 */ +/* 12 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -353,7 +415,85 @@ Enumerable.fn.lastOrDefault = function(fn) }; /***/ }), -/* 11 */ +/* 13 */ +/***/ (function(module, exports, __webpack_require__) { + +var Enumerable = __webpack_require__(0); + +Enumerable.fn.shuffle = function() +{ + var currentIndex = this.collection.length; + var temporaryValue; + var randomIndex; + + while (0 !== currentIndex) + { + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; + + temporaryValue = this.collection[currentIndex]; + this.collection[currentIndex] = this.collection[randomIndex]; + this.collection[randomIndex] = temporaryValue; + } + + return new Enumerable(this.collection); +}; + +/***/ }), +/* 14 */ +/***/ (function(module, exports, __webpack_require__) { + +var Enumerable = __webpack_require__(0); + +Enumerable.fn.except = function(arr, fn) +{ + if(this.collection.length <= 0) + { + return new Enumerable(this.collection); + } + + var result = []; + + for(var i = 0; i < this.collection.length; i++) + { + var item = this.collection[i]; + + var contains = false; + + for(var x = 0; x < arr.length; x++) + { + var item2 = arr[x]; + + if(fn && fn.getType() === "function") + { + if(fn(item, item2) === true) + { + contains = true; + break; + } + } + else + { + if(item2 === item) + { + contains = true; + break; + } + } + } + + if(contains === false) + { + result.push(item); + } + } + + return result.asEnumerable(); +}; + + +/***/ }), +/* 15 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -364,7 +504,7 @@ Enumerable.fn.maximum = function() function isNumeric(x) { - return (x.getType() === 'number') && (x % 1 === 0); + return (x !== null && x.getType() === 'number') && (x % 1 === 0); } var temp = []; @@ -386,7 +526,7 @@ Enumerable.fn.maximum = function() }; /***/ }), -/* 12 */ +/* 16 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -397,7 +537,7 @@ Enumerable.fn.minimum = function() function isNumeric(x) { - return (x.getType() === 'number') && (x % 1 === 0); + return (x !== null && x.getType() === 'number') && (x % 1 === 0); } var temp = []; @@ -414,12 +554,12 @@ Enumerable.fn.minimum = function() { result = Math.min.apply(null, temp); } - + return result; }; /***/ }), -/* 13 */ +/* 17 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -546,7 +686,7 @@ Enumerable.fn.orderBy = function(property, fn) }; /***/ }), -/* 14 */ +/* 18 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -673,7 +813,7 @@ Enumerable.fn.orderByDescending = function(property, fn) }; /***/ }), -/* 15 */ +/* 19 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -682,14 +822,23 @@ Enumerable.fn.select = function(obj) { var results = []; - for(var i = 0; i < this.collection.length; i++) + function explicit(item, obj) { - var item = this.collection[i]; - - if(obj !== null && obj !== undefined && obj.length > 0) + var _temp = {}; + + if(obj.length == 1) + { + for(var p in item) + { + if(p === obj[0]) + { + _temp = item[p]; + break; + } + } + } + else { - var _temp = {}; - for(var x = 0; x < obj.length; x++) { var prop = obj[x]; @@ -702,8 +851,30 @@ Enumerable.fn.select = function(obj) } } } - - results.push(_temp); + } + + results.push(_temp); + } + + function funcEval(obj, i, item, col) + { + results.push(obj(i, item, col)); + } + + for(var i = 0; i < this.collection.length; i++) + { + var item = this.collection[i]; + + if(obj !== null && obj !== undefined && obj.length > 0) + { + if(obj.getType() == 'function') + { + funcEval(obj, i, item, this.collection); + } + else + { + explicit(item, obj); + } } else { @@ -715,7 +886,7 @@ Enumerable.fn.select = function(obj) }; /***/ }), -/* 16 */ +/* 20 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -738,7 +909,7 @@ Enumerable.fn.single = function() }; /***/ }), -/* 17 */ +/* 21 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -774,7 +945,7 @@ Enumerable.fn.singleOrDefault = function(fn) /***/ }), -/* 18 */ +/* 22 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -783,21 +954,33 @@ Enumerable.fn.skip = function(count) { var results = []; - var canSkip = ((count > 0) && (count < this.collection.length)); + var hasItems = (count > 0); + var validSkipCount = (count < this.collection.length); + + var canSkip = (hasItems && validSkipCount); if(canSkip) { - for(var i = (count - 1); i < count; i++) + for(var i = 0; i < this.collection.length; i++) { + if(i < count) + { + continue; + } + results.push(this.collection[i]); } } + else + { + return this; + } return results.asEnumerable(); }; /***/ }), -/* 19 */ +/* 23 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -808,17 +991,29 @@ Enumerable.fn.skipWhile = function(fn) var hasFn = (fn !== null && fn !== undefined); var FnIsFunction = (hasFn && typeof fn === "function"); var canSkip = (hasFn && FnIsFunction); + var hasItems = this.collection.length > 0; - if(canSkip) + if(!hasFn || !FnIsFunction) { + return this; + } + + if(canSkip && hasItems) + { + var end = this.collection.length; + for(var i = 0; i < this.collection.length; i++) { var item = this.collection[i]; var _callbackResult = fn(i, item, this.collection); - if(_callbackResult !== true) + if(_callbackResult === true) + { + continue; + } + else { - results.push(this.collection[i]); + return this.collection.slice(i, end).asEnumerable(); } } } @@ -827,7 +1022,7 @@ Enumerable.fn.skipWhile = function(fn) }; /***/ }), -/* 20 */ +/* 24 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -857,7 +1052,7 @@ Enumerable.fn.take = function(count) }; /***/ }), -/* 21 */ +/* 25 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); @@ -866,19 +1061,26 @@ Enumerable.fn.takeWhile = function(fn) { var results = []; + var hasFn = (fn !== null && fn !== undefined && typeof fn === 'function'); + + if(!hasFn) + { + return this; + } + for(var i = 0; i < this.collection.length; i++) { var item = this.collection[i]; var _callbackResult = fn(i, item, this.collection); - if(_callbackResult !== true) + if(_callbackResult === true) { - return results.asEnumerable(); + results.push(item); } else { - results.push(this.collection[i]); + break; } } @@ -886,7 +1088,7 @@ Enumerable.fn.takeWhile = function(fn) }; /***/ }), -/* 22 */ +/* 26 */ /***/ (function(module, exports, __webpack_require__) { var Enumerable = __webpack_require__(0); diff --git a/build/js/enumerable.min.js b/build/js/enumerable.min.js index 950e57c..22ef4a3 100644 --- a/build/js/enumerable.min.js +++ b/build/js/enumerable.min.js @@ -1 +1 @@ -!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.Enumerable=n():t.Enumerable=n()}("undefined"!=typeof self?self:this,function(){return function(t){function n(o){if(e[o])return e[o].exports;var i=e[o]={i:o,l:!1,exports:{}};return t[o].call(i.exports,i,i.exports,n),i.l=!0,i.exports}var e={};return n.m=t,n.c=e,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:o})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="",n(n.s=0)}([function(t,n,e){function o(t){this.collection=t||[]}Array.prototype.asEnumerable=function(){return new o(this)},o.fn=o.prototype,t.exports=o,e(1)},function(t,n,e){e(2),e(3),e(4),e(5),e(6),e(7),e(8),e(9),e(10),e(11),e(12),e(13),e(14),e(15),e(16),e(17),e(18),e(19),e(20),e(21),e(22)},function(t,n,e){var o=e(0);Array.prototype.asEnumerable=function(){return new o(this)}},function(t,n){Object.prototype.getType=function(){var t=Object.prototype.toString.call(this);return"[object Date]"===t?"date":"[object String]"===t?"string":"[object Number]"===t?"number":"[object Boolean]"===t?"boolean":"[object Array]"===t?"array":"[object Object]"===t?"object":t}},function(t,n,e){e(0).fn.toArray=function(){return this.collection}},function(t,n,e){e(0).fn.any=function(t){for(var n=null!==t&&void 0!==t&&"function"==typeof t,e=0;e0)return!0}}return!1}},function(t,n,e){e(0).fn.count=function(t){if(null!==t&&void 0!==t&&"function"==typeof t){for(var n=[],e=0;e0)return this.collection[0];throw"sequence contains no elements"}},function(t,n,e){e(0).fn.firstOrDefault=function(t){return this.collection.length>0?this.collection[0]:null!==t&&void 0!==t?"function"==typeof t?t():t:null}},function(t,n,e){e(0).fn.last=function(){if(this.collection.length>0)return this.collection[this.collection.length-1];throw"sequence contains no elements"}},function(t,n,e){e(0).fn.lastOrDefault=function(t){var n=null!==t&&void 0!==t,e=n&&"function"==typeof t;return this.collection.length>0?this.collection[this.collection.length-1]:n?e?t():t:null}},function(t,n,e){e(0).fn.maximum=function(){function t(t){return"number"===t.getType()&&t%1==0}for(var n=null,e=[],o=0;o0&&(n=Math.max.apply(null,e)),n}},function(t,n,e){e(0).fn.minimum=function(){function t(t){return"number"===t.getType()&&t%1==0}for(var n=null,e=[],o=0;o0&&(n=Math.min.apply(null,e)),n}},function(t,n,e){var o=e(0);o.fn.orderBy=function(t,n){if(!this.collection)return null;if("[object Array]"!=Object.prototype.toString.call(this.collection))return null;if(0===this.collection.length)return new o([]);var e=n&&"function"==typeof n,i=!!t,l=[],r=this.collection,c=i?r[0][t].getType():r[0].getType();if(e){r=[];for(var u=0;ul?1:0}):"number"==c?l=r.sort(function(n,e){return i?n[t]-e[t]:n-e}):"date"==c?l=r.sort(function(n,e){return i?new Date(n[t])-new Date(e[t]):new Date(n)-new Date(e)}):"boolean"==c?l=r.sort(function(t,n){return t===n?0:t?1:-1}):"array"==c?l=this.collection:"object"==c&&(l=this.collection),l.asEnumerable()}},function(t,n,e){var o=e(0);o.fn.orderByDescending=function(t,n){if(!this.collection)return null;if("[object Array]"!=Object.prototype.toString.call(this.collection))return null;if(0===this.collection.length)return new o([]);var e=n&&"function"==typeof n,i=!!t,l=[],r=this.collection,c=i?r[0][t].getType():r[0].getType();if(e){r=[];for(var u=0;ul?-1:0}):"number"==c?l=r.sort(function(n,e){return i?e[t]-n[t]:e-n}):"date"==c?l=r.sort(function(n,e){return i?new Date(e[t])-new Date(n[t]):new Date(e)-new Date(n)}):"boolean"==c?l=r.sort(function(t,n){return t===n?0:t?-1:1}):"array"==c?l=this.collection:"object"==c&&(l=this.collection),l.asEnumerable()}},function(t,n,e){e(0).fn.select=function(t){for(var n=[],e=0;e0){for(var i={},l=0;l1)throw"collection contains more than one item";return n?e?t():t:null}},function(t,n,e){e(0).fn.skip=function(t){var n=[];if(t>0&&tt){for(var n=[],e=0;et?new o(this.collection.splice(this.collection.length-t,t)):new o}},function(t,n,e){var o=e(0);o.fn.dropLast=function(t){return t<=0?new o(this.collection):this.collection.length>t?new o(this.collection.splice(0,this.collection.length-t)):new o}},function(t,n,e){e(0).fn.last=function(){if(0t){for(var n=[],e=0;e count) + { + return new Enumerable(this.collection.splice(this.collection.length - count, count)); + } + else + { + return new Enumerable(); + } +}; \ No newline at end of file diff --git a/src/core/dropLast.js b/src/core/dropLast.js new file mode 100644 index 0000000..ec7738c --- /dev/null +++ b/src/core/dropLast.js @@ -0,0 +1,18 @@ +var Enumerable = require('../enumerable'); + +Enumerable.fn.dropLast = function(count) +{ + if(count <= 0) + { + return new Enumerable(this.collection); + } + + if(this.collection.length > count) + { + return new Enumerable(this.collection.splice(0, this.collection.length - count)); + } + else + { + return new Enumerable(); + } +}; \ No newline at end of file diff --git a/src/core/except.js b/src/core/except.js new file mode 100644 index 0000000..2841a83 --- /dev/null +++ b/src/core/except.js @@ -0,0 +1,47 @@ +var Enumerable = require('../enumerable'); + +Enumerable.fn.except = function(arr, fn) +{ + if(this.collection.length <= 0) + { + return new Enumerable(this.collection); + } + + var result = []; + + for(var i = 0; i < this.collection.length; i++) + { + var item = this.collection[i]; + + var contains = false; + + for(var x = 0; x < arr.length; x++) + { + var item2 = arr[x]; + + if(fn && fn.getType() === "function") + { + if(fn(item, item2) === true) + { + contains = true; + break; + } + } + else + { + if(item2 === item) + { + contains = true; + break; + } + } + } + + if(contains === false) + { + result.push(item); + } + } + + return result.asEnumerable(); +}; diff --git a/src/core/shuffle.js b/src/core/shuffle.js new file mode 100644 index 0000000..69c574a --- /dev/null +++ b/src/core/shuffle.js @@ -0,0 +1,20 @@ +var Enumerable = require('../enumerable'); + +Enumerable.fn.shuffle = function() +{ + var currentIndex = this.collection.length; + var temporaryValue; + var randomIndex; + + while (0 !== currentIndex) + { + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; + + temporaryValue = this.collection[currentIndex]; + this.collection[currentIndex] = this.collection[randomIndex]; + this.collection[randomIndex] = temporaryValue; + } + + return new Enumerable(this.collection); +}; \ No newline at end of file diff --git a/src/dependencies.js b/src/dependencies.js index 2141219..5c6db57 100644 --- a/src/dependencies.js +++ b/src/dependencies.js @@ -5,8 +5,12 @@ require("./core/any"); require("./core/count"); require("./core/first"); require("./core/firstOrDefault"); +require("./core/dropFirst"); +require("./core/dropLast"); require("./core/last"); require("./core/lastOrDefault"); +require("./core/shuffle"); +require("./core/except"); require("./core/maximum"); require("./core/minimum"); require("./core/orderBy");