5
I’m creating a module of Node.js
and wanted to provide usage settings. Let’s say I wanted to provide a prefix for the console.log
just as an example:
let opcoes = {};
function imprimir(texto) {
console.log(opcoes.prefixo, texto);
}
module.exports = ({
prefixo = 'padrao'
} = {}) => {
opcoes = {
prefixo
};
return { imprimir };
};
In case the above module call would look something like this:
const { imprimir } = require('modulo')({ prefixo: 'prefixo' });
imprimir('teste');
The above output will be the following:
prefix test
But I wish that this setting could be done only once in use by overriding the default. For example, make the above call on modulo1
of my system and the call below on modulo2
:
const { imprimir } = require('modulo')();
imprimir('teste');
I would like the result to be:
prefix test
And not how (Which is what happens today):
Undefined test
And neither:
test pattern
Which is what would happen with minor modifications.
Contextualizing:
I have a module that performs the call to a service via HTTP
, but sometimes the service version is updated and I want to allow those who are consuming the module to perform this update without having to wait for me to update the link
version. But this setting is only required once and not on every service call.
Observing: I’m using the Airbnb Style Guide and would like to continue in this pattern.