Skip to content

Commit c092aa1

Browse files
authored
feat(helpers): new method objectEntry (#2123)
1 parent ede6ffa commit c092aa1

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/modules/helpers/index.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,14 @@ export class HelpersModule {
764764
}
765765

766766
/**
767-
* Returns a random key from given object or `undefined` if no key could be found.
767+
* Returns a random key from given object.
768768
*
769769
* @template T The type of the object to select from.
770770
*
771771
* @param object The object to be used.
772772
*
773+
* @throws If the given object is empty.
774+
*
773775
* @example
774776
* faker.helpers.objectKey({ myProperty: 'myValue' }) // 'myProperty'
775777
*
@@ -781,12 +783,14 @@ export class HelpersModule {
781783
}
782784

783785
/**
784-
* Returns a random value from given object or `undefined` if no key could be found.
786+
* Returns a random value from given object.
785787
*
786788
* @template T The type of object to select from.
787789
*
788790
* @param object The object to be used.
789791
*
792+
* @throws If the given object is empty.
793+
*
790794
* @example
791795
* faker.helpers.objectValue({ myProperty: 'myValue' }) // 'myValue'
792796
*
@@ -797,6 +801,27 @@ export class HelpersModule {
797801
return object[key];
798802
}
799803

804+
/**
805+
* Returns a random `[key, value]` pair from the given object.
806+
*
807+
* @template T The type of the object to select from.
808+
*
809+
* @param object The object to be used.
810+
*
811+
* @throws If the given object is empty.
812+
*
813+
* @example
814+
* faker.helpers.objectEntry({ prop1: 'value1', prop2: 'value2' }) // ['prop1', 'value1']
815+
*
816+
* @since 8.0.0
817+
*/
818+
objectEntry<T extends Record<string, unknown>>(
819+
object: T
820+
): [keyof T, T[keyof T]] {
821+
const key = this.faker.helpers.objectKey(object);
822+
return [key, object[key]];
823+
}
824+
800825
/**
801826
* Returns random element from the given array.
802827
*

test/__snapshots__/helpers.spec.ts.snap

+21
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ exports[`helpers > 42 > mustache > template with method 1`] = `"Hello John!"`;
110110

111111
exports[`helpers > 42 > mustache > template with string 1`] = `"Hello John!"`;
112112

113+
exports[`helpers > 42 > objectEntry > simple 1`] = `
114+
[
115+
"b",
116+
2,
117+
]
118+
`;
119+
113120
exports[`helpers > 42 > objectKey > simple 1`] = `"b"`;
114121

115122
exports[`helpers > 42 > objectValue > simple 1`] = `2`;
@@ -343,6 +350,13 @@ exports[`helpers > 1211 > mustache > template with method 1`] = `"Hello John!"`;
343350

344351
exports[`helpers > 1211 > mustache > template with string 1`] = `"Hello John!"`;
345352

353+
exports[`helpers > 1211 > objectEntry > simple 1`] = `
354+
[
355+
"c",
356+
3,
357+
]
358+
`;
359+
346360
exports[`helpers > 1211 > objectKey > simple 1`] = `"c"`;
347361

348362
exports[`helpers > 1211 > objectValue > simple 1`] = `3`;
@@ -558,6 +572,13 @@ exports[`helpers > 1337 > mustache > template with method 1`] = `"Hello John!"`;
558572

559573
exports[`helpers > 1337 > mustache > template with string 1`] = `"Hello John!"`;
560574

575+
exports[`helpers > 1337 > objectEntry > simple 1`] = `
576+
[
577+
"a",
578+
1,
579+
]
580+
`;
581+
561582
exports[`helpers > 1337 > objectKey > simple 1`] = `"a"`;
562583

563584
exports[`helpers > 1337 > objectValue > simple 1`] = `1`;

test/helpers.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ describe('helpers', () => {
157157
t.it('simple', { a: 1, b: 2, c: 3 });
158158
});
159159

160+
t.describe('objectEntry', (t) => {
161+
t.it('simple', { a: 1, b: 2, c: 3 });
162+
});
163+
160164
t.describe('fake', (t) => {
161165
t.it('with empty string', '')
162166
.it('with a static template', 'my test string')
@@ -935,6 +939,27 @@ describe('helpers', () => {
935939
});
936940
});
937941

942+
describe('objectEntry', () => {
943+
it('should return a random key, value pair', () => {
944+
const testObject = {
945+
hello: 'to',
946+
you: 'my',
947+
friend: '!',
948+
};
949+
const [key, value] = faker.helpers.objectEntry(testObject);
950+
951+
expect(Object.keys(testObject)).toContain(key);
952+
expect(Object.values(testObject)).toContain(value);
953+
expect(testObject[key]).toEqual(value);
954+
});
955+
956+
it('should throw if given object is empty', () => {
957+
expect(() => faker.helpers.objectEntry({})).toThrowError(
958+
new FakerError('Cannot get value from empty dataset.')
959+
);
960+
});
961+
});
962+
938963
describe('fake()', () => {
939964
it('does allow empty string input', () => {
940965
const actual = faker.helpers.fake('');

0 commit comments

Comments
 (0)