From 693b8417390d92bc408da8e8bd220a754d306258 Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Sun, 6 Sep 2015 22:43:29 +0500 Subject: [PATCH] lodash: changed _.pick() method --- lodash/lodash-tests.ts | 19 ++++++++++---- lodash/lodash.d.ts | 58 ++++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 4e3bb97da..0896eb87b 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -1594,11 +1594,20 @@ result = _({ 'name': 'moe', 'age': 40 }).omit(function (value) { result = _.pairs({ 'moe': 30, 'larry': 40 }); result = _({ 'moe': 30, 'larry': 40 }).pairs().value(); -result = _.pick({ 'name': 'moe', '_userid': 'moe1' }, 'name'); -result = _.pick({ 'name': 'moe', '_userid': 'moe1' }, ['name']); -result = _.pick({ 'name': 'moe', '_userid': 'moe1' }, function (value, key) { - return key.charAt(0) != '_'; -}); +// _.pick +interface TestPickFn { + (element: any, key: string, collection: any): boolean; +} +{ + let testPickFn: TestPickFn; + let result: TResult; + result = _.pick({}, 0, '1', true, [2], ['3'], [true], [4, '5', true]); + result = _.pick({}, testPickFn); + result = _.pick({}, testPickFn, any); + result = _({}).pick(0, '1', true, [2], ['3'], [true], [4, '5', true]).value(); + result = _({}).pick(testPickFn).value(); + result = _({}).pick(testPickFn, any).value(); +} // _.set result = <{ a: { b: { c: number; }}[]}>_.set({ 'a': [{ 'b': { 'c': 3 } }] }, 'a[0].b.c', 4); diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index f54bbbbb4..4483563ed 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -7450,36 +7450,50 @@ declare module _ { pairs(): LoDashArrayWrapper; } - //_.picks + //_.pick interface LoDashStatic { /** - * Creates a shallow clone of object composed of the specified properties. Property names may be - * specified as individual arguments or as arrays of property names. If a callback is provided - * it will be executed for each property of object picking the properties the callback returns - * truey for. The callback is bound to thisArg and invoked with three arguments; (value, key, - * object). - * @param object Object to strip unwanted key/value pairs. - * @param keys Property names to pick - * @return An object composed of the picked properties. - **/ - pick( + * Creates an object composed of the picked object properties. Property names may be specified as individual + * arguments or as arrays of property names. If predicate is provided it’s invoked for each property of object + * picking the properties predicate returns truthy for. The predicate is bound to thisArg and invoked with + * three arguments: (value, key, object). + * + * @param object The source object. + * @param predicate The function invoked per iteration or property names to pick, specified as individual + * property names or arrays of property names. + * @param thisArg The this binding of predicate. + * @return An object composed of the picked properties. + */ + pick( object: T, - ...keys: string[]): Picked; + predicate: ObjectIterator, + thisArg?: any + ): TResult; /** - * @see _.pick - **/ - pick( + * @see _.pick + */ + pick( object: T, - keys: string[]): Picked; + ...predicate: Array> + ): TResult; + } + + interface LoDashObjectWrapper { + /** + * @see _.pick + */ + pick( + predicate: ObjectIterator, + thisArg?: any + ): LoDashObjectWrapper; /** - * @see _.pick - **/ - pick( - object: T, - callback: ObjectIterator, - thisArg?: any): Picked; + * @see _.pick + */ + pick( + ...predicate: Array> + ): LoDashObjectWrapper; } //_.set