numbro icon indicating copy to clipboard operation
numbro copied to clipboard

Return values object or individual parts of the final string

Open joh-klein opened this issue 6 years ago • 6 comments

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?

joh-klein avatar Aug 31 '18 23:08 joh-klein

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" }

BenjaminVanRyseghem avatar Aug 31 '18 23:08 BenjaminVanRyseghem

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.

joh-klein avatar Sep 01 '18 00:09 joh-klein

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

BenjaminVanRyseghem avatar Sep 01 '18 00:09 BenjaminVanRyseghem

OK, true. Let's change the whole thing, then :) (I am kidding, of course).

joh-klein avatar Sep 01 '18 00:09 joh-klein

Any updates on this feature, would be a fantastic thing to add

AlixH avatar Aug 06 '21 10:08 AlixH

If you are aware of any library providing the feature on top of i18n formatting would love to use it :D

AlixH avatar Aug 06 '21 12:08 AlixH