@cowwoc/requirements
Version:
A fluent API for enforcing design contracts with automatic message generation.
64 lines • 1.85 kB
JavaScript
import { Type, StringMappers, internalValueToString } from "./internal.mjs";
/**
* Returns the String representation of
*/
class MutableStringMappers {
typeToMapper;
/**
* Creates a new instance.
*
* @param typeToMapper - a mapping from the name of a type to the string representation of its values
*/
constructor(typeToMapper) {
this.typeToMapper = new Map(typeToMapper);
}
/**
* Returns a mutable copy of the StringMappers.
*
* @param mappers - a `StringMappers` object
* @returns a mutable copy of the StringMappers
*/
static from(mappers) {
return new MutableStringMappers(mappers.typeToMapper);
}
/**
* Returns an immutable copy of the mapper configuration.
*
* @returns an immutable copy of the mapper configuration
*/
toImmutable() {
return new StringMappers(this.typeToMapper);
}
/**
* Sets the function that maps a value of the given type to a string. This method is useful for customizing
* the formatting of validation failure messages.
*
* @param type - a type
* @param mapper - a function that returns the String representation of the type's instances
* @returns this
*/
put(type, mapper) {
this.typeToMapper.set(type, mapper);
return this;
}
/**
* Removes a mapper for a type.
*
* @param type - the type
* @returns this
*/
remove(type) {
this.typeToMapper.delete(type);
return this;
}
/**
* Returns the string representation of this instance
*
* @returns the string representation of this instance
*/
toString() {
return internalValueToString(this.typeToMapper);
}
}
export { MutableStringMappers };
//# sourceMappingURL=MutableStringMappers.mjs.map