unicode_alphanumeric 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/unicode_alphanumeric.rb +51 -0
  2. metadata +94 -0
@@ -0,0 +1,51 @@
1
+ require 'unicode'
2
+
3
+ module UnicodeAlphanumeric
4
+ def self.filter(str, options = {})
5
+ replace(str, '', options)
6
+ end
7
+
8
+ def self.replace(str, replacement, options = {})
9
+ str.gsub(ascii_regex(options)) do |char|
10
+ alphanumeric?(char, options) ? char : replacement
11
+ end
12
+ end
13
+
14
+ def self.scan(str, options = {})
15
+ non_alphanumeric = []
16
+ str.scan(ascii_regex(options)) do |char|
17
+ non_alphanumeric << char if !alphanumeric?(char, options)
18
+ end
19
+ non_alphanumeric
20
+ end
21
+
22
+ def self.map(str, options = {})
23
+ str.gsub(ascii_regex(options)) do |char|
24
+ alphanumeric?(char, options) ? char : yield(char)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def self.alphanumeric?(char, options)
31
+ # If there's any intersection between the string's categories and the alphanumeric
32
+ # categories, then the character is alphanumeric.
33
+ !(alphanumeric_categories(options) & Unicode.categories(char)).empty?
34
+ end
35
+
36
+ def self.alphanumeric_categories(options)
37
+ categories = [:Lowercase_Letter, :Uppercase_Letter, :Decimal_Number]
38
+ if options[:spaces] and options[:spaces] != :ascii
39
+ categories << :Space_Separator
40
+ end
41
+ categories
42
+ end
43
+
44
+ def self.ascii_regex(options)
45
+ if options[:spaces]
46
+ /[^a-zA-Z0-9 ]/
47
+ else
48
+ /[^a-zA-Z0-9]/
49
+ end
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unicode_alphanumeric
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jarrett Colby
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: unicode
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.4.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.4.4
30
+ - !ruby/object:Gem::Dependency
31
+ name: minitest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: turn
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Removes, replaces, transforms, or detects non-alphanumeric Unicode characters
63
+ in a string
64
+ email: jarrett@madebyhq.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - lib/unicode_alphanumeric.rb
70
+ homepage: http://madebyhq.com/
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.25
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Filters non-alphanumeric Unicode characters
94
+ test_files: []