Skip to content

Commit

Permalink
Implemented Map polyfill and toMap IEnumerable extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
BSick7 committed Jun 17, 2014
1 parent b19bc61 commit e2b591a
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 4 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.7",
"version": "0.1.8",
"homepage": "https://github.com/BSick7/exjs",
"authors": [
"BSick7 <brad.sickles@gmail.com>"
Expand Down
33 changes: 33 additions & 0 deletions dist/ex.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare module exjs {
takeWhile(predicate: (t: T, index?: number) => boolean): IEnumerable<T>;
toArray(): T[];
toList(): IList<T>;
toMap<TKey, TValue>(keySelector: (t: T) => TKey, valueSelector: (t: T) => TValue): IMap<TKey, TValue>;
union(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
union(second: T[], comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
where(filter: (t: T) => boolean): IEnumerable<T>;
Expand Down Expand Up @@ -126,6 +127,7 @@ declare module exjs {
public take(count: number): IEnumerable<T>;
public takeWhile(predicate: (t: T, index?: number) => boolean): IEnumerable<T>;
public toArray(): T[];
public toMap<TKey, TValue>(keySelector: (t: T) => TKey, valueSelector: (t: T) => TValue): Map<TKey, TValue>;
public toList(): IList<T>;
public union(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
public union(second: T[], comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
Expand Down Expand Up @@ -184,6 +186,37 @@ declare module exjs {
public removeWhere(predicate: (t: T, index?: number) => boolean): IEnumerable<T>;
}
}
declare module exjs {
interface IMap<TKey, TValue> {
size: number;
clear(): any;
delete(key: TKey): boolean;
entries(): IEnumerable<any[]>;
forEach(callbackFn: (value: TValue, key: TKey, map?: IMap<TKey, TValue>) => void, thisArg?: any): any;
get(key: TKey): TValue;
has(key: TKey): boolean;
keys(): IEnumerable<TKey>;
set(key: TKey, value: TValue): any;
values(): IEnumerable<TValue>;
}
class Map<TKey, TValue> implements IMap<TKey, TValue> {
private _keys;
private _values;
public size : number;
constructor();
constructor(enumerable: any[][]);
constructor(enumerable: IEnumerable<any[]>);
public clear(): void;
public delete(key: TKey): boolean;
public entries(): IEnumerable<any[]>;
public forEach(callbackFn: (value: TValue, key: TKey, map?: IMap<TKey, TValue>) => void, thisArg?: any): void;
public get(key: TKey): TValue;
public has(key: TKey): boolean;
public keys(): IEnumerable<TKey>;
public set(key: TKey, value: TValue): any;
public values(): IEnumerable<TValue>;
}
}
declare module exjs {
}
declare module exjs {
Expand Down
96 changes: 96 additions & 0 deletions dist/ex.js

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

2 changes: 1 addition & 1 deletion dist/ex.min.js

Large diffs are not rendered by default.

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




Binary file added nuget/exjs.0.1.8.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.7",
"version": "0.1.8",
"description": "Extension library",
"main": "Gruntfile.js",
"directories": {
Expand Down
8 changes: 8 additions & 0 deletions src/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module exjs {
takeWhile(predicate: (t: T, index?: number) => boolean): IEnumerable<T>;
toArray(): T[];
toList(): IList<T>;
toMap<TKey, TValue>(keySelector: (t: T) => TKey, valueSelector: (t: T) => TValue): IMap<TKey, TValue>;
//toDictionary();
union(second: IEnumerable<T>, comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
union(second: T[], comparer?: (f: T, s: T) => boolean): IEnumerable<T>;
Expand Down Expand Up @@ -315,6 +316,13 @@ module exjs {
}
return arr;
}
toMap<TKey, TValue>(keySelector: (t: T) => TKey, valueSelector: (t: T) => TValue): Map<TKey, TValue> {
var m = new Map<TKey, TValue>();
for (var en = this.getEnumerator(); en.moveNext();) {
m.set(keySelector(en.current), valueSelector(en.current));
}
return m;
}
toList(): IList<T> { throw new Error("Not implemented"); }
//toDictionary() {
//}
Expand Down
97 changes: 97 additions & 0 deletions src/map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module exjs {
export interface IMap<TKey, TValue> {
size: number;
clear();
delete(key: TKey): boolean;
entries(): IEnumerable<any[]>;
forEach(callbackFn: (value: TValue, key: TKey, map?: IMap<TKey, TValue>) => void, thisArg?: any);
get(key: TKey): TValue;
has(key: TKey): boolean;
keys(): IEnumerable<TKey>;
set(key: TKey, value: TValue): any;
values(): IEnumerable<TValue>;
}

export class Map<TKey, TValue> implements IMap<TKey, TValue> {
private _keys: TKey[] = [];
private _values: TValue[] = [];

get size (): number {
return this._keys.length;
}

constructor ();
constructor (enumerable: any[][]);
constructor (enumerable: IEnumerable<any[]>);
constructor (enumerable?: any) {
var enu: IEnumerable<any[]>;
if (enumerable instanceof Array) {
enu = (<Array<any[]>>enumerable).en();
} else if (enumerable && enumerable.getEnumerator instanceof Function) {
enu = <IEnumerable<any[]>>enumerable;
}

if (!enu)
return;
for (var en = enu.getEnumerator(); en && en.moveNext();) {
this.set(en.current[0], en.current[1]);
}
}

clear () {
this._keys.length = 0;
this._values.length = 0;
}

delete (key: TKey): boolean {
var index = this._keys.indexOf(key);
if (!(index > -1))
return false;
this._keys.splice(index, 1);
this._values.splice(index, 1);
return true;
}

entries (): IEnumerable<any[]> {
return exjs.range(0, this.size).select(i => [this._keys[i], this._values[i]]);
}

forEach (callbackFn: (value: TValue, key: TKey, map?: IMap<TKey, TValue>) => void, thisArg?: any) {
if (thisArg == null)
thisArg = this;
for (var i = 0, keys = this._keys, vals = this._values, len = keys.length; i < len; i++) {
callbackFn.call(thisArg, vals[i], keys[i], this);
}
}

get (key: TKey): TValue {
var index = this._keys.indexOf(key);
return this._values[index];
}

has (key: TKey): boolean {
return this._keys.indexOf(key) > -1;
}

keys (): IEnumerable<TKey> {
return this._keys.en();
}

set (key: TKey, value: TValue): any {
var index = this._keys.indexOf(key);
if (index > -1) {
this._values[index] = value;
} else {
this._keys.push(key);
this._values.push(value);
}
return undefined;
}

values (): IEnumerable<TValue> {
return this._values.en();
}
}
if (!(<any>window).Map)
(<any>window).Map = Map;
}
Loading

0 comments on commit e2b591a

Please sign in to comment.