titlecaser 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 60a6c2ed4a614a61dd007694b6cd0126dfa7072936c624fc04514445d42c109c
4
+ data.tar.gz: f76fdbff55626512d3b6ae935b72f05c6304451d973b4f4c05c6df3b2eab1eb0
5
+ SHA512:
6
+ metadata.gz: 2bec1fbdeea30e2ed6fac919fd51adcf1953d91864bcd80585895fef2355f627fe95572a50b262acfedbbc8982f95c9cb38378c350db0d44bb6dbb7eaaaa82a3
7
+ data.tar.gz: 9f8b3d68de8ec7af2a39b8a172d5f6e36dd0da4908cb9aaffe9a734d95cea3658404ed5a93a7d84d20c9e2ec8b2509018797126272dd48bb928d10de270b33bc
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Peter Cooper
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Titlecaser
2
+
3
+
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add titlecaser
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install titlecaser
14
+
15
+ ## Usage
16
+
17
+ Convenience method:
18
+
19
+ ```ruby
20
+ Titlecaser.titlecase("this is a test") # => "This is a Test"
21
+ ```
22
+
23
+ Or in a more longwinded way:
24
+
25
+ ```ruby
26
+ Titlecaser::TitleCase.new("this is a test").convert
27
+ ```
28
+
29
+ Currently, we don't monkeypatch `String` but that could be an option later.
30
+
31
+ ## Tests
32
+
33
+ Note that there are tests in `/test` and a `titles.txt` file that's used to test a variety of title scenarios. These tests can be extended.
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/peterc/titlecaser. However, please note this library has been developed to fit our particular style guide, so changes to specific title-casing choices may be rejected or questioned. We could extend the library to support multiple approaches in future, by way of non-default options, however.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ task default: %i[]
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "test"
9
+ t.libs << "lib"
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ end
12
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Titlecaser
4
+ VERSION = "0.1.0"
5
+ end
data/lib/titlecaser.rb ADDED
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "titlecaser/version"
4
+
5
+ module Titlecaser
6
+ class TitleCase
7
+ def initialize(string)
8
+ @string = string
9
+ end
10
+
11
+ def convert
12
+ words = @string.split(' ')
13
+ previous_word = nil
14
+ converted_words = words.map.with_index do |word, index|
15
+ # Take any initial punctuation character (e.g. quotes) and store it to rejoin later
16
+ starting_punctuation = word[/\A[^\p{L}0-9]/].to_s
17
+
18
+ # Take the rest of the word
19
+ word = word[starting_punctuation.length..-1]
20
+
21
+ # If it's already capitalized and NOT a minor word, leave everything as-is
22
+ if already_capitalized?(word) && !minor_word?(word)
23
+ word = starting_punctuation + word
24
+ else
25
+ # Otherwise, determine if it's a major word and handle appropriately
26
+ if major_word?(word, index, previous_word)
27
+ word = starting_punctuation + capitalize_word(word)
28
+ else
29
+ # Force a downcase if need be
30
+ word = starting_punctuation + word.downcase
31
+ end
32
+ end
33
+
34
+ # Store the word for the next iteration, as it may have useful trailing punctuation
35
+ previous_word = word
36
+ end
37
+
38
+ # Put it all back together
39
+ converted_words.join(' ')
40
+ end
41
+
42
+ private
43
+
44
+ def already_capitalized?(word)
45
+ word[0] == word[0].upcase
46
+ end
47
+
48
+ def major_word?(word, index, previous_word)
49
+ return true if index.zero? || word.length > 4
50
+
51
+ # Also return true if the word follows a colon,
52
+ # full stop, or question mark, ignoring any whitespace,
53
+ # as these essentially 'restart' the sentence.
54
+ # e.g. "blah: blah" should become "Blah: Blah"
55
+ return true if previous_word && previous_word.match?(/[:.?!]\z/)
56
+
57
+ !minor_word?(word)
58
+ end
59
+
60
+ def minor_word?(word)
61
+ %w[and as but for if nor or so yet a an the at by for in is of off on per to up via with].include?(word.downcase)
62
+ end
63
+
64
+ def capitalize_word(word)
65
+ word.capitalize
66
+ end
67
+ end
68
+
69
+ def self.titlecase(string)
70
+ TitleCase.new(string).convert
71
+ end
72
+ end
@@ -0,0 +1,4 @@
1
+ module Titlecaser
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: titlecaser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Cooper
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Uses standard title casing rules to titleize strings
14
+ email:
15
+ - git@peterc.org
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - Rakefile
23
+ - lib/titlecaser.rb
24
+ - lib/titlecaser/version.rb
25
+ - sig/titlecaser.rbs
26
+ homepage: https://github.com/peterc/titlecaser
27
+ licenses: []
28
+ metadata:
29
+ homepage_uri: https://github.com/peterc/titlecaser
30
+ source_code_uri: https://github.com/peterc/titlecaser
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 3.0.0
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubygems_version: 3.5.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Uses standard title casing rules to titleize strings
50
+ test_files: []