@darlean/valueobjects
Version:
Library for DDD-like value objects that can be validated, serialized and represented in other programming languages as native objects with native naming.
38 lines (37 loc) • 1.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deriveTypeName = exports.ValidationError = void 0;
//type NativeValue = unknown;
class ValidationError extends Error {
}
exports.ValidationError = ValidationError;
//type NativePrimitive = undefined | boolean | number | string | Buffer | Date;
//type NativeStruct = { [key: string]: NativeType } | Map<string, NativeType> | object;
//type NativeArray = NativeType[];
//type NativeType = NativePrimitive | NativeStruct | NativeArray | ICanonical | Value;
function deriveTypeName(name) {
// TODO: Optimize
let result = '';
for (const char of name) {
if (char >= 'A' && char <= 'Z') {
if (result != '') {
result += '-';
}
result += char.toLowerCase();
}
else if (char === '_') {
result += '-';
}
else if (char >= '0' && char <= '9') {
result += char;
}
else if (char >= 'a' && char <= 'z') {
result += char;
}
else {
throw new Error(`Invalid character "${char}" in name: ${name}`);
}
}
return result;
}
exports.deriveTypeName = deriveTypeName;