Skip to content

Instantly share code, notes, and snippets.

@tk3369
Created March 13, 2021 19:46
Show Gist options
  • Save tk3369/e4f3d6e6fa8f32905e52ec042fbfb58b to your computer and use it in GitHub Desktop.
Save tk3369/e4f3d6e6fa8f32905e52ec042fbfb58b to your computer and use it in GitHub Desktop.
Generate javascript code for mapping emoji short name to unicode
module JavaScriptEmoji
export download_emojis, parse_emojis, gen_javascript
import JSON
function download_emojis()
url = "https://raw.githubusercontent.com/iamcal/emoji-data/master/emoji.json"
return JSON.parsefile(download(url))
end
function parse_emojis(data)
result = Dict()
for emj in data
name = lowercase(replace(emj["short_name"], r"[ _]" => "-"))
unicode = emj["unified"]
if '-' in unicode
# println(name * " has dash: " * unicode)
continue
end
result[name] = (name = name, code = unicode, char = "$(Char(parse(UInt32, unicode, base = 16)))")
end
return result
end
function gen_javascript(filename, emojis)
skeys = sort(collect(keys(emojis)))
open(filename, "w") do fh
println(fh, "const emojis = {")
for key in skeys
emoji = emojis[key]
println(fh, " '", emoji.name, "': '", emoji.code, "',")
end
println(fh, "};")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment