philiprehberger-string_kit 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 +10 -0
- data/README.md +29 -0
- data/lib/philiprehberger/string_kit/version.rb +1 -1
- data/lib/philiprehberger/string_kit.rb +56 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f38ef0d79a14fb2d1ee763b52cd9e01ade47d478944f766676f6e32ac1d89eda
|
|
4
|
+
data.tar.gz: b7f0c00dee4c9fd5d7287a16b41b9e7bd5f701068d7c2d52a878a6991cc261ef
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ddb00275a54597ae4a4b0b9265acdf50360ce8b537787d7c7fef89705dbc9173e73dfb67ded292672151ddde4dac6798bb027d0806b6fdf270df68b52489f715
|
|
7
|
+
data.tar.gz: 9652f8595c23648bfe3a42b633e04f758c16d49db5c912237b669d80e8053a28db67ddf7b7758cf08d32be4e0a4ef7a858dcf9ea15b0439b9207223b091dca0a
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,16 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.4.0] - 2026-05-30
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `StringKit.mask(str, show_first:, show_last:, mask_char:)` for partial string obfuscation
|
|
14
|
+
- `StringKit.between(str, left, right)` to extract text between delimiters
|
|
15
|
+
- `StringKit.truncate_words(str, max_words, omission:)` for word-aware truncation
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
- README now includes the standard package card image after the badges
|
|
19
|
+
|
|
10
20
|
## [0.3.0] - 2026-04-25
|
|
11
21
|
|
|
12
22
|
### Added
|
data/README.md
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
[](https://rubygems.org/gems/philiprehberger-string_kit)
|
|
5
5
|
[](https://github.com/philiprehberger/rb-string-kit/commits/main)
|
|
6
6
|
|
|
7
|
+

|
|
8
|
+
|
|
7
9
|
Comprehensive string utilities without ActiveSupport dependency
|
|
8
10
|
|
|
9
11
|
## Requirements
|
|
@@ -91,6 +93,30 @@ Philiprehberger::StringKit.levenshtein('kitten', 'sitting') # => 3
|
|
|
91
93
|
Philiprehberger::StringKit.similarity('kitten', 'sitting') # => ~0.571
|
|
92
94
|
```
|
|
93
95
|
|
|
96
|
+
### Masking
|
|
97
|
+
|
|
98
|
+
```ruby
|
|
99
|
+
Philiprehberger::StringKit.mask('4242424242424242', show_last: 4) # => "************4242"
|
|
100
|
+
Philiprehberger::StringKit.mask('alice@example.com', show_first: 2, show_last: 4) # => "al***********.com"
|
|
101
|
+
Philiprehberger::StringKit.mask('password123', show_last: 3, mask_char: '#') # => "########123"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Between Delimiters
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
Philiprehberger::StringKit.between('hello [world] there', '[', ']') # => "world"
|
|
108
|
+
Philiprehberger::StringKit.between('a(b)c(d)', '(', ')') # => "b"
|
|
109
|
+
Philiprehberger::StringKit.between('no brackets here', '[', ']') # => nil
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Word-Aware Truncation
|
|
113
|
+
|
|
114
|
+
```ruby
|
|
115
|
+
Philiprehberger::StringKit.truncate_words('The quick brown fox jumps', 3) # => "The quick brown…"
|
|
116
|
+
Philiprehberger::StringKit.truncate_words('Two words', 5) # => "Two words"
|
|
117
|
+
Philiprehberger::StringKit.truncate_words('a b c d e', 2, omission: '...') # => "a b..."
|
|
118
|
+
```
|
|
119
|
+
|
|
94
120
|
## API
|
|
95
121
|
|
|
96
122
|
| Method | Description |
|
|
@@ -118,6 +144,9 @@ Philiprehberger::StringKit.similarity('kitten', 'sitting') # => ~0.571
|
|
|
118
144
|
| `.strip_zero_width(str)` | Remove zero-width and invisible Unicode characters |
|
|
119
145
|
| `.levenshtein(a, b)` | Edit distance between two strings |
|
|
120
146
|
| `.similarity(a, b)` | 0.0–1.0 similarity derived from Levenshtein distance |
|
|
147
|
+
| `StringKit.mask(str, show_first:, show_last:, mask_char:)` | Mask the middle of a string for partial obfuscation |
|
|
148
|
+
| `StringKit.between(str, left, right)` | Extract text between the first occurrence of two delimiters |
|
|
149
|
+
| `StringKit.truncate_words(str, max_words, omission:)` | Truncate to the first `max_words` words with an omission marker |
|
|
121
150
|
|
|
122
151
|
## Development
|
|
123
152
|
|
|
@@ -325,6 +325,62 @@ module Philiprehberger
|
|
|
325
325
|
1.0 - (levenshtein(a, b).to_f / max)
|
|
326
326
|
end
|
|
327
327
|
|
|
328
|
+
# Mask a string by replacing its middle portion with `mask_char`,
|
|
329
|
+
# leaving `show_first` characters at the start and `show_last` at the end.
|
|
330
|
+
# Returns `str` unchanged when there is not enough room to mask at least
|
|
331
|
+
# two characters in the middle.
|
|
332
|
+
#
|
|
333
|
+
# @param str [String]
|
|
334
|
+
# @param show_first [Integer] number of characters to leave visible at the start
|
|
335
|
+
# @param show_last [Integer] number of characters to leave visible at the end
|
|
336
|
+
# @param mask_char [String] character used to mask the hidden portion (default: '*')
|
|
337
|
+
# @return [String]
|
|
338
|
+
def self.mask(str, show_first: 0, show_last: 0, mask_char: '*')
|
|
339
|
+
validate!(str)
|
|
340
|
+
return str if show_first + show_last >= str.length - 1
|
|
341
|
+
|
|
342
|
+
masked_length = str.length - show_first - show_last
|
|
343
|
+
str[0, show_first] + (mask_char * masked_length) + str[str.length - show_last, show_last].to_s
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
# Returns the substring strictly between the first occurrence of `left`
|
|
347
|
+
# and the first occurrence of `right` after `left`. Returns `nil` when
|
|
348
|
+
# either delimiter is missing.
|
|
349
|
+
#
|
|
350
|
+
# @param str [String]
|
|
351
|
+
# @param left [String]
|
|
352
|
+
# @param right [String]
|
|
353
|
+
# @return [String, nil]
|
|
354
|
+
def self.between(str, left, right)
|
|
355
|
+
validate!(str)
|
|
356
|
+
left_index = str.index(left)
|
|
357
|
+
return nil if left_index.nil?
|
|
358
|
+
|
|
359
|
+
start_pos = left_index + left.length
|
|
360
|
+
right_index = str.index(right, start_pos)
|
|
361
|
+
return nil if right_index.nil?
|
|
362
|
+
|
|
363
|
+
str[start_pos...right_index]
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
# Truncate a string to the first `max_words` words. When truncation
|
|
367
|
+
# happens, append `omission` to the result. The string is unchanged
|
|
368
|
+
# when the word count is less than or equal to `max_words`.
|
|
369
|
+
#
|
|
370
|
+
# @param str [String]
|
|
371
|
+
# @param max_words [Integer] maximum number of words to keep (must be positive)
|
|
372
|
+
# @param omission [String] string appended when truncation occurs (default: '…')
|
|
373
|
+
# @return [String]
|
|
374
|
+
def self.truncate_words(str, max_words, omission: '…')
|
|
375
|
+
validate!(str)
|
|
376
|
+
raise Error, 'max_words must be a positive Integer' unless max_words.is_a?(Integer) && max_words.positive?
|
|
377
|
+
|
|
378
|
+
words = str.split(/\s+/).reject(&:empty?)
|
|
379
|
+
return str if words.length <= max_words
|
|
380
|
+
|
|
381
|
+
"#{words.first(max_words).join(' ')}#{omission}"
|
|
382
|
+
end
|
|
383
|
+
|
|
328
384
|
class << self
|
|
329
385
|
private
|
|
330
386
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: philiprehberger-string_kit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Philip Rehberger
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-05-31 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: String case conversion, slug generation, transliteration, padding, HTML
|
|
14
14
|
stripping, whitespace normalization, word counting, reading time estimation, excerpt
|