Skip to content

Commit 7972c9f

Browse files
chore: add value helper
1 parent e083b86 commit 7972c9f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

__tests__/utils2.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import {testEnv} from '@technote-space/github-action-test-helper';
33
import {Utils} from '../src';
44

5-
const {generateNewPatchVersion, generateNewMinorVersion, generateNewMajorVersion, arrayChunk, versionCompare, isCommandDebug, isOutputDebug, objectGet} = Utils;
6-
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit, replaceVariables, mask, ensureNotNull} = Utils;
5+
const {generateNewPatchVersion, generateNewMinorVersion, generateNewMajorVersion, arrayChunk, versionCompare, isCommandDebug, isOutputDebug, objectGet, mask} = Utils;
6+
const {isBranch, isTagRef, normalizeRef, trimRef, getTag, getRefspec, getRemoteRefspec, getLocalRefspec, getOctokit, replaceVariables, ensureNotNullValue, ensureNotNull} = Utils;
77

88
jest.useFakeTimers();
99

@@ -320,6 +320,21 @@ describe('isOutputDebug', () => {
320320
});
321321
});
322322

323+
describe('ensureNotNullValue', () => {
324+
it('should return value', () => {
325+
expect(ensureNotNullValue('test', '')).toBe('test');
326+
expect(ensureNotNullValue(true, false)).toBe(true);
327+
expect(ensureNotNullValue(1, 0)).toBe(1);
328+
});
329+
330+
it('should return default value', () => {
331+
expect(ensureNotNullValue(null, '')).toBe('');
332+
expect(ensureNotNullValue(undefined, '')).toBe('');
333+
expect(ensureNotNullValue(null, false)).toBe(false);
334+
expect(ensureNotNullValue(undefined, 3)).toBe(3);
335+
});
336+
});
337+
323338
describe('ensureNotNull', () => {
324339
it('should return value', () => {
325340
expect(ensureNotNull('test')).toBe('test');

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,6 @@ export const objectGet = <T>(value: { [key: string]: any } | undefined, key: str
274274
return value[keys[0]];
275275
};
276276

277-
export const ensureNotNull = (value: string | null | undefined): string => value ?? '';
277+
export const ensureNotNullValue = <T>(value: T | null | undefined, defaultValue: T): T => value ?? defaultValue;
278+
279+
export const ensureNotNull = (value: string | null | undefined): string => ensureNotNullValue(value, '');

0 commit comments

Comments
 (0)