-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Map polyfill and toMap IEnumerable extension.
- Loading branch information
Showing
11 changed files
with
372 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.