domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
47 lines • 2.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPropertyNameAReferenceExplicitly = void 0;
const change_case_1 = require("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
* - `home: Address` => false 🚫
* - `engineerUuid: Engineer` => true
* - `leadEngineerUuid: Engineer` => true
* - `engineerUuids: Engineer` => true
* - `assignedEngineerUuids: Engineer` => true
* - `lead: Engineer` => false 🚫
* - `leadUuid: Engineer` => false 🚫
* - `assignedUuids: Engineer` => false 🚫
* - `leadEngineerUuid: LeadEngineer` => true
* - `headEngineerUuid: LeadEngineer` => false 🚫 // close, but not fully qualified -> not explicit
* - `engineerUuid: LeadEngineer` => false 🚫 // close, but not fully qualified -> not explicit
*/
const isPropertyNameAReferenceExplicitly = ({ propertyName: propertyNamePotentiallyWithIrrelevantSuffixes, domainObjectName, }) => {
// remove the potential `uuid(s)` suffix of the property name (used in implicit references)
const propertyName = propertyNamePotentiallyWithIrrelevantSuffixes.replace(/Uuids?$/, '');
// check whether the property is exactly named after it
const namedAfterItExactly = new RegExp(`${(0, change_case_1.camelCase)(domainObjectName)}s?$`).test(propertyName); // e.g., /leadEngineers?$/.test('leadEngineer');
if (namedAfterItExactly)
return true;
// check whether the property is named after it as a suffix
const namedAfterItAsASuffix = new RegExp(`${(0, change_case_1.pascalCase)(domainObjectName)}s?$`).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(`^${(0, change_case_1.camelCase)(domainObjectName)}s?`).test(propertyName); // e.g., /^Engineers?/.test('engineerLead');
if (namedAfterItAsAPrefix)
return true;
// otherwise, false
return false;
};
exports.isPropertyNameAReferenceExplicitly = isPropertyNameAReferenceExplicitly;
//# sourceMappingURL=isPropertyNameAReferenceExplicitly.js.map