Skip to content

Commit cbacd07

Browse files
committed
return undefined instead of null
1 parent d56a0bc commit cbacd07

6 files changed

Lines changed: 15 additions & 15 deletions

File tree

lib/Onyx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
346346
if (newValue !== oldValue) {
347347
cache.set(key, newValue);
348348

349-
const collectionKey = OnyxUtils.getCollectionKey(key) ?? undefined;
349+
const collectionKey = OnyxUtils.getCollectionKey(key);
350350

351351
if (collectionKey) {
352352
if (!keyValuesToResetAsCollection[collectionKey]) {

lib/OnyxCache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,13 @@ class OnyxCache {
458458
/**
459459
* Get the collection key for a given member key
460460
*/
461-
getCollectionKey(key: OnyxKey): OnyxKey | null {
461+
getCollectionKey(key: OnyxKey): OnyxKey | undefined {
462462
for (const collectionKey of this.collectionKeys) {
463463
if (key.startsWith(collectionKey) && key.length > collectionKey.length) {
464464
return collectionKey;
465465
}
466466
}
467-
return null;
467+
return undefined;
468468
}
469469

470470
/**

lib/OnyxSnapshotCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class OnyxSnapshotCache {
135135

136136
// Check if the key is a collection member and invalidate the collection base key
137137
const collectionBaseKey = OnyxUtils.getCollectionKey(keyToInvalidate);
138-
if (collectionBaseKey !== null) {
138+
if (collectionBaseKey) {
139139
this.snapshotCache.delete(collectionBaseKey);
140140
}
141141
}

lib/OnyxUtils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collect
477477
function isCollectionMember(key: OnyxKey): boolean {
478478
const collectionKey = getCollectionKey(key);
479479
// If the key is longer than the collection key, it's a collection member
480-
return collectionKey !== null && key.length > collectionKey.length;
480+
return !!collectionKey && key.length > collectionKey.length;
481481
}
482482

483483
/**
@@ -499,7 +499,7 @@ function isCollectionMember(key: OnyxKey): boolean {
499499
*/
500500
function isRamOnlyKey(key: OnyxKey): boolean {
501501
const collectionKey = getCollectionKey(key);
502-
if (collectionKey !== null) {
502+
if (collectionKey) {
503503
return cache.isRamOnlyKey(collectionKey);
504504
}
505505

@@ -523,7 +523,7 @@ function splitCollectionMemberKey<TKey extends CollectionKey, CollectionKeyType
523523

524524
if (!collectionKey) {
525525
const resolvedKey = getCollectionKey(key);
526-
if (resolvedKey === null) {
526+
if (!resolvedKey) {
527527
throw new Error(`Invalid '${key}' key provided, only collection keys are allowed.`);
528528
}
529529
// eslint-disable-next-line no-param-reassign
@@ -551,9 +551,9 @@ function isKeyMatch(configKey: OnyxKey, key: OnyxKey): boolean {
551551
* - `getCollectionKey("sharedNVP_user_-1_something")` would return "sharedNVP_user_"
552552
*
553553
* @param key - The collection key to process.
554-
* @returns The plain collection key or null if the key is not a collection one.
554+
* @returns The plain collection key or undefined if the key is not a collection one.
555555
*/
556-
function getCollectionKey(key: CollectionKey): string | null {
556+
function getCollectionKey(key: CollectionKey): string | undefined {
557557
// Start by finding the position of the last underscore in the string
558558
let lastUnderscoreIndex = key.lastIndexOf('_');
559559

@@ -571,7 +571,7 @@ function getCollectionKey(key: CollectionKey): string | null {
571571
lastUnderscoreIndex = key.lastIndexOf('_', lastUnderscoreIndex - 1);
572572
}
573573

574-
return null;
574+
return undefined;
575575
}
576576

577577
/**
@@ -750,7 +750,7 @@ function keyChanged<TKey extends OnyxKey>(
750750
// do the same in keysChanged, because we only call that function when a collection key changes, and it doesn't happen that often.
751751
// For performance reason, we look for the given key and later if don't find it we look for the collection key, instead of checking if it is a collection key first.
752752
let stateMappingKeys = onyxKeyToSubscriptionIDs.get(key) ?? [];
753-
const collectionKey = getCollectionKey(key) ?? undefined;
753+
const collectionKey = getCollectionKey(key);
754754

755755
if (collectionKey) {
756756
// Getting the collection key from the specific key because only collection keys were stored in the mapping.

tests/unit/OnyxSnapshotCacheTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe('OnyxSnapshotCache', () => {
158158

159159
it('should invalidate non-collection keys without affecting others', () => {
160160
mockedOnyxUtils.isCollectionKey.mockReturnValue(false);
161-
mockedOnyxUtils.getCollectionKey.mockReturnValue(null);
161+
mockedOnyxUtils.getCollectionKey.mockReturnValue(undefined);
162162

163163
cache.invalidateForKey('nonCollectionKey');
164164

tests/unit/onyxUtilsTest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ describe('OnyxUtils', () => {
342342
});
343343
});
344344

345-
it('should return null if key does not contain underscore', () => {
346-
expect(OnyxUtils.getCollectionKey(ONYXKEYS.TEST_KEY)).toBeNull();
347-
expect(OnyxUtils.getCollectionKey('')).toBeNull();
345+
it('should return undefined if key does not contain underscore', () => {
346+
expect(OnyxUtils.getCollectionKey(ONYXKEYS.TEST_KEY)).toBeUndefined();
347+
expect(OnyxUtils.getCollectionKey('')).toBeUndefined();
348348
});
349349
});
350350

0 commit comments

Comments
 (0)