From c00b6c330cbe71aa1652cb957a1b6554c0ccd808 Mon Sep 17 00:00:00 2001 From: DomiR Date: Wed, 13 Jan 2016 22:57:04 +0100 Subject: [PATCH] (feature) Split _.sortedIndex into _.sortedIndexBy --- lodash/lodash-tests.ts | 107 +++++++------ lodash/lodash.d.ts | 338 ++++++++++++++++++++++++++++++----------- 2 files changed, 313 insertions(+), 132 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index f0abe1d44..506aac21f 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -1380,70 +1380,89 @@ module TestSortedIndex { let result: number; result = _.sortedIndex('', ''); - result = _.sortedIndex('', '', stringIterator); - result = _.sortedIndex('', '', stringIterator, any); - result = _.sortedIndex('', '', stringIterator); - result = _.sortedIndex('', '', stringIterator, any); result = _.sortedIndex(array, value); - result = _.sortedIndex(array, value, arrayIterator); - result = _.sortedIndex(array, value, arrayIterator, any); - result = _.sortedIndex(array, value, ''); - result = _.sortedIndex(array, value, {a: 42}); - result = _.sortedIndex(array, value, arrayIterator); - result = _.sortedIndex(array, value, arrayIterator, any); - result = _.sortedIndex<{a: number}, SampleType>(array, value, {a: 42}); result = _.sortedIndex(list, value); - result = _.sortedIndex(list, value, listIterator); - result = _.sortedIndex(list, value, listIterator, any); - result = _.sortedIndex(list, value, ''); - result = _.sortedIndex(list, value, {a: 42}); - result = _.sortedIndex(list, value, listIterator); - result = _.sortedIndex(list, value, listIterator, any); - result = _.sortedIndex<{a: number}, SampleType>(list, value, {a: 42}); result = _('').sortedIndex(''); - result = _('').sortedIndex('', stringIterator); - result = _('').sortedIndex('', stringIterator, any); result = _(array).sortedIndex(value); - result = _(array).sortedIndex(value, arrayIterator); - result = _(array).sortedIndex(value, arrayIterator, any); - result = _(array).sortedIndex(value, ''); - result = _(array).sortedIndex<{a: number}>(value, {a: 42}); result = _(list).sortedIndex(value); - result = _(list).sortedIndex(value, listIterator); - result = _(list).sortedIndex(value, listIterator, any); - result = _(list).sortedIndex(value, ''); - result = _(list).sortedIndex(value, {a: 42}); - result = _(list).sortedIndex(value, listIterator); - result = _(list).sortedIndex(value, listIterator, any); - result = _(list).sortedIndex<{a: number}, SampleType>(value, {a: 42}); + } { let result: _.LoDashExplicitWrapper; result = _('').chain().sortedIndex(''); - result = _('').chain().sortedIndex('', stringIterator); - result = _('').chain().sortedIndex('', stringIterator, any); result = _(array).chain().sortedIndex(value); - result = _(array).chain().sortedIndex(value, arrayIterator); - result = _(array).chain().sortedIndex(value, arrayIterator, any); - result = _(array).chain().sortedIndex(value, ''); - result = _(array).chain().sortedIndex<{a: number}>(value, {a: 42}); result = _(list).chain().sortedIndex(value); - result = _(list).chain().sortedIndex(value, listIterator); - result = _(list).chain().sortedIndex(value, listIterator, any); - result = _(list).chain().sortedIndex(value, ''); - result = _(list).chain().sortedIndex(value, {a: 42}); - result = _(list).chain().sortedIndex(value, listIterator); - result = _(list).chain().sortedIndex(value, listIterator, any); - result = _(list).chain().sortedIndex<{a: number}, SampleType>(value, {a: 42}); + + } +} + +// _.sortedIndexBy +module TestSortedIndexBy { + type SampleType = {a: number; b: string; c: boolean;}; + + let array: SampleType[]; + let list: _.List; + + let value: SampleType; + + let stringIterator: (x: string) => number; + let arrayIterator: (x: SampleType) => number; + let listIterator: (x: SampleType) => number; + + { + let result: number; + + result = _.sortedIndexBy('', '', stringIterator); + result = _.sortedIndexBy('', '', stringIterator); + + result = _.sortedIndexBy(array, value, arrayIterator); + result = _.sortedIndexBy(array, value, ''); + result = _.sortedIndexBy(array, value, {a: 42}); + result = _.sortedIndexBy(array, value, arrayIterator); + result = _.sortedIndexBy<{a: number}, SampleType>(array, value, {a: 42}); + + result = _.sortedIndexBy(list, value, listIterator); + result = _.sortedIndexBy(list, value, ''); + result = _.sortedIndexBy(list, value, {a: 42}); + result = _.sortedIndexBy(list, value, listIterator); + result = _.sortedIndexBy<{a: number}, SampleType>(list, value, {a: 42}); + + result = _('').sortedIndexBy('', stringIterator); + + result = _(array).sortedIndexBy(value, arrayIterator); + result = _(array).sortedIndexBy(value, ''); + result = _(array).sortedIndexBy<{a: number}>(value, {a: 42}); + + result = _(list).sortedIndexBy(value, listIterator); + result = _(list).sortedIndexBy(value, ''); + result = _(list).sortedIndexBy(value, {a: 42}); + result = _(list).sortedIndexBy(value, listIterator); + result = _(list).sortedIndexBy<{a: number}, SampleType>(value, {a: 42}); + } + + { + let result: _.LoDashExplicitWrapper; + + result = _('').chain().sortedIndexBy('', stringIterator); + + result = _(array).chain().sortedIndexBy(value, arrayIterator); + result = _(array).chain().sortedIndexBy(value, ''); + result = _(array).chain().sortedIndexBy<{a: number}>(value, {a: 42}); + + result = _(list).chain().sortedIndexBy(value, listIterator); + result = _(list).chain().sortedIndexBy(value, ''); + result = _(list).chain().sortedIndexBy(value, {a: 42}); + result = _(list).chain().sortedIndexBy(value, listIterator); + result = _(list).chain().sortedIndexBy<{a: number}, SampleType>(value, {a: 42}); } } diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index ff005a991..03eb0b2d1 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -32,7 +32,7 @@ TODO: - [x] Split _.max & _.min into _.maxBy & _.minBy - [x] Split _.omit & _.pick into _.omitBy & _.pickBy - [x] Split _.sample into _.sampleSize -- [ ] Split _.sortedIndex into _.sortedIndexBy +- [x] Split _.sortedIndex into _.sortedIndexBy - [ ] Split _.sortedLastIndex into _.sortedLastIndexBy - [ ] Split _.uniq into _.sortedUniq, _.sortedUniqBy, & _.uniqBy @@ -2130,24 +2130,26 @@ declare module _ { //_.sortedIndex interface LoDashStatic { /** - * Uses a binary search to determine the lowest index at which value should be inserted into array in order to maintain its sort order. If an iteratee function is provided it’s invoked for value and each element of array to compute their sort ranking. The iteratee is bound to thisArg and invoked with one argument; (value). + * Uses a binary search to determine the lowest index at which `value` should + * be inserted into `array` in order to maintain its sort order. * - * If a property name is provided for iteratee the created _.property style callback returns the property value of the given element. + * @static + * @memberOf _ + * @category Array + * @param {Array} array The sorted array to inspect. + * @param {*} value The value to evaluate. + * @returns {number} Returns the index at which `value` should be inserted into `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. + * _.sortedIndex([30, 50], 40); + * // => 1 * - * 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. - * - * @param array The sorted array to inspect. - * @param value The value to evaluate. - * @param iteratee The function invoked per iteration. - * @return The this binding of iteratee. + * _.sortedIndex([4, 5], 4); + * // => 0 */ sortedIndex( array: List, - value: T, - iteratee?: (x: T) => TSort, - thisArg?: any + value: T ): number; /** @@ -2155,9 +2157,7 @@ declare module _ { */ sortedIndex( array: List, - value: T, - iteratee?: (x: T) => any, - thisArg?: any + value: T ): number; /** @@ -2165,8 +2165,7 @@ declare module _ { */ sortedIndex( array: List, - value: T, - iteratee: string + value: T ): number; /** @@ -2174,8 +2173,7 @@ declare module _ { */ sortedIndex( array: List, - value: T, - iteratee: W + value: T ): number; /** @@ -2183,8 +2181,7 @@ declare module _ { */ sortedIndex( array: List, - value: T, - iteratee: Object + value: T ): number; } @@ -2193,9 +2190,7 @@ declare module _ { * @see _.sortedIndex */ sortedIndex( - value: string, - iteratee?: (x: string) => TSort, - thisArg?: any + value: string ): number; } @@ -2204,25 +2199,14 @@ declare module _ { * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee?: (x: T) => TSort, - thisArg?: any + value: T ): number; /** * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee: string - ): number; - - /** - * @see _.sortedIndex - */ - sortedIndex( - value: T, - iteratee: W + value: T ): number; } @@ -2231,42 +2215,21 @@ declare module _ { * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee?: (x: T) => TSort, - thisArg?: any + value: T ): number; /** * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee?: (x: T) => any, - thisArg?: any - ): number; - - /** - * @see _.sortedIndex - */ - sortedIndex( - value: T, - iteratee: string + value: T ): number; /** * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee: W - ): number; - - /** - * @see _.sortedIndex - */ - sortedIndex( - value: T, - iteratee: Object + value: T ): number; } @@ -2275,9 +2238,7 @@ declare module _ { * @see _.sortedIndex */ sortedIndex( - value: string, - iteratee?: (x: string) => TSort, - thisArg?: any + value: string ): LoDashExplicitWrapper; } @@ -2286,25 +2247,21 @@ declare module _ { * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee?: (x: T) => TSort, - thisArg?: any + value: T ): LoDashExplicitWrapper; /** * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee: string + value: T ): LoDashExplicitWrapper; /** * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee: W + value: T ): LoDashExplicitWrapper; } @@ -2313,40 +2270,245 @@ declare module _ { * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee?: (x: T) => TSort, - thisArg?: any + value: T ): LoDashExplicitWrapper; /** * @see _.sortedIndex */ sortedIndex( - value: T, - iteratee?: (x: T) => any, - thisArg?: any - ): LoDashExplicitWrapper; - - /** - * @see _.sortedIndex - */ - sortedIndex( - value: T, - iteratee: string + value: T ): LoDashExplicitWrapper; /** * @see _.sortedIndex */ sortedIndex( + value: T + ): LoDashExplicitWrapper; + + + } + + //_.sortedIndexBy + interface LoDashStatic { + /** + * This method is like `_.sortedIndex` except that it accepts `iteratee` + * which is invoked for `value` and each element of `array` to compute their + * sort ranking. The iteratee is invoked with one argument: (value). + * + * @static + * @memberOf _ + * @category Array + * @param {Array} array The sorted array to inspect. + * @param {*} value The value to evaluate. + * @param {Function|Object|string} [iteratee=_.identity] The iteratee invoked per element. + * @returns {number} Returns the index at which `value` should be inserted into `array`. + * @example + * + * var dict = { 'thirty': 30, 'forty': 40, 'fifty': 50 }; + * + * _.sortedIndexBy(['thirty', 'fifty'], 'forty', _.propertyOf(dict)); + * // => 1 + * + * // using the `_.property` iteratee shorthand + * _.sortedIndexBy([{ 'x': 4 }, { 'x': 5 }], { 'x': 4 }, 'x'); + * // => 0 + */ + sortedIndexBy( + array: List, + value: T, + iteratee: (x: T) => TSort + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + array: List, + value: T, + iteratee: (x: T) => any + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + array: List, + value: T, + iteratee: string + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + array: List, + value: T, + iteratee: W + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + array: List, + value: T, + iteratee: Object + ): number; + } + + interface LoDashImplicitWrapper { + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: string, + iteratee: (x: string) => TSort + ): number; + } + + interface LoDashImplicitArrayWrapper { + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: (x: T) => TSort + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: string + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: W + ): number; + } + + interface LoDashImplicitObjectWrapper { + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: (x: T) => TSort + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: (x: T) => any + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: string + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: W + ): number; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: Object + ): number; + } + + interface LoDashExplicitWrapper { + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: string, + iteratee: (x: string) => TSort + ): LoDashExplicitWrapper; + } + + interface LoDashExplicitArrayWrapper { + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: (x: T) => TSort + ): LoDashExplicitWrapper; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: string + ): LoDashExplicitWrapper; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: W + ): LoDashExplicitWrapper; + } + + interface LoDashExplicitObjectWrapper { + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: (x: T) => TSort + ): LoDashExplicitWrapper; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: (x: T) => any + ): LoDashExplicitWrapper; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( + value: T, + iteratee: string + ): LoDashExplicitWrapper; + + /** + * @see _.sortedIndexBy + */ + sortedIndexBy( value: T, iteratee: W ): LoDashExplicitWrapper; /** - * @see _.sortedIndex + * @see _.sortedIndexBy */ - sortedIndex( + sortedIndexBy( value: T, iteratee: Object ): LoDashExplicitWrapper;