Numeral-js
Numeral-js copied to clipboard
Support for static text in formatting string
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.
I have seen this requested a couple of times, so I will add it to the roadmap. I'll probably use { }
.
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?
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 ?
is there any update on this? it's a much needed feature for me. thanks
Hello, any updates ? thanks
after 3 years still no updates for this issue?
After 4 years and I still can't prefix my kibana visualizations with R$ because of this.
I have temperature sensor data in kibana that I would love to suffix with °F
. This change would make that possible.
+1
I want to format 90539600
as 90.54 Million AUD
in Kibana. Simple requirement!
PS: I know about adding a
pattern for abbreviation.
+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.