From 06958b51fa273fc09b2f701e27b6d0c90856276c Mon Sep 17 00:00:00 2001 From: dcrusader Date: Mon, 5 Jan 2015 17:58:36 -0800 Subject: [PATCH 1/3] Proxy array functions to _Chain Underscore [proxies array functions](http://underscorejs.org/#chaining) through _.chain() --- underscore/underscore.d.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 4434f633c..36e95f68a 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -3243,6 +3243,25 @@ interface _Chain { **/ template(settings?: _.TemplateSettings): (...data: any[]) => _Chain; + /************* * + * Array proxy * + ************** */ + + concat(...arr: Array): _Chain; + indexOf(item: T): _Chain; + join(separator?: any): _Chain; + lastIndexOf(item: T): _Chain; + pop(): _Chain; + push(...item: Array): _Chain; + reverse(): _Chain; + shift(): _Chain; + slice(start: number, end?: number): _Chain; + sort(compareFn: (a: T, b: T) => boolean): _Chain; + splice(index: number, quantity: number, ...item: Array): _Chain; + toString(): _Chain; + unshift(...item: Array): _Chain; + valueOf(): _Chain; + /********** * * Chaining * *********** */ From 2f8ec2a4887b15b666158b74c5807c46f5653e2c Mon Sep 17 00:00:00 2001 From: dcrusader Date: Wed, 7 Jan 2015 09:12:02 -0800 Subject: [PATCH 2/3] Removed already existing array functions Removed already existing array functions, added documentation comments from Mozilla Fixed indexOf and lastIndexOf to return ChainSingle instead of Chain --- underscore/underscore.d.ts | 90 ++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 14 deletions(-) diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 36e95f68a..c118e8020 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -2846,18 +2846,18 @@ interface _Chain { * Wrapped type `any[]`. * @see _.indexOf **/ - indexOf(value: T, isSorted?: boolean): _Chain; + indexOf(value: T, isSorted?: boolean): _ChainSingle; /** * @see _.indexOf **/ - indexOf(value: T, startFrom: number): _Chain; + indexOf(value: T, startFrom: number): _ChainSingle; /** * Wrapped type `any[]`. * @see _.lastIndexOf **/ - lastIndexOf(value: T, from?: number): _Chain; + lastIndexOf(value: T, from?: number): _ChainSingle; /** * Wrapped type `any[]`. @@ -3244,23 +3244,85 @@ interface _Chain { template(settings?: _.TemplateSettings): (...data: any[]) => _Chain; /************* * - * Array proxy * + * Array proxy * ************** */ + /** + * Returns a new array comprised of the array on which it is called + * joined with the array(s) and/or value(s) provided as arguments. + * @param arr Arrays and/or values to concatenate into a new array. See the discussion below for details. + * @return A new array comprised of the array on which it is called + **/ concat(...arr: Array): _Chain; - indexOf(item: T): _Chain; - join(separator?: any): _Chain; - lastIndexOf(item: T): _Chain; - pop(): _Chain; - push(...item: Array): _Chain; + + /** + * Join all elements of an array into a string. + * @param separator Optional. Specifies a string to separate each element of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma. + * @return The string conversions of all array elements joined into one string. + **/ + join(separator?: any): _ChainSingle; + + /** + * Removes the last element from an array and returns that element. + * @return Returns the popped element. + **/ + pop(): _ChainSingle; + + /** + * Adds one or more elements to the end of an array and returns the new length of the array. + * @param item The elements to add to the end of the array. + * @return The new length property of the object upon which the method was called. + **/ + push(...item: Array): _ChainSingle; + + /** + * Reverses an array in place. The first array element becomes the last and the last becomes the first. + * @return The reversed array. + **/ reverse(): _Chain; - shift(): _Chain; + + /** + * Removes the first element from an array and returns that element. This method changes the length of the array. + * @return The shifted element. + **/ + shift(): _ChainSingle; + + /** + * Returns a shallow copy of a portion of an array into a new array object. + * @param start Zero-based index at which to begin extraction. + * @param end Optional. Zero-based index at which to end extraction. slice extracts up to but not including end. + * @return A shallow copy of a portion of an array into a new array object. + **/ slice(start: number, end?: number): _Chain; + + /** + * Sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points. + * @param compareFn Optional. Specifies a function that defines the sort order. If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element. + * @return The sorted array. + **/ sort(compareFn: (a: T, b: T) => boolean): _Chain; - splice(index: number, quantity: number, ...item: Array): _Chain; - toString(): _Chain; - unshift(...item: Array): _Chain; - valueOf(): _Chain; + + /** + * Changes the content of an array by removing existing elements and/or adding new elements. + * @param index Index at which to start changing the array. If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end. + * @param quantity An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at index, then all of the elements through the end of the array will be deleted. + * @param items The element to add to the array. If you don't specify any elements, splice will only remove elements from the array. + * @return An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned. + **/ + splice(index: number, quantity: number, ...items: Array): _Chain; + + /** + * A string representing the specified array and its elements. + * @return A string representing the specified array and its elements. + **/ + toString(): _ChainSingle; + + /** + * Adds one or more elements to the beginning of an array and returns the new length of the array. + * @param items The elements to add to the front of the array. + * @return The new length property of the object upon which the method was called. + **/ + unshift(...items: Array): _ChainSingle; /********** * * Chaining * From 215ed57080e399459b8dc4bc52fee1f58ac9608d Mon Sep 17 00:00:00 2001 From: dcrusader Date: Wed, 7 Jan 2015 10:12:52 -0800 Subject: [PATCH 3/3] Fix push & unshift to reflect underscore behavior --- underscore/underscore.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index c118e8020..f46173ff3 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -3271,9 +3271,9 @@ interface _Chain { /** * Adds one or more elements to the end of an array and returns the new length of the array. * @param item The elements to add to the end of the array. - * @return The new length property of the object upon which the method was called. + * @return The array with the element added to the end. **/ - push(...item: Array): _ChainSingle; + push(...item: Array): _Chain; /** * Reverses an array in place. The first array element becomes the last and the last becomes the first. @@ -3320,9 +3320,9 @@ interface _Chain { /** * Adds one or more elements to the beginning of an array and returns the new length of the array. * @param items The elements to add to the front of the array. - * @return The new length property of the object upon which the method was called. + * @return The array with the element added to the beginning. **/ - unshift(...items: Array): _ChainSingle; + unshift(...items: Array): _Chain; /********** * * Chaining *