Skip to content

Commit 9ff85e0

Browse files
authored
Merge pull request #128 from myty/feature/collection-utils-isempty
Typed nullable collection option to isEmpty, hasValues, and isNotEmpty functions
2 parents 6a9b516 + 218fb01 commit 9ff85e0

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

src/utilities/collection-utils.test.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,22 @@ describe("CollectionUtils", () => {
6060
});
6161

6262
test("given collections is undefined, it returns false", (): void => {
63-
// Arrange & Act
64-
const result = CollectionUtils.hasValues(
65-
(undefined as unknown) as any[]
66-
);
63+
// Arrange
64+
const collection: any[] | undefined = undefined;
65+
66+
// Act
67+
const result = CollectionUtils.hasValues(collection);
6768

6869
// Assert
6970
expect(result).toBeFalse();
7071
});
7172

7273
test("given collections is null, it returns false", (): void => {
73-
// Arrange & Act
74-
const result = CollectionUtils.hasValues(
75-
(null as unknown) as any[]
76-
);
74+
// Arrange
75+
const collection: any[] | null = null;
76+
77+
// Act
78+
const result = CollectionUtils.hasValues(collection);
7779

7880
// Assert
7981
expect(result).toBeFalse();
@@ -140,18 +142,22 @@ describe("CollectionUtils", () => {
140142
});
141143

142144
test("given collections is undefined, it returns true", (): void => {
143-
// Arrange & Act
144-
const result = CollectionUtils.isEmpty(
145-
(undefined as unknown) as any[]
146-
);
145+
// Arrange
146+
const collection: any[] | undefined = undefined;
147+
148+
// Act
149+
const result = CollectionUtils.isEmpty(collection);
147150

148151
// Assert
149152
expect(result).toBeTrue();
150153
});
151154

152155
test("given collections is null, it returns true", (): void => {
153-
// Arrange & Act
154-
const result = CollectionUtils.isEmpty((null as unknown) as any[]);
156+
// Arrange
157+
const collection: any[] | null = null;
158+
159+
// Act
160+
const result = CollectionUtils.isEmpty(collection);
155161

156162
// Assert
157163
expect(result).toBeTrue();
@@ -210,20 +216,22 @@ describe("CollectionUtils", () => {
210216
});
211217

212218
test("when collections param is undefined, it returns false", (): void => {
213-
// Arrange & Act
214-
const result = CollectionUtils.isNotEmpty(
215-
(undefined as unknown) as any[]
216-
);
219+
// Arrange
220+
const collection: any[] | undefined = undefined;
221+
222+
// Act
223+
const result = CollectionUtils.isNotEmpty(collection);
217224

218225
// Assert
219226
expect(result).toBeFalse();
220227
});
221228

222229
test("when collections param is null, it returns false", (): void => {
223-
// Arrange & Act
224-
const result = CollectionUtils.isNotEmpty(
225-
(null as unknown) as any[]
226-
);
230+
// Arrange
231+
const collection: any[] | null = null;
232+
233+
// Act
234+
const result = CollectionUtils.isNotEmpty(collection);
227235

228236
// Assert
229237
expect(result).toBeFalse();

src/utilities/collection-utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ const flattenDeep = <T>(
8080
*
8181
* Note: only takes a single collection as parameter to make use of Typescript Type guard ability
8282
*
83-
* @param {(any[] | Immutable.List<any> | undefined)} collection
83+
* @param {(any[] | Immutable.List<any> | null | undefined)} collection
8484
* @returns {boolean} False if `collection` is `undefined`, `null`, or has 0 elements.
8585
* True if collection contains any elements.
8686
*/
8787
const hasValues = (
88-
collection: any[] | Immutable.List<any> | undefined
88+
collection: any[] | Immutable.List<any> | null | undefined
8989
): collection is any[] | Immutable.List<any> => !isEmpty(collection);
9090

9191
/**
@@ -100,7 +100,7 @@ const hasValues = (
100100
* @param collection
101101
*/
102102
const isEmpty = (
103-
collection: any[] | Immutable.List<any> | undefined
103+
collection: any[] | Immutable.List<any> | null | undefined
104104
): collection is undefined => {
105105
if (collection == null) {
106106
return true;
@@ -119,12 +119,12 @@ const isEmpty = (
119119
*
120120
* Note: only takes a single collection as parameter to make use of Typescript Type guard ability
121121
*
122-
* @param {(any[] | Immutable.List<any> | undefined)} collection
122+
* @param {(any[] | Immutable.List<any> | null | undefined)} collection
123123
* @returns {boolean} False if `collection` is `undefined`, `null`, or has 0 elements.
124124
* True if collection contains any elements.
125125
*/
126126
const isNotEmpty = (
127-
collection: any[] | Immutable.List<any> | undefined
127+
collection: any[] | Immutable.List<any> | null | undefined
128128
): collection is any[] | Immutable.List<any> => hasValues(collection);
129129

130130
/**

0 commit comments

Comments
 (0)