-
Notifications
You must be signed in to change notification settings - Fork 1
NamingConventions
NamingConventions is a helper class used by Applications to pick right business methods for serving incoming requests.
In essence:
- a
{type: 'user_accounts', id: 1}request would be handled with aUserAccounts.getItem ()call; -
{type: 'user_accounts', id: 1, action: 'delete'}better routed toUserAccounts.doDelete (); - and so on.
The actual rules are totally configurable, but, in any case, module and method names are computed based on request parameters.
Some magical parameters (like type, action etc.) are expected to represent parts of identifiers in snake_case or kebab-case. Those values are combined (normally, concatenated with some constants), then checked for validity (length, character set) and, finally, translated into camelCase or PascalCase.
The argument is the bag of options, one configuration object by entity name. In this example, the getDefaultOptions () result is shown, commented out. Uncommenting and altering any lines will lead to local modifications, as the whole configuration is merged recursively.
const {NamingConventions} = require ('doix')
const namingConventions = new NamingConventions (
//{
// maxLength: 64,
// sep: '_',
// types: {
// module: {
// name: request => request.type ?? null,
// case: 'pascal',
// },
// method: {
// name: ({action, part, id}) =>
// action ? 'do_' + action :
// 'get_' + (
// part ?? (
// id ? 'item' : 'list'
// )
// ),
// case: 'camel',
// },
// },
//}
)| Name | Type | Default | Description |
|---|---|---|---|
maxLength |
Number |
64 |
The threshold value for the generated name's length. |
sep |
String |
'_' |
The input values separator: may be set to '-' for kebab-case
|
types |
Object |
(see example) | Name generation options, by entity type name |
| Name | Type | Description |
|---|---|---|
name |
Object => String |
The function returning the entity name calculated based on the object (request parameters, Job's .request field), in snake/kebab case |
case |
String |
The case the generated name is to be translated into |
| Name | Description |
|---|---|
'camel' |
The name is translated into theCamelCase
|
'pascal' |
The name is translated into ThePascalCase
|
'none' |
The name is left intact |
Returns the default bag of options, as shown in the constructor example.
For a given string s, returns the zero based position of the first char that is:
- not a lowercase Latin letter ('a'..'z');
- nor a digit ('0'..'9') (or a digit as the first character);
- nor the
sepoption. If all characters are valid, returns-1.
Generates the name of the required type ('module' or 'method') based on the request value, according to the configuration set during the constructor call.
request should be the request parameters (Job's .request field).
If the name generated is not a string nor null or maxLength (before applying toCamel) is exceeded, throws an error.
If getIndexOfInvalidChar returns a non-negative value, throws an error.
Translates the given name from the snake/kebab case into either theCamelCase (with falsy ucFirst) or ThePascalCase (otherwise).
The name must be a string or null, in the latter case the result is null.
The string must contain only lowercase Latin letters (from 'a' to 'z') and underscore characters ('_'), but, for the sake of productivity, this is not verified.
All (sequences of) underscore characters ('_') are stripped off.
Each character following an underscore (and the first one for a truthy ucFirst) is transposed to the upper case.