diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index 2e550ee825..3fb993a3d3 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -1,6 +1,6 @@ /// -declare var $; +declare var $: any; _.each([1, 2, 3], (num) => alert(num.toString())); _.each({ one: 1, two: 2, three: 3 }, (value, key) => alert(value.toString())); @@ -296,7 +296,7 @@ _.templateSettings = { }; var template2 = _.template("Hello {{ name }}!"); template2({ name: "Mustache" }); -_.template("Using 'with': <%= data.answer %>", { answer: 'no' }, { variable: 'data' }); +_.template("Using 'with': <%= data.answer %>", { variable: 'data' }); _(['test', 'test']).pick(['test2', 'test2']); @@ -333,3 +333,15 @@ function chain_tests() { .first() .value(); } + +var obj: { [k: string] : number } = { + 'test' : 5, + 'another' : 8, + 'third' : 10 + }, + empty = {}; + +_.chain(obj).map(function (value, key) { + empty[key] = value; + console.log("vk", value, key); +}); diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 511eb3e71c..010932c606 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Underscore 1.6.0 +// Type definitions for Underscore 1.7.0 // Project: http://underscorejs.org/ // Definitions by: Boris Yankov , Josh Baldwin // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -1082,13 +1082,24 @@ interface UnderscoreStatic { * Creates a version of the function that will only be run after first being called count times. Useful * for grouping asynchronous responses, where you want to be sure that all the async calls have finished, * before proceeding. - * @param count Number of times to be called before actually executing. - * @fn The function to defer execution `count` times. + * @param number count Number of times to be called before actually executing. + * @param Function fn The function to defer execution `count` times. * @return Copy of `fn` that will not execute until it is invoked `count` times. **/ - after( + after( count: number, - fn: T): T; + fn: Function): Function; + + /** + * Creates a version of the function that can be called no more than count times. The result of + * the last function call is memoized and returned when count has been reached. + * @param number count The maxmimum number of times the function can be called. + * @param Function fn The function to limit the number of times it can be called. + * @return Copy of `fn` that can only be called `count` times. + **/ + before( + count: number, + fn: Function): Function; /** * Wraps the first function inside of the wrapper function, passing it as the first argument. This allows @@ -1102,6 +1113,13 @@ interface UnderscoreStatic { fn: Function, wrapper: (fn: Function, ...args: any[]) => any): Function; + /** + * Returns a negated version of the pass-in predicate. + * @param Function predicate + * @return boolean + **/ + negate(predicate: Function): boolean; + /** * Returns the composition of a list of functions, where each function consumes the return value of the * function that follows. In math terms, composing the functions f(), g(), and h() produces f(g(h())). @@ -1179,6 +1197,13 @@ interface UnderscoreStatic { object: any, ...keys: any[]): any; + /** + * @see _.pick + **/ + pick( + object: any, + fn: (value: any, key: any, object: any) => any): any; + /** * Return a copy of the object, filtered to omit the blacklisted keys (or array of keys). * @param object Object to strip unwanted key/value pairs. @@ -1196,6 +1221,13 @@ interface UnderscoreStatic { object: any, keys: string[]): any; + /** + * @see _.omit + **/ + omit( + object: any, + iteratee: Function): any; + /** * Fill in null and undefined properties in object with values from the defaults objects, * and return the object. As soon as the property is filled, further defaults will have no effect. @@ -1390,6 +1422,14 @@ interface UnderscoreStatic { **/ constant(value: T): () => T; + /** + * Returns undefined irrespective of the arguments passed to it. Useful as the default + * for optional callback arguments. + * Note there is no way to indicate a 'undefined' return, so it is currently typed as void. + * @return undefined + **/ + noop(): void; + /** * Invokes the given iterator function n times. * Each invocation of iterator is called with an index argument @@ -1422,6 +1462,19 @@ interface UnderscoreStatic { **/ mixin(object: any): void; + /** + * A mostly-internal function to generate callbacks that can be applied to each element + * in a collection, returning the desired result -- either identity, an arbitrary callback, + * a property matcher, or a propetery accessor. + * @param string|Function|Object value The value to iterate over, usually the key. + * @param any context + * @param number argCount + * @return Callback that can be applied to each element in a collection. + **/ + iteratee(value: string): Function; + iteratee(value: Function, context?: any, argCount?: number): Function; + iteratee(value: Object): Function; + /** * Generate a globally-unique id for client-side models or DOM elements that need one. * If prefix is passed, the id will be appended to it. Without prefix, returns an integer. @@ -1473,7 +1526,6 @@ interface UnderscoreStatic { * @return Returns the compiled Underscore HTML template. **/ template(templateString: string, settings?: _.TemplateSettings): (...data: any[]) => string; - template(templateString: string, data: any, settings?: _.TemplateSettings): string; /** * By default, Underscore uses ERB-style template delimiters, change the @@ -2035,7 +2087,13 @@ interface Underscore { * Wrapped type `number`. * @see _.after **/ - after(func: Function): Function; + after(fn: Function): Function; + + /** + * Wrapped type `number`. + * @see _.before + **/ + before(fn: Function): Function; /** * Wrapped type `Function`. @@ -2043,6 +2101,12 @@ interface Underscore { **/ wrap(wrapper: Function): () => Function; + /** + * Wrapped type `Function`. + * @see _.negate + **/ + negate(): boolean; + /** * Wrapped type `Function[]`. * @see _.compose @@ -2100,6 +2164,7 @@ interface Underscore { **/ pick(...keys: string[]): any; pick(keys: string[]): any; + pick(fn: (value: any, key: any, object: any) => any): any; /** * Wrapped type `object`. @@ -2107,6 +2172,7 @@ interface Underscore { **/ omit(...keys: string[]): any; omit(keys: string[]): any; + omit(fn: Function): any; /** * Wrapped type `object`. @@ -2256,6 +2322,12 @@ interface Underscore { **/ constant(): () => T; + /** + * Wrapped type `any`. + * @see _.noop + **/ + noop(): void; + /** * Wrapped type `number`. * @see _.times @@ -2279,6 +2351,12 @@ interface Underscore { **/ mixin(): void; + /** + * Wrapped type `string|Function|Object`. + * @see _.iteratee + **/ + iteratee(context?: any, argCount?: number): Function; + /** * Wrapped type `string`. * @see _.uniqueId @@ -2307,7 +2385,7 @@ interface Underscore { * Wrapped type `string`. * @see _.template **/ - template(data?: any, settings?: _.TemplateSettings): (...data: any[]) => string; + template(settings?: _.TemplateSettings): (...data: any[]) => string; /********** * * Chaining * @@ -2868,12 +2946,24 @@ interface _Chain { **/ after(func: Function): _Chain; + /** + * Wrapped type `number`. + * @see _.before + **/ + before(fn: Function): _Chain; + /** * Wrapped type `Function`. * @see _.wrap **/ wrap(wrapper: Function): () => _Chain; + /** + * Wrapped type `Function`. + * @see _.negate + **/ + negate(): _Chain; + /** * Wrapped type `Function[]`. * @see _.compose @@ -2930,12 +3020,15 @@ interface _Chain { * @see _.pick **/ pick(...keys: string[]): _Chain; + pick(fn: (value: any, key: any, object: any) => any): _Chain; /** * Wrapped type `object`. * @see _.omit **/ omit(...keys: string[]): _Chain; + omit(keys: string[]): _Chain; + omit(iteratee: Function): _Chain; /** * Wrapped type `object`. @@ -3085,6 +3178,12 @@ interface _Chain { **/ constant(): _Chain; + /** + * Wrapped type `any`. + * @see _.noop + **/ + noop(): _Chain; + /** * Wrapped type `number`. * @see _.times @@ -3108,6 +3207,12 @@ interface _Chain { **/ mixin(): _Chain; + /** + * Wrapped type `string|Function|Object`. + * @see _.iteratee + **/ + iteratee(context?: any, argCount?: number): _Chain; + /** * Wrapped type `string`. * @see _.uniqueId @@ -3136,7 +3241,7 @@ interface _Chain { * Wrapped type `string`. * @see _.template **/ - template(data?: any, settings?: _.TemplateSettings): (...data: any[]) => _Chain; + template(settings?: _.TemplateSettings): (...data: any[]) => _Chain; /********** * * Chaining *