diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index fd335b7361..432b47ea04 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -166,6 +166,8 @@ _.some({ a: 'a', b: 'B', c: 'C', d: 'd' }, l => l === l.toUpperCase()); _.contains([1, 2, 3], 3); +_.contains([1, 2, 3], 3, 1); + _.invoke([[5, 1, 7], [3, 2, 1]], 'sort'); var stooges = [{ name: 'moe', age: 40 }, { name: 'larry', age: 50 }, { name: 'curly', age: 60 }]; @@ -278,10 +280,12 @@ _.defer(function () { alert('deferred'); }); var updatePosition = (param:string) => alert('updating position... Param: ' + param); var throttled = _.throttle(updatePosition, 100); $(window).scroll(throttled); +throttled.cancel(); var calculateLayout = (param:string) => alert('calculating layout... Param: ' + param); var lazyLayout = _.debounce(calculateLayout, 300); $(window).resize(lazyLayout); +lazyLayout.cancel(); var createApplication = (param:string) => alert('creating application... Param: ' + param); var initialize = _.once(createApplication); @@ -406,6 +410,7 @@ function useBoolean(arg: Boolean) {}; function useDate(arg: Date) {}; function useRegExp(arg: RegExp) {}; function useArray(arg: T[]) {}; +function useSymbol(arg: symbol) {}; var guardedType: {}; if(_.isElement(guardedType)) useElement(guardedType); @@ -419,6 +424,7 @@ if(_.isNumber(guardedType)) useNumber(guardedType); if(_.isBoolean(guardedType)) useBoolean(guardedType); if(_.isDate(guardedType)) useDate(guardedType); if(_.isRegExp(guardedType)) useRegExp(guardedType); +if(_.isSymbol(guardedType)) useSymbol(guardedType); /////////////////////////////////////////////////////////////////////////////////////// diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 0891d6d304..d8d08a3b75 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Underscore 1.7.0 +// Type definitions for Underscore 1.8.3 // Project: http://underscorejs.org/ // Definitions by: Boris Yankov , Josh Baldwin , Christopher Currens // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -74,6 +74,10 @@ declare module _ { interface MemoObjectIterator { (prev: TResult, curr: T, key: string, list: Dictionary): TResult; } + + interface Cancelable { + cancel() : void; + } } interface UnderscoreStatic { @@ -458,10 +462,10 @@ interface UnderscoreStatic { object: _.Dictionary, iterator?: _.ObjectIterator, context?: any): boolean; - + any( list: _.List, - value: T): boolean; + value: T): boolean; /** * Returns true if the value is present in the list. Uses indexOf internally, @@ -472,7 +476,8 @@ interface UnderscoreStatic { **/ contains( list: _.List, - value: T): boolean; + value: T, + fromIndex?: number): boolean; /** * @see _.contains @@ -486,7 +491,8 @@ interface UnderscoreStatic { **/ include( list: _.Collection, - value: T): boolean; + value: T, + fromIndex?: number): boolean; /** * @see _.contains @@ -495,6 +501,21 @@ interface UnderscoreStatic { object: _.Dictionary, value: T): boolean; + /** + * @see _.contains + **/ + includes( + list: _.Collection, + value: T, + fromIndex?: number): boolean; + + /** + * @see _.contains + **/ + includes( + object: _.Dictionary, + value: T): boolean; + /** * Calls the method named by methodName on each value in the list. Any extra arguments passed to * invoke will be forwarded on to the method invocation. @@ -1055,6 +1076,14 @@ interface UnderscoreStatic { **/ range(stop: number): number[]; + /** + * Split an **array** into several arrays containing **count** or less elements + * of initial array. + * @param array The array to split + * @param count The maximum size of the inner arrays. + */ + chunk(array: _.Collection, count: number): (_.Collection)[] + /************* * Functions * *************/ @@ -3433,7 +3462,7 @@ interface UnderscoreStatic { throttle( func: T, wait: number, - options?: _.ThrottleSettings): T; + options?: _.ThrottleSettings): T & _.Cancelable; /** * Creates and returns a new debounced version of the passed function that will postpone its execution @@ -3452,7 +3481,7 @@ interface UnderscoreStatic { debounce( fn: T, wait: number, - immediate?: boolean): T; + immediate?: boolean): T & _.Cancelable; /** * Creates a version of the function that can only be called one time. Repeated calls to the modified @@ -3463,6 +3492,12 @@ interface UnderscoreStatic { **/ once(fn: T): T; + /** + * Similar to ES6's rest param (http://ariya.ofilabs.com/2013/03/es6-and-rest-parameter.html) + * This accumulates the arguments passed into an array, after a given index. + **/ + restArgs(func: Function, starIndex?: number) : Function; + /** * 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, @@ -3624,6 +3659,14 @@ interface UnderscoreStatic { destination: any, ...source: any[]): any; + /** + * Returns the first key on an object that passes a predicate test. + * @param obj the object to search in + * @param predicate Predicate function. + * @param context `this` object in `iterator`, optional. + */ + findKey(obj: _.Dictionary, predicate: _.ObjectIterator, context? : any): T + /** * Return a copy of the object, filtered to only have values for the whitelisted keys * (or array of valid keys). @@ -3677,6 +3720,16 @@ interface UnderscoreStatic { object: any, ...defaults: any[]): any; + + /** + * Creates an object that inherits from the given prototype object. + * If additional properties are provided then they will be added to the + * created object. + * @param prototype The prototype that the returned object will inherit from. + * @param props Additional props added to the returned object. + **/ + create(prototype: any, props?: Object): any; + /** * Create a shallow-copied clone of the object. * Any nested objects or arrays will be copied by reference, not duplicated. @@ -3710,6 +3763,14 @@ interface UnderscoreStatic { **/ matches(attrs: T): _.ListIterator; + /** + * Returns a predicate function that will tell you if a passed in object contains all of the key/value properties present in attrs. + * @see _.matches + * @param attrs Object with key values pair + * @return Predicate function + **/ + matcher(attrs: T): _.ListIterator; + /** * Returns a function that will itself return the key property of any passed-in object. * @param key Property of the object. @@ -3760,7 +3821,7 @@ interface UnderscoreStatic { * @param object Check if this object is an Array. * @return True if `object` is an Array, otherwise false. **/ - isArray(object: any): object is []; + isArray(object: any): object is any[]; /** * Returns true if object is an Array. @@ -3769,6 +3830,13 @@ interface UnderscoreStatic { **/ isArray(object: any): object is T[]; + /** + * Returns true if object is a Symbol. + * @param object Check if this object is a Symbol. + * @return True if `object` is a Symbol, otherwise false. + **/ + isSymbol(object: any): object is symbol; + /** * Returns true if value is an Object. Note that JavaScript arrays and functions are objects, * while (normal) strings and numbers are not. @@ -3935,11 +4003,10 @@ interface UnderscoreStatic { * 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: Function, context?: any): Function; iteratee(value: Object): Function; /** @@ -4178,13 +4245,19 @@ interface Underscore { * Wrapped type `any[]`. * @see _.contains **/ - contains(value: T): boolean; + contains(value: T, fromIndex? : number): boolean; /** * Alias for 'contains'. * @see contains **/ - include(value: T): boolean; + include(value: T, fromIndex? : number): boolean; + + /** + * Alias for 'contains'. + * @see contains + **/ + includes(value: T, fromIndex? : number): boolean; /** * Wrapped type `any[]`. @@ -4513,6 +4586,12 @@ interface Underscore { **/ range(): number[]; + /** + * Wrapped type any[][]. + * @see _.chunk + **/ + chunk(): any[][]; + /* *********** * Functions * ************ */ @@ -4562,13 +4641,13 @@ interface Underscore { * Wrapped type `Function`. * @see _.throttle **/ - throttle(wait: number, options?: _.ThrottleSettings): Function; + throttle(wait: number, options?: _.ThrottleSettings): Function & _.Cancelable; /** * Wrapped type `Function`. * @see _.debounce **/ - debounce(wait: number, immediate?: boolean): Function; + debounce(wait: number, immediate?: boolean): Function & _.Cancelable; /** * Wrapped type `Function`. @@ -4576,6 +4655,12 @@ interface Underscore { **/ once(): Function; + /** + * Wrapped type `Function`. + * @see _.once + **/ + restArgs(starIndex?: number) : Function; + /** * Wrapped type `number`. * @see _.after @@ -4657,6 +4742,12 @@ interface Underscore { **/ extend(...sources: any[]): any; + /** + * Wrapped type `object`. + * @see _.extend + **/ + findKey(predicate: _.ObjectIterator, context? : any): any + /** * Wrapped type `object`. * @see _.pick @@ -4679,6 +4770,12 @@ interface Underscore { **/ defaults(...defaults: any[]): any; + /** + * Wrapped type `any`. + * @see _.create + **/ + create(props?: Object): any; + /** * Wrapped type `any[]`. * @see _.clone @@ -4703,6 +4800,12 @@ interface Underscore { **/ matches(): _.ListIterator; + /** + * Wrapped type `any[]`. + * @see _.matcher + **/ + matcher(): _.ListIterator; + /** * Wrapped type `string`. * @see _.property @@ -4745,6 +4848,12 @@ interface Underscore { **/ isArray(): boolean; + /** + * Wrapped type `object`. + * @see _.isSymbol + **/ + isSymbol(): boolean; + /** * Wrapped type `object`. * @see _.isObject @@ -4872,7 +4981,7 @@ interface Underscore { * Wrapped type `string|Function|Object`. * @see _.iteratee **/ - iteratee(context?: any, argCount?: number): Function; + iteratee(context?: any): Function; /** * Wrapped type `string`. @@ -5096,13 +5205,19 @@ interface _Chain { * Wrapped type `any[]`. * @see _.contains **/ - contains(value: T): _ChainSingle; + contains(value: T, fromIndex?: number): _ChainSingle; /** * Alias for 'contains'. * @see contains **/ - include(value: T): _ChainSingle; + include(value: T, fromIndex?: number): _ChainSingle; + + /** + * Alias for 'contains'. + * @see contains + **/ + includes(value: T, fromIndex?: number): _ChainSingle; /** * Wrapped type `any[]`. @@ -5431,6 +5546,12 @@ interface _Chain { **/ range(): _Chain; + /** + * Wrapped type `any[][]`. + * @see _.chunk + **/ + chunk(): _Chain; + /* *********** * Functions * ************ */ @@ -5494,6 +5615,12 @@ interface _Chain { **/ once(): _Chain; + /** + * Wrapped type `Function`. + * @see _.once + **/ + restArgs(startIndex? : number): _Chain; + /** * Wrapped type `number`. * @see _.after @@ -5575,6 +5702,12 @@ interface _Chain { **/ extend(...sources: any[]): _Chain; + /** + * Wrapped type `object`. + * @see _.extend + **/ + findKey(predicate: _.ObjectIterator, context? : any): _Chain + /** * Wrapped type `object`. * @see _.pick @@ -5597,6 +5730,12 @@ interface _Chain { **/ defaults(...defaults: any[]): _Chain; + /** + * Wrapped type `any`. + * @see _.create + **/ + create(props?: Object): _Chain; + /** * Wrapped type `any[]`. * @see _.clone @@ -5621,6 +5760,12 @@ interface _Chain { **/ matches(): _Chain; + /** + * Wrapped type `any[]`. + * @see _.matcher + **/ + matcher(): _Chain; + /** * Wrapped type `string`. * @see _.property @@ -5663,6 +5808,12 @@ interface _Chain { **/ isArray(): _Chain; + /** + * Wrapped type `object`. + * @see _.isSymbol + **/ + isSymbol(): _Chain; + /** * Wrapped type `object`. * @see _.isObject @@ -5790,7 +5941,7 @@ interface _Chain { * Wrapped type `string|Function|Object`. * @see _.iteratee **/ - iteratee(context?: any, argCount?: number): _Chain; + iteratee(context?: any): _Chain; /** * Wrapped type `string`.