Skip to content

Commit

Permalink
Added "difference" to give the 3 exclusive regions in a venn diagram …
Browse files Browse the repository at this point in the history
…between 2 sets.
  • Loading branch information
BSick7 committed May 13, 2014
1 parent 923fa2c commit d18d2e5
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 3 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.4",
"version": "0.1.5",
"homepage": "https://github.com/BSick7/exjs",
"authors": [
"BSick7 <brad.sickles@gmail.com>"
Expand Down
9 changes: 9 additions & 0 deletions dist/ex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ declare module exjs {
concat(second: IEnumerable<T>): IEnumerable<T>;
concat(second: T[]): IEnumerable<T>;
count(predicate?: (t: T) => boolean): number;
difference(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IDifference<T>;
difference(second: T[], comparer?: (f: T, s: T) => boolean): IDifference<T>;
distinct(comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
except(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
except(second: T[], comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
Expand Down Expand Up @@ -52,6 +54,11 @@ declare module exjs {
interface IGrouping<TKey, TElement> extends IEnumerable<TElement> {
key: TKey;
}
interface IDifference<T> {
intersection: IEnumerable<T>;
aNotB: IEnumerable<T>;
bNotA: IEnumerable<T>;
}
interface IList<T> extends IEnumerable<T> {
toString(): string;
toLocaleString(): string;
Expand Down Expand Up @@ -89,6 +96,8 @@ declare module exjs {
public concat(second: IEnumerable<T>): IEnumerable<T>;
public concat(second: T[]): IEnumerable<T>;
public count(predicate?: (t: T) => boolean): number;
public difference(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IDifference<T>;
public difference(second: T[], comparer?: (f: T, s: T) => boolean): IDifference<T>;
public distinct(comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
public except(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
public except(second: T[], comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
Expand Down
14 changes: 14 additions & 0 deletions dist/ex.js

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

3 changes: 2 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.4</version>
<version>0.1.5</version>
<title>exjs</title>
<authors>Brad Sickles</authors>
<licenseUrl>https://raw.githubusercontent.com/BSick7/exjs/master/LICENSE</licenseUrl>
Expand All @@ -20,3 +20,4 @@ This is a lightweight library to aid in data manipulation and transformation.</d
</package>



Binary file added nuget/exjs.0.1.5.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.4",
"version": "0.1.5",
"description": "Extension library",
"main": "Gruntfile.js",
"directories": {
Expand Down
22 changes: 22 additions & 0 deletions src/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module exjs {
concat(second: IEnumerable<T>): IEnumerable<T>;
concat(second: T[]): IEnumerable<T>;
count(predicate?: (t: T) => boolean): number;
difference(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IDifference<T>;
difference(second: T[], comparer?: (f: T, s: T) => boolean): IDifference<T>;
distinct(comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
except(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
except(second: T[], comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
Expand Down Expand Up @@ -56,6 +58,12 @@ module exjs {
key: TKey;
}

export interface IDifference<T> {
intersection: IEnumerable<T>;
aNotB: IEnumerable<T>;
bNotA: IEnumerable<T>;
}

export interface IList<T> extends IEnumerable<T> {
toString(): string;
toLocaleString(): string;
Expand Down Expand Up @@ -166,6 +174,20 @@ module exjs {
}
return count;
}

difference(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IDifference<T>;
difference(second: T[], comparer?: (f: T, s: T) => boolean): IDifference<T>;
difference(second: any, comparer?: (f: T, s: T) => boolean): IDifference<T> {
comparer = comparer || function (f2: T, s2: T) { return f2 === s2; };
if (second instanceof Array)
second = second.en();
return {
intersection: this.intersect(second, comparer).toArray().en(),
aNotB: this.except(second, comparer).toArray().en(),
bNotA: second.except(this, comparer).toArray().en()
};
}

distinct(comparer?: (f: T, s: T) => boolean): IEnumerable<T> { throw new Error("Not implemented"); }

except(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
Expand Down
20 changes: 20 additions & 0 deletions test/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,26 @@ test("count", () => {
strictEqual(arr.en().count(n => n > 1), 2);
});

test("difference", () => {
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [4, 5, 6, 7, 8];
var diff = arr1.en().difference(arr2);

strictEqual(diff.intersection.count(), 2);
strictEqual(diff.intersection.at(0), 4);
strictEqual(diff.intersection.at(1), 5);

strictEqual(diff.aNotB.count(), 3);
strictEqual(diff.aNotB.at(0), 1);
strictEqual(diff.aNotB.at(1), 2);
strictEqual(diff.aNotB.at(2), 3);

strictEqual(diff.bNotA.count(), 3);
strictEqual(diff.bNotA.at(0), 6);
strictEqual(diff.bNotA.at(1), 7);
strictEqual(diff.bNotA.at(2), 8);
});

test("distinct", () => {
var arr = [];
var res = arr.en().distinct().toArray();
Expand Down

0 comments on commit d18d2e5

Please sign in to comment.