Skip to content

Commit

Permalink
Updated collection keys to handle multiple underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
gedu committed Jun 26, 2024
1 parent d55dcd1 commit d4569dd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collect
* @returns A tuple where the first element is the collection part and the second element is the ID part.
*/
function splitCollectionMemberKey<TKey extends CollectionKey>(key: TKey): [TKey extends `${infer Prefix}_${string}` ? `${Prefix}_` : never, string] {
const underscoreIndex = key.indexOf('_');
const underscoreIndex = key.lastIndexOf('_');

if (underscoreIndex === -1) {
throw new Error(`Invalid ${key} key provided, only collection keys are allowed.`);
Expand Down Expand Up @@ -434,16 +434,18 @@ function addToEvictionBlockList(key: OnyxKey, connectionID: number): void {
* - `getCollectionKey("report_123")` would return "report_"
* - `getCollectionKey("report")` would return "report"
* - `getCollectionKey("report_")` would return "report_"
* - `getCollectionKey(null)` would return ""
*
* @param {OnyxKey} key - The key to process.
* @return {string} The pure key without any numeric
*/
function getCollectionKey(key: OnyxKey): string {
if (!key) {
return '';
const underscoreIndex = key.lastIndexOf('_');

if (underscoreIndex === -1) {
return key;
}
return key.replace(/_\w+/g, '_');

return key.substring(0, underscoreIndex + 1);
}

/**
Expand Down Expand Up @@ -1203,6 +1205,7 @@ const OnyxUtils = {
keyChanged,
sendDataToConnection,
addKeyToRecentlyAccessedIfNeeded,
getCollectionKey,
getCollectionDataAndSendAsObject,
scheduleSubscriberUpdate,
scheduleNotifyCollectionSubscribers,
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/onyxUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import OnyxUtils from '../../lib/OnyxUtils';

describe('OnyxUtils', () => {
it('splitCollectionMemberKey should return correct values', () => {
const dataResult: Record<string, [string, string]> = {
test_: ['test_', ''],
test_level_: ['test_level_', ''],
test_level_1: ['test_level_', '1'],
test_level_2: ['test_level_', '2'],
test_level_last_3: ['test_level_last_', '3'],
};

Object.keys(dataResult).forEach((key) => {
const [collectionKey, id] = OnyxUtils.splitCollectionMemberKey(key);
expect(collectionKey).toEqual(dataResult[key][0]);
expect(id).toEqual(dataResult[key][1]);
});
});

it('splitCollectionMemberKey should throw error if key does not contain underscore', () => {
expect(() => {
OnyxUtils.splitCollectionMemberKey('test');
}).toThrowError('Invalid test key provided, only collection keys are allowed.');
expect(() => {
OnyxUtils.splitCollectionMemberKey('');
}).toThrowError('Invalid key provided, only collection keys are allowed.');
});

it('getCollectionKey should return correct values', () => {
const dataResult: Record<string, string> = {
test: 'test',
test_: 'test_',
test_level_: 'test_level_',
test_level_1: 'test_level_',
test_level_2: 'test_level_',
test_level_last_3: 'test_level_last_',
};

Object.keys(dataResult).forEach((key) => {
const collectionKey = OnyxUtils.getCollectionKey(key);
expect(collectionKey).toEqual(dataResult[key]);
});
});
});

0 comments on commit d4569dd

Please sign in to comment.