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
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ yarn.lock
.vscode
.idea
*.swp
*.swo
*.swo
10 changes: 7 additions & 3 deletions signale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const util = require('util');
const path = require('path');
const chalk = require('chalk');
const figures = require('figures');
Expand Down Expand Up @@ -161,12 +162,15 @@ 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.

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.

Copy link
Owner

@klaudiosinani klaudiosinani Jun 9, 2018

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

@klaudiosinani klaudiosinani Jun 9, 2018

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

msg = message;
let [{prefix, message, suffix}] = args;
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

@klaudiosinani klaudiosinani Jun 9, 2018

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

@klaudiosinani klaudiosinani Jun 9, 2018

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.

message = args;
}
msg = util.format(util.inspect(...message));
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

}
} else {
msg = args.join(' ');
msg = util.format(...args);
}

const signale = this._meta();
Expand Down