From 7b4ab5384c58f9508d4a6d877dc64b5e3a51fee0 Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Fri, 21 Aug 2015 07:12:25 +0500 Subject: [PATCH] lodash: changed _.clone() and _.cloneDeep() methods --- lodash/lodash-tests.ts | 120 +++++++++++++++++++++++++++-------------- lodash/lodash.d.ts | 112 +++++++++++++++++++++++++++++--------- 2 files changed, 168 insertions(+), 64 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 2cbec6853..ef2d5e457 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -1091,41 +1091,89 @@ helloWrap2(); * Lang * ********/ -// _.cloneDeep -interface TestCloneDeepFn { +// _.clone +interface TestCloneCustomizerFn { (value: any): any; } -var testCloneDeepFn: TestCloneDeepFn; -result = _.cloneDeep(1); -result = _.cloneDeep(1, testCloneDeepFn); -result = _.cloneDeep(1, testCloneDeepFn, any); -result = _.cloneDeep('a'); -result = _.cloneDeep('a', testCloneDeepFn); -result = _.cloneDeep('a', testCloneDeepFn, any); -result = _.cloneDeep(true); -result = _.cloneDeep(true, testCloneDeepFn); -result = _.cloneDeep(true, testCloneDeepFn, any); -result = _.cloneDeep([1, 2]); -result = _.cloneDeep([1, 2], testCloneDeepFn); -result = _.cloneDeep([1, 2], testCloneDeepFn, any); -result = <{a: {b: number;}}>_.cloneDeep<{a: {b: number;}}>({a: {b: 2}}); -result = <{a: {b: number;}}>_.cloneDeep<{a: {b: number;}}>({a: {b: 2}}, testCloneDeepFn); -result = <{a: {b: number;}}>_.cloneDeep<{a: {b: number;}}>({a: {b: 2}}, testCloneDeepFn, any); -result = _(1).cloneDeep(); -result = _(1).cloneDeep(testCloneDeepFn); -result = _(1).cloneDeep(testCloneDeepFn, any); -result = _('a').cloneDeep(); -result = _('a').cloneDeep(testCloneDeepFn); -result = _('a').cloneDeep(testCloneDeepFn, any); -result = _(true).cloneDeep(); -result = _(true).cloneDeep(testCloneDeepFn); -result = _(true).cloneDeep(testCloneDeepFn, any); -result = _([1, 2]).cloneDeep(); -result = _([1, 2]).cloneDeep(testCloneDeepFn); -result = _([1, 2]).cloneDeep(testCloneDeepFn, any); -result = <{a: {b: number;}}>_({a: {b: 2}}).cloneDeep(); -result = <{a: {b: number;}}>_({a: {b: 2}}).cloneDeep(testCloneDeepFn); -result = <{a: {b: number;}}>_({a: {b: 2}}).cloneDeep(testCloneDeepFn, any); +var testCloneCustomizerFn: TestCloneCustomizerFn; +{ + let result: number; + result = _.clone(42); + result = _.clone(42, false); + result = _.clone(42, false, testCloneCustomizerFn); + result = _.clone(42, false, testCloneCustomizerFn, any); + result = _.clone(42, testCloneCustomizerFn); + result = _.clone(42, testCloneCustomizerFn, any); + result = _(42).clone(); + result = _(42).clone(false); + result = _(42).clone(false, testCloneCustomizerFn); + result = _(42).clone(false, testCloneCustomizerFn, any); + result = _(42).clone(testCloneCustomizerFn); + result = _(42).clone(testCloneCustomizerFn, any); +} +{ + let result: string[]; + result = _.clone([]); + result = _.clone([], false); + result = _.clone([], false, testCloneCustomizerFn); + result = _.clone([], false, testCloneCustomizerFn, any); + result = _.clone([], testCloneCustomizerFn); + result = _.clone([], testCloneCustomizerFn, any); + result = _([]).clone(); + result = _([]).clone(false); + result = _([]).clone(false, testCloneCustomizerFn); + result = _([]).clone(false, testCloneCustomizerFn, any); + result = _([]).clone(testCloneCustomizerFn); + result = _([]).clone(testCloneCustomizerFn, any); +} +{ + let result: {a: {b: number;}}; + result = _.clone<{a: {b: number;}}>({a: {b: 2}}); + result = _.clone<{a: {b: number;}}>({a: {b: 2}}, false); + result = _.clone<{a: {b: number;}}>({a: {b: 2}}, false, testCloneCustomizerFn); + result = _.clone<{a: {b: number;}}>({a: {b: 2}}, false, testCloneCustomizerFn, any); + result = _.clone<{a: {b: number;}}>({a: {b: 2}}, testCloneCustomizerFn); + result = _.clone<{a: {b: number;}}>({a: {b: 2}}, testCloneCustomizerFn, any); + result = _({a: {b: 2}}).clone(); + result = _({a: {b: 2}}).clone(false); + result = _({a: {b: 2}}).clone(false, testCloneCustomizerFn); + result = _({a: {b: 2}}).clone(false, testCloneCustomizerFn, any); + result = _({a: {b: 2}}).clone(testCloneCustomizerFn); + result = _({a: {b: 2}}).clone(testCloneCustomizerFn, any); +} + +// _.cloneDeep +interface TestCloneDeepCustomizerFn { + (value: any): any; +} +var testCloneDeepCustomizerFn: TestCloneDeepCustomizerFn; +{ + let result: number; + result = _.cloneDeep(42); + result = _.cloneDeep(42, testCloneDeepCustomizerFn); + result = _.cloneDeep(42, testCloneDeepCustomizerFn, any); + result = _(42).cloneDeep(); + result = _(42).cloneDeep(testCloneDeepCustomizerFn); + result = _(42).cloneDeep(testCloneDeepCustomizerFn, any); +} +{ + let result: string[]; + result = _.cloneDeep([]); + result = _.cloneDeep([], testCloneDeepCustomizerFn); + result = _.cloneDeep([], testCloneDeepCustomizerFn, any); + result = _([]).cloneDeep(); + result = _([]).cloneDeep(testCloneDeepCustomizerFn); + result = _([]).cloneDeep(testCloneDeepCustomizerFn, any); +} +{ + let result: {a: {b: number;}}; + result = _.cloneDeep<{a: {b: number;}}>({a: {b: 2}}); + result = _.cloneDeep<{a: {b: number;}}>({a: {b: 2}}, testCloneDeepCustomizerFn); + result = _.cloneDeep<{a: {b: number;}}>({a: {b: 2}}, testCloneDeepCustomizerFn, any); + result = _({a: {b: 2}}).cloneDeep(); + result = _({a: {b: 2}}).cloneDeep(testCloneDeepCustomizerFn); + result = _({a: {b: 2}}).cloneDeep(testCloneDeepCustomizerFn, any); +} // _.gt result = _.gt(1, 2); @@ -1262,12 +1310,6 @@ result = <{}>_(testCreateProto).create(testCreateProps).value(); result = _(testCreateProto).create().value(); result = _(testCreateProto).create(testCreateProps).value(); -result = _.clone(stoogesAges); -result = _.clone(stoogesAges, true); -result = _.clone(stoogesAges, true, function (value) { - return _.isElement(value) ? value.cloneNode(false) : undefined; -}); - interface Food { name: string; type: string; diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index c088f5459..16b39a64d 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -5997,6 +5997,88 @@ declare module _ { * Lang * ********/ + //_.clone + interface LoDashStatic { + /** + * Creates a clone of value. If isDeep is true nested objects are cloned, otherwise they are assigned by + * reference. If customizer is provided it’s invoked to produce the cloned values. If customizer returns + * undefined cloning is handled by the method instead. The customizer is bound to thisArg and invoked with up + * to three argument; (value [, index|key, object]). + * Note: This method is loosely based on the structured clone algorithm. The enumerable properties of arguments + * objects and objects created by constructors other than Object are cloned to plain Object objects. An empty + * object is returned for uncloneable values such as functions, DOM nodes, Maps, Sets, and WeakMaps. + * @param value The value to clone. + * @param isDeep Specify a deep clone. + * @param customizer The function to customize cloning values. + * @param thisArg The this binding of customizer. + * @return Returns the cloned value. + */ + clone( + value: T, + isDeep?: boolean, + customizer?: (value: any) => any, + thisArg?: any): T; + + /** + * @see _.clone + */ + clone( + value: T, + customizer?: (value: any) => any, + thisArg?: any): T; + } + + interface LoDashWrapper { + /** + * @see _.clone + */ + clone( + isDeep?: boolean, + customizer?: (value: any) => any, + thisArg?: any): T; + + /** + * @see _.clone + */ + clone( + customizer?: (value: any) => any, + thisArg?: any): T; + } + + interface LoDashArrayWrapper { + /** + * @see _.clone + */ + clone( + isDeep?: boolean, + customizer?: (value: any) => any, + thisArg?: any): T[]; + + /** + * @see _.clone + */ + clone( + customizer?: (value: any) => any, + thisArg?: any): T[]; + } + + interface LoDashObjectWrapper { + /** + * @see _.clone + */ + clone( + isDeep?: boolean, + customizer?: (value: any) => any, + thisArg?: any): T; + + /** + * @see _.clone + */ + clone( + customizer?: (value: any) => any, + thisArg?: any): T; + } + //_.cloneDeep interface LoDashStatic { /** @@ -6007,13 +6089,13 @@ declare module _ { * objects and objects created by constructors other than Object are cloned to plain Object objects. An empty * object is returned for uncloneable values such as functions, DOM nodes, Maps, Sets, and WeakMaps. * @param value The value to deep clone. - * @param callback The function to customize cloning values. + * @param customizer The function to customize cloning values. * @param thisArg The this binding of customizer. * @return Returns the deep cloned value. */ cloneDeep( value: T, - callback?: (value: any) => any, + customizer?: (value: any) => any, thisArg?: any): T; } @@ -6022,7 +6104,7 @@ declare module _ { * @see _.cloneDeep */ cloneDeep( - callback?: (value: any) => any, + customizer?: (value: any) => any, thisArg?: any): T; } @@ -6031,7 +6113,7 @@ declare module _ { * @see _.cloneDeep */ cloneDeep( - callback?: (value: any) => any, + customizer?: (value: any) => any, thisArg?: any): T[]; } @@ -6040,7 +6122,7 @@ declare module _ { * @see _.cloneDeep */ cloneDeep( - callback?: (value: any) => any, + customizer?: (value: any) => any, thisArg?: any): T; } @@ -6496,26 +6578,6 @@ declare module _ { create(properties?: Object): LoDashObjectWrapper; } - //_.clone - interface LoDashStatic { - /** - * Creates a clone of value. If deep is true nested objects will also be cloned, otherwise - * they will be assigned by reference. If a callback is provided it will be executed to produce - * the cloned values. If the callback returns undefined cloning will be handled by the method - * instead. The callback is bound to thisArg and invoked with one argument; (value). - * @param value The value to clone. - * @param deep Specify a deep clone. - * @param callback The function to customize cloning values. - * @param thisArg The this binding of callback. - * @return The cloned value. - **/ - clone( - value: T, - deep?: boolean, - callback?: (value: any) => any, - thisArg?: any): T; - } - //_.defaults interface LoDashStatic { /**