philiprehberger-string_kit 0.4.0 → 0.5.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 +5 -0
- data/README.md +16 -0
- data/lib/philiprehberger/string_kit/version.rb +1 -1
- data/lib/philiprehberger/string_kit.rb +37 -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: 72e46f8edcca8e48d21af91556ac0e4126553b361787cb01a3afd322a1549b1f
|
|
4
|
+
data.tar.gz: 0dfb5daf94589ea718e366fa58e4bee558a5bd082940b0cf6b32d5c6cd53226a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0a4f5c0b411e7f40dcb455e199c93bc6cda99486a7286ce80cc17ca82d499a8f502f14c879863de900abe241cd2980c2ba26bf29d90fe0bd97cc7b2271c44d94
|
|
7
|
+
data.tar.gz: 451a1b8a585ec486fee920b9ef1f881e8b8ff1a1a8c264d94f65acbcb0baea4e803063bb8ffca006b3a10b79f28cd013a0788f579e2aba98b40af69f9a6d2a96
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,11 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.5.0] - 2026-05-31
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `StringKit.word_wrap(str, width)` wraps a string to lines of at most `width` characters on word boundaries. Words longer than `width` are kept intact on their own line; existing newlines are honored as forced breaks. Raises `Error` for non-positive widths.
|
|
14
|
+
|
|
10
15
|
## [0.4.0] - 2026-05-30
|
|
11
16
|
|
|
12
17
|
### Added
|
data/README.md
CHANGED
|
@@ -117,6 +117,21 @@ Philiprehberger::StringKit.truncate_words('Two words', 5)
|
|
|
117
117
|
Philiprehberger::StringKit.truncate_words('a b c d e', 2, omission: '...') # => "a b..."
|
|
118
118
|
```
|
|
119
119
|
|
|
120
|
+
### Word Wrap
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
Philiprehberger::StringKit.word_wrap('The quick brown fox jumps over the lazy dog', 15)
|
|
124
|
+
# => "The quick brown\nfox jumps over\nthe lazy dog"
|
|
125
|
+
|
|
126
|
+
# Words longer than `width` are kept intact on their own line
|
|
127
|
+
Philiprehberger::StringKit.word_wrap('Supercalifragilistic words', 10)
|
|
128
|
+
# => "Supercalifragilistic\nwords"
|
|
129
|
+
|
|
130
|
+
# Existing newlines are honored as forced breaks
|
|
131
|
+
Philiprehberger::StringKit.word_wrap("one two\nthree four", 7)
|
|
132
|
+
# => "one two\nthree\nfour"
|
|
133
|
+
```
|
|
134
|
+
|
|
120
135
|
## API
|
|
121
136
|
|
|
122
137
|
| Method | Description |
|
|
@@ -147,6 +162,7 @@ Philiprehberger::StringKit.truncate_words('a b c d e', 2, omission: '...')
|
|
|
147
162
|
| `StringKit.mask(str, show_first:, show_last:, mask_char:)` | Mask the middle of a string for partial obfuscation |
|
|
148
163
|
| `StringKit.between(str, left, right)` | Extract text between the first occurrence of two delimiters |
|
|
149
164
|
| `StringKit.truncate_words(str, max_words, omission:)` | Truncate to the first `max_words` words with an omission marker |
|
|
165
|
+
| `StringKit.word_wrap(str, width)` | Wrap to lines of at most `width` characters on word boundaries |
|
|
150
166
|
|
|
151
167
|
## Development
|
|
152
168
|
|
|
@@ -381,6 +381,21 @@ module Philiprehberger
|
|
|
381
381
|
"#{words.first(max_words).join(' ')}#{omission}"
|
|
382
382
|
end
|
|
383
383
|
|
|
384
|
+
# Wrap the string to lines of at most `width` characters, breaking on
|
|
385
|
+
# word boundaries. Words longer than `width` are kept intact on their own
|
|
386
|
+
# line rather than split mid-word. Existing newlines are honored as
|
|
387
|
+
# forced breaks — each input line is wrapped independently.
|
|
388
|
+
#
|
|
389
|
+
# @param str [String]
|
|
390
|
+
# @param width [Integer] maximum line width (must be positive)
|
|
391
|
+
# @return [String] newline-joined wrapped lines
|
|
392
|
+
def self.word_wrap(str, width)
|
|
393
|
+
validate!(str)
|
|
394
|
+
raise Error, 'width must be a positive Integer' unless width.is_a?(Integer) && width.positive?
|
|
395
|
+
|
|
396
|
+
str.each_line.map { |line| wrap_line(line.chomp, width) }.join("\n")
|
|
397
|
+
end
|
|
398
|
+
|
|
384
399
|
class << self
|
|
385
400
|
private
|
|
386
401
|
|
|
@@ -396,6 +411,28 @@ module Philiprehberger
|
|
|
396
411
|
.downcase
|
|
397
412
|
.split
|
|
398
413
|
end
|
|
414
|
+
|
|
415
|
+
def wrap_line(line, width)
|
|
416
|
+
return '' if line.empty?
|
|
417
|
+
|
|
418
|
+
words = line.split(/\s+/).reject(&:empty?)
|
|
419
|
+
return '' if words.empty?
|
|
420
|
+
|
|
421
|
+
lines = []
|
|
422
|
+
current = +''
|
|
423
|
+
words.each do |word|
|
|
424
|
+
if current.empty?
|
|
425
|
+
current = word.dup
|
|
426
|
+
elsif current.length + 1 + word.length <= width
|
|
427
|
+
current << ' ' << word
|
|
428
|
+
else
|
|
429
|
+
lines << current
|
|
430
|
+
current = word.dup
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
lines << current unless current.empty?
|
|
434
|
+
lines.join("\n")
|
|
435
|
+
end
|
|
399
436
|
end
|
|
400
437
|
end
|
|
401
438
|
end
|
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.5.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-06-01 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
|