domain-objects
Version:
A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.
33 lines • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDomainObjectNameAfterDroppingSomeQualifiers = void 0;
const change_case_1 = require("change-case");
/**
* examples:
* - HomeAddress, 1 => Address
* - HomeAddress, 2 => null
* - PlaceExternalId, 1 => ExternalId
* - PlaceExternalId, 2 => null
* - TrainStationDockingGate, 2 => DockingGate
* - TrainStationDockingGate, 3 => Gate
* - TrainStationDockingGate, 4 => null
*/
const getDomainObjectNameAfterDroppingSomeQualifiers = ({ domainObjectName, qualifiersToDrop, }) => {
// sanity check
if (qualifiersToDrop < 0)
throw new Error('qualifiers to drop must be greater than 0');
// handle base case
if (qualifiersToDrop === 0)
return domainObjectName; // nothing to do in this case
// drop the qualifiers
const partsOfName = (0, change_case_1.noCase)(domainObjectName) // no case splits up string w/ spaces
.split(' ');
const nameWithoutRequestedQualifiers = (0, change_case_1.pascalCase)(partsOfName.slice(qualifiersToDrop).join(' '));
// check that its still a valid name
if (nameWithoutRequestedQualifiers.length < 3)
return null; // less than 3 char -> not a valid name
// return the name without qualifiers
return nameWithoutRequestedQualifiers;
};
exports.getDomainObjectNameAfterDroppingSomeQualifiers = getDomainObjectNameAfterDroppingSomeQualifiers;
//# sourceMappingURL=getDomainObjectNameAfterDroppingSomeQualifiers.js.map