emoji-datasource 7.0.2 → 14.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ff37e9f02831eb01ad39be6947d601f844e228e36ff66f772f0549e661213ce
4
- data.tar.gz: 7a5cbe593ea2e618b2d0755df9a11fe22f764af8267496711ed980cb7ce1825e
3
+ metadata.gz: b821544f15170c9489e0ce8938978549829e3c925d3ec6444dfacb7c125b94ca
4
+ data.tar.gz: bf4e9c21d3c5ea9de80132de4aa56bfec68c705aba979863151d650facc73f77
5
5
  SHA512:
6
- metadata.gz: 7cfa33ddb0a3472bc47db53279c7aa4a83c7ede61897a556cdb28c115852f3b40e2d11c1444ae1ae4ac98b6815785c131615e3fe7ba1cd530f1f7fb6c9f8d463
7
- data.tar.gz: f6bde81f5d4d6825590d15cc2cf4a0330424a1e482d7cddc4c8cbcb77ee3f3eb6e8459ba80abb998e58766ae04ea9d52fb5c01fd090d85e0a63b2e6431cd42e5
6
+ metadata.gz: 03d5041a86c4d7bccde94965634abffd3194c7cc3dcfa70e1de6f573ea1b6d444c6382cc0275b88165597247dd1e97f1ced2050ca671c51183fcb62412a8dcea
7
+ data.tar.gz: 994db50c5e4221bf51ed491d108d0b3fb48dea1fae2837d3129d39586d8eb7d080e0423bec5aac775eba4ffc0360299bd20cb64b607f4333c0b9f36a6b2805f2
@@ -7,37 +7,38 @@ jobs:
7
7
  name: Test
8
8
  strategy:
9
9
  matrix:
10
- ruby: [2.5, 2.6, 2.7, 3.0, head]
10
+ ruby: [2.6, 2.7, '3.0', 3.1, head]
11
11
 
12
12
  runs-on: ubuntu-latest
13
13
 
14
14
  steps:
15
- - uses: actions/checkout@v2
15
+ - name: Checkout
16
+ uses: actions/checkout@v3
17
+
16
18
  - name: Set up Ruby ${{ matrix.ruby }}
17
19
  uses: ruby/setup-ruby@v1
18
20
  with:
19
21
  ruby-version: ${{ matrix.ruby }}
20
- - name: Install dependencies
21
- run: |
22
- gem install bundler
23
- bundle install --jobs 4 --retry 3
22
+ bundler-cache: true
23
+
24
24
  - name: RSpec
25
25
  run: |
26
26
  bundle exec rspec
27
+
27
28
  rubocop:
28
29
  name: RuboCop
29
30
  runs-on: ubuntu-latest
30
31
 
31
32
  steps:
32
- - uses: actions/checkout@v2
33
+ - name: Checkout
34
+ uses: actions/checkout@v3
35
+
33
36
  - name: Set up Ruby
34
37
  uses: ruby/setup-ruby@v1
35
38
  with:
36
- ruby-version: 2.7
37
- - name: Install dependencies
38
- run: |
39
- gem install bundler
40
- bundle install --jobs 4 --retry 3
39
+ ruby-version: 3.1
40
+ bundler-cache: true
41
+
41
42
  - name: RuboCop
42
43
  run: |
43
44
  bundle exec rubocop
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /Gemfile.lock
9
10
 
10
11
  node_modules
11
12
 
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.5
2
+ TargetRubyVersion: 2.6
3
3
  NewCops: enable
4
4
  SuggestExtensions: false
5
5
 
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 Justas Palumickas
3
+ Copyright (c) 2023 Justas Palumickas
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -22,6 +22,21 @@ Find emoji by short name
22
22
 
23
23
  ```ruby
24
24
  EmojiDatasource.find_by_short_name('+1')
25
+ #=> EmojiDatasource::Emoji: :+1: (👍)
26
+ ```
27
+
28
+ Find emoji by raw string:
29
+
30
+ ```ruby
31
+ EmojiDatasource.find_by_char('👍🏾')
32
+ #=> EmojiDatasource::Emoji: :+1::skin-tone-5: (👍🏾)
33
+ ```
34
+
35
+ Find emoji by unicode hex character code:
36
+
37
+ ```ruby
38
+ EmojiDatasource.find_by_unified("1F469-200D-2764-FE0F-200D-1F468")
39
+ #=> EmojiDatasource::Emoji: :woman-heart-man: (👩‍❤️‍👨)
25
40
  ```
26
41
 
27
42
  Convert emoji short name to character
@@ -33,19 +48,27 @@ EmojiDatasource.short_name_to_char('+1') # => 👍
33
48
  this also supports skin variations
34
49
 
35
50
  ```ruby
36
- EmojiDatasource.short_name_to_char('+1::skin-tone-2') # => 👍
51
+ EmojiDatasource.short_name_to_char('+1::skin-tone-2') # => 👍🏻
37
52
  ```
38
53
 
54
+ Get base emoji for skin variation:
55
+
56
+ ```ruby
57
+ emoji = EmojiDatasource.find_by_short_name(':+1::skin-tone-5:')
58
+ #=> EmojiDatasource::Emoji: :+1::skin-tone-5: (👍🏾)
59
+ emoji.base
60
+ #=> EmojiDatasource::Emoji: :+1: (👍)
61
+ ```
39
62
 
40
63
  ## Supported Ruby Versions
41
64
 
42
65
  This library aims to support and is [tested against][github_actions] the following Ruby
43
66
  implementations:
44
67
 
45
- * Ruby 2.5.0
46
68
  * Ruby 2.6.0
47
69
  * Ruby 2.7.0
48
70
  * Ruby 3.0.0
71
+ * Ruby 3.1.0
49
72
 
50
73
  ## License
51
74
 
@@ -13,12 +13,13 @@ Gem::Specification.new do |spec|
13
13
  '(https://github.com/iamcal/emoji-data) npm package'
14
14
  spec.homepage = 'http://github.com/jpalumickas/emoji-datasource-ruby'
15
15
  spec.license = 'MIT'
16
- spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
17
17
 
18
18
  spec.metadata['homepage_uri'] = spec.homepage
19
19
  spec.metadata['source_code_uri'] = 'https://github.com/jpalumickas/emoji-datasource-ruby'
20
20
  spec.metadata['changelog_uri'] = 'https://github.com/jpalumickas/emoji-datasource-ruby/releases'
21
21
  spec.metadata['bug_tracker_uri'] = 'https://github.com/jpalumickas/emoji-datasource-ruby/issues'
22
+ spec.metadata['rubygems_mfa_required'] = 'true'
22
23
 
23
24
  # Specify which files should be added to the gem when it is released.
24
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -33,7 +34,7 @@ Gem::Specification.new do |spec|
33
34
 
34
35
  spec.add_development_dependency 'pry', '~> 0.14'
35
36
  spec.add_development_dependency 'rake', '~> 13.0'
36
- spec.add_development_dependency 'rspec', '~> 3.10'
37
- spec.add_development_dependency 'rubocop', '~> 1.16'
38
- spec.add_development_dependency 'simplecov', '~> 0.21'
37
+ spec.add_development_dependency 'rspec', '~> 3.12'
38
+ spec.add_development_dependency 'rubocop', '~> 1.45'
39
+ spec.add_development_dependency 'simplecov', '~> 0.22'
39
40
  end
@@ -2,38 +2,75 @@
2
2
 
3
3
  module EmojiDatasource
4
4
  class Emoji
5
- attr_reader :data
5
+ attr_reader :data, :unified
6
6
 
7
- def initialize(data)
7
+ def initialize(data, variation: nil)
8
8
  @data = data
9
+ @variation = variation
10
+ @unified = variation ? @data.dig('skin_variations', variation, 'unified') : @data['unified']
9
11
  end
10
12
 
13
+ # Raw emoji string
11
14
  def to_char
12
- EmojiDatasource.unified_to_char(data[:unified])
15
+ EmojiDatasource.unified_to_char(unified)
13
16
  end
14
17
 
15
- def skin_variations
16
- return unless @data[:skin_variations]
18
+ # Official emoji name (pretty long)
19
+ def name
20
+ return @data['name'] unless @variation
17
21
 
18
- @data[:skin_variations].transform_keys(&:to_s)
22
+ "#{@data['name']} (#{variation_emojis.map(&:name).join(', ')})"
23
+ end
24
+
25
+ # Short code that often can be used to find emoji in pickers and chat apps
26
+ def short_name
27
+ return @data['short_name'] unless @variation
28
+
29
+ "#{@data['short_name']}::#{variation_emojis.map(&:short_name).join('::')}"
30
+ end
31
+
32
+ # All known short names (base +short_name+ and maybe some aliases)
33
+ def short_names
34
+ return @data['short_names'] unless @variation
35
+
36
+ @data['short_names'].map do |short_name|
37
+ "#{short_name}::#{variation_emojis.map(&:short_name).join('::')}"
38
+ end
39
+ end
40
+
41
+ # All known skin tone variations
42
+ def variations
43
+ return @variations = [] if @variation
44
+
45
+ @variations ||= @data.fetch('skin_variations', {}).each_key.map do |key|
46
+ self.class.new(data, variation: key)
47
+ end
48
+ end
49
+
50
+ # Base emoji, without variations applied.
51
+ # E.g. for `:+1::skin-tone-2:` base will be just `:+1:`
52
+ def base
53
+ return self unless @variation
54
+
55
+ EmojiDatasource.find_by_unified(@data['unified'])
19
56
  end
20
57
 
21
58
  def method_missing(method_name, *arguments, &block)
22
- if @data.key?(method_name)
23
- @data[method_name]
59
+ if @data.key?(method_name.to_s)
60
+ @data[method_name.to_s]
24
61
  else
25
62
  super
26
63
  end
27
64
  end
28
65
 
29
66
  def respond_to_missing?(method_name, include_private = false)
30
- return true if @data.key?(method_name)
67
+ return true if @data.key?(method_name.to_s)
31
68
 
32
69
  super
33
70
  end
34
71
 
35
72
  def inspect
36
- "#{self.class.name}:#{short_name}"
73
+ "#{self.class.name}: :#{short_name}: (#{to_char})"
37
74
  end
38
75
 
39
76
  def to_s
@@ -47,5 +84,15 @@ module EmojiDatasource
47
84
  def to_json(**args)
48
85
  @data.to_json(**args)
49
86
  end
87
+
88
+ private
89
+
90
+ def variation_emojis
91
+ return [] unless @variation
92
+
93
+ @variation.split('-').map do |unified|
94
+ EmojiDatasource.find_by_unified(unified)
95
+ end
96
+ end
50
97
  end
51
98
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EmojiDatasource
4
- VERSION = '7.0.2'
4
+ VERSION = '14.0.1'
5
5
  end
@@ -4,23 +4,50 @@ require 'json'
4
4
 
5
5
  require_relative 'emoji_datasource/version'
6
6
  require_relative 'emoji_datasource/emoji'
7
- require_relative 'emoji_datasource/short_name_to_char'
8
7
 
9
8
  module EmojiDatasource
10
9
  class Error < StandardError; end
11
10
 
12
11
  EMOJI_DATA_PATH = File.join(__dir__, '..', 'vendor', 'emoji-datasource', 'emoji.json')
13
12
 
13
+ # @example
14
+ # short_name_to_char('+1') #=> "👍"
14
15
  def self.short_name_to_char(name)
15
- EmojiDatasource::ShortNameToChar.generate(name)
16
+ find_by_short_name(name)&.to_char
16
17
  end
17
18
 
19
+ # Finds emoji by short code (e.g. `+1`, `:+1:`, `:+1::skin-tone-5:`)
20
+ # @param name [String] short code with or without wrapping colons.
21
+ # @return [EmojiDatasource::Emoji] if there is an emoji matching +name+
22
+ # @return [nil] if there are no emojis matching +name+
23
+ # @example
24
+ # find_by_short_name('+1') #=> EmojiDatasource::Emoji: :+1: (👍)
18
25
  def self.find_by_short_name(name)
19
26
  return unless name
20
27
 
21
- data.detect do |emoji|
22
- emoji.short_name == name || emoji.short_names.include?(name)
23
- end
28
+ name = name.delete_prefix(':').delete_suffix(':') # Allow to find `+1` by `:+1:`
29
+ name.delete_suffix!('::skin-tone-1') # special case for default skin tone
30
+ short_name_lookup_map[name]
31
+ end
32
+
33
+ # Finds emoji by unified hex representation of Unicode codepoints
34
+ # @param unified [String] Dash separated hexadecimal Unicode character codes for a single emoji
35
+ # @return [EmojiDatasource::Emoji] if given Unicode codepoints is a known emoji
36
+ # @return [nil] if there are no emojis with given codepoints in the dataset
37
+ # @example
38
+ # short_name_to_char('1F44D') #=> EmojiDatasource::Emoji: :+1: (👍)
39
+ def self.find_by_unified(unified)
40
+ unified_lookup_map[unified.upcase]
41
+ end
42
+
43
+ # Find emoji object by raw Unicode string with a single emoji in it
44
+ # @param raw_emoji [String] Single emoji
45
+ # @return [EmojiDatasource::Emoji] if given string contains a single known emoji
46
+ # @return [nil] if there are no such emoji in the dataset
47
+ # @example
48
+ # find_by_char('👍') #=> EmojiDatasource::Emoji: :+1: (👍)
49
+ def self.find_by_char(raw_emoji)
50
+ find_by_unified(char_to_unified(raw_emoji))
24
51
  end
25
52
 
26
53
  def self.unified_to_char(unified_name)
@@ -29,10 +56,38 @@ module EmojiDatasource
29
56
  unified_name.split('-').map(&:hex).pack('U*')
30
57
  end
31
58
 
59
+ def self.char_to_unified(raw_emoji)
60
+ return unless raw_emoji
61
+
62
+ raw_emoji.unpack('U*').map { |c| c.to_s(16).upcase }.join('-')
63
+ end
64
+
32
65
  def self.data
33
- @data ||= JSON.parse(File.read(EMOJI_DATA_PATH), symbolize_names: true)
66
+ @data ||= JSON.parse(File.read(EMOJI_DATA_PATH))
34
67
  .map { |emoji_data| EmojiDatasource::Emoji.new(emoji_data) }
35
68
  end
69
+
70
+ # Utility hash map to search by emoji short code, including variants
71
+ # @api private
72
+ def self.short_name_lookup_map
73
+ @short_name_lookup_map ||= data.each_with_object({}) do |emoji, result|
74
+ emoji.short_names.each { |short_name| result[short_name] = emoji }
75
+ emoji.variations.each do |emoji_variant|
76
+ emoji_variant.short_names.each { |short_name| result[short_name] = emoji_variant }
77
+ end
78
+ end
79
+ end
80
+
81
+ # Utility hash map to search by unicode character sequence hex codes, including variants
82
+ # @api private
83
+ def self.unified_lookup_map
84
+ @unified_lookup_map ||= data.each_with_object({}) do |emoji, result|
85
+ result[emoji.unified] = emoji
86
+ emoji.variations.each do |emoji_variant|
87
+ result[emoji_variant.unified] = emoji_variant
88
+ end
89
+ end
90
+ end
36
91
  end
37
92
 
38
93
  # Preload emojies on startup
data/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "private": true,
7
7
  "dependencies": {},
8
8
  "devDependencies": {
9
- "emoji-datasource": "^7.0.2"
9
+ "emoji-datasource": "^14.0.0"
10
10
  },
11
11
  "scripts": {
12
12
  "postinstall": "cp ./node_modules/emoji-datasource/emoji.json ./vendor/emoji-datasource/emoji.json"