From bee4d73867e1f363a34731d74bf035a46521d95a Mon Sep 17 00:00:00 2001 From: DomiR Date: Thu, 14 Jan 2016 01:09:54 +0100 Subject: [PATCH] (feature) Absorbed _.sortByAll into _.sortBy --- lodash/lodash-tests.ts | 76 +--------- lodash/lodash.d.ts | 324 ++++++++++++----------------------------- 2 files changed, 100 insertions(+), 300 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 63fd99870..c19e3af69 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -4451,19 +4451,16 @@ module TestSortBy { result = _.sortBy(array); result = _.sortBy(array, listIterator); - result = _.sortBy(array, listIterator, any); result = _.sortBy(array, ''); result = _.sortBy<{a: number}, TResult>(array, {a: 42}); result = _.sortBy(list); result = _.sortBy(list, listIterator); - result = _.sortBy(list, listIterator, any); result = _.sortBy(list, ''); result = _.sortBy<{a: number}, TResult>(list, {a: 42}); result = _.sortBy(dictionary); result = _.sortBy(dictionary, dictionaryIterator); - result = _.sortBy(dictionary, dictionaryIterator, any); result = _.sortBy(dictionary, ''); result = _.sortBy<{a: number}, TResult>(dictionary, {a: 42}); } @@ -4473,19 +4470,16 @@ module TestSortBy { result = _(array).sortBy(); result = _(array).sortBy(listIterator); - result = _(array).sortBy(listIterator, any); result = _(array).sortBy(''); result = _(array).sortBy<{a: number}>({a: 42}); result = _(list).sortBy(); result = _(list).sortBy(listIterator); - result = _(list).sortBy(listIterator, any); result = _(list).sortBy(''); result = _(list).sortBy<{a: number}, TResult>({a: 42}); result = _(dictionary).sortBy(); result = _(dictionary).sortBy(dictionaryIterator); - result = _(dictionary).sortBy(dictionaryIterator, any); result = _(dictionary).sortBy(''); result = _(dictionary).sortBy<{a: number}, TResult>({a: 42}); } @@ -4495,89 +4489,27 @@ module TestSortBy { result = _(array).chain().sortBy(); result = _(array).chain().sortBy(listIterator); - result = _(array).chain().sortBy(listIterator, any); result = _(array).chain().sortBy(''); result = _(array).chain().sortBy<{a: number}>({a: 42}); result = _(list).chain().sortBy(); result = _(list).chain().sortBy(listIterator); - result = _(list).chain().sortBy(listIterator, any); result = _(list).chain().sortBy(''); result = _(list).chain().sortBy<{a: number}, TResult>({a: 42}); result = _(dictionary).chain().sortBy(); result = _(dictionary).chain().sortBy(dictionaryIterator); - result = _(dictionary).chain().sortBy(dictionaryIterator, any); result = _(dictionary).chain().sortBy(''); result = _(dictionary).chain().sortBy<{a: number}, TResult>({a: 42}); } } -// _.sortByAll -module TestSortByAll { - type SampleObject = {a: number; b: string; c: boolean}; +result = _.sortBy(stoogesAges, function(stooge) { return Math.sin(stooge.age); }, function(stooge) { return stooge.name.slice(1); }); +result = _.sortBy(stoogesAges, ['name', 'age']); +result = _.sortBy(stoogesAges, 'name', function(stooge) { return Math.sin(stooge.age); }); - let array: SampleObject[]; - let list: _.List; - let numericDictionary: _.NumericDictionary; - let dictionary: _.Dictionary;; +result = _(foodsOrganic).sortBy('organic', (food) => food.name, { organic: true }).value(); - { - let iteratees: (value: string) => any|((value: string) => any)[]; - let result: string[]; - - result = _.sortByAll('acbd', iteratees); - } - - { - let iteratees: (value: SampleObject) => any|string|{a: number}|((value: SampleObject) => any|string|{a: number})[]; - let result: SampleObject[]; - - result = _.sortByAll<{a: number}, SampleObject>(array, iteratees); - result = _.sortByAll(array, iteratees); - - result = _.sortByAll<{a: number}, SampleObject>(list, iteratees); - result = _.sortByAll(list, iteratees); - - result = _.sortByAll<{a: number}, SampleObject>(numericDictionary, iteratees); - result = _.sortByAll(numericDictionary, iteratees); - - result = _.sortByAll<{a: number}, SampleObject>(dictionary, iteratees); - result = _.sortByAll(dictionary, iteratees); - } - - { - let iteratees: (value: SampleObject) => any|string|{a: number}|((value: SampleObject) => any|string|{a: number})[]; - let result: _.LoDashImplicitArrayWrapper; - - result = _(array).sortByAll<{a: number}>(iteratees); - - result = _(list).sortByAll<{a: number}, SampleObject>(iteratees); - result = _(list).sortByAll(iteratees); - - result = _(numericDictionary).sortByAll<{a: number}, SampleObject>(iteratees); - result = _(numericDictionary).sortByAll(iteratees); - - result = _(dictionary).sortByAll<{a: number}, SampleObject>(iteratees); - result = _(dictionary).sortByAll(iteratees); - } - - { - let iteratees: (value: SampleObject) => any|string|{a: number}|((value: SampleObject) => any|string|{a: number})[]; - let result: _.LoDashExplicitArrayWrapper; - - result = _(array).chain().sortByAll<{a: number}>(iteratees); - - result = _(list).chain().sortByAll<{a: number}, SampleObject>(iteratees); - result = _(list).chain().sortByAll(iteratees); - - result = _(numericDictionary).chain().sortByAll<{a: number}, SampleObject>(iteratees); - result = _(numericDictionary).chain().sortByAll(iteratees); - - result = _(dictionary).chain().sortByAll<{a: number}, SampleObject>(iteratees); - result = _(dictionary).chain().sortByAll(iteratees); - } -} // _.orderBy module TestorderBy { diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 6f9f2f223..dc4594691 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -39,8 +39,8 @@ TODO: - [x] Split _.sortedLastIndex into _.sortedLastIndexBy - [ ] Split _.uniq into _.sortedUniq, _.sortedUniqBy, & _.uniqBy -- [ ] Check for aliases in this group -- [ ] Absorbed _.sortByAll into _.sortBy +- [ ] TODO remove _.sortBy duplicates +- [x] Absorbed _.sortByAll into _.sortBy - [x] Changed the category of _.at to “Object” - [x] Changed the category of _.bindAll to “Utility” - [ ] Made “By” methods provide a single param to iteratees @@ -7343,29 +7343,41 @@ declare module _ { //_.sortBy interface LoDashStatic { /** - * Creates an array of elements, sorted in ascending order by the results of running each element in a - * collection through iteratee. This method performs a stable sort, that is, it preserves the original sort - * order of equal elements. The iteratee is bound to thisArg and invoked with three arguments: - * (value, index|key, collection). + * Creates an array of elements, sorted in ascending order by the results of + * running each element in a collection through each iteratee. This method + * performs a stable sort, that is, it preserves the original sort order of + * equal elements. The iteratees are invoked with one argument: (value). * - * If a property name is provided for iteratee the created _.property style callback returns the property - * valueof the given element. + * @static + * @memberOf _ + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {...(Function|Function[]|Object|Object[]|string|string[])} [iteratees=[_.identity]] + * The iteratees to sort by, specified individually or in arrays. + * @returns {Array} Returns the new sorted array. + * @example * - * If a value is also provided for thisArg the created _.matchesProperty style callback returns true for - * elements that have a matching property value, else false. + * var users = [ + * { 'user': 'fred', 'age': 48 }, + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 42 }, + * { 'user': 'barney', 'age': 34 } + * ]; * - * If an object is provided for iteratee the created _.matches style callback returns true for elements that - * have the properties of the given object, else false. + * _.sortBy(users, function(o) { return o.user; }); + * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]] * - * @param collection The collection to iterate over. - * @param iteratee The function invoked per iteration. - * @param thisArg The this binding of iteratee. - * @return Returns the new sorted array. + * _.sortBy(users, ['user', 'age']); + * // => objects for [['barney', 34], ['barney', 36], ['fred', 42], ['fred', 48]] + * + * _.sortBy(users, 'user', function(o) { + * return Math.floor(o.age / 10); + * }); + * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]] */ sortBy( collection: List, - iteratee?: ListIterator, - thisArg?: any + iteratee?: ListIterator ): T[]; /** @@ -7373,8 +7385,7 @@ declare module _ { */ sortBy( collection: Dictionary, - iteratee?: DictionaryIterator, - thisArg?: any + iteratee?: DictionaryIterator ): T[]; /** @@ -7399,6 +7410,20 @@ declare module _ { sortBy( collection: List|Dictionary ): T[]; + + /** + * @see _.sortBy + */ + sortBy( + collection: (Array|List), + iteratees: (ListIterator|string|Object)[]): T[]; + + /** + * @see _.sortBy + */ + sortBy( + collection: (Array|List), + ...iteratees: (ListIterator|Object|string)[]): T[]; } interface LoDashImplicitArrayWrapper { @@ -7406,8 +7431,7 @@ declare module _ { * @see _.sortBy */ sortBy( - iteratee?: ListIterator, - thisArg?: any + iteratee?: ListIterator ): LoDashImplicitArrayWrapper; /** @@ -7424,6 +7448,16 @@ declare module _ { * @see _.sortBy */ sortBy(): LoDashImplicitArrayWrapper; + + /** + * @see _.sortBy + */ + sortBy(...iteratees: (ListIterator|Object|string)[]): LoDashImplicitArrayWrapper; + + /** + * @see _.sortBy + **/ + sortBy(iteratees: (ListIterator|string|Object)[]): LoDashImplicitArrayWrapper; } interface LoDashImplicitObjectWrapper { @@ -7431,8 +7465,7 @@ declare module _ { * @see _.sortBy */ sortBy( - iteratee?: ListIterator|DictionaryIterator, - thisArg?: any + iteratee?: ListIterator|DictionaryIterator ): LoDashImplicitArrayWrapper; /** @@ -7456,8 +7489,7 @@ declare module _ { * @see _.sortBy */ sortBy( - iteratee?: ListIterator, - thisArg?: any + iteratee?: ListIterator ): LoDashExplicitArrayWrapper; /** @@ -7481,8 +7513,7 @@ declare module _ { * @see _.sortBy */ sortBy( - iteratee?: ListIterator|DictionaryIterator, - thisArg?: any + iteratee?: ListIterator|DictionaryIterator ): LoDashExplicitArrayWrapper; /** @@ -7501,208 +7532,34 @@ declare module _ { sortBy(): LoDashExplicitArrayWrapper; } - //_.sortByAll - interface LoDashStatic { - /** - * This method is like _.sortBy except that it can sort by multiple iteratees or property names. - * - * If a property name is provided for an iteratee the created _.property style callback returns the property - * value of the given element. - * - * If an object is provided for an iteratee the created _.matches style callback returns true for elements that - * have the properties of the given object, else false. - * - * @param collection The collection to iterate over. - * @param iteratees The iteratees to sort by, specified as individual values or arrays of values. - * @return Returns the new sorted array. - */ - sortByAll( - collection: List, - iteratees: ListIterator|string|W|(ListIterator|string|W)[] - ): T[]; - - /** - * @see _.sortByAll - */ - sortByAll( - collection: List, - iteratees: ListIterator|string|Object|(ListIterator|string|Object)[] - ): T[]; - - /** - * @see _.sortByAll - */ - sortByAll( - collection: NumericDictionary, - iteratees: NumericDictionaryIterator|string|W|(NumericDictionaryIterator|string|W)[] - ): T[]; - - /** - * @see _.sortByAll - */ - sortByAll( - collection: NumericDictionary, - iteratees: NumericDictionaryIterator|string|Object|(NumericDictionaryIterator|string|Object)[] - ): T[]; - - /** - * @see _.sortByAll - */ - sortByAll( - collection: Dictionary, - iteratees: DictionaryIterator|string|W|(DictionaryIterator|string|W)[] - ): T[]; - - /** - * @see _.sortByAll - */ - sortByAll( - collection: Dictionary, - iteratees: DictionaryIterator|string|Object|(DictionaryIterator|string|Object)[] - ): T[]; - } - - interface LoDashImplicitWrapper { - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: ListIterator|string|(ListIterator|string)[] - ): LoDashImplicitArrayWrapper; - } - - interface LoDashImplicitArrayWrapper { - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: ListIterator|string|W|(ListIterator|string|W)[] - ): LoDashImplicitArrayWrapper; - } - - interface LoDashImplicitObjectWrapper { - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: ListIterator|string|W|(ListIterator|string|W)[] - ): LoDashImplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: ListIterator|string|Object|(ListIterator|string|Object)[] - ): LoDashImplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: NumericDictionaryIterator|string|W|(NumericDictionaryIterator|string|W)[] - ): LoDashImplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: NumericDictionaryIterator|string|Object|(NumericDictionaryIterator|string|Object)[] - ): LoDashImplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: DictionaryIterator|string|W|(DictionaryIterator|string|W)[] - ): LoDashImplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: DictionaryIterator|string|Object|(DictionaryIterator|string|Object)[] - ): LoDashImplicitArrayWrapper; - } - - interface LoDashExplicitWrapper { - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: ListIterator|string|(ListIterator|string)[] - ): LoDashExplicitArrayWrapper; - } - - interface LoDashExplicitArrayWrapper { - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: ListIterator|string|W|(ListIterator|string|W)[] - ): LoDashExplicitArrayWrapper; - } - - interface LoDashExplicitObjectWrapper { - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: ListIterator|string|W|(ListIterator|string|W)[] - ): LoDashExplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: ListIterator|string|Object|(ListIterator|string|Object)[] - ): LoDashExplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: NumericDictionaryIterator|string|W|(NumericDictionaryIterator|string|W)[] - ): LoDashExplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: NumericDictionaryIterator|string|Object|(NumericDictionaryIterator|string|Object)[] - ): LoDashExplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: DictionaryIterator|string|W|(DictionaryIterator|string|W)[] - ): LoDashExplicitArrayWrapper; - - /** - * @see _.sortByAll - */ - sortByAll( - iteratees: DictionaryIterator|string|Object|(DictionaryIterator|string|Object)[] - ): LoDashExplicitArrayWrapper; - } - //_.orderBy interface LoDashStatic { /** - * This method is like _.sortByAll except that it allows specifying the sort orders of the iteratees to sort - * by. If orders is unspecified, all values are sorted in ascending order. Otherwise, a value is sorted in - * ascending order if its corresponding order is "asc", and descending if "desc". + * This method is like `_.sortBy` except that it allows specifying the sort + * orders of the iteratees to sort by. If `orders` is unspecified, all values + * are sorted in ascending order. Otherwise, specify an order of "desc" for + * descending or "asc" for ascending sort order of corresponding values. * - * If a property name is provided for an iteratee the created _.property style callback returns the property - * value of the given element. + * @static + * @memberOf _ + * @category Collection + * @param {Array|Object} collection The collection to iterate over. + * @param {Function[]|Object[]|string[]} [iteratees=[_.identity]] The iteratees to sort by. + * @param {string[]} [orders] The sort orders of `iteratees`. + * @param- {Object} [guard] Enables use as an iteratee for functions like `_.reduce`. + * @returns {Array} Returns the new sorted array. + * @example * - * If an object is provided for an iteratee the created _.matches style callback returns true for elements - * that have the properties of the given object, else false. + * var users = [ + * { 'user': 'fred', 'age': 48 }, + * { 'user': 'barney', 'age': 34 }, + * { 'user': 'fred', 'age': 42 }, + * { 'user': 'barney', 'age': 36 } + * ]; * - * @param collection The collection to iterate over. - * @param iteratees The iteratees to sort by. - * @param orders The sort orders of iteratees. - * @return Returns the new sorted array. + * // sort by `user` in ascending order and by `age` in descending order + * _.orderBy(users, ['user', 'age'], ['asc', 'desc']); + * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]] */ orderBy( collection: List, @@ -13355,11 +13212,22 @@ declare module _ { //_.words interface LoDashStatic { /** - * Splits string into an array of its words. + * Splits `string` into an array of its words. * - * @param string The string to inspect. - * @param pattern The pattern to match words. - * @return Returns the words of string. + * @static + * @memberOf _ + * @category String + * @param {string} [string=''] The string to inspect. + * @param {RegExp|string} [pattern] The pattern to match words. + * @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`. + * @returns {Array} Returns the words of `string`. + * @example + * + * _.words('fred, barney, & pebbles'); + * // => ['fred', 'barney', 'pebbles'] + * + * _.words('fred, barney, & pebbles', /[^, ]+/g); + * // => ['fred', 'barney', '&', 'pebbles'] */ words( string?: string,