nicetitle 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d5b9964e2b7b9a8adce1471dc741fa73b203dbb2f93029514ccfd95f01cdcad
4
- data.tar.gz: 77a597415ea22bbc23083f9b772c97878dcce8fa1bcf2677aa68bd41bd64f8ef
3
+ metadata.gz: acdf70e5f6210b7dc71ec29abbe687d930fbf6545ac94b5497685efe0f9f7cfc
4
+ data.tar.gz: ef8ef1482ba1048733fbbf240c2265ecbd6541bb5d79874e76716616318494bb
5
5
  SHA512:
6
- metadata.gz: f46c3fc6be32ad7fefd0d32116b6a91635f74c700a305cf1c2e6d2ed084c931b757810b44557d2cccb2939b6e063295b7cc5d4ed35db107a53e1a6c1a573c7a2
7
- data.tar.gz: 8ece349e479c3034bf185264d1f0d4193d172d43f66d0d5d7d7faf959601459381ebd87efe5740c80de38dd5b0f20c1e480be8f5d47582cefd0ac2129fc681b6
6
+ metadata.gz: 95dbfaee5c62af9e4c91ead24e1b5c780747856c69f46452d61fd8bbcaae69ed21e405d74013173f1a1e5c7d65f4317dcc5d215eb4e146af73402cdd091e555a
7
+ data.tar.gz: '0591667493b91dc4947164dfbfe7750f379edfef3a2e4acc8365eb28257955f7316b06242eb5c532282aed09864a9c6016319469fd2554bc2e8518e497eb676f'
data/README.md CHANGED
@@ -17,7 +17,7 @@ The rule set is:
17
17
  - Words containing dots are not capitalized.
18
18
  - All caps sentences are down-cased before applying above mentioned rules.
19
19
 
20
- Check out the [test cases](https://github.com/evaneykelen/nicetitle/test/nicetitle_test.rb) for a detailed overview.
20
+ Check out the [test cases](https://github.com/evaneykelen/nicetitle/blob/master/test/nicetitle_test.rb) for a detailed overview.
21
21
 
22
22
  ## Installation
23
23
 
@@ -39,7 +39,7 @@ Or install it yourself as:
39
39
 
40
40
  Calling
41
41
 
42
- `Nicetitle::Titlecase.titlecase('What am I reading/listing/applying these days?')`
42
+ `Nice.title('What am I reading/listing/applying these days?')`
43
43
 
44
44
  outputs
45
45
 
data/lib/nicetitle.rb CHANGED
@@ -1,2 +1,8 @@
1
1
  require "nicetitle/version"
2
2
  require "nicetitle/titlecase"
3
+
4
+ class Nice
5
+ def self.title(str)
6
+ Nicetitle::Titlecase.titlecase(str)
7
+ end
8
+ end
@@ -0,0 +1,70 @@
1
+ module Nicetitle
2
+ class Titlecase
3
+ SMALL_WORDS = Regexp.new('\b(a(nd|n|s|t)?|b(ut|y)|en|for|i(f|n)|o(f|n|r)|t(he|o)|vs?\.?)\b').freeze
4
+
5
+ def self.is_small_word?(word)
6
+ SMALL_WORDS.match(word)
7
+ end
8
+
9
+ # "__foo" => "__Foo"
10
+ def self.upcase_first_real_letter(word)
11
+ word.sub(/[a-zA-Z0-9]/, &:upcase)
12
+ end
13
+
14
+ # step-by-step => Step-by-Step
15
+ def self.upcase_word_with_dashes(word)
16
+ word.split('-').map { |part| is_small_word?(part) ? part : part.capitalize }.join("-")
17
+ end
18
+
19
+ # before/after => Before/After
20
+ def self.upcase_word_with_slashes(word)
21
+ word.split('/').map(&:capitalize).join('/')
22
+ end
23
+
24
+ def self.titlecase(str)
25
+ # Replace tabs by single space
26
+ # Replace weird spaces by regular space
27
+ str = (str || '').gsub(/\t/, ' ').gsub("\u{2011}", ' ').strip
28
+ return '' if str.empty?
29
+
30
+ # Downcase an all-upcase sentence
31
+ str.downcase! if str.scan(/[A-Z]|\s|\W/).length == str.length
32
+
33
+ # Split sentence at space boundaries
34
+ word_arr = str.split(' ')
35
+
36
+ # Initialize operand array
37
+ operand_arr = Array.new(word_arr.size, :capitalize)
38
+
39
+ word_arr.each_with_index do |word, idx|
40
+ # Don't capitalize small words...
41
+ # ... unless it's first word
42
+ # ... unless it's last word
43
+ # ... unless word is preceded by word ending with colon
44
+ operand_arr[idx] = :do_not_upcase if idx != 0 && idx != word_arr.size - 1 && is_small_word?(word) && word_arr[idx - 1][-1] != ':'
45
+ # Don't simply capitalize first letter if word starts with (, _, ', or "
46
+ operand_arr[idx] = :upcase_later if word[0].match(/\(|_|'|"/)
47
+ # Capitalize first letter and letters preceded by -
48
+ operand_arr[idx] = :upcase_dashed if word.count('-') > 0
49
+ # Capitalize letters preceded by / inside word
50
+ operand_arr[idx] = :upcase_slashed if word[1..].count('/') > 0
51
+ # Don't capitalize word if it starts with /
52
+ operand_arr[idx] = :do_not_upcase if word[0] == '/'
53
+ # Don't capitalize URLs
54
+ operand_arr[idx] = :do_not_upcase if word.match(/https?:\/\//i)
55
+ # Don't capitalize words containing capitals besides first letter
56
+ # Don't capitalize words containing dots inside word
57
+ operand_arr[idx] = :do_not_upcase if word[1..].match(/[A-Z]/) || word[1..-3].match(/\.|&/)
58
+ end
59
+
60
+ word_arr.each_with_index do |word, idx|
61
+ word_arr[idx] = upcase_first_real_letter(word) if operand_arr[idx] == :upcase_later
62
+ word_arr[idx] = upcase_word_with_dashes(word) if operand_arr[idx] == :upcase_dashed
63
+ word_arr[idx] = upcase_word_with_slashes(word) if operand_arr[idx] == :upcase_slashed
64
+ word_arr[idx] = word.capitalize if operand_arr[idx] == :capitalize
65
+ end
66
+
67
+ word_arr.join(' ')
68
+ end
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module Nicetitle
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nicetitle
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik van Eykelen
@@ -60,7 +60,6 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
- - ".travis.yml"
64
63
  - Gemfile
65
64
  - LICENSE.txt
66
65
  - README.md
@@ -68,6 +67,7 @@ files:
68
67
  - bin/console
69
68
  - bin/setup
70
69
  - lib/nicetitle.rb
70
+ - lib/nicetitle/titlecase.rb
71
71
  - lib/nicetitle/version.rb
72
72
  - nicetitle.gemspec
73
73
  homepage: https://www.msgtrail.com/articles/20190525-1530-title-casing-is-harder-than-i-thought/
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.6.1
7
- before_install: gem install bundler -v 2.0.1