Skip to content

Commit

Permalink
Added remove and removeWhere method to List<T>.
Browse files Browse the repository at this point in the history
  • Loading branch information
BSick7 committed May 13, 2014
1 parent d18d2e5 commit 76bb7c8
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "exjs",
"version": "0.1.5",
"version": "0.1.6",
"homepage": "https://github.com/BSick7/exjs",
"authors": [
"BSick7 <brad.sickles@gmail.com>"
Expand Down
4 changes: 4 additions & 0 deletions dist/ex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ declare module exjs {
reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
length: number;
[n: number]: T;
remove(item: T): boolean;
removeWhere(predicate: (t: T, index?: number) => boolean): IEnumerable<T>;
}
class Enumerable<T> implements IEnumerable<T> {
constructor();
Expand Down Expand Up @@ -176,6 +178,8 @@ declare module exjs {
public reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
public length: number;
[n: number]: T;
public remove(item: T): boolean;
public removeWhere(predicate: (t: T, index?: number) => boolean): IEnumerable<T>;
}
}
declare module exjs {
Expand Down
26 changes: 25 additions & 1 deletion dist/ex.js

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

4 changes: 3 additions & 1 deletion exjs.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>exjs</id>
<version>0.1.5</version>
<version>0.1.6</version>
<title>exjs</title>
<authors>Brad Sickles</authors>
<licenseUrl>https://raw.githubusercontent.com/BSick7/exjs/master/LICENSE</licenseUrl>
Expand All @@ -21,3 +21,5 @@ This is a lightweight library to aid in data manipulation and transformation.</d





Binary file added nuget/exjs.0.1.6.nupkg
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "exjs",
"version": "0.1.5",
"version": "0.1.6",
"description": "Extension library",
"main": "Gruntfile.js",
"directories": {
Expand Down
14 changes: 14 additions & 0 deletions src/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module exjs {
}

export interface IList<T> extends IEnumerable<T> {
//Array<T> methods
toString(): string;
toLocaleString(): string;
pop(): T;
Expand All @@ -88,6 +89,19 @@ module exjs {
reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U;
length: number;
[n: number]: T;

//List<T> methods
/**
* Removes item from list.
* @param item - item to remove
*/
remove(item: T): boolean;
/**
* Removes items that match predicate.
* Returns items that were removed.
* @param predicate - function to match items that should be removed (index parameter is based on original list)
*/
removeWhere(predicate: (t: T, index?: number) => boolean): IEnumerable<T>;
}

export class Enumerable<T> implements IEnumerable<T> {
Expand Down
23 changes: 21 additions & 2 deletions src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module exjs {
return <IList<T>>l;
};


export class List<T> extends Enumerable<T> implements IList<T> {
//Array<T> methods
toString (): string { throw new Error("Not implemented"); }
toLocaleString (): string { throw new Error("Not implemented"); }
pop (): T { throw new Error("Not implemented"); }
Expand All @@ -39,9 +39,13 @@ module exjs {
reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U { throw new Error("Not implemented"); }
length: number;
[n: number]: T;

//List<T> methods
remove(item: T): boolean { throw new Error("Not implemented"); }
removeWhere(predicate: (t: T, index?: number) => boolean): IEnumerable<T> { throw new Error("Not implemented"); }
}

for (var p in Array) if (List.hasOwnProperty(p)) List[p] = Array[p];
for (var p in Array) if (Array.hasOwnProperty(p)) List[p] = Array[p];
function __ () { this.constructor = List; }
__.prototype = Array.prototype;
List.prototype = new __();
Expand All @@ -67,4 +71,19 @@ module exjs {
};
return e;
};
List.prototype.remove = function<T>(item: T): boolean {
return this.removeWhere(t => t === item).any();
}
List.prototype.removeWhere = function<T>(predicate: (t: T, index?: number) => boolean): IEnumerable<T> {
var removed = [];
var cur: T;
for (var i = this.length - 1; i >= 0; i--) {
cur = this[i];
if (predicate(cur, i) === true) {
this.splice(i, 1);
removed.push(cur);
}
}
return removed.en().reverse();
};
}
26 changes: 0 additions & 26 deletions test/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,6 @@ test("array", () => {
strictEqual(arr2[4], 5);
});

test("list", () => {
var arr = [1, 2, 3, 4, 5];
var list = arr.en().toList();
notStrictEqual(arr, list);
ok(list instanceof exjs.List);
ok(list instanceof Array);
strictEqual(list.length, 5);
strictEqual(list[0], 1);
strictEqual(list[1], 2);
strictEqual(list[2], 3);
strictEqual(list[3], 4);
strictEqual(list[4], 5);

ok(list.select instanceof Function);
var n = list.select(i => i).toArray();
ok(n instanceof Array);
notStrictEqual(arr, n);
notStrictEqual(list, n);
strictEqual(n.length, 5);
strictEqual(n[0], 1);
strictEqual(n[1], 2);
strictEqual(n[2], 3);
strictEqual(n[3], 4);
strictEqual(n[4], 5);
});

test("range", () => {
var arr = exjs.range(0, 1).toArray();
strictEqual(arr.length, 1, "#1");
Expand Down
78 changes: 78 additions & 0 deletions test/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/// <reference path="../lib/qunit/qunit.d.ts" />
/// <reference path="../dist/ex.d.ts" />

QUnit.module("list");

test("toList", () => {
var arr = [1, 2, 3, 4, 5];
var list = arr.en().toList();
notStrictEqual(arr, list);
ok(list instanceof exjs.List);
ok(list instanceof Array);
strictEqual(list.length, 5);
strictEqual(list[0], 1);
strictEqual(list[1], 2);
strictEqual(list[2], 3);
strictEqual(list[3], 4);
strictEqual(list[4], 5);

ok(list.select instanceof Function);
var n = list.select(i => i).toArray();
ok(n instanceof Array);
notStrictEqual(arr, n);
notStrictEqual(list, n);
strictEqual(n.length, 5);
strictEqual(n[0], 1);
strictEqual(n[1], 2);
strictEqual(n[2], 3);
strictEqual(n[3], 4);
strictEqual(n[4], 5);
});

test("remove", () => {
var arr = [1, 2, 3, 4, 5];
var list = arr.en().toList();

ok(list.remove(1));
strictEqual(list.length, 4);
strictEqual(list[0], 2);
strictEqual(list[1], 3);
strictEqual(list[2], 4);
strictEqual(list[3], 5);

ok(!list.remove(<any>'2'));
strictEqual(list.length, 4);
strictEqual(list[0], 2);
strictEqual(list[1], 3);
strictEqual(list[2], 4);
strictEqual(list[3], 5);

ok(!list.remove(10));
strictEqual(list.length, 4);
strictEqual(list[0], 2);
strictEqual(list[1], 3);
strictEqual(list[2], 4);
strictEqual(list[3], 5);
});

test("removeWhere", () => {
var arr = [1, 2, 3, 4, 5];
var list = arr.en().toList();

var removed = list.removeWhere(n => n === 1).toArray();
strictEqual(removed.length, 1);
strictEqual(removed[0], 1);
strictEqual(list.length, 4);
strictEqual(list[0], 2);
strictEqual(list[1], 3);
strictEqual(list[2], 4);
strictEqual(list[3], 5);

removed = list.removeWhere((n, index) => index % 2 === 0).toArray();
strictEqual(removed.length, 2);
strictEqual(removed[0], 2);
strictEqual(removed[1], 4);
strictEqual(list.length, 2);
strictEqual(list[0], 3);
strictEqual(list[1], 5);
});
1 change: 1 addition & 0 deletions test/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<script src='../dist/ex.js'></script>
<!-- your tests, any and all to run with the given fixtures below -->
<script src='enum.js'></script>
<script src='list.js'></script>
<script src='json.js'></script>
</head>
<body>
Expand Down

0 comments on commit 76bb7c8

Please sign in to comment.