From 95db172afc397a156de98ed3a2719688c0891d09 Mon Sep 17 00:00:00 2001 From: Alexey Vagarenko Date: Fri, 21 Jun 2013 20:24:45 +0500 Subject: [PATCH] Add generics and new module system support --- underscore/underscore-tests.ts | 13 +- underscore/underscore.d.ts | 410 ++++++++++++++++----------------- 2 files changed, 209 insertions(+), 214 deletions(-) diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index 5fbab818b..17398a3dd 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -1,6 +1,7 @@ -/// +import _ = require("underscore") declare var $; +declare function alert(any); _.each([1, 2, 3], (num) => alert(num)); _.each({ one: 1, two: 2, three: 3 }, (num, key) => alert(num)); @@ -205,7 +206,7 @@ var moe2 = { name: 'moe' }; moe2 === _.identity(moe); var genie; -_(3).times(function(n){ genie.grantWishNumber(n); }); +_(3).times(function (n) { genie.grantWishNumber(n); }); _.random(0, 100); @@ -240,10 +241,8 @@ var template2 = _.template("Hello {{ name }}!"); template2({ name: "Mustache" }); _.template("Using 'with': <%= data.answer %>", { answer: 'no' }, { variable: 'data' }); -// fix: http://stackoverflow.com/questions/14585324/how-to-use-underscore-lib-from-definitelytyped-with-typescript -// fix: https://github.com/borisyankov/DefinitelyTyped/issues/225 -declare var _: UnderscoreStatic; -_.countBy([1,2,3], function(item) { - return item%2; + +_.countBy([1, 2, 3], function (item) { + return item % 2; }); \ No newline at end of file diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 42d4abeb5..88dc84584 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -5,8 +5,8 @@ interface UnderscoreWrappedObject { - value () : any; - filter(arg?) : any; + value(): any; + filter(arg?): any; } interface TemplateSettings { @@ -15,218 +15,214 @@ interface TemplateSettings { escape?: RegExp; } -interface ListIterator { - (value, key, list?): void; +interface ListIterator { + (value: T, index: number, list?: List): TResult; } -interface ObjectIterator { - (element, index, list?): void; +interface ObjectIterator { + (element: T, key: string, list?: any): TResult; } +interface Collection { } + // Common interface between Arrays and jQuery objects -interface List { - [index: number]: any; +interface List extends Collection { + [index: number]: T; length: number; } -interface UnderscoreStatic { - (arg?:any) : any; - - /**** - Collections - *****/ - each(list: List, iterator: ListIterator, context?: any): any[]; - each(object: any, iterator: ObjectIterator, context?: any): any[]; - forEach(list: List, iterator: ObjectIterator, context?: any): any[]; - forEach(object: any, iterator: ListIterator, context?: any): any[]; - - map(list: List, iterator: ListIterator, context?: any): any[]; - map(object: any, iterator: ObjectIterator, context?: any): any[]; - collect(list: List, iterator: ListIterator, context?: any): any[]; - collect(object: any, iterator: ObjectIterator, context?: any): any[]; - - reduce(list: List, iterator: any, memo: any, context?: any): any; - reduce(list: any[], iterator: any, memo: any, context?: any): any; - inject(list: List, iterator: any, memo: any, context?: any): any; - inject(list: any[], iterator: any, memo: any, context?: any): any; - foldl(list: List, iterator: any, memo: any, context?: any): any; - foldl(list: any[], iterator: any, memo: any, context?: any): any; - - reduceRight(list: List, iterator: any, memo: any, context?: any): any[]; - reduceRight(list: any[], iterator: any, memo: any, context?: any): any[]; - foldr(list: List, iterator: any, memo: any, context?: any): any[]; - foldr(list: any[], iterator: any, memo: any, context?: any): any[]; - - find(list: List, iterator: any, context?: any): any; - find(list: any[], iterator: any, context?: any): any; - detect(list: List, iterator: any, context?: any): any; - detect(list: any[], iterator: any, context?: any): any; - - filter(list: List, iterator: any, context?: any): any[]; - filter(list: any[], iterator: any, context?: any): any[]; - select(list: List, iterator: any, context?: any): any[]; - select(list: any[], iterator: any, context?: any): any[]; - - where(list: List, properties: any): any[]; - where(list: any[], properties: any): any[]; - - reject(list: List, iterator: any, context?: any): any[]; - reject(list: any[], iterator: any, context?: any): any[]; - - all(list: List, iterator: any, context?: any): bool; - all(list: any[], iterator: any, context?: any): bool; - every(list: List, iterator: any, context?: any): bool; - every(list: any[], iterator: any, context?: any): bool; - - any(list: List, iterator?: any, context?: any): bool; - any(list: any[], iterator?: any, context?: any): bool; - some(list: List, iterator?: any, context?: any): bool; - some(list: any[], iterator?: any, context?: any): bool; - - contains(list: any, value: any): bool; - contains(list: List, value: any): bool; - include(list: any, value: any): bool; - include(list: List, value: any): bool; - - invoke(list: List, methodName: string, arguments: any[]): any; - invoke(object: any, methodName: string, ...arguments: any[]): any; - - pluck(list: List, propertyName: string): string[]; - pluck(list: any[], propertyName: string): string[]; - max(list: List, iterator?: any, context?: any): any; - max(list: any[], iterator?: any, context?: any): any; - min(list: List, iterator?: any, context?: any): any; - min(list: any[], iterator?: any, context?: any): any; - sortBy(list: List, iterator?: any, context?: any): any; - sortBy(list: any[], iterator?: any, context?: any): any; - groupBy(list: List, iterator: any): any; - groupBy(list: any[], iterator: any): any; - countBy(list: List, iterator: any): any; - countBy(list: any[], iterator: any): any; - shuffle(list: any[]): any[]; - toArray(list: any): any[]; - size(list: any): number; - - /**** - Arrays - *****/ - first(array: List, n?: number): any; - first(array: any[], n?: number): any; - head(array: List, n?: number): any; - head(array: any[], n?: number): any; - take(array: List, n?: number): any; - take(array: any[], n?: number): any; - - initial(array: List, n?: number): any[]; - initial(array: any[], n?: number): any[]; - - last(array: List, n?: number): any; - last(array: any[], n?: number): any; - - rest(array: List, n?: number): any[]; - rest(array: any[], n?: number): any[]; - tail(array: List, n?: number): any[]; - tail(array: any[], n?: number): any[]; - drop(array: List, n?: number): any[]; - drop(array: any[], n?: number): any[]; - - compact(array: any[]): any[]; - flatten(array: List, shallow?: bool): any[]; - flatten(array: any[], shallow?: bool): any[]; - without(array: List, ...values: any[]): any[]; - without(array: any[], ...values: any[]): any[]; - union(...arrays: any[][]): any[]; - intersection(...arrays: any[][]): any[]; - difference(array: List, ...others: any[][]): any[]; - difference(array: any[], ...others: any[][]): any[]; - - uniq(array: List, isSorted?: bool, iterator?: any): any[]; - uniq(array: any[], isSorted?: bool, iterator?: any): any[]; - unique(array: List, isSorted?: bool, iterator?: any): any[]; - unique(array: any[], isSorted?: bool, iterator?: any): any[]; - - zip(...arrays: any[]): any[]; - object(list: List, values?: any[]): any; - object(list: any[], values?: any[]): any; - indexOf(array: List, value: any, isSorted?: bool): number; - indexOf(array: any[], value: any, isSorted?: bool): number; - lastIndexOf(array: List, value: any, fromIndex?: number): number; - lastIndexOf(array: any[], value: any, fromIndex?: number): number; - sortedIndex(list: List, valueL: any, iterator?: any): number; - sortedIndex(list: any[], valueL: any, iterator?: any): number; - range(stop: number): any[]; - range(start: number, stop: number, step?: number): any[]; - - /**** - Functions - *****/ - bind(func: (...as : any[]) => any, context: any, ...arguments: any[]): () => any; - bindAll(object: any, ...methodNames: string[]): any; - memoize(func: any, hashFunction?: any): any; - defer(func: () => any); - delay(func: any, wait: number, ...arguments: any[]): any; - delay(func: any, ...arguments: any[]): any; - throttle(func: any, wait: number): any; - debounce(func: any, wait: number, immediate?: bool): any; - once(func: any): any; - after(count: number, func: any): any; - wrap(func: (...as : any[]) => any, wrapper: any): () => any; - compose(...functions: any[]): any; - - /**** - Objects - *****/ - keys(object: any): any[]; - values(object: any): any[]; - pairs(object: any): any[]; - invert(object: any): any; - - functions(object: any): string[]; - methods(object: any): string[]; - - extend(destination: any, ...sources: any[]): any; - pick(object: any, ...keys: string[]): any; - omit(object: any, ...keys: string[]): any; - defaults(object: any, ...defaults: any[]): any; - clone(object: any): any; - tap(object: any, interceptor: (...as : any[]) => any): any; - has(object: any, key: string): bool; - isEqual(object: any, other: any): bool; - isEmpty(object: any): bool; - isElement(object: any): bool; - isArray(object: any): bool; - isObject(value: any): bool; - isArguments(object: any): bool; - isFunction(object: any): bool; - isString(object: any): bool; - isNumber(object: any): bool; - isFinite(object: any): bool; - isBoolean(object: any): bool; - isDate(object: any): bool; - isRegExp(object: any): bool; - isNaN(object: any): bool; - isNull(object: any): bool; - isUndefined(value: any): bool; - - /**** - Utility - *****/ - noConflict(): any; - identity(value: any): any; - times(n: number, iterator: (index : number) => void, context?: any): void; - random(min: number, max: number): number; - mixin(object: any): void; - uniqueId(prefix: string): string; - uniqueId(): number; - escape(str: string): string; - result(object: any, property: string): any; - templateSettings: TemplateSettings; - template(templateString: string, data?: any, settings?: any): (...data: any[]) => string; - - /**** - Chaining - *****/ - chain(object: any): UnderscoreWrappedObject; +interface Dictionary extends Collection { + [index: string]: T; } -declare var _: UnderscoreStatic; \ No newline at end of file +declare module "underscore" { + function underscore(arg?: any): any; + + module underscore { + /**** + Collections + *****/ + export function each(list: List, iterator: ListIterator, context?: any): void; + export function each(object: Dictionary, iterator: ObjectIterator, context?: any): void; + export function forEach(list: List, iterator: ListIterator, context?: any): void; + export function forEach(object: Dictionary, iterator: ObjectIterator, context?: any): void; + + export function map(list: List, iterator: ListIterator, context?: any): TResult[]; + export function map(object: Dictionary, iterator: ObjectIterator, context?: any): TResult[]; + export function collect(list: List, iterator: ListIterator, context?: any): TResult[]; + export function collect(object: Dictionary, iterator: ObjectIterator, context?: any): TResult[]; + + export function reduce(list: Collection, iterator: (prev: TResult, curr: T) => TResult, memo: TResult, context?: any): TResult; + export function inject(list: Collection, iterator: (prev: TResult, curr: T) => TResult, memo: TResult, context?: any): TResult; + + export function reduceRight(list: Collection, iterator: (prev: TResult, curr: T) => TResult, memo: TResult, context?: any): TResult; + export function foldr(list: Collection, iterator: (prev: TResult, curr: T) => TResult, memo: TResult, context?: any): TResult; + + export function find(list: Collection, iterator: (x: T) => boolean, context?: any): T; + export function detect(list: Collection, iterator: (x: T) => boolean, context?: any): T; + + export function filter(list: Collection, iterator: (x: T) => boolean, context?: any): T[]; + export function select(list: Collection, iterator: (x: T) => boolean, context?: any): T[]; + + export function where(list: Collection, properties: any): T[]; + + export function reject(list: Collection, iterator: (x: T) => boolean, context?: any): T[]; + + export function all(list: Collection, iterator: (x: T) => boolean, context?: any): boolean; + export function every(list: Collection, iterator: (x: T) => boolean, context?: any): boolean; + + export function any(list: Collection, iterator?: (x: T) => boolean, context?: any): boolean; + export function some(list: Collection, iterator?: (x: T) => boolean, context?: any): boolean; + + export function contains(list: Collection, value: T): boolean; + export function include(list: Collection, value: T): boolean; + + export function invoke(list: Collection, methodName: string, ...arguments: any[]): any; + + export function pluck(list: Collection, propertyName: string): any[]; + + export function max(list: Collection, iterator?: (x: T) => any, context?: any): T; + + export function min(list: Collection, iterator?: (x: T) => any, context?: any): T; + + export function sortBy(list: List, iterator?: (x: T) => any, context?: any): T[]; + export function sortBy(list: any, iterator?: (x: T) => any, context?: any): T[]; + export function sortBy(list: List, iterator: string, context?: any): T[]; + export function sortBy(list: any, iterator: string, context?: any): T[]; + + export function groupBy(list: List, iterator?: (x: T) => any, context?: any): Dictionary>; + export function groupBy(list: any, iterator?: (x: T) => any, context?: any): Dictionary>; + export function groupBy(list: List, iterator: string, context?: any): Dictionary>; + export function groupBy(list: any, iterator: string, context?: any): Dictionary>; + + export function countBy(list: List, iterator?: (x: T) => any, context?: any): Dictionary>; + export function countBy(list: any, iterator?: (x: T) => any, context?: any): Dictionary>; + export function countBy(list: List, iterator: string, context?: any): Dictionary>; + export function countBy(list: any, iterator: string, context?: any): Dictionary>; + + export function shuffle(list: Collection): T[]; + + export function toArray(list: Collection): T[]; + + export function size(list: Collection): number; + + /**** + Arrays + *****/ + export function first(array: List): T; + export function first(array: List, n: number): T[]; + export function head(array: List): T; + export function head(array: List, n: number): T[]; + export function take(array: List): T; + export function take(array: List, n: number): T[]; + + export function initial(array: List, n?: number): T[]; + + export function last(array: List): T; + export function last(array: List, n: number): T[]; + + export function rest(array: List, n?: number): T[]; + export function tail(array: List, n?: number): T[]; + export function drop(array: List, n?: number): T[]; + + export function compact(array: List): T[]; + + export function flatten(array: any[], shallow?: boolean): any[]; + + export function without(array: List, ...values: T[]): T[]; + + export function union(...arrays: List[]): T[]; + + export function intersection(...arrays: List[]): T[]; + + export function difference(array: List, ...others: List[]): T[]; + + export function uniq(array: List, isSorted?: boolean, iterator?: (x: T) => any): T[]; + export function unique(array: List, isSorted?: boolean, iterator?: (x: T) => any): T[]; + + export function zip(...arrays: any[]): any[]; + + export function object(list: any[], values?: any): any; + + export function indexOf(array: List, value: T, isSorted?: boolean): number; + export function indexOf(array: List, value: T, startFrom: number): number; + + export function lastIndexOf(array: List, value: T, fromIndex?: number): number; + + export function sortedIndex(list: List, value: T, iterator?: (x: T) => any, context?: any): number; + + export function range(stop: number): any[]; + export function range(start: number, stop: number, step?: number): any[]; + + /**** + Functions + *****/ + export function bind(func: (...as: any[]) => any, context: any, ...arguments: any[]): () => any; + export function bindAll(object: any, ...methodNames: string[]): any; + export function memoize(func: any, hashFunction?: any): any; + export function defer(func: () => any); + export function delay(func: any, wait: number, ...arguments: any[]): any; + export function delay(func: any, ...arguments: any[]): any; + export function throttle(func: any, wait: number): any; + export function debounce(func: any, wait: number, immediate?: boolean): any; + export function once(func: any): any; + export function after(count: number, func: any): any; + export function wrap(func: (...as: any[]) => any, wrapper: any): () => any; + export function compose(...functions: any[]): any; + + /**** + Objects + *****/ + export function keys(object: any): string[]; + export function values(object: Dictionary): T[]; + export function pairs(object: any): any[]; + export function invert(object: any): any; + export function functions(object: any): string[]; + export function methods(object: any): string[]; + export function extend(destination: any, ...sources: any[]): any; + export function pick(object: any, ...keys: string[]): any; + export function omit(object: any, ...keys: string[]): any; + export function defaults(object: any, ...defaults: any[]): any; + export function clone(object: any): any; + export function tap(object: any, interceptor: (...as: any[]) => any): any; + export function has(object: any, key: string): boolean; + export function isEqual(object: any, other: any): boolean; + export function isEmpty(object: any): boolean; + export function isElement(object: any): boolean; + export function isArray(object: any): boolean; + export function isObject(value: any): boolean; + export function isArguments(object: any): boolean; + export function isFunction(object: any): boolean; + export function isString(object: any): boolean; + export function isNumber(object: any): boolean; + export function isFinite(object: any): boolean; + export function isBoolean(object: any): boolean; + export function isDate(object: any): boolean; + export function isRegExp(object: any): boolean; + export function isNaN(object: any): boolean; + export function isNull(object: any): boolean; + export function isUndefined(value: any): boolean; + + /**** + Utility + *****/ + export function noConflict(): any; + export function identity(value: any): any; + export function times(n: number, iterator: (index: number) => void , context?: any): void; + export function random(min: number, max: number): number; + export function mixin(object: any): void; + export function uniqueId(prefix: string): string; + export function uniqueId(): number; + export function escape(str: string): string; + export function result(object: any, property: string): any; + export var templateSettings: TemplateSettings; + export function template(templateString: string, data?: any, settings?: any): (...data: any[]) => string; + + /**** + Chaining + *****/ + export function chain(object: any): UnderscoreWrappedObject; + } + + export = underscore; +} \ No newline at end of file