Skip to content

Commit 25d682c

Browse files
chore: tweaks
1 parent f6165ee commit 25d682c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

__tests__/utils2.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,9 @@ describe('objectGet', () => {
357357
expect(objectGet({test1: {test2: 123}}, '')).toBeUndefined();
358358
expect(objectGet({test1: {test2: 123}}, 'test1.test3')).toBeUndefined();
359359
});
360+
361+
it('should return default value', () => {
362+
expect(objectGet(undefined, 'test1.test2', 123)).toBe(123);
363+
expect(objectGet({test1: {test2: 123}}, '', false)).toBe(false);
364+
});
360365
});

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,17 @@ export const isCommandDebug = (): boolean => getInput('UTILS_COMMAND_DEBUG') ===
258258
export const isOutputDebug = (): boolean => getInput('UTILS_OUTPUT_DEBUG') === 'true';
259259

260260
// eslint-disable-next-line @typescript-eslint/no-explicit-any
261-
export const objectGet = <T>(value: { [key: string]: any } | undefined, key: string): T | undefined => {
261+
export const objectGet = <T>(value: { [key: string]: any } | undefined | null, key: string, defaultValue?: T): T | undefined => {
262262
const keys = key.split('.');
263263

264264
if (!keys.length || !value || !(keys[0] in value)) {
265-
return undefined;
265+
return defaultValue;
266266
}
267267

268268
// eslint-disable-next-line no-magic-numbers
269269
if (keys.length > 1) {
270270
// eslint-disable-next-line no-magic-numbers
271-
return objectGet(value[keys[0]], keys.slice(1).join('.'));
271+
return objectGet(value[keys[0]], keys.slice(1).join('.'), defaultValue);
272272
}
273273

274274
return value[keys[0]];

0 commit comments

Comments
 (0)