numbro
numbro copied to clipboard
Return values object or individual parts of the final string
I am looking for a way to style parts of a resulting value individually. For example:
numbro(52).format({output: "ordinal"}).toObject() //{value: 52, ordinal: 'nd', formattedValue: '52nd'}
numbro(52).format({output: "ordinal"}).value //52
numbro(52).format({output: "ordinal"}).ordinal //nd
Is there already something like this and I just haven't seen it?
no, but it shouldn't be too hard to add.
I would go for an option to format
named token
which when is true, output as tokens instead of string.
numbro(52).format({output: "ordinal"}) // => "52nd"
numbro(52).format({output: "ordinal", token: true}) // => { value: 52, ordinal: "nd" }
I am happy to hear, that you understand the idea!
To be honest, I don't really understand, why you would use token
, though. I quite like the way, moment.js does it with ".toObject()". It is, at least for me, very easy to follow and understand.
in moment, you have to call toString
or toObject
.
in numbro, format
returns a string, and it's not possible in JS to attach a function to a string.
Try
let a = "123";
a.toObject = () => 21;
a.toObject //=> undefined
OK, true. Let's change the whole thing, then :) (I am kidding, of course).
Any updates on this feature, would be a fantastic thing to add
If you are aware of any library providing the feature on top of i18n formatting would love to use it :D