Skip to content

Commit

Permalink
Merge pull request #66 from jsumners/pr-60-tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners authored Jan 26, 2022
2 parents d48ff08 + 53f8e17 commit 1adf245
Show file tree
Hide file tree
Showing 14 changed files with 412 additions and 338 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
sandbox.js

output/
*.alfredworkflow
*.sig
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cd icons
node ${parentDir}/lib/genicons.js
cd ..

cp icons/beer.png ./icon.png
cp icons/beer_mug.png ./icon.png

echo "Updating version ..."
curVersion=$(node -e "console.log(require('${parentDir}/package.json').version)")
Expand Down
37 changes: 23 additions & 14 deletions lib/genicons.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,43 @@

const fs = require('fs')
const fontkit = require('fontkit')
const emojilib = require('emojilib')
const emojiData = require('unicode-emoji-json')
const emojiComponents = require('unicode-emoji-json/data-emoji-components')
const orderedEmoji = require('unicode-emoji-json/data-ordered-emoji')

const modifiers = emojilib.fitzpatrick_scale_modifiers
// compatability layer:
const modifiers = [
emojiComponents.light_skin_tone,
emojiComponents.medium_light_skin_tone,
emojiComponents.medium_skin_tone,
emojiComponents.medium_dark_skin_tone,
emojiComponents.dark_skin_tone
]
const font = fontkit.openSync('/System/Library/Fonts/Apple Color Emoji.ttc').fonts[0]

const saveIcon = (emoji, name) => {
const glyph = emoji.glyphs[0].getImageForSize(64)
fs.writeFileSync(`${process.env['PWD']}/${name}.png`, glyph.data)
fs.writeFileSync(`${process.env.PWD}/${name}.png`, glyph.data)
}

const addModifier = (emoji, modifier) => {
if (!modifier || !emoji.fitzpatrick_scale) return emoji.char
const zwj = new RegExp('โ€', 'g')
return emoji.char.match(zwj) ? emoji.char.replace(zwj, modifier + 'โ€') : emoji.char + modifier
const addModifier = (char, modifier) => {
if (!modifier || !emojiData[char].skin_tone_support) return char
const zwj = new RegExp('โ€', 'g') // eslint-disable-line
return char.match(zwj) ? char.replace(zwj, modifier + 'โ€') : char + modifier
}

emojilib.ordered.forEach((name) => {
orderedEmoji.forEach((char) => {
const emoji = emojiData[char]
try {
const emoji = font.layout(emojilib.lib[name].char)
saveIcon(emoji, name)
saveIcon(font.layout(char), emoji.slug)

if (emojilib.lib[name].fitzpatrick_scale) {
if (emoji.skin_tone_support) {
modifiers.forEach((modifier, index) => {
const emoji = font.layout(addModifier(emojilib.lib[name], modifier))
saveIcon(emoji, `${name}_${index}`)
const charMod = font.layout(addModifier(char, modifier))
saveIcon(charMod, `${emoji.slug}_${index}`)
})
}
} catch (e) {
console.error('Could not generate icon for "%s": %s', name, e.message)
console.error('Could not generate icon for "%s": %s', emoji ? emoji.name : char, e.message)
}
})
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"clean": "rm -f *.alfredworkflow; rm -rf output/*",
"build": "./build.sh",
"load": "npm run build && open alfred-emoji.alfredworkflow",
"test": "tap 'test/**/*.test.js'",
"test:watch": "npm test -- --watch",
"test-ci": "tap --cov 'test/**/*.test.js'",
"test": "tap 'test/**/*.test.js' -R terse",
"test:watch": "npm test -- --watch --no-coverage-report",
"test-ci": "tap --cov 'test/**/*.test.js' -R terse",
"lint": "standard | snazzy",
"lint-ci": "standard"
},
Expand All @@ -16,16 +16,17 @@
"test"
],
"dependencies": {
"emojilib": "^2.4.0",
"fontkit": "^1.7.7"
"emojilib": "^3.0.2",
"fontkit": "^1.7.7",
"unicode-emoji-json": "^0.3.0"
},
"devDependencies": {
"mock-require": "^3.0.3",
"pre-commit": "^1.2.2",
"snazzy": "^8.0.0",
"standard": "^12.0.1",
"tap": "^14.1.0",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2"
"snazzy": "^9.0.0",
"standard": "^16.0.4",
"tap": "^15.1.6",
"webpack": "^5.67.0",
"webpack-cli": "^4.9.2"
}
}
74 changes: 43 additions & 31 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
'use strict'

const emojilib = require('emojilib')
const emojiNames = emojilib.ordered
const modifiers = emojilib.fitzpatrick_scale_modifiers
const emojiData = require('unicode-emoji-json')
const emojiComponents = require('unicode-emoji-json/data-emoji-components')
const orderedEmoji = require('unicode-emoji-json/data-ordered-emoji')
const emojiKeywords = require('emojilib')

// compatability layer:
const modifiers = [
emojiComponents.light_skin_tone,
emojiComponents.medium_light_skin_tone,
emojiComponents.medium_skin_tone,
emojiComponents.medium_dark_skin_tone,
emojiComponents.dark_skin_tone
]

let skinTone
let modifier
Expand All @@ -20,8 +30,8 @@ const setSkinToneModifier = (tone) => {
modifier = skinTone ? modifiers[skinTone] : null
}

const addModifier = (emoji, modifier) => {
if (!modifier || !emoji.fitzpatrick_scale) return emoji.char
const addModifier = (emoji, char, modifier) => {
if (!modifier || !emoji.skin_tone_support) return char

/*
* There are some emojis categorized as a sequence of emojis
Expand All @@ -31,20 +41,21 @@ const addModifier = (emoji, modifier) => {
* https://emojipedia.org/emoji-zwj-sequence/
*/

const zwj = new RegExp('โ€', 'g')
return emoji.char.match(zwj) ? emoji.char.replace(zwj, modifier + 'โ€') : emoji.char + modifier
const zwj = new RegExp('โ€', 'g') // eslint-disable-line
return char.match(zwj) ? char.replace(zwj, modifier + 'โ€') : char + modifier
}

const getIconName = (emoji, name) => {
if (emoji.fitzpatrick_scale && skinTone && skinTone >= 0 && skinTone < 5) {
return `${name}_${skinTone}`
const getIconName = (emoji) => {
if (emoji.skin_tone_support && skinTone && skinTone >= 0 && skinTone < 5) {
return `${emoji.slug}_${skinTone}`
}
return name
return emoji.slug
}

const alfredItem = (emoji, name) => {
const modifiedEmoji = addModifier(emoji, modifier)
const icon = getIconName(emoji, name)
const alfredItem = (emoji, char) => {
const modifiedEmoji = addModifier(emoji, char, modifier)
const icon = getIconName(emoji)
const name = emoji.name
return {
uid: name,
title: name,
Expand All @@ -53,40 +64,41 @@ const alfredItem = (emoji, name) => {
autocomplete: name,
icon: { path: `./icons/${icon}.png` },
mods: {
// copy a code for the emoji, e.g. :thumbs_down:
alt: {
subtitle: `${verb} ":${name}:" (${emoji.char}) ${preposition}`,
arg: `:${name}:`,
icon: { path: `./icons/${name}.png` }
subtitle: `${verb} ":${emoji.slug}:" (${char}) ${preposition}`,
arg: `:${emoji.slug}:`,
icon: { path: `./icons/${emoji.slug}.png` }
},
// copy the default symbol for the emoji, without skin tones
shift: {
subtitle: `${verb} "${emoji.char}" (${name}) ${preposition}`,
arg: emoji.char,
icon: { path: `./icons/${name}.png` }
subtitle: `${verb} "${char}" (${name}) ${preposition}`,
arg: char,
icon: { path: `./icons/${emoji.slug}.png` }
}
}
}
}

const alfredItems = (names) => {
const alfredItems = (chars) => {
const items = []
names.forEach((name) => {
const emoji = emojilib.lib[name]
if (!emoji) return
items.push(alfredItem(emoji, name))
chars.forEach((char) => {
items.push(alfredItem(emojiData[char], char))
})
return { items }
}

const all = () => alfredItems(emojiNames)
const all = () => alfredItems(orderedEmoji)

const libHasEmoji = (name, term) => {
return emojilib.lib[name] &&
emojilib.lib[name].keywords.some((keyword) => keyword.includes(term))
const libHasEmoji = (char, term) => {
return emojiKeywords[char] &&
emojiKeywords[char].some((keyword) => keyword.includes(term))
}
const matches = (terms) => {
return emojiNames.filter((name) => {
return orderedEmoji.filter((char) => {
const name = emojiData[char].name
return terms.every((term) => {
return name.includes(term) || libHasEmoji(name, term)
return name.includes(term) || libHasEmoji(char, term)
})
})
}
Expand Down
9 changes: 6 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export const NUM_SKIN_TONES = 5
const NUM_SKIN_TONES = 5

export function getRandomSkinTone () {
return Math.round(Math.random() * (NUM_SKIN_TONES - 1))
module.exports = {
NUM_SKIN_TONES,
getRandomSkinTone () {
return Math.round(Math.random() * (NUM_SKIN_TONES - 1))
}
}
40 changes: 40 additions & 0 deletions test/emojiKeywordsMock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"๐Ÿค”": ["face", "hmmm", "think", "consider"],
"๐Ÿ’ญ": ["bubble", "cloud", "speech", "thinking", "dream"],
"๐Ÿ—ฏ": ["caption", "speech", "thinking", "mad"],
"๐Ÿ˜ฌ": ["face", "grimace", "teeth"],
"๐Ÿคฎ": ["face", "sick"],
"๐Ÿ‘": ["thumbsup", "yes", "awesome", "good", "agree", "accept", "cool", "hand", "like"],
"๐Ÿ‘Ž": ["thumbsdown", "no", "dislike", "hand"],
"๐Ÿ™‰": ["animal", "monkey", "nature"],
"๐Ÿฆ„": ["animal", "nature", "mystical"],
"๐ŸŒˆ": ["nature", "happy", "unicorn_face", "photo", "sky", "spring"],
"๐ŸŒฉ": ["weather", "thunder"],
"โšก": ["thunder", "weather", "lightning bolt", "fast"],
"๐Ÿ": ["fruit", "nature"],
"๐ŸŽ": ["fruit", "mac", "school"],
"๐Ÿ": ["fruit", "nature", "food"],
"๐ŸŠ": ["food", "fruit", "nature", "orange"],
"๐Ÿ‹": ["fruit", "nature"],
"๐ŸŒ": ["fruit", "food", "monkey"],
"๐Ÿ‰": ["fruit", "food", "picnic", "summer"],
"๐Ÿ‡": ["fruit", "food", "wine"],
"๐Ÿ“": ["fruit", "food", "nature"],
"๐Ÿˆ": ["fruit", "nature", "food"],
"๐Ÿ’": ["food", "fruit"],
"๐Ÿ‘": ["fruit", "nature", "food"],
"๐Ÿ": ["fruit", "nature", "food"],
"๐Ÿฅฅ": ["fruit", "nature", "food", "palm"],
"๐Ÿฅ": ["fruit", "food"],
"๐Ÿ‘จโ€๐ŸŽค": ["rockstar", "entertainer", "man", "human"],
"๐Ÿฅญ": ["fruit", "food", "tropical"],
"๐Ÿฅ‘": ["fruit", "food"],
"๐Ÿฅฆ": ["fruit", "food", "vegetable"],
"๐Ÿ…": ["fruit", "vegetable", "nature", "food"],
"๐Ÿ†": ["vegetable", "nature", "food", "aubergine"],
"๐Ÿฅ’": ["fruit", "food", "pickle"],
"๐ŸŽฐ": ["bet", "gamble", "vegas", "fruit machine", "luck", "casino"],
"๐Ÿงธ": ["plush", "stuffed"],
"๐Ÿ“–": ["book", "read", "library", "knowledge", "literature", "learn", "study"],
"๐Ÿ‡ฑ๐Ÿ‡น": ["lt", "flag", "nation", "country", "banner"]
}
Loading

0 comments on commit 1adf245

Please sign in to comment.