Skip to content

Commit

Permalink
Add category/details converter (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
ypc-faros authored Oct 16, 2023
1 parent 4482c2a commit 997a180
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,27 @@ export class Utils {
setTimeout(resolve, millis);
});
}

static toCategoryDetail<EnumType extends {Custom: any}>(
enumObject: EnumType & Record<string, any>,
category: string,
categoryMapping: Record<string, string> = {}
): {
category: EnumType[keyof EnumType];
detail: string;
} {
const enumSymbol =
enumObject[category] ?? enumObject[categoryMapping[category]];
if (enumSymbol) {
return {
category: enumSymbol,
detail: category,
};
}

return {
category: enumObject.Custom,
detail: category,
};
}
}
60 changes: 60 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,64 @@ describe('parse primary keys', () => {
true)
).toEqual(['lat', 'lon']);
});

describe('to category detail converter', () => {
enum Example {
Option1 = 'Option1',
Option2 = 'Option2',
Custom = 'Custom',
}

test('category matches enum symbol', () => {
expect(
sut.Utils.toCategoryDetail(Example, 'Option1')
).toEqual({
category: Example.Option1,
detail: 'Option1',
});
});

test('mapped category matches enum symbol', () => {
const sourceCategory = 'Option One';
const questionCategoryMapping = {
[sourceCategory]: 'Option1',
};

expect(
sut.Utils.toCategoryDetail(
Example,
sourceCategory,
questionCategoryMapping
)
).toEqual({
category: Example.Option1,
detail: sourceCategory,
});
});

test('unmatched category', () => {
const sourceCategory = 'Option One';
const questionCategoryMapping = {
[sourceCategory]: 'NotARealCategory',
};

expect(
sut.Utils.toCategoryDetail(
Example,
sourceCategory,
questionCategoryMapping
)
).toEqual({
category: Example.Custom,
detail: sourceCategory,
});

expect(
sut.Utils.toCategoryDetail(Example, sourceCategory)
).toEqual({
category: Example.Custom,
detail: sourceCategory,
});
});
});
});

0 comments on commit 997a180

Please sign in to comment.