Skip to content

Commit c3fc549

Browse files
committed
feat(utils): implement type guard for nullable object props
1 parent e5f743a commit c3fc549

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

packages/plugin-lighthouse/src/lib/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CliFlags } from 'lighthouse';
2-
import type { ExcludeNullFromPropertyTypes } from '@code-pushup/utils';
2+
import type { ExcludeNullableProps } from '@code-pushup/utils';
33

4-
export type LighthouseOptions = ExcludeNullFromPropertyTypes<
4+
export type LighthouseOptions = ExcludeNullableProps<
55
Partial<
66
Omit<
77
CliFlags,

packages/utils/src/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ export {
3939
truncateText,
4040
truncateTitle,
4141
} from './lib/formatting.js';
42-
export {
43-
formatGitPath,
44-
getGitRoot,
45-
guardAgainstLocalChanges,
46-
safeCheckout,
47-
toGitPath,
48-
} from './lib/git/git.js';
4942
export {
5043
getCurrentBranchOrTag,
5144
getHashFromTag,
@@ -54,10 +47,18 @@ export {
5447
getSemverTags,
5548
type LogResult,
5649
} from './lib/git/git.commits-and-tags.js';
50+
export {
51+
formatGitPath,
52+
getGitRoot,
53+
guardAgainstLocalChanges,
54+
safeCheckout,
55+
toGitPath,
56+
} from './lib/git/git.js';
5757
export { groupByStatus } from './lib/group-by-status.js';
5858
export {
5959
isPromiseFulfilledResult,
6060
isPromiseRejectedResult,
61+
hasNoNullableProps,
6162
} from './lib/guards.js';
6263
export { logMultipleResults } from './lib/log-results.js';
6364
export { link, ui, type CliUi, type Column } from './lib/logging.js';
@@ -114,7 +115,7 @@ export {
114115
type CliArgsObject,
115116
} from './lib/transform.js';
116117
export type {
117-
ExcludeNullFromPropertyTypes,
118+
ExcludeNullableProps,
118119
ExtractArray,
119120
ExtractArrays,
120121
ItemOrArray,

packages/utils/src/lib/guards.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ExcludeNullableProps } from './types.js';
2+
13
export function isPromiseFulfilledResult<T>(
24
result: PromiseSettledResult<T>,
35
): result is PromiseFulfilledResult<T> {
@@ -9,3 +11,9 @@ export function isPromiseRejectedResult(
911
): result is PromiseRejectedResult {
1012
return result.status === 'rejected';
1113
}
14+
15+
export function hasNoNullableProps<T extends object>(
16+
obj: T,
17+
): obj is ExcludeNullableProps<T> {
18+
return Object.values(obj).every(value => value != null);
19+
}

packages/utils/src/lib/guards.unit.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { describe } from 'vitest';
2-
import { isPromiseFulfilledResult, isPromiseRejectedResult } from './guards.js';
2+
import {
3+
hasNoNullableProps,
4+
isPromiseFulfilledResult,
5+
isPromiseRejectedResult,
6+
} from './guards.js';
37

48
describe('promise-result', () => {
59
it('should get fulfilled result', () => {
@@ -20,3 +24,21 @@ describe('promise-result', () => {
2024
expect(isPromiseRejectedResult(result)).toBe(true);
2125
});
2226
});
27+
28+
describe('hasNoNullableProps', () => {
29+
it('should return true if object prop values are neither null nor undefined', () => {
30+
expect(hasNoNullableProps({ a: 42, b: 'foo', c: {}, d: [] })).toBe(true);
31+
});
32+
33+
it('should return false if some prop is null', () => {
34+
expect(hasNoNullableProps({ x: 42, y: null })).toBe(false);
35+
});
36+
37+
it('should return false if some prop is set to undefined', () => {
38+
expect(hasNoNullableProps({ x: undefined })).toBe(false);
39+
});
40+
41+
it('should return true for empty object', () => {
42+
expect(hasNoNullableProps({})).toBe(true);
43+
});
44+
});

packages/utils/src/lib/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export type ExcludeNullFromPropertyTypes<T> = {
2-
[P in keyof T]: Exclude<T[P], null>;
1+
export type ExcludeNullableProps<T> = {
2+
[P in keyof T]: NonNullable<T[P]>;
33
};
44

55
export type ItemOrArray<T> = T | T[];

0 commit comments

Comments
 (0)