From c092aa1276a5c249de1ada47e807f12dd6de36f7 Mon Sep 17 00:00:00 2001 From: Suyash Gulati Date: Wed, 3 May 2023 22:19:50 +0530 Subject: feat(helpers): new method `objectEntry` (#2123) --- src/modules/helpers/index.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src/modules') diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index ba7e083a..e6cf9075 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -764,12 +764,14 @@ export class HelpersModule { } /** - * Returns a random key from given object or `undefined` if no key could be found. + * Returns a random key from given object. * * @template T The type of the object to select from. * * @param object The object to be used. * + * @throws If the given object is empty. + * * @example * faker.helpers.objectKey({ myProperty: 'myValue' }) // 'myProperty' * @@ -781,12 +783,14 @@ export class HelpersModule { } /** - * Returns a random value from given object or `undefined` if no key could be found. + * Returns a random value from given object. * * @template T The type of object to select from. * * @param object The object to be used. * + * @throws If the given object is empty. + * * @example * faker.helpers.objectValue({ myProperty: 'myValue' }) // 'myValue' * @@ -797,6 +801,27 @@ export class HelpersModule { return object[key]; } + /** + * Returns a random `[key, value]` pair from the given object. + * + * @template T The type of the object to select from. + * + * @param object The object to be used. + * + * @throws If the given object is empty. + * + * @example + * faker.helpers.objectEntry({ prop1: 'value1', prop2: 'value2' }) // ['prop1', 'value1'] + * + * @since 8.0.0 + */ + objectEntry>( + object: T + ): [keyof T, T[keyof T]] { + const key = this.faker.helpers.objectKey(object); + return [key, object[key]]; + } + /** * Returns random element from the given array. * -- cgit v1.2.3