string_magic 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +1 -1
- data/lib/string_magic/advanced/security.rb +38 -0
- data/lib/string_magic/core/transformation.rb +29 -0
- data/lib/string_magic/formatting/highlighting.rb +19 -0
- data/lib/string_magic/formatting/truncation.rb +25 -0
- data/lib/string_magic/version.rb +1 -1
- data/lib/string_magic.rb +6 -1
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7c2974ab8c5a68c297377233418f680d5f92ddc66d1c2c4490744c136ac6982a
|
4
|
+
data.tar.gz: da5732a7df509fb6d1ff3c73990a477f9b1417fb0ffd50e4c18819fd495b17f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b82312455117a5efdf8cc12d58dd825e0cc268c351199185c5430125eede62d4180ec71f5497bbec627dca9a3ebffee177d5a8480fd3fedfb469709145530f2b
|
7
|
+
data.tar.gz: 2520fb7095a7ccdfc3ea91e9e702e0906c37dc6f810b1f5e9231c4aeb8759747ccfce1a05a20488187463627e537c298bd864647a7b3309ddc894c5dd69c551e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# StringMagic
|
2
2
|
|
3
|
-
[](https://badge.fury.io/rb/string_magic)
|
4
4
|
[](https://dl.circleci.com/status-badge/redirect/circleci/8MamMcAVAVNWTcUqkjQk7R/Sh2DQkMWqqCv4MFvAmYWDL/tree/main)
|
5
5
|
[](https://opensource.org/licenses/MIT)
|
6
6
|
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module StringMagic
|
2
|
+
module Advanced
|
3
|
+
module Security
|
4
|
+
def mask_sensitive_data(text, options = {})
|
5
|
+
return text if text.nil? || text.empty?
|
6
|
+
|
7
|
+
mask_char = options[:mask_char] || "*"
|
8
|
+
options[:preserve_count] || 4
|
9
|
+
|
10
|
+
patterns = {
|
11
|
+
credit_card: /\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b/,
|
12
|
+
ssn: /\b\d{3}[-\s]?\d{2}[-\s]?\d{4}\b/,
|
13
|
+
email: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/
|
14
|
+
}
|
15
|
+
|
16
|
+
result = text.dup
|
17
|
+
|
18
|
+
patterns.each do |type, pattern|
|
19
|
+
result.gsub!(pattern) do |match|
|
20
|
+
case type
|
21
|
+
when :credit_card
|
22
|
+
digits = match.gsub(/[-\s]/, "")
|
23
|
+
mask_char * 12 + digits[-4..]
|
24
|
+
when :ssn
|
25
|
+
digits = match.gsub(/[-\s]/, "")
|
26
|
+
mask_char * 5 + digits[-4..]
|
27
|
+
when :email
|
28
|
+
local, domain = match.split("@")
|
29
|
+
"#{mask_char * local.length}@#{domain}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
result
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -1,6 +1,35 @@
|
|
1
1
|
module StringMagic
|
2
2
|
module Core
|
3
3
|
module Transformation
|
4
|
+
def titleize_names(text)
|
5
|
+
return text if text.nil? || text.empty?
|
6
|
+
|
7
|
+
special_cases = {
|
8
|
+
"mcdonald" => "McDonald",
|
9
|
+
"o'reilly" => "O'Reilly",
|
10
|
+
"macbook" => "MacBook",
|
11
|
+
"iphone" => "iPhone",
|
12
|
+
"ipad" => "iPad",
|
13
|
+
"ebay" => "eBay"
|
14
|
+
}
|
15
|
+
|
16
|
+
prefixes = Set.new(%w[van de la du das dos di da delle degli delle])
|
17
|
+
articles = Set.new(%w[a an the]) # Add this line
|
18
|
+
|
19
|
+
words = text.downcase.split(/\s+/)
|
20
|
+
words.map.with_index do |word, index|
|
21
|
+
if special_cases.key?(word)
|
22
|
+
special_cases[word]
|
23
|
+
elsif prefixes.include?(word) && !index.zero?
|
24
|
+
word
|
25
|
+
elsif articles.include?(word) && !index.zero? # Add this condition
|
26
|
+
word.downcase
|
27
|
+
else
|
28
|
+
word.capitalize
|
29
|
+
end
|
30
|
+
end.join(" ")
|
31
|
+
end
|
32
|
+
|
4
33
|
def to_snake_case(string)
|
5
34
|
return "" if string.nil?
|
6
35
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module StringMagic
|
2
|
+
module Formatting
|
3
|
+
module Highlighting
|
4
|
+
def highlight(text, phrases, options = {})
|
5
|
+
return text if text.nil? || phrases.nil?
|
6
|
+
|
7
|
+
tag = options[:tag] || "mark"
|
8
|
+
css_class = options[:class]
|
9
|
+
phrases = Array(phrases)
|
10
|
+
|
11
|
+
class_attr = css_class ? %( class="#{css_class}") : ""
|
12
|
+
|
13
|
+
phrases.reduce(text) do |result, phrase|
|
14
|
+
result.gsub(/(#{Regexp.escape(phrase)})/i, "<#{tag}#{class_attr}>\\1</#{tag}>")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module StringMagic
|
2
|
+
module Formatting
|
3
|
+
module Truncation
|
4
|
+
def truncate_words(text, count, options = {})
|
5
|
+
return text if text.nil? || count.nil? || count < 1
|
6
|
+
|
7
|
+
suffix = options[:suffix] || "..."
|
8
|
+
words = text.split(/\s+/)
|
9
|
+
return text if words.length <= count
|
10
|
+
|
11
|
+
words[0...count].join(" ") + suffix
|
12
|
+
end
|
13
|
+
|
14
|
+
def truncate_sentences(text, count, options = {})
|
15
|
+
return text if text.nil? || count.nil? || count < 1
|
16
|
+
|
17
|
+
suffix = options[:suffix] || "..."
|
18
|
+
sentences = text.split(/(?<=[.!?])\s+/)
|
19
|
+
return text if sentences.length <= count
|
20
|
+
|
21
|
+
sentences[0...count].join(" ") + suffix
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/string_magic/version.rb
CHANGED
data/lib/string_magic.rb
CHANGED
@@ -3,14 +3,19 @@
|
|
3
3
|
require_relative "string_magic/version"
|
4
4
|
require_relative "string_magic/core/analysis"
|
5
5
|
require_relative "string_magic/core/transformation"
|
6
|
+
require_relative "string_magic/formatting/highlighting"
|
7
|
+
require_relative "string_magic/formatting/truncation"
|
8
|
+
require_relative "string_magic/advanced/security"
|
6
9
|
|
7
10
|
module StringMagic
|
8
11
|
class Error < StandardError; end
|
9
12
|
class MalformedInputError < Error; end
|
10
13
|
|
11
|
-
# Now extend the modules
|
12
14
|
extend Core::Analysis
|
13
15
|
extend Core::Transformation
|
16
|
+
extend Formatting::Highlighting
|
17
|
+
extend Formatting::Truncation
|
18
|
+
extend Advanced::Security
|
14
19
|
|
15
20
|
def self.hello_world
|
16
21
|
"hello world!"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string_magic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emad Rahimi
|
@@ -69,8 +69,11 @@ files:
|
|
69
69
|
- README.md
|
70
70
|
- Rakefile
|
71
71
|
- lib/string_magic.rb
|
72
|
+
- lib/string_magic/advanced/security.rb
|
72
73
|
- lib/string_magic/core/analysis.rb
|
73
74
|
- lib/string_magic/core/transformation.rb
|
75
|
+
- lib/string_magic/formatting/highlighting.rb
|
76
|
+
- lib/string_magic/formatting/truncation.rb
|
74
77
|
- lib/string_magic/version.rb
|
75
78
|
- sig/string_magic.rbs
|
76
79
|
homepage: https://github.com/erscript/string-magic
|