Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Feature/code cleanup (#16)
Browse files Browse the repository at this point in the history
* code cleanup

* fixed takeWhile to return correct results

* fixed skipWhile to return correct results

* fixed skip to return the proper results

* increased test coverage

* Update package.json
  • Loading branch information
Gerald Burkholder authored Dec 3, 2017
1 parent ebcdc5f commit a8ce657
Show file tree
Hide file tree
Showing 27 changed files with 383 additions and 180 deletions.
182 changes: 97 additions & 85 deletions build/js/enumerable.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/js/enumerable.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var del = require("del");
var webpack = require("webpack");
var gutil = require('gulp-util');

gulp.task('default', ['minify']);
gulp.task('default', ['build']);

gulp.task('clean', function()
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asenumerable",
"version": "1.0.10",
"version": "1.1.0",
"description": "a JavaScript library that provides linq functions on native JavaScript arrays",
"dependencies": {},
"main": "src/enumerable.js",
Expand Down
28 changes: 28 additions & 0 deletions spec/enumerable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var Enumerable = require('../src/enumerable');

describe("Enumerable", function ()
{
describe("when initialized from an empty array", function ()
{
it("should return an empty enumerable object", function ()
{
var testArray = [];

var result = new Enumerable(testArray);

expect(result).toEqual(new Enumerable([]));
});
});

describe("when initialized from a null array", function ()
{
it("should return an empty enumerable object", function ()
{
var testArray = null;

var result = new Enumerable(testArray);

expect(result).toEqual(new Enumerable([]));
});
});
});
52 changes: 52 additions & 0 deletions spec/skip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var Enumerable = require('../src/enumerable');

describe("skip", function ()
{
describe("when an enumerable object has 3 items", function ()
{
it("should skip specified number of items and return the remaining items", function ()
{
var testArray = new Enumerable([{ name: 'test', id: 1 }, { name: '1234', id: 2 }, { name: '5678', id: 3 }]);

var result = testArray.skip(2);

expect(result).toEqual(new Enumerable([{ name: '5678', id: 3 }]));
});
});

describe("when an enumerable object has multiple items", function ()
{
it("should skip specified number of items and return the remaining items", function ()
{
var testArray = new Enumerable([98, 92, 85, 82, 70, 59, 56]);

var result = testArray.skip(3);

expect(result).toEqual(new Enumerable([82, 70, 59, 56]));
});
});

describe("when an enumerable object is empty", function ()
{
it("should return empty enumerable", function ()
{
var testArray = new Enumerable([]);

var result = testArray.skip(2);

expect(result).toEqual(new Enumerable([]));
});
});

describe("when count is a greater integer than the length of the enumerable object", function ()
{
it("should return current enumerable", function ()
{
var testArray = new Enumerable([{ name: 'test', id: 1 }, { name: '1234', id: 2 }, { name: '5678', id: 3 }]);

var result = testArray.skip(50);

expect(result).toEqual(testArray);
});
});
});
58 changes: 52 additions & 6 deletions spec/skipWhile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,51 @@

describe("skip while", function ()
{
describe("when an enumerable object has 3 items", function ()
describe("when an enumerable object has multiple items", function ()
{
it("should skip items that match a predicate function and return the others", function ()
it("should return everything after the item where the predicate function evaluated to false", function ()
{
var testArray = new Enumerable([{ name: 'test', id: 1 }, { name: '1234', id: 2 }, { name: '5678', id: 3 }]);
var testArray = new Enumerable([{ name: 'test', id: 1 }, { name: '1234', id: 2 }, { name: '5678', id: 3 }, { name: '9012', id: 4 }]);

var result = testArray.skipWhile(function (i, item, collection)
{
return (item.name === "test");
return (item.id < 3);
});

expect(result).toEqual(new Enumerable([{ name: '5678', id: 3 }, { name: '9012', id: 4 }]));
});
});

describe("when an enumerable object has multiple numbers", function ()
{
it("should return everything after the item where the predicate function evaluated to false", function ()
{
var testArray = new Enumerable([98, 92, 85, 82, 70, 59, 56]);

var result = testArray.skipWhile(function (i, item, collection)
{
return (item >= 80);
});

expect(result).toEqual(new Enumerable([{ name: '1234', id: 2 }, { name: '5678', id: 3 }]));
expect(result).toEqual(new Enumerable([70, 59, 56]));
});
});

describe("when no function is passed", function ()
{
it("should return the collection", function ()
{
var testArray = new Enumerable([98, 92, 85, 82, 70, 59, 56]);

var result = testArray.skipWhile();

expect(result).toEqual(testArray);
});
});

describe("when an enumerable object has 1 item", function ()
{
it("should skips items that match a predicate function and return the others", function ()
it("should return everything after the item where the predicate function evaluated to false", function ()
{
var testArray = new Enumerable([{ name: 'test', id: 1 }]);

Expand All @@ -46,4 +73,23 @@ describe("skip while", function ()
expect(result).toEqual(new Enumerable([]));
});
});

describe("when an enumerable object has multiple items and the predicate return true midway through the collection", function ()
{
it("should return everything after the item where the predicate function evaluated to false", function ()
{
var testArray = new Enumerable([{ name: 'test', id: 1 }, { name: '1234', id: 2 }, { name: '5678', id: 3 }]);

var result = testArray.skipWhile(function (i, item, collection)
{
return (item.id < 3);
});

expect(result).toEqual(new Enumerable([{ name: '5678', id: 3 }]));
});
});

//skip while >= 80
//98, 92, 85, 82, 70, 59, 56
//returns 70, 59, 56
});
Loading

0 comments on commit a8ce657

Please sign in to comment.