Skip to content

Commit 36cd461

Browse files
xDivisionByZeroxLeyla JähnigShinigami92
authored
feat: separate methods for object key value (#503)
Co-authored-by: Leyla Jähnig <leyla.jaehnig@outlook.de> Co-authored-by: Shinigami92 <chrissi92@hotmail.de>
1 parent 6cfd1e1 commit 36cd461

5 files changed

Lines changed: 139 additions & 24 deletions

File tree

src/finance.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class Finance {
171171
* faker.finance.currencyCode() // 'USD'
172172
*/
173173
currencyCode(): string {
174-
return this.faker.random.objectElement(
174+
return this.faker.helpers.objectValue(
175175
this.faker.definitions.finance.currency
176176
)['code'];
177177
}
@@ -183,10 +183,9 @@ export class Finance {
183183
* faker.finance.currencyName() // 'US Dollar'
184184
*/
185185
currencyName(): string {
186-
return this.faker.random.objectElement(
187-
this.faker.definitions.finance.currency,
188-
'key'
189-
);
186+
return this.faker.helpers.objectKey(
187+
this.faker.definitions.finance.currency
188+
) as string;
190189
}
191190

192191
/**
@@ -198,7 +197,7 @@ export class Finance {
198197
currencySymbol(): string {
199198
let symbol: string;
200199
while (!symbol) {
201-
symbol = this.faker.random.objectElement(
200+
symbol = this.faker.helpers.objectValue(
202201
this.faker.definitions.finance.currency
203202
)['symbol'];
204203
}
@@ -265,7 +264,7 @@ export class Finance {
265264
} else {
266265
// Choose a random provider
267266
// Credit cards are in an object structure
268-
const formats = this.faker.random.objectElement(localeFormat, 'value'); // There could be multiple formats
267+
const formats = this.faker.helpers.objectValue(localeFormat); // There could be multiple formats
269268
format = this.faker.random.arrayElement(formats);
270269
}
271270
format = format.replace(/\//g, '');

src/helpers.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export interface Transaction {
112112
}
113113

114114
/**
115-
* Module with various helper methods that don't fit in a particular category.
115+
* Module with various helper methods that transform the method input rather than returning values from locales.
116+
* The transformation process may call methods that use the locale data.
116117
*/
117118
export class Helpers {
118119
constructor(private readonly faker: Faker) {
@@ -701,9 +702,9 @@ export class Helpers {
701702
* @param options.probability The probability (`[0.00, 1.00]`) of the callback being invoked. Defaults to `0.5`.
702703
*
703704
* @example
704-
* faker.random.maybe(() => 'Hello World!') // 'Hello World!'
705-
* faker.random.maybe(() => 'Hello World!', { probability: 0.1 }) // undefined
706-
* faker.random.maybe(() => 'Hello World!', { probability: 0.9 }) // 'Hello World!'
705+
* faker.helpers.maybe(() => 'Hello World!') // 'Hello World!'
706+
* faker.helpers.maybe(() => 'Hello World!', { probability: 0.1 }) // undefined
707+
* faker.helpers.maybe(() => 'Hello World!', { probability: 0.9 }) // 'Hello World!'
707708
*/
708709
maybe<T>(
709710
callback: () => T,
@@ -715,4 +716,30 @@ export class Helpers {
715716
}
716717
return undefined;
717718
}
719+
720+
/**
721+
* Returns a random key from given object.
722+
*
723+
* @param object The object to be used.
724+
*
725+
* @example
726+
* faker.helpers.objectKey({ myProperty: 'myValue' }) // 'myProperty'
727+
*/
728+
objectKey<T extends Record<string, unknown>>(object: T): keyof T {
729+
const array: Array<keyof T> = Object.keys(object);
730+
return this.faker.random.arrayElement(array);
731+
}
732+
733+
/**
734+
* Returns a random value from given object.
735+
*
736+
* @param object The object to be used.
737+
*
738+
* @example
739+
* faker.helpers.objectValue({ myProperty: 'myValue' }) // 'myValue'
740+
*/
741+
objectValue<T extends Record<string, unknown>>(object: T): T[keyof T] {
742+
const key = this.faker.helpers.objectKey(object);
743+
return object[key];
744+
}
718745
}

src/random.ts

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,42 @@ export class Random {
161161
}
162162

163163
/**
164-
* Returns a random key or value from given object.
164+
* Returns a random key from given object.
165165
*
166166
* @template T The type of `Record` to pick from.
167167
* @template K The keys of `T`.
168-
* @param object The object to get the keys or values from.
168+
* @param object The object to get the keys from.
169169
* @param field If this is set to `'key'`, this method will a return a random key of the given instance.
170-
* If this is set to `'value'`, this method will a return a random value of the given instance.
171-
* Defaults to `'value'`.
170+
*
171+
* @see faker.helpers.objectKey()
172172
*
173173
* @example
174174
* const object = { keyA: 'valueA', keyB: 42 };
175-
* faker.random.objectElement(object) // 42
176175
* faker.random.objectElement(object, 'key') // 'keyB'
177-
* faker.random.objectElement(object, 'value') // 'valueA'
176+
*
177+
* @deprecated
178178
*/
179179
objectElement<T extends Record<string, unknown>, K extends keyof T>(
180180
object: T,
181181
field: 'key'
182182
): K;
183+
/**
184+
* Returns a random value from given object.
185+
*
186+
* @template T The type of `Record` to pick from.
187+
* @template K The keys of `T`.
188+
* @param object The object to get the values from.
189+
* @param field If this is set to `'value'`, this method will a return a random value of the given instance.
190+
*
191+
* @see faker.helpers.objectValue()
192+
*
193+
* @example
194+
* const object = { keyA: 'valueA', keyB: 42 };
195+
* faker.random.objectElement(object) // 42
196+
* faker.random.objectElement(object, 'value') // 'valueA'
197+
*
198+
* @deprecated
199+
*/
183200
objectElement<T extends Record<string, unknown>, K extends keyof T>(
184201
object: T,
185202
field?: unknown
@@ -194,24 +211,56 @@ export class Random {
194211
* If this is set to `'value'`, this method will a return a random value of the given instance.
195212
* Defaults to `'value'`.
196213
*
214+
* @see faker.helpers.objectKey()
215+
* @see faker.helpers.objectValue()
216+
*
197217
* @example
198218
* const object = { keyA: 'valueA', keyB: 42 };
199219
* faker.random.objectElement(object) // 42
200220
* faker.random.objectElement(object, 'key') // 'keyB'
201221
* faker.random.objectElement(object, 'value') // 'valueA'
222+
*
223+
* @deprecated
202224
*/
203225
objectElement<T extends Record<string, unknown>, K extends keyof T>(
204-
object: T,
226+
object?: T,
205227
field?: 'key' | 'value'
206228
): K | T[K];
229+
/**
230+
* Returns a random key or value from given object.
231+
*
232+
* @template T The type of `Record` to pick from.
233+
* @template K The keys of `T`.
234+
* @param object The object to get the keys or values from.
235+
* @param field If this is set to `'key'`, this method will a return a random key of the given instance.
236+
* If this is set to `'value'`, this method will a return a random value of the given instance.
237+
* Defaults to `'value'`.
238+
*
239+
* @see faker.helpers.objectKey()
240+
* @see faker.helpers.objectValue()
241+
*
242+
* @example
243+
* const object = { keyA: 'valueA', keyB: 42 };
244+
* faker.random.objectElement(object) // 42
245+
* faker.random.objectElement(object, 'key') // 'keyB'
246+
* faker.random.objectElement(object, 'value') // 'valueA'
247+
*
248+
* @deprecated
249+
*/
207250
objectElement<T extends Record<string, unknown>, K extends keyof T>(
208-
object = { foo: 'bar', too: 'car' } as unknown as T,
209-
field = 'value'
251+
object: T = { foo: 'bar', too: 'car' } as unknown as T,
252+
field: 'key' | 'value' = 'value'
210253
): K | T[K] {
211-
const array: Array<keyof T> = Object.keys(object);
212-
const key = this.arrayElement(array);
213-
214-
return field === 'key' ? (key as K) : (object[key] as T[K]);
254+
const useKey = field === 'key';
255+
deprecated({
256+
deprecated: `faker.random.objectElement(${useKey ? "obj, 'key'" : ''})`,
257+
proposed: `faker.helpers.object${useKey ? 'Key' : 'Value'}()`,
258+
since: 'v6.3.0',
259+
until: 'v7.0.0',
260+
});
261+
return field === 'key'
262+
? (this.faker.helpers.objectKey(object) as K)
263+
: (this.faker.helpers.objectValue(object) as T[K]);
215264
}
216265

217266
/**

test/helpers.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,32 @@ describe('helpers', () => {
831831
});
832832
});
833833

834+
describe('objectKey', () => {
835+
it('should return a random key', () => {
836+
const testObject = {
837+
hello: 'to',
838+
you: 'my',
839+
friend: '!',
840+
};
841+
const actual = faker.helpers.objectKey(testObject);
842+
843+
expect(Object.keys(testObject)).toContain(actual);
844+
});
845+
});
846+
847+
describe('objectValue', () => {
848+
it('should return a random value', () => {
849+
const testObject = {
850+
hello: 'to',
851+
you: 'my',
852+
friend: '!',
853+
};
854+
const actual = faker.helpers.objectValue(testObject);
855+
856+
expect(Object.values(testObject)).toContain(actual);
857+
});
858+
});
859+
834860
describe('deprecation warnings', () => {
835861
it.each([['randomize', 'random.arrayElement']])(
836862
'should warn user that function helpers.%s is deprecated',

test/random.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ describe('random', () => {
159159

160160
describe('objectElement', () => {
161161
it('should return a random value', () => {
162+
const spy = vi.spyOn(console, 'warn');
163+
162164
const testObject = {
163165
hello: 'to',
164166
you: 'my',
@@ -167,9 +169,16 @@ describe('random', () => {
167169
const actual = faker.random.objectElement(testObject);
168170

169171
expect(Object.values(testObject)).toContain(actual);
172+
expect(spy).toHaveBeenCalledWith(
173+
`[@faker-js/faker]: faker.random.objectElement() is deprecated since v6.3.0 and will be removed in v7.0.0. Please use faker.helpers.objectValue() instead.`
174+
);
175+
176+
spy.mockRestore();
170177
});
171178

172179
it('should return a random key', () => {
180+
const spy = vi.spyOn(console, 'warn');
181+
173182
const testObject = {
174183
hello: 'to',
175184
you: 'my',
@@ -178,6 +187,11 @@ describe('random', () => {
178187
const actual = faker.random.objectElement(testObject, 'key');
179188

180189
expect(Object.keys(testObject)).toContain(actual);
190+
expect(spy).toHaveBeenCalledWith(
191+
`[@faker-js/faker]: faker.random.objectElement(obj, 'key') is deprecated since v6.3.0 and will be removed in v7.0.0. Please use faker.helpers.objectKey() instead.`
192+
);
193+
194+
spy.mockRestore();
181195
});
182196
});
183197

0 commit comments

Comments
 (0)