4 ms·
For web this is done with 1 line of Javascript and a div with rounded borders, really const initials = name.split(' ').map((n) => n.substr(0,1)).slice(0,2).
by dmitriid 9y ago
For web this is done with 1 line of Javascript and a div with rounded borders, really
const initials = name.split(' ').map((n) => n.substr(0,1)).slice(0,2).join('')
<div style="border-radius: 9999px; background-color: navy">
<initials here>
</div>
Adjust width, height, font-size to your liking
- lasserafn 9y agoI'd absolutely recommend anyone, that has the ability, to generate them using HTML and CSS, it's way faster. You could even use the core package the generate the initials (as it's a little more than just each letter of each name)
- padolsey 9y agoYep! Or even: const initials = name.match(/\b\w/g).join('')
- tyingq 9y agoJavaScript's \b in a regex doesn't like unicode. But, yes, this does seem an odd thing to call out to a remote api for. This would need some work to match the features of what was posted, but not much. https://jsfiddle.net/g6nrs5cq/ https://jsfiddle.net/g6nrs5cq/
- lgessler 9y agoNote that the "take the first codepoint" approach doesn't work for all languages. Take Hindi for example. This is a name: प्रेरणा 'preraṇa' The convention for initials in Hindi is such that this would be the "initial": प्रे॰ (< प + ् + र + े + ॰) 'pre' which is the first four codepoints combined (because they are in some sense considered a single character) plus another codepoint! If for some reason you needed to respect locale-specific initialism conventions for a lot of different locales, it might be worth introducing an abstraction for determining the initials of a name.
- lasserafn 9y agoThanks for letting me know! I will stuff different languages over the coming months to better manage such things :) didn’t know that
- dmitriid 9y agohttp://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-b... :)
- wavelen 9y agohow about "border-radius: 50%"?
- dmitriid 9y agoYeah, it also works. 9999px stuck in my head for some reason.