rossmacarthur / emojis

✨ Lookup emoji in O(1) time, access metadata and GitHub shortcodes, iterate over all emoji
Apache License 2.0
55 stars 7 forks source link

Publish emoji fields #3

Closed 19wintersp closed 2 years ago

19wintersp commented 2 years ago

Hello, and thank you for this crate; I've found it very useful. I would find it rather helpful to have some of the fields in the Emoji struct to be public, such as the variations and skin tones, either by making the fields themselves public or adding getter methods for them. Sorry if I've missed an obvious way of doing this, or if there's a reason why these fields are private.

Thanks for your time

rossmacarthur commented 2 years ago

@19wintersp I'm interested in catering for this. Do have any ideas for how the API could look? I started working on exposing the variations and skin tones in a nice way but I wasn't sure exactly on the best API since I didn't have a use case. I was thinking of adding a function on the emoji that could look up the variation by skin tone and also add a way to iterate through skin tones.

Ideas:

Skin tone lookup

let hand = emojis::lookup("👋")?;
let hand_medium = hand.with_skin_tone(SkinTone::Medium)?;
assert_eq!(hand_medium, "👋🏽")

Iterating over skin tones

let hand = emojis::lookup("👋")?;
let hands: Vec<_> = hand.iter_skin_tones().collect();
// hands would contain all the emojis 👋 👋🏻 👋🏼 👋🏽 👋🏾 👋🏿

I was even thinking of adding a with_hair fn

let emoji = emojis::lookup("man")?
  .with_skin_tone(SkinTone::MediumDark)?
  .with_hair(Hair::Red)?;
assert_eq!(emoji, "👨🏾‍🦰")
19wintersp commented 2 years ago

My current use case is just a basic emoji picker, where I'd like for the user to be able to select an emoji and see the other skin tone variations as well. Having that sort of builder-method-style API would be a rather interesting concept, but just iterating over them would also work fine in the short term.

19wintersp commented 2 years ago

I'm not too familiar with how Unicode works, but I believe there are also certain sequences or modifiers which can be used with emoji? Having an API to create those would be great, though don't feel obligated to do this :)

rossmacarthur commented 2 years ago

What do you think of the following API 🤔?

https://github.com/rossmacarthur/emojis/blob/1149f2cb52ee1c37cb6a2749d331c5857ebfc161/src/lib.rs#L112-L157

Other ideas:

a) Add SkinTone::Default and return that from the skin_tone function as the skin tone for emojis where skin tone is applicable . b) Add SkinTone::Default and return that as the default instead of an Option from the skin_tone function, even for emojis that don't have other skin tones. c) Don't include the default skin tone emoji when iterating over the skin tones. d) Don't return Option from skin_tones function and always return an iterator which only has a single element for emojis without skin tones.

19wintersp commented 2 years ago

This API looks good. Having it return SkinTone::Default for the default yellow emoji where they can have other skin tones but None for those which don't would make sense, and possibly also having a with_skin_tone method or similar? For the various variations this API would also work well, but for now this is sufficient for what I need. Thanks :D

rossmacarthur commented 2 years ago

Changes are released https://docs.rs/emojis/0.2 (with some other ones as well)

19wintersp commented 2 years ago

Sorry, I've just noticed that the iter function seems to exclude any emoji with a skin tone, which includes those with default skin tones like :surfer:. Is this intentional?

rossmacarthur commented 2 years ago

Oh woops that wasn't intentional, I meant it to include the default skin tone ones. Let me fix

rossmacarthur commented 2 years ago

This is done, also I opened #6, there are some outstanding questions with regards to emojis with multiple skin tones. Feel free to put any ideas there.