From 54f6893c981e54924d1bb531a71833b4ee97272a Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Thu, 29 Nov 2018 15:50:03 +0000 Subject: [PATCH 01/19] Add .editorconfig file with indent_size = 2 --- .editorconfig | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b429316 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,2 @@ +[*] +indent_size = 2 From 1c7a5d6ec6b0591408426b1413fc4f7092063c27 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Thu, 29 Nov 2018 16:08:30 +0000 Subject: [PATCH 02/19] Add `getEmojiDataFromNative` util function to get emoji data from native --- src/index.js | 1 + src/utils/index.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/index.js b/src/index.js index 7df17f8..cb1c3e1 100644 --- a/src/index.js +++ b/src/index.js @@ -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' diff --git a/src/utils/index.js b/src/utils/index.js index 241b18b..3bbb118 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,5 +1,6 @@ import { buildSearch } from './data' import stringFromCodePoint from '../polyfills/stringFromCodePoint' +import { uncompress } from './data' const _JSON = JSON @@ -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) { @@ -191,6 +226,7 @@ function measureScrollbar() { export { getData, + getEmojiDataFromNative, getSanitizedData, uniq, intersect, From f90defbed702381a808cf282683acb10d43e4492 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Thu, 29 Nov 2018 16:09:28 +0000 Subject: [PATCH 03/19] Add `Get emoji data from Native` story --- stories/index.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/stories/index.js b/stories/index.js index 3cb54d4..38c144e 100644 --- a/stories/index.js +++ b/stories/index.js @@ -11,7 +11,8 @@ import { color, } from '@storybook/addon-knobs' -import { Picker, Emoji, emojiIndex } from '../dist' +import { Picker, Emoji, emojiIndex, getEmojiDataFromNative } from '../dist' +import data from '../data/all.json' import '../css/emoji-mart.css' const SETS = ['apple', 'google', 'twitter', 'emojione', 'messenger', 'facebook'] @@ -214,3 +215,35 @@ storiesOf('Headless Search', module) ) }) + +storiesOf('Get emoji data from Native', module) + .addDecorator(withKnobs) + .add('Default', () => { + let emojiData = getEmojiDataFromNative( + text('Unicode', 'πŸ€ΎπŸ½β€β™‚οΈ'), + select('Emoji pack', SETS, SETS[0]), + data + ) + if (!emojiData) { + return ( +
+ Couldn't find emojiData from native string... +
+ ) + } + + return ( +
+ + +
+          emojiData: {JSON.stringify(emojiData, null, 2)}
+        
+
+ ) + }) From fd4056025a2b2747003df6ce883e1c035fc89308 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Thu, 29 Nov 2018 16:09:52 +0000 Subject: [PATCH 04/19] Add section to readme for `getEmojiDataFromNative` util function --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 5207216..95f1603 100644 --- a/README.md +++ b/README.md @@ -277,6 +277,23 @@ emojiIndex.search('christmas').map((o) => o.native) // => [πŸŽ„, πŸŽ…πŸΌ, πŸ””, 🎁, ⛄️, ❄️] ``` +## Get emoji data from Native +You can get emoji data from native emoji unicode using the `getEmojiDataFromNative` util function. + +```js +import { getEmojiDataFromNative, Emoji } from 'emoji-mart' +import data from 'emoji-mart/data/all.json' + +let emojiData = getEmojiDataFromNative('πŸ€ΎπŸ½β€β™‚οΈ', 'apple', data) + + +``` + ### With custom data ```js import data from 'emoji-mart/datasets/messenger' From fe17925fac5a23ac8c8640e2cf8f6d1abce70cfa Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Thu, 29 Nov 2018 16:19:55 +0000 Subject: [PATCH 05/19] Fix story/readme skin prop typo --- README.md | 2 +- stories/index.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 95f1603..321f736 100644 --- a/README.md +++ b/README.md @@ -287,7 +287,7 @@ import data from 'emoji-mart/data/all.json' let emojiData = getEmojiDataFromNative('πŸ€ΎπŸ½β€β™‚οΈ', 'apple', data) { let emojiData = getEmojiDataFromNative( - text('Unicode', 'πŸ€ΎπŸ½β€β™‚οΈ'), + text('Unicode', 'πŸŠπŸ½β€β™€οΈ'), select('Emoji pack', SETS, SETS[0]), data ) if (!emojiData) { return (
- Couldn't find emojiData from native string... + Couldn`t find any emoji data...
) } @@ -235,7 +235,7 @@ storiesOf('Get emoji data from Native', module) return (
Date: Thu, 29 Nov 2018 16:27:44 +0000 Subject: [PATCH 06/19] Use same emoji in readme as in story --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 321f736..2ecf386 100644 --- a/README.md +++ b/README.md @@ -284,7 +284,7 @@ You can get emoji data from native emoji unicode using the `getEmojiDataFromNat import { getEmojiDataFromNative, Emoji } from 'emoji-mart' import data from 'emoji-mart/data/all.json' -let emojiData = getEmojiDataFromNative('πŸ€ΎπŸ½β€β™‚οΈ', 'apple', data) +let emojiData = getEmojiDataFromNative('πŸŠπŸ½β€β™€οΈ', 'apple', data) Date: Thu, 29 Nov 2018 16:35:57 +0000 Subject: [PATCH 07/19] Add example of `emojiData` object --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 2ecf386..67344bc 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,19 @@ let emojiData = getEmojiDataFromNative('πŸŠπŸ½β€β™€οΈ', 'apple', data) /> ``` +#### Example of `emojiData` object: +```js +emojiData: { + "id": "woman-swimming", + "name": "Woman Swimming", + "colons": ":woman-swimming::skin-tone-4:", + "emoticons": [], + "unified": "1f3ca-1f3fd-200d-2640-fe0f", + "skin": 4, + "native": "πŸŠπŸ½β€β™€οΈ" +} +``` + ### With custom data ```js import data from 'emoji-mart/datasets/messenger' From 682d8d8be44628e8263791eaae3277e0d0365b35 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Thu, 29 Nov 2018 16:39:36 +0000 Subject: [PATCH 08/19] Remove whitespace --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 67344bc..8187087 100644 --- a/README.md +++ b/README.md @@ -278,7 +278,7 @@ emojiIndex.search('christmas').map((o) => o.native) ``` ## Get emoji data from Native -You can get emoji data from native emoji unicode using the `getEmojiDataFromNative` util function. +You can get emoji data from native emoji unicode using the `getEmojiDataFromNative` util function. ```js import { getEmojiDataFromNative, Emoji } from 'emoji-mart' From f760c554404f67147914b8b89aa27653a945b816 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Wed, 20 Mar 2019 11:25:48 +0000 Subject: [PATCH 09/19] Return skin emojis in search. Refactor getEmojiDataFromNative --- src/components/picker/nimble-picker.js | 2 + src/components/search.js | 2 +- src/utils/emoji-index/nimble-emoji-index.js | 27 +++++++-- src/utils/index.js | 61 ++++++++++++--------- 4 files changed, 60 insertions(+), 32 deletions(-) diff --git a/src/components/picker/nimble-picker.js b/src/components/picker/nimble-picker.js index 736e92f..5910e75 100644 --- a/src/components/picker/nimble-picker.js +++ b/src/components/picker/nimble-picker.js @@ -495,6 +495,7 @@ export default class NimblePicker extends React.PureComponent { skinEmoji, notFound, notFoundEmoji, + set, } = this.props, { skin } = this.state, width = perLine * (emojiSize + 12) + 12 + 2 + measureScrollbar() @@ -522,6 +523,7 @@ export default class NimblePicker extends React.PureComponent { ref={this.setSearchRef} onSearch={this.handleSearch} data={this.data} + set={set} i18n={this.i18n} emojisToShowFilter={emojisToShowFilter} include={include} diff --git a/src/components/search.js b/src/components/search.js index b8cbfc8..bfb05ef 100644 --- a/src/components/search.js +++ b/src/components/search.js @@ -17,7 +17,7 @@ export default class Search extends React.PureComponent { } this.data = props.data - this.emojiIndex = new NimbleEmojiIndex(this.data) + this.emojiIndex = new NimbleEmojiIndex(this.data, props.set) this.setRef = this.setRef.bind(this) this.clear = this.clear.bind(this) this.handleKeyUp = this.handleKeyUp.bind(this) diff --git a/src/utils/emoji-index/nimble-emoji-index.js b/src/utils/emoji-index/nimble-emoji-index.js index 8b1bdc3..f4a3b90 100644 --- a/src/utils/emoji-index/nimble-emoji-index.js +++ b/src/utils/emoji-index/nimble-emoji-index.js @@ -1,13 +1,15 @@ import { getData, getSanitizedData, intersect } from '..' import { uncompress } from '../data' +import store from '../store' export default class NimbleEmojiIndex { - constructor(data) { + constructor(data, set) { if (data.compressed) { uncompress(data) } this.data = data || {} + this.set = set || null this.originalPool = {} this.index = {} this.emojis = {} @@ -20,7 +22,7 @@ export default class NimbleEmojiIndex { buildIndex() { for (let emoji in this.data.emojis) { let emojiData = this.data.emojis[emoji], - { short_names, emoticons } = emojiData, + { short_names, emoticons, skin_variations } = emojiData, id = short_names[0] if (emoticons) { @@ -33,7 +35,16 @@ export default class NimbleEmojiIndex { }) } - this.emojis[id] = getSanitizedData(id, null, null, this.data) + // If skin variations include them + if (skin_variations) { + this.emojis[id] = {} + for (let skinTone = 1; skinTone <= 6; skinTone++) { + this.emojis[id][skinTone] = getSanitizedData({id: id, skin: skinTone}, skinTone, this.set, this.data) + } + } else { + this.emojis[id] = getSanitizedData(id, null, this.set, this.data) + } + this.originalPool[id] = emojiData } } @@ -70,6 +81,8 @@ export default class NimbleEmojiIndex { if (this.customEmojisList != custom) this.addCustomToPool(custom, this.originalPool) + const skinTone = store.get('skin') || 1; + maxResults || (maxResults = 75) include || (include = []) exclude || (exclude = []) @@ -79,7 +92,7 @@ export default class NimbleEmojiIndex { if (value.length) { if (value == '-' || value == '-1') { - return [this.emojis['-1']] + return [this.emojis['-1'][skinTone]] } var values = value.toLowerCase().split(/[\s|,|\-|_]+/), @@ -148,7 +161,11 @@ export default class NimbleEmojiIndex { let score = subIndex + 1 if (sub == id) score = 0 - aIndex.results.push(this.emojis[id]) + if (this.emojis[id] && this.emojis[id][skinTone]) { + aIndex.results.push(this.emojis[id][skinTone]) + } else { + aIndex.results.push(this.emojis[id]) + } aIndex.pool[id] = emoji scores[id] = score diff --git a/src/utils/index.js b/src/utils/index.js index 57b3d46..33e444a 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -136,37 +136,46 @@ function getData(emoji, skin, set, data) { } function getEmojiDataFromNative(nativeString, set, data) { - if (data.compressed) { - uncompress(data); + if (data.compressed) { + uncompress(data); + } + + const skinTones = ['', '🏻', '🏼', '🏽', '🏾', '🏿'] + const skinCodes = ['', '1F3FB', '1F3FC', '1F3FD', '1F3FE', '1F3FF'] + + let skin + let skinCode + let baseNativeString = nativeString + + skinTones.forEach((skinTone) => { + if (nativeString.indexOf(skinTone) > 0) { + const skinToneIndex = skinTones.indexOf(skinTone) + skin = skinToneIndex + 1 + skinCode = skinCodes[skinToneIndex] + } + }) + + const emojiData = Object.values(data.emojis).find((emoji) => { + emoji = JSON.parse(_JSON.stringify(emoji)) + + if (emoji.variations && emoji.variations.length) { + emoji.unified = emoji.variations.shift() } - 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 + if (skin && emoji.skin_variations && emoji.skin_variations[skinCode]) { + emoji.unified = emoji.skin_variations[skinCode].unified } - emojiData.id = emojiData.short_names[0] + return unifiedToNative(emoji.unified) === baseNativeString + }) - return getSanitizedData(emojiData, skin, set, data) + if (!emojiData) { + return null + } + + emojiData.id = emojiData.short_names[0] + + return getSanitizedData(emojiData, skin, set, data) } function uniq(arr) { From 51d59341c8bbfbaffa6e561c00f8f09abb4205a9 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Wed, 20 Mar 2019 11:32:26 +0000 Subject: [PATCH 10/19] Remove duplicate `set` --- src/components/picker/nimble-picker.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/picker/nimble-picker.js b/src/components/picker/nimble-picker.js index 5910e75..3ccc500 100644 --- a/src/components/picker/nimble-picker.js +++ b/src/components/picker/nimble-picker.js @@ -495,7 +495,6 @@ export default class NimblePicker extends React.PureComponent { skinEmoji, notFound, notFoundEmoji, - set, } = this.props, { skin } = this.state, width = perLine * (emojiSize + 12) + 12 + 2 + measureScrollbar() From 9afc613feee0050f0816b7ad3afce71b0da1d63a Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Sat, 23 Mar 2019 12:00:55 +0000 Subject: [PATCH 11/19] Split out .editorconfig addition into separate PR --- .editorconfig | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index b429316..0000000 --- a/.editorconfig +++ /dev/null @@ -1,2 +0,0 @@ -[*] -indent_size = 2 From ed241d1d42ce5bb9c1967946a81f06600f5ce9f2 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Sat, 23 Mar 2019 15:34:46 +0000 Subject: [PATCH 12/19] Revert skin tone emoji on search code --- src/components/picker/nimble-picker.js | 1 - src/components/search.js | 2 +- src/utils/emoji-index/nimble-emoji-index.js | 27 ++++----------------- 3 files changed, 6 insertions(+), 24 deletions(-) diff --git a/src/components/picker/nimble-picker.js b/src/components/picker/nimble-picker.js index 3ccc500..736e92f 100644 --- a/src/components/picker/nimble-picker.js +++ b/src/components/picker/nimble-picker.js @@ -522,7 +522,6 @@ export default class NimblePicker extends React.PureComponent { ref={this.setSearchRef} onSearch={this.handleSearch} data={this.data} - set={set} i18n={this.i18n} emojisToShowFilter={emojisToShowFilter} include={include} diff --git a/src/components/search.js b/src/components/search.js index bfb05ef..b8cbfc8 100644 --- a/src/components/search.js +++ b/src/components/search.js @@ -17,7 +17,7 @@ export default class Search extends React.PureComponent { } this.data = props.data - this.emojiIndex = new NimbleEmojiIndex(this.data, props.set) + this.emojiIndex = new NimbleEmojiIndex(this.data) this.setRef = this.setRef.bind(this) this.clear = this.clear.bind(this) this.handleKeyUp = this.handleKeyUp.bind(this) diff --git a/src/utils/emoji-index/nimble-emoji-index.js b/src/utils/emoji-index/nimble-emoji-index.js index f4a3b90..8b1bdc3 100644 --- a/src/utils/emoji-index/nimble-emoji-index.js +++ b/src/utils/emoji-index/nimble-emoji-index.js @@ -1,15 +1,13 @@ import { getData, getSanitizedData, intersect } from '..' import { uncompress } from '../data' -import store from '../store' export default class NimbleEmojiIndex { - constructor(data, set) { + constructor(data) { if (data.compressed) { uncompress(data) } this.data = data || {} - this.set = set || null this.originalPool = {} this.index = {} this.emojis = {} @@ -22,7 +20,7 @@ export default class NimbleEmojiIndex { buildIndex() { for (let emoji in this.data.emojis) { let emojiData = this.data.emojis[emoji], - { short_names, emoticons, skin_variations } = emojiData, + { short_names, emoticons } = emojiData, id = short_names[0] if (emoticons) { @@ -35,16 +33,7 @@ export default class NimbleEmojiIndex { }) } - // If skin variations include them - if (skin_variations) { - this.emojis[id] = {} - for (let skinTone = 1; skinTone <= 6; skinTone++) { - this.emojis[id][skinTone] = getSanitizedData({id: id, skin: skinTone}, skinTone, this.set, this.data) - } - } else { - this.emojis[id] = getSanitizedData(id, null, this.set, this.data) - } - + this.emojis[id] = getSanitizedData(id, null, null, this.data) this.originalPool[id] = emojiData } } @@ -81,8 +70,6 @@ export default class NimbleEmojiIndex { if (this.customEmojisList != custom) this.addCustomToPool(custom, this.originalPool) - const skinTone = store.get('skin') || 1; - maxResults || (maxResults = 75) include || (include = []) exclude || (exclude = []) @@ -92,7 +79,7 @@ export default class NimbleEmojiIndex { if (value.length) { if (value == '-' || value == '-1') { - return [this.emojis['-1'][skinTone]] + return [this.emojis['-1']] } var values = value.toLowerCase().split(/[\s|,|\-|_]+/), @@ -161,11 +148,7 @@ export default class NimbleEmojiIndex { let score = subIndex + 1 if (sub == id) score = 0 - if (this.emojis[id] && this.emojis[id][skinTone]) { - aIndex.results.push(this.emojis[id][skinTone]) - } else { - aIndex.results.push(this.emojis[id]) - } + aIndex.results.push(this.emojis[id]) aIndex.pool[id] = emoji scores[id] = score From 37340679ec994bd8e453b1c913b99a51a458a56a Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Sat, 23 Mar 2019 16:29:17 +0000 Subject: [PATCH 13/19] Update story/readme --- README.md | 4 ++-- stories/index.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1d70c0e..5c0c963 100644 --- a/README.md +++ b/README.md @@ -310,9 +310,9 @@ import data from 'emoji-mart/data/all.json' let emojiData = getEmojiDataFromNative('πŸŠπŸ½β€β™€οΈ', 'apple', data) ``` diff --git a/stories/index.js b/stories/index.js index de013ac..9fccb9d 100644 --- a/stories/index.js +++ b/stories/index.js @@ -220,14 +220,14 @@ storiesOf('Get emoji data from Native', module) .addDecorator(withKnobs) .add('Default', () => { let emojiData = getEmojiDataFromNative( - text('Unicode', 'πŸŠπŸ½β€β™€οΈ'), + text('Unicode', 'πŸ‹πŸΏβ€β™‚οΈ'), select('Emoji pack', SETS, SETS[0]), data ) if (!emojiData) { return (
- Couldn`t find any emoji data... + Couldn`t find any emoji data from native...
) } @@ -235,9 +235,9 @@ storiesOf('Get emoji data from Native', module) return (
From 0ab34e20a3ca97e7fd2f911b5f32fd29c3a41e6e Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Sat, 23 Mar 2019 21:12:40 +0000 Subject: [PATCH 14/19] Remove empty strings from skinTones and skinCodes. --- src/utils/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 2551dff..37055af 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -143,8 +143,8 @@ function getEmojiDataFromNative(nativeString, set, data) { uncompress(data); } - const skinTones = ['', '🏻', '🏼', '🏽', '🏾', '🏿'] - const skinCodes = ['', '1F3FB', '1F3FC', '1F3FD', '1F3FE', '1F3FF'] + const skinTones = ['🏻', '🏼', '🏽', '🏾', '🏿'] + const skinCodes = ['1F3FB', '1F3FC', '1F3FD', '1F3FE', '1F3FF'] let skin let skinCode @@ -153,7 +153,7 @@ function getEmojiDataFromNative(nativeString, set, data) { skinTones.forEach((skinTone) => { if (nativeString.indexOf(skinTone) > 0) { const skinToneIndex = skinTones.indexOf(skinTone) - skin = skinToneIndex + 1 + skin = skinToneIndex + 2 skinCode = skinCodes[skinToneIndex] } }) From 90b36d829fdde17263faffec9e3d68dbf887fa20 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Sat, 23 Mar 2019 21:13:02 +0000 Subject: [PATCH 15/19] Get skinToneIndex from forEach function --- src/utils/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 37055af..2e1dcfa 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -150,9 +150,8 @@ function getEmojiDataFromNative(nativeString, set, data) { let skinCode let baseNativeString = nativeString - skinTones.forEach((skinTone) => { + skinTones.forEach((skinTone, skinToneIndex) => { if (nativeString.indexOf(skinTone) > 0) { - const skinToneIndex = skinTones.indexOf(skinTone) skin = skinToneIndex + 2 skinCode = skinCodes[skinToneIndex] } From 5c64cc69088a3abd32412bdc0f5a984700e85d46 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Sat, 23 Mar 2019 21:14:52 +0000 Subject: [PATCH 16/19] Use for loop and refactor lookup code --- src/utils/index.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 2e1dcfa..1b66fad 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -157,19 +157,23 @@ function getEmojiDataFromNative(nativeString, set, data) { } }) - const emojiData = Object.values(data.emojis).find((emoji) => { - emoji = JSON.parse(_JSON.stringify(emoji)) + let emojiData; + + for (let id in data.emojis) { + let emoji = data.emojis[id] + + let emojiUnified = emoji.unified if (emoji.variations && emoji.variations.length) { - emoji.unified = emoji.variations.shift() + emojiUnified = emoji.variations.shift() } if (skin && emoji.skin_variations && emoji.skin_variations[skinCode]) { - emoji.unified = emoji.skin_variations[skinCode].unified + emojiUnified = emoji.skin_variations[skinCode].unified } - return unifiedToNative(emoji.unified) === baseNativeString - }) + if (unifiedToNative(emojiUnified) === baseNativeString) emojiData = emoji + } if (!emojiData) { return null From 5570a7ce1a4346a69af9cf1cbc6022a66dabf35e Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Sat, 23 Mar 2019 21:15:47 +0000 Subject: [PATCH 17/19] Run prettier --- src/utils/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 1b66fad..6862779 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -140,7 +140,7 @@ function getData(emoji, skin, set, data) { function getEmojiDataFromNative(nativeString, set, data) { if (data.compressed) { - uncompress(data); + uncompress(data) } const skinTones = ['🏻', '🏼', '🏽', '🏾', '🏿'] @@ -157,7 +157,7 @@ function getEmojiDataFromNative(nativeString, set, data) { } }) - let emojiData; + let emojiData for (let id in data.emojis) { let emoji = data.emojis[id] @@ -165,7 +165,7 @@ function getEmojiDataFromNative(nativeString, set, data) { let emojiUnified = emoji.unified if (emoji.variations && emoji.variations.length) { - emojiUnified = emoji.variations.shift() + emojiUnified = emoji.variations.shift() } if (skin && emoji.skin_variations && emoji.skin_variations[skinCode]) { From d140dd8f0480a0952cc0b5221aeebcb8241c9388 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Sat, 23 Mar 2019 21:22:14 +0000 Subject: [PATCH 18/19] typo --- README.md | 2 +- stories/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4e16bdf..8cab778 100644 --- a/README.md +++ b/README.md @@ -308,7 +308,7 @@ You can get emoji data from native emoji unicode using the `getEmojiDataFromNati import { getEmojiDataFromNative, Emoji } from 'emoji-mart' import data from 'emoji-mart/data/all.json' -let emojiData = getEmojiDataFromNative('πŸŠπŸ½β€β™€οΈ', 'apple', data) +const emojiData = getEmojiDataFromNative('πŸŠπŸ½β€β™€οΈ', 'apple', data) { - let emojiData = getEmojiDataFromNative( + const emojiData = getEmojiDataFromNative( text('Unicode', 'πŸ‹πŸΏβ€β™‚οΈ'), select('Emoji pack', SETS, SETS[0]), data From ab2f6004ac5d99e3823630bfe6f98870205e99d2 Mon Sep 17 00:00:00 2001 From: Peder Johnsen Date: Sat, 23 Mar 2019 21:31:11 +0000 Subject: [PATCH 19/19] Add tests --- .../get-emoji-data-from-native.test.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/utils/__tests__/get-emoji-data-from-native.test.js diff --git a/src/utils/__tests__/get-emoji-data-from-native.test.js b/src/utils/__tests__/get-emoji-data-from-native.test.js new file mode 100644 index 0000000..72efc30 --- /dev/null +++ b/src/utils/__tests__/get-emoji-data-from-native.test.js @@ -0,0 +1,27 @@ +import React from 'react' +import { getEmojiDataFromNative } from '..' + +import data from '../../../data/apple' + +test('will find man lifting weights with skin tone 6', () => { + const emojiData = getEmojiDataFromNative('πŸ‹πŸΏβ€β™‚οΈ', 'apple', data) + expect(emojiData.id).toEqual('man-lifting-weights') + expect(emojiData.skin).toEqual(6) +}) + +test('will find woman swimming with skin tone 4', () => { + const emojiData = getEmojiDataFromNative('πŸŠπŸ½β€β™€οΈ', 'apple', data) + expect(emojiData.id).toEqual('woman-swimming') + expect(emojiData.skin).toEqual(4) +}) + +test('will find person in lotus positions', () => { + const emojiData = getEmojiDataFromNative('🧘', 'apple', data) + expect(emojiData.id).toEqual('person_in_lotus_position') + expect(emojiData.skin).toEqual(1) +}) + +test('returns null if no match', () => { + const emojiData = getEmojiDataFromNative('', 'apple', data) + expect(emojiData).toEqual(null) +})