Skip to content

Commit ad49152

Browse files
Merge pull request #343 from technote-space/release/next-v4.4.3
release: v4.4.4
2 parents f6165ee + 4c3a6d9 commit ad49152

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
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
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/github-action-helper",
3-
"version": "4.4.3",
3+
"version": "4.4.4",
44
"description": "Helper for GitHub Action.",
55
"keywords": [
66
"github",

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)