Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for string interpolation. Fixes #20 #23

Merged
merged 17 commits into from
Jun 9, 2018

Conversation

rjoydip-zz
Copy link
Contributor

@rjoydip-zz rjoydip-zz commented May 27, 2018

This PR provides a mechanism for support printf-like formatting. Strings have been formatted with util.format().

Fixes #20 , #24

@klaudiosinani klaudiosinani added the enhancement New feature or request label May 27, 2018
@klaudiosinani klaudiosinani self-requested a review May 27, 2018 13:47
Copy link
Owner

@klaudiosinani klaudiosinani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Only a few changes are needed to get this merged : )

@nick-keller
Copy link

Awesome PR!

@markusberg-spt
Copy link

Awesome feature! Is this going to be merged? @rjoydip @klauscfhq

@klaudiosinani
Copy link
Owner

@novddd A couple of tweaks and it will be good to go 🚀

@klaudiosinani klaudiosinani changed the title feat(): support printf-like formatting Added support for string interpolation. Fixes #20 Jun 6, 2018
signale.js Outdated
additional = Object.assign({}, {suffix, prefix});
}
} else {
msg = args.join(' ');
msg = util.format(args[0], ...args.slice(1, args.length));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To simplify it even more, the args argument can be passed directly to the util.format() method using the rest parameter syntax:

msg = util.format(...args);

signale.js Outdated
const [{prefix, message, suffix}] = args;
msg = message;
const [{prefix, suffix}] = args;
msg = util.format(util.inspect(...args));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same can be done here:

msg = util.format(...message);

signale.js Outdated
} else {
const [{prefix, message, suffix}] = args;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here message is needed for destructuring args

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed message because now it became death code.
Here is the senerior which i am testing and getting { prefix: undefined, message: undefined, suffix: undefined }

signale.success({a: 2});
signale.success([1, 2]);

Can you give some use cases so tat i can check that.

signale.js Outdated
@@ -131,14 +132,14 @@ class Signale {

if (args.length === 1 && typeof (args[0]) === 'object' && args[0] !== null) {
if (args[0] instanceof Error) {
[msg] = args;
[msg] = args; // See this github issue comment (https://github.com/nodejs/node/issues/9406#issuecomment-257724625)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error instances can receive only a single string as argument on initialization, thus string interpolation is expected to not be applicable here, which make the comment redundant

.gitignore Outdated
@@ -2,6 +2,9 @@
node_modules
yarn.lock

# folder
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should be removed since it is not part of the default developing environment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my fault. I am removing this.

@rjoydip-zz
Copy link
Contributor Author

Soon I will do requested changes.

signale.js Outdated
@@ -161,12 +162,12 @@ class Signale {
if (args[0] instanceof Error) {
[msg] = args;
} else {
const [{prefix, message, suffix}] = args;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice! The only thing left is to bring back message for destructuring args :

const [{prefix, message, suffix}] = args;

and directly apply interpolation on it:

msg = util.format(...message);

Thank you a lot for taking the time to help out : )

Copy link
Contributor Author

@rjoydip-zz rjoydip-zz Jun 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that I have to do. I am getting {prefix, message, suffix} as undefined all the times

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it is only applicable when using the prefix and suffix attributes, for example:

const signale = require('signale');

signale.complete({prefix: '[task]', message: ['Fix issue #%s', 59], suffix: '(@santa)'});
//=> [task] ☒  complete  Fix issue #59 (@santa)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now I have to check undefined case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. message for destructuring args

signale.js Outdated
@@ -163,7 +163,7 @@ class Signale {
[msg] = args;
} else {
let [{prefix, message, suffix}] = args;
if(message === undefined) {
if (message === undefined) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! It is possible to simplify it even more to a one-liner, instead of using an if statement:

msg = message ? util.format(...message) : '';

This way, in the rare case where no message is given by the user, we can pass just an empty string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am doing like this >> msg = message ? util.format(...message) : util.format(...args);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, if message is undefined, which means that we have no message to log, the args object will at best only hold the suffix and prefix attributes, thus they will be printed twice, as they are handled independently on lines 175 & 212, for example:

  • Running with: msg = message ? util.format(...message) : util.format(...args);
const signale = require('signale');

signale.complete({prefix: '[task]', suffix: '(@santa)'}); // `message` is undefined
// => [task] ☒  complete  { prefix: '[task]', suffix: '(@santa)' } (@santa)

Above, the printed object { prefix: '[task]', suffix: '(@santa)' } is unexpected, since we left message undefined, which means nothing should have been printed in its place

  • Running with: msg = message ? util.format(...message) : '';
const signale = require('signale');

signale.complete({prefix: '[task]', suffix: '(@santa)'}); // `message` is undefined
// => [task] ☒  complete   (@santa)

In this case, message was not defined and as it was expected nothing was printed in its the place

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I edited the markdown of the above comment, so the example can look better and be more clear. Please let me know what you think about this approach : )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If i do like msg = message ? util.format(...message) : ''; then it will not print message for below example:

signale.success({a: 2});
signale.success([1, 2]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow I managed previous issue occurrence. By like this

// for fix xo warning
_prefixSufixNestedTernary(prefix, suffix, args) {
    return prefix || suffix ? '' : util.format(args);
}

msg = message ? util.format(...message) : this._prefixSufixNestedTernary(prefix, suffix, ...args);

Is this fine?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you are totally right, that is an issue also described in #24. At this moment signale loggers can only accept one or more comma-delimited strings or an object containing the suffix, prefix and message attributes, which means that currently the only way to print any other object is to use template literals, which on their own are not such a perfect replacement either:

const object = {a: 2};
const array = [1, 2];

signale.success(`${object}`);
//=> ✔  success   [object Object]
signale.success(`${array}`);
//=> ✔  success   1,2

But with string interpolation it will be possible to cover these cases effectively:

signale.success('%O', {a: 2});
//=> ✔  success   { a: 2 }
signale.success('%O', [1, 2]);
//=> ✔  success   [ 1, 2 ]

Thus it is acceptable to use msg = message ? util.format(...message) : ''; in order to deliver string interpolation and also to preserve the fuctionality of signale when using the {suffix, prefix, message} object where the message attribute can be undefined. The idial case is exactly what you described, and it is something will be added as soon as possible, as a feature on its own, since it will be possible to log any object directly, in the same fashion that console.log does : )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope except signale.success(${object}); case other will works fine.

signale.js Outdated
@@ -162,7 +162,7 @@ class Signale {
if (args[0] instanceof Error) {
[msg] = args;
} else {
let [{prefix, message, suffix}] = args;
const [{prefix, message, suffix}] = args;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original comment: #23 (comment)

signale.js Outdated
@@ -162,7 +162,7 @@ class Signale {
if (args[0] instanceof Error) {
[msg] = args;
} else {
let [{prefix, message, suffix}] = args;
const [{prefix, message, suffix}] = args;
msg = message ? util.format(...message) : util.format(...args);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original comment: #23 (comment)
(just in case the initial notification didn't make it through)

signale.js Outdated
@@ -155,6 +155,10 @@ class Signale {
return meta;
}

_prefixSufixNestedTernary(prefix, suffix, args) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! A couple of minor tweaks and it is ready to be merged! 🎉

  _hasAdditional({suffix, prefix}, args) { // Rename to something simpler, for example: _hasAdditional() 
    // Use destructuring directly on function definition
    return (suffix || prefix) ? '' : util.format(...args); // Add simple parentheses around the condition to make it clearer
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better than previous

signale.js Outdated
@@ -163,7 +167,7 @@ class Signale {
[msg] = args;
} else {
const [{prefix, message, suffix}] = args;
msg = message ? util.format(...message) : util.format(...args);
msg = message ? util.format(...message) : this._prefixSufixNestedTernary(prefix, suffix, ...args);
additional = Object.assign({}, {suffix, prefix});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor reordering of the assignments:

const [{prefix, message, suffix}] = args;
additional = Object.assign({}, {suffix, prefix});
msg = message ? util.format(...message) : this._hasAdditional(additional, args);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better than my previous method

@klaudiosinani klaudiosinani merged commit 3d1b9c7 into klaudiosinani:master Jun 9, 2018
@klaudiosinani
Copy link
Owner

Awesome work! Huge thank you for all your time and effort! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants