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

What about proper String, Date and Number formatting? #939

Closed
devlato opened this Issue Jun 22, 2017 · 5 comments

Comments

Projects
None yet
5 participants
@devlato

devlato commented Jun 22, 2017

Hello guys and ladies,

what do you think about adding some common String and Date formatting APIs finally? Currently if you need to format a date, you have to use third party library or write tons of code, and formatting of dates is required in almost any project. Why don't add some APIs similar to moment.js API to the standard library?

I.e.

const now = new Date();
const formatted = now.format('DD.MM.YYYY HH:mm:ss');
assert(formatted, '22.06.2017 15:48:18');

Or for strings:

const formatted = 'Hello, number ${value}'.format({
  value: 12,
});
assert(formatted, 'Hello, number 12');

Or for numbers:

const formatted = 12.format('%4${value}');
assert(formatted, '    12');

I mean, it's 2017, and there is still no formatting API in standard library

@IgnoredAmbience

This comment has been minimized.

Show comment
Hide comment
@IgnoredAmbience

IgnoredAmbience Jun 22, 2017

Member

Template literals partially cover your second request. https://hacks.mozilla.org/2015/05/es6-in-depth-template-strings-2/
I believe an updated Date API is in progress, here: https://github.com/maggiepint/proposal-temporal

Member

IgnoredAmbience commented Jun 22, 2017

Template literals partially cover your second request. https://hacks.mozilla.org/2015/05/es6-in-depth-template-strings-2/
I believe an updated Date API is in progress, here: https://github.com/maggiepint/proposal-temporal

@devlato

This comment has been minimized.

Show comment
Hide comment
@devlato

devlato Jun 22, 2017

@IgnoredAmbience yes, but I guess I cannot use a template string as a template? I mean, is it possible to store the template in a variable and interpolate on demand? I.e.:

// I'm using usual quotes there because otherwise the string will be interpolated immediately
const template = 'Hello, Mr ${lastName}';
// ...
// And somewhere there we use template variable from above to format the result.

I'm asking because all the languages typically have this feature and because this approach is serializable. Typical use case – to read the template string from localization file or from any another source and use as a template.

devlato commented Jun 22, 2017

@IgnoredAmbience yes, but I guess I cannot use a template string as a template? I mean, is it possible to store the template in a variable and interpolate on demand? I.e.:

// I'm using usual quotes there because otherwise the string will be interpolated immediately
const template = 'Hello, Mr ${lastName}';
// ...
// And somewhere there we use template variable from above to format the result.

I'm asking because all the languages typically have this feature and because this approach is serializable. Typical use case – to read the template string from localization file or from any another source and use as a template.

@domenic

This comment has been minimized.

Show comment
Hide comment

@domenic domenic closed this Jun 22, 2017

@littledan

This comment has been minimized.

Show comment
Hide comment
@littledan

littledan Jun 23, 2017

Member

ECMA 402 is a good place to discuss this further as well; see tc39/ecma402#21 . Generally, for now, you can use moment.js for this sort of thing.

Member

littledan commented Jun 23, 2017

ECMA 402 is a good place to discuss this further as well; see tc39/ecma402#21 . Generally, for now, you can use moment.js for this sort of thing.

@jungshik

This comment has been minimized.

Show comment
Hide comment
@jungshik

jungshik Jun 24, 2017

Contributor

Ecma 402 certainly is. There's a better bug for this than tc39/ecma402#21. I can't find it now, but will add a reference to that later.

Contributor

jungshik commented Jun 24, 2017

Ecma 402 certainly is. There's a better bug for this than tc39/ecma402#21. I can't find it now, but will add a reference to that later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment