Skip to content

Commit

Permalink
Add getEmojiDataFromNative util function to get emoji data from native
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsenpeder committed Nov 29, 2018
1 parent 54f6893 commit 1c7a5d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { default as emojiIndex } from './utils/emoji-index/emoji-index'
export { default as NimbleEmojiIndex } from './utils/emoji-index/nimble-emoji-index'
export { default as store } from './utils/store'
export { default as frequently } from './utils/frequently'
export { getEmojiDataFromNative } from './utils'

export { default as Picker } from './components/picker/picker'
export { default as NimblePicker } from './components/picker/nimble-picker'
Expand Down
36 changes: 36 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { buildSearch } from './data'
import stringFromCodePoint from '../polyfills/stringFromCodePoint'
import { uncompress } from './data'

const _JSON = JSON

Expand Down Expand Up @@ -134,6 +135,40 @@ function getData(emoji, skin, set, data) {
return emojiData
}

function getEmojiDataFromNative(nativeString, set, data) {
if (data.compressed) {
uncompress(data);
}

const skinTones = ['', '🏻', '🏼', '🏽', '🏾', '🏿']

let skin
let baseNativeString = nativeString

skinTones.forEach((skinTone) => {
baseNativeString = baseNativeString.replace(skinTone, '')
if (nativeString.indexOf(skinTone) > 0) {
skin = skinTones.indexOf(skinTone) + 1
}
})

const emojiData = Object.values(data.emojis).find((emoji) => {
if (emoji.variations && emoji.variations.length) {
emoji.unifed = emoji.variations.shift()
}

return unifiedToNative(emoji.unified) === baseNativeString
})

if (!emojiData) {
return null
}

emojiData.id = emojiData.short_names[0]

return getSanitizedData(emojiData, skin, set, data)
}

function uniq(arr) {
return arr.reduce((acc, item) => {
if (acc.indexOf(item) === -1) {
Expand Down Expand Up @@ -191,6 +226,7 @@ function measureScrollbar() {

export {
getData,
getEmojiDataFromNative,
getSanitizedData,
uniq,
intersect,
Expand Down

0 comments on commit 1c7a5d6

Please sign in to comment.