提交 e68b0193 编写于 作者: J Johannes Rieken

debt - no code is the best code, remove more unsued types and functions

上级 575cbbde
......@@ -90,25 +90,6 @@ export function contains<T>(from:any, what:any):boolean {
return hasOwnProperty.call(from, what);
}
export function keys<T>(from:IStringDictionary<T>):IIterable<string>;
export function keys<T>(from:INumberDictionary<T>):IIterable<number>;
export function keys<T>(from:any):IIterable<any> {
return {
every: function(callback:(element:any)=>boolean):boolean {
for (var key in from) {
if (hasOwnProperty.call(from, key)) {
if(!callback(key)) {
return false;
}
}
}
return true;
}
};
}
/**
* Returns an array which contains all values that reside
* in the given set.
......@@ -167,145 +148,3 @@ export function groupBy<T>(data: T[], groupFn: (element: T) => string): IStringD
data.forEach(element => lookupOrInsert(result, groupFn(element), []).push(element));
return result;
}
/**
* An iterable of a given type. This iterable is
* compatible with the JavaScript array.
*/
export interface IIterable<E> {
/**
* Iterates over every element in the array
* as long as the callback does not return some
* 'falsy' value.
* @param callback A function that is called for each element
* @return {{true}} if every element has been visited,
* {{false}} if it returned early
*/
every(callback:(element:E)=>boolean):boolean;
}
export var EmptyIterable:IIterable<any> = {
every: function(callback) {
return true;
}
};
export function combine<E>(iterables:IIterable<E>[]):IIterable<E> {
var len = iterables.length;
if(len === 0) {
return EmptyIterable;
} else if(len === 1) {
return iterables[0];
}
return {
every: function(callback:(element:E)=>any) {
for(var i = 0; i < len; i++) {
if(!iterables[i].every(callback)) {
return false;
}
}
return true;
}
};
}
export function singleton<E>(element:E):IIterable<E> {
return {
every: function(callback) {
return callback(element);
}
};
}
export function toArray<E>(iterable:IIterable<E>):E[] {
if(Array.isArray(iterable)) {
return <E[]> iterable;
} else {
var result:E[] = [];
iterable.every((e) => {
result.push(e);
return true;
});
return result;
}
}
///**
// * ECMAScript 6 iterator
// */
//export interface IIterator<T> {
// next(): { done: boolean; value?: T; };
//}
//
//export function empty<T>():IIterator<T> {
// return {
// next: function() { return { done: true }; }
// };
//}
//
//export function iterator<T>(array: T[]): IIterator<T> {
// var i = 0;
// return {
// next: () => {
// if(i < array.length) {
// return {
// done: false,
// value: array[i++]
// };
// } else {
// return {
// done: true
// };
// }
// }
// };
//}
interface ICacheRow<T> {
element: T;
onRemove: ()=>void;
}
/**
* Limited size cache. Provided a certain cache size limit, it
* removes the older elements as new ones are inserted.
*/
export class LimitedSizeCache<T> {
private cache: { [id: string]: ICacheRow<T> };
private order: string[];
constructor(private size: number) {
this.cache = Object.create(null);
this.order = [];
}
public get(id: string): T {
var result = this.cache[id];
return result && result.element;
}
public put(id: string, element: T, onRemove: ()=>void): void {
var existing = this.cache[id];
var row: ICacheRow<T> = { element: element, onRemove: onRemove };
this.cache[id] = row;
if (!existing) {
this.order.push(id);
}
this.swipe();
}
private swipe(): void {
while (this.order.length > this.size) {
var id = this.order.shift();
var row = this.cache[id];
row.onRemove();
delete this.cache[id];
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册