Numeral-js icon indicating copy to clipboard operation
Numeral-js copied to clipboard

Support for static text in formatting string

Open Eric24 opened this issue 7 years ago • 10 comments

It would be very useful (and actually addresses a handful of other issues, like #281) to allow static text in the formatting string, like moment.js does by escaping the test with [ ]. This would allow unit suffixes, non-standard prefixes, etc. I realize numeral.js already makes use of [ ] for certain formatting, so I'm not sure of the best way to add this while maintaining backward compatibility. Use of [ ] would be great (in that it keeps with moment.js syntax), but that might also break existing uses of [ ]. Of course a different escape character is the easy choice, but I think there is value in maintaining the similarity to moment.js.

Eric24 avatar Nov 30 '16 15:11 Eric24

I have seen this requested a couple of times, so I will add it to the roadmap. I'll probably use { }.

adamwdraper avatar Dec 01 '16 03:12 adamwdraper

Glad to hear it. But since there already are configurable settings in numeral.js, why not make the token delimiters a configurable option (along with the existing [ ])? That way, static text is { } by default and the existing [ ] stays in place, but can be redefined if needed.

By the way, I'm not entirely sure what to call the current use of [ ]. How would you define it?

Eric24 avatar Dec 01 '16 03:12 Eric24

Hey @adamwdraper,

I know this story is probably much more complicated than I seems, but do you have any update on it ? Is is this part of the roadmap for now ?

maxired avatar Jun 09 '17 09:06 maxired

is there any update on this? it's a much needed feature for me. thanks

obedm503 avatar Feb 16 '18 17:02 obedm503

Hello, any updates ? thanks

aMahdaoui avatar Oct 03 '19 09:10 aMahdaoui

after 3 years still no updates for this issue?

nimaansary avatar Nov 04 '19 09:11 nimaansary

After 4 years and I still can't prefix my kibana visualizations with R$ because of this.

pbassut avatar Jun 14 '20 16:06 pbassut

I have temperature sensor data in kibana that I would love to suffix with °F. This change would make that possible.

andrewthad avatar Apr 08 '21 20:04 andrewthad

+1

I want to format 90539600 as 90.54 Million AUD in Kibana. Simple requirement!

PS: I know about adding a pattern for abbreviation.

paras-lehana avatar Jul 16 '22 09:07 paras-lehana

+1

A "simple" approach like this custom format should theoretically work

// handle prefix or suffix enclosed in {}
numeral.register("format", "suffixprefix", {
  regexps: {
    format: /^(({.+})?([^{}]+)({.+})|({.+})([^{}]+)({.+})?|({.+})([^{}]+)({.+}))$/
  },
  format: function (value, format, roundingFunction) {
    // extract prefixes, suffixes and inner format
    let match = /^({.+})?([^{}]+)({.+})?$/.exec(format);
    let prefix = (match[1] ?? "").replace(/{|}/g,'');
    let innerFormat = match[2] ?? "";
    let suffix = (match[3] ?? "").replace(/{|}/g,'');
    output = numeral(value).format(innerFormat);

    return prefix + output + suffix;
  }
});

(see also this codepen: https://codepen.io/digemall/pen/jOpzbXv)

But the problem is that it cannot work when suffix or prefix contain another format specifier (e.g. the ordinal "o"): in that case that format will be used instead of the custom one.
The problem is that there isn't a way to "prioritize" formats in case of conflicts, but when more than one format regex matches, simply the first one is taken from the dictionary.

As a suggestion you could define a 4th parameter "priority", then the loop over the dictionary should be done sorting by priority.

digEmAll avatar Jan 24 '23 08:01 digEmAll