From 79f52467c0109e63b7141beced479a9421560bbe Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Fri, 24 Jul 2015 03:00:57 +0500 Subject: [PATCH] lodash: changed _.isEqual() method --- lodash/lodash-tests.ts | 35 ++++++++++------ lodash/lodash.d.ts | 91 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 97 insertions(+), 29 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 627dda565..957bd56cc 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -986,21 +986,30 @@ result = _.isEmpty([1, 2, 3]); result = _.isEmpty({}); result = _.isEmpty(''); -var moe = { 'name': 'moe', 'age': 40 }; -var copy = { 'name': 'moe', 'age': 40 }; +// _.isEqual (alias: _.eq) +result = _.isEqual(1, 1); +result = _(1).isEqual(1); +result = _.eq(1, 1); +result = _(1).eq(1); -result = _.isEqual(moe, copy); +var testEqObject = { 'user': 'fred' }; +var testEqOtherObject = { 'user': 'fred' }; +result = _.isEqual(testEqObject, testEqOtherObject); +result = _(testEqObject).isEqual(testEqOtherObject); +result = _.eq(testEqObject, testEqOtherObject); +result = _(testEqObject).eq(testEqOtherObject); -var words = ['hello', 'goodbye']; -var otherWords = ['hi', 'goodbye']; - -result = _.isEqual(words, otherWords, function (a, b) { - var reGreet = /^(?:hello|hi)$/i, - aGreet = _.isString(a) && reGreet.test(a), - bGreet = _.isString(b) && reGreet.test(b); - - return (aGreet || bGreet) ? (aGreet == bGreet) : undefined; -}); +var testEqArray = ['hello', 'goodbye']; +var testEqOtherArray = ['hi', 'goodbye']; +var testEqCustomizerFn = (value: any, other: any): boolean => { + if (_.every([value, other], RegExp.prototype.test, /^h(?:i|ello)$/)) { + return true; + } +}; +result = _.isEqual(testEqArray, testEqOtherArray, testEqCustomizerFn); +result = _(testEqArray).isEqual(testEqOtherArray, testEqCustomizerFn); +result = _.eq(testEqArray, testEqOtherArray, testEqCustomizerFn); +result = _(testEqArray).eq(testEqOtherArray, testEqCustomizerFn); result = _.isFinite(-101); result = _.isFinite('10'); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 7bc1df648..06d5acd00 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -5907,25 +5907,84 @@ declare module _ { isError(value: any): boolean; } - //_.isEqual + interface EqCustomizer { + (value: any, other: any, indexOrKey?: number|string): boolean; + } + interface LoDashStatic { /** - * Performs a deep comparison between two values to determine if they are equivalent to each - * other. If a callback is provided it will be executed to compare values. If the callback - * returns undefined comparisons will be handled by the method instead. The callback is bound to - * thisArg and invoked with two arguments; (a, b). - * @param a The value to compare. - * @param b The other value to compare. - * @param callback The function to customize comparing values. - * @param thisArg The this binding of callback. - * @return True if the values are equivalent, else false. - **/ - isEqual( - a?: any, - b?: any, - callback?: (a: any, b: any) => boolean, - thisArg?: any): boolean; + * Performs a deep comparison between two values to determine if they are equivalent. If customizer is + * provided it is invoked to compare values. If customizer returns undefined comparisons are handled + * by the method instead. The customizer is bound to thisArg and invoked with three + * arguments: (value, other [, index|key]). + * @param value The value to compare. + * @param other The other value to compare. + * @param callback The function to customize value comparisons. + * @param thisArg The this binding of customizer. + * @return True if the values are equivalent, else false. + */ + isEqual(value?: any, + other?: any, + callback?: EqCustomizer, + thisArg?: any): boolean; + + /** + * @see _.isEqual + */ + eq(value?: any, + other?: any, + callback?: EqCustomizer, + thisArg?: any): boolean; + } + + interface LoDashWrapper { + /** + * @see _.isEqual + */ + isEqual(other?: any, + callback?: EqCustomizer, + thisArg?: any): boolean; + + /** + * @see _.isEqual + */ + eq(other?: any, + callback?: EqCustomizer, + thisArg?: any): boolean; + + } + + interface LoDashArrayWrapper { + /** + * @see _.isEqual + */ + isEqual(other?: any, + callback?: EqCustomizer, + thisArg?: any): boolean; + + /** + * @see _.isEqual + */ + eq(other?: any, + callback?: EqCustomizer, + thisArg?: any): boolean; + } + + interface LoDashObjectWrapper { + /** + * @see _.isEqual + */ + isEqual(other?: any, + callback?: EqCustomizer, + thisArg?: any): boolean; + + /** + * @see _.isEqual + */ + eq(other?: any, + callback?: EqCustomizer, + thisArg?: any): boolean; } //_.isFinite