Skip to content

Commit

Permalink
fix(rels): expose reference intuitive.via and support explicit refere…
Browse files Browse the repository at this point in the history
…nce prefix
  • Loading branch information
uladkasach committed Jul 12, 2024
1 parent 08ca946 commit 351e305
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ describe('isPropertyNameAReferenceExplicitly', () => {
domainObjectName: 'LeadEngineer',
expectedResult: false,
},
{
propertyName: 'leadEngineerRef',
domainObjectName: 'LeadEngineerRef',
expectedResult: true,
},
{
propertyName: 'priceDue',
domainObjectName: 'Price',
expectedResult: true,
},
];

cases.forEach((testCase) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { camelCase, pascalCase } from 'change-case';
/**
* determines whether property name is an intuitive reference to a domain object
*
* options
* - entire match
* - prefix match
* - suffix match
*
* for example:
* - `address: Address` => true
* - `homeAddress: Address` => true
Expand All @@ -25,7 +30,7 @@ export const isPropertyNameAReferenceExplicitly = ({
propertyName: string;
domainObjectName: string;
}): boolean => {
// remove the potential `uuid` or `uuids` suffix of the property name (used in implicit uuid references)
// remove the potential `uuid(s)` suffix of the property name (used in implicit references)
const propertyName = propertyNamePotentiallyWithIrrelevantSuffixes.replace(
/Uuids?$/,
'',
Expand All @@ -43,6 +48,12 @@ export const isPropertyNameAReferenceExplicitly = ({
).test(propertyName); // e.g., /Engineers?$/.test('leadEngineer');
if (namedAfterItAsASuffix) return true;

// check whether the property is named after it as a prefix
const namedAfterItAsAPrefix = new RegExp(
`^${camelCase(domainObjectName)}s?`,
).test(propertyName); // e.g., /^Engineers?/.test('engineerLead');
if (namedAfterItAsAPrefix) return true;

// otherwise, false
return false;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ describe('isPropertyNameAReferenceIntuitively', () => {
const cases: {
propertyName: string;
domainObjectName: string;
expectedResult: boolean;
expectedResult: false | { via: string };
}[] = [
{
propertyName: 'address',
domainObjectName: 'Address',
expectedResult: true,
expectedResult: { via: 'Address' },
},
{
propertyName: 'homeAddress',
domainObjectName: 'Address',
expectedResult: true,
expectedResult: { via: 'Address' },
},
{
propertyName: 'home',
Expand All @@ -24,32 +24,32 @@ describe('isPropertyNameAReferenceIntuitively', () => {
{
propertyName: 'engineer',
domainObjectName: 'Engineer',
expectedResult: true,
expectedResult: { via: 'Engineer' },
},
{
propertyName: 'engineers',
domainObjectName: 'Engineer',
expectedResult: true,
expectedResult: { via: 'Engineer' },
},
{
propertyName: 'engineerUuid',
domainObjectName: 'Engineer',
expectedResult: true,
expectedResult: { via: 'Engineer' },
},
{
propertyName: 'leadEngineerUuid',
domainObjectName: 'Engineer',
expectedResult: true,
expectedResult: { via: 'Engineer' },
},
{
propertyName: 'engineerUuids',
domainObjectName: 'Engineer',
expectedResult: true,
expectedResult: { via: 'Engineer' },
},
{
propertyName: 'assignedEngineerUuids',
domainObjectName: 'Engineer',
expectedResult: true,
expectedResult: { via: 'Engineer' },
},
{
propertyName: 'lead',
Expand All @@ -69,22 +69,24 @@ describe('isPropertyNameAReferenceIntuitively', () => {
{
propertyName: 'leadEngineerUuid',
domainObjectName: 'LeadEngineer',
expectedResult: true,
expectedResult: { via: 'LeadEngineer' },
},
{
propertyName: 'headEngineerUuid',
domainObjectName: 'LeadEngineer',
expectedResult: true,
expectedResult: { via: 'Engineer' },
},
{
propertyName: 'engineerUuid',
domainObjectName: 'LeadEngineer',
expectedResult: true,
expectedResult: { via: 'Engineer' },
},
];

cases.forEach((testCase) => {
it(`should return ${testCase.expectedResult} for ${testCase.propertyName}: ${testCase.domainObjectName}`, () => {
it(`should return ${JSON.stringify(testCase.expectedResult)} for ${
testCase.propertyName
}: ${testCase.domainObjectName}`, () => {
expect(
isPropertyNameAReferenceIntuitively({
propertyName: testCase.propertyName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { isPropertyNameAReferenceExplicitly } from './isPropertyNameAReferenceEx
/**
* determines whether property name is an intuitive reference to a domain object
*
* note
* - shares the qualifier-stripped dobj.name via which the reference became intuitive
*
* for example:
* - `address: Address` => true
* - `homeAddress: Address` => true
Expand All @@ -25,7 +28,7 @@ export const isPropertyNameAReferenceIntuitively = ({
}: {
propertyName: string;
domainObjectName: string;
}): boolean => {
}): false | { via: string } => {
let qualifiersToDrop = 0;
let iterationLimitExceeded = false;
while (!iterationLimitExceeded) {
Expand All @@ -46,7 +49,7 @@ export const isPropertyNameAReferenceIntuitively = ({
});

// if its an explicit reference now, then its an intuitive reference
if (isExplicitReferenceNow) return true;
if (isExplicitReferenceNow) return { via: domainObjectNameMinusQualifiers };

// otherwise, go another layer deeper and try again
qualifiersToDrop += 1;
Expand Down

0 comments on commit 351e305

Please sign in to comment.