aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSuyash Gulati <[email protected]>2023-05-03 22:19:50 +0530
committerGitHub <[email protected]>2023-05-03 18:49:50 +0200
commitc092aa1276a5c249de1ada47e807f12dd6de36f7 (patch)
tree0740df1ad8ca2d3d7666e6b78e7ad1af8fae04ed /src
parentede6ffac383a853c12b7a47cebbb031ba80627aa (diff)
downloadfaker-c092aa1276a5c249de1ada47e807f12dd6de36f7.tar.xz
faker-c092aa1276a5c249de1ada47e807f12dd6de36f7.zip
feat(helpers): new method `objectEntry` (#2123)
Diffstat (limited to 'src')
-rw-r--r--src/modules/helpers/index.ts29
1 files changed, 27 insertions, 2 deletions
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'
*
@@ -798,6 +802,27 @@ export class HelpersModule {
}
/**
+ * 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<T extends Record<string, unknown>>(
+ 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.
*
* @template T The type of the elements to pick from.