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 3 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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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.

example

# logs
*.log

Expand All @@ -12,4 +15,4 @@ yarn.lock
.vscode
.idea
*.swp
*.swo
*.swo
9 changes: 5 additions & 4 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 @@ -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

} 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;
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);

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[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);

}

const signale = this._meta();
Expand Down