unaccent 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ require 'unaccent'
2
+
3
+ module Unaccent
4
+ module String
5
+ def unaccent
6
+ Unaccent.unaccent(self)
7
+ end
8
+
9
+ def unaccent_via_gsub
10
+ Unaccent.via_gsub(self)
11
+ end
12
+
13
+ def unaccent_via_each_char
14
+ Unaccent.via_each_char(self)
15
+ end
16
+
17
+ def unaccent_via_split_map
18
+ Unaccent.via_split_map(self)
19
+ end
20
+ end
21
+ end
22
+
23
+ String.include Unaccent::String
@@ -0,0 +1,3 @@
1
+ module Unaccent
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/unaccent.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'unaccent/accentmap'
2
+ require 'unaccent/version'
3
+
4
+ module Unaccent
5
+ class << self
6
+ # Replace a string's accented characters with unaccented characters.
7
+ #
8
+ # @example
9
+ # s = 'Å Ç ß'
10
+ # Unaccent.unaccent(s) = > 'AA C ss'
11
+ #
12
+ # @return [String] a string that has no accents
13
+
14
+ def unaccent(str)
15
+ via_gsub(str)
16
+ end
17
+
18
+ # Replace a string's accented characters with unaccented characters,
19
+ # by using string `#gsub` to replace non-ascii characters.
20
+ #
21
+ # @example
22
+ # str = 'Å Ç ß'
23
+ # Unaccent.via_gsub(str) = > 'AA C ss'
24
+ #
25
+ # @return [String] a string that has no accents
26
+
27
+ def via_gsub(str)
28
+ return str if str.ascii_only?
29
+
30
+ str.gsub(/[^[:ascii:]]/) { |c| ACCENTMAP.fetch(c, c) }
31
+ end
32
+
33
+ # Replace a string's accented characters with unaccented characters,
34
+ # by using string `#scan` to iterate on characters.
35
+ #
36
+ # @example
37
+ # str = 'Å Ç ß'
38
+ # Unaccent.via_scan(str) = > 'AA C ss'
39
+ #
40
+ # @return [String] a string that has no accents
41
+
42
+ def via_scan(str)
43
+ return str if str.ascii_only?
44
+
45
+ res = ''; str.scan(/./) { |c| res << ACCENTMAP.fetch(c, c) }; res
46
+ end
47
+
48
+ # Replace a string's accented characters with unaccented characters,
49
+ # by using string `#each_char` to iterate on characters.
50
+ #
51
+ # @example
52
+ # str = 'Å Ç ß'
53
+ # Unaccent.via_each_char(str) = > 'AA C ss'
54
+ #
55
+ # @return [String] a string that has no accents
56
+
57
+ def via_each_char(str)
58
+ return str if str.ascii_only?
59
+
60
+ res = ''; str.each_char { |c| res << ACCENTMAP.fetch(c, c) }; res
61
+ end
62
+
63
+ # Replace a string's accented characters with unaccented characters,
64
+ # by using string `#split` and `#map` to iterate on characters.
65
+ #
66
+ # @example
67
+ # str = 'Å Ç ß'
68
+ # Unaccent.via_split_map(str) = > 'AA C ss'
69
+ #
70
+ # @return [String] a string that has no accents
71
+
72
+ def via_split_map(str)
73
+ return str if str.ascii_only?
74
+
75
+ str.split(//u).map { |c| ACCENTMAP.fetch(c, c) }.join
76
+ end
77
+ end
78
+ end
data/unaccent.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ require_relative 'lib/unaccent/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'unaccent'
5
+ spec.version = Unaccent::VERSION
6
+ spec.authors = ['Jonian Guveli']
7
+ spec.email = ['jonian@hardpixel.eu']
8
+
9
+ spec.summary = 'Replace accented characters with unaccented characters'
10
+ spec.description = 'Replace accented characters with unaccented characters.'
11
+ spec.homepage = 'https://github.com/hardpixel/unaccent'
12
+ spec.license = 'MIT'
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 2.0'
23
+ spec.add_development_dependency 'minitest', '~> 5.0'
24
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unaccent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonian Guveli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ description: Replace accented characters with unaccented characters.
42
+ email:
43
+ - jonian@hardpixel.eu
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".github/workflows/build.yml"
49
+ - ".gitignore"
50
+ - ".ruby-version"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/benchmark
56
+ - bin/console
57
+ - bin/setup
58
+ - lib/unaccent.rb
59
+ - lib/unaccent/accentmap.rb
60
+ - lib/unaccent/string.rb
61
+ - lib/unaccent/version.rb
62
+ - unaccent.gemspec
63
+ homepage: https://github.com/hardpixel/unaccent
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.3.14
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Replace accented characters with unaccented characters
86
+ test_files: []