fat_core 4.0.0 → 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1a7476217f8c6438e15010dcb43a6b3e5a7fbdc3
4
- data.tar.gz: 99d35b1291dfa588bef9fbed7185e82c12e6b8ad
3
+ metadata.gz: 22ba4ebccef0d93de997d4084f88b34d7f7c610a
4
+ data.tar.gz: 57c2850907b46711546e663dd55f06388b4e966d
5
5
  SHA512:
6
- metadata.gz: 003b3ab76e3d11011b7f67504ea0422d1e34cb8c4f32606a4f0b28e216416d13f18d5e713c78ca30200ded965076e2593b75ff80b0002ff35fffc5abdf487cb9
7
- data.tar.gz: 901ad06b503df0199ef15ccf46232d2552cd8ae0eb76bbb0b29da4e4e7036a82b274d5be3b07587917116d2450ae608a877714a3dcce0ca4d03b8658083f1ba6
6
+ metadata.gz: ba2ac468ff8870868af83f45385892fbdfce0d5efdd8e5aa1c499a6f88cb4ed7e85f56f082c568e47909e361cf595aad7aa4653349202f4745225adb8df22c77
7
+ data.tar.gz: 495d4b303977ff82d02c6ee476f8f1d0ea0d4ac176984a9653c3ac0248cbc738c3bab4aa76692917632cb652e6ac7d5d0a6ab2ad6583ac72976663f4c1c8ae0a
data/fat_core.gemspec CHANGED
@@ -32,6 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency 'redcarpet'
33
33
 
34
34
  spec.add_runtime_dependency 'activesupport'
35
- spec.add_runtime_dependency 'erubis'
36
35
  spec.add_runtime_dependency 'damerau-levenshtein'
37
36
  end
@@ -110,6 +110,21 @@ module FatCore
110
110
  ::Date.new($1.to_i, $2.to_i, $3.to_i) if self =~ %r{(\d\d\d\d)[-/]?(\d\d?)[-/]?(\d\d?)}
111
111
  end
112
112
 
113
+ UPPERS = ('A'..'Z').to_a
114
+
115
+ private
116
+
117
+ def upper?
118
+ UPPERS.include?(self[0])
119
+ end
120
+
121
+ # Return true if all the letters in self are upper case
122
+ def all_upper?
123
+ tr('^A-Za-z', '').split('').all? {|c| ('A'..'Z').to_a.include? c}
124
+ end
125
+
126
+ public
127
+
113
128
  # Return self capitalized according to the conventions for capitalizing
114
129
  # titles of books or articles. Tries to follow the rules of the University
115
130
  # of Chicago's *A Manual of Style*, Section 7.123, except to the extent that
@@ -132,6 +147,7 @@ module FatCore
132
147
  def entitle
133
148
  little_words = %w[a an the at for up and but
134
149
  or nor in on under of from as by to]
150
+ preserve_acronyms = !all_upper?
135
151
  newwords = []
136
152
  capitalize_next = false
137
153
  words = split(/\s+/)
@@ -164,7 +180,7 @@ module FatCore
164
180
  elsif w =~ /^[^aeiouy]*$/i && w.size > 2
165
181
  # All consonants and at least 3 chars, probably abbr
166
182
  newwords.push(w.upcase)
167
- elsif w =~ /^[A-Z0-9]+\z/
183
+ elsif w =~ /^[A-Z0-9]+\z/ && preserve_acronyms
168
184
  # All uppercase and numbers, keep as is
169
185
  newwords.push(w)
170
186
  elsif w =~ /^(\w+)-(\w+)$/i
@@ -307,34 +323,74 @@ module FatCore
307
323
  end
308
324
 
309
325
  # If the string is a valid number, return a string that adds grouping commas
310
- # to the whole number part; otherwise, return self.
326
+ # to the whole number part; otherwise, return self. Round the number to the
327
+ # given number places after the decimal if places is positive; round to the
328
+ # left of the decimal if places is negative. Pad with zeroes on the right
329
+ # for positive places, on the left for negative places.
311
330
  #
312
331
  # @example
313
- # 'hello'.commas #=> 'hello'
314
- # '+4654656.33e66'.commas #=> '+4,654,656.33e66'
332
+ # 'hello'.commas #=> 'hello'
333
+ # '+4654656.33e66'.commas #=> '+4,654,656.33e66'
334
+ # '6789345612.14'.commas(-5) #=> '6,789,350,000'
335
+ # '6543.14'.commas(5) #=> '6,543.14000'
315
336
  #
316
337
  # @return [String] self if not a valid number
317
338
  # @return [String] commified number as a String
318
339
  def commas(places = nil)
319
340
  numeric_re = /\A([-+])?([\d_]*)((\.)?([\d_]*))?([eE][+-]?[\d_]+)?\z/
320
341
  return self unless clean =~ numeric_re
321
-
322
- # Round if places given
323
- num = BigDecimal(self)
324
- str =
325
- if places.nil?
326
- num.whole? ? num.to_i.to_s : num.to_f.to_s
327
- else
328
- num.to_f.round(places).to_s
329
- end
330
-
331
- # Break the number into parts
332
- str =~ numeric_re
333
342
  sig = $1 || ''
334
343
  whole = $2 ? $2.delete('_') : ''
335
344
  frac = $5 || ''
336
345
  exp = $6 || ''
337
346
 
347
+ # Round frac or whole if places given. For positve places, round fraction
348
+ # to that many places; for negative, round the whole-number part to the
349
+ # absolute value of places left of the decimal.
350
+ if places
351
+ new_frac = frac.dup
352
+ new_whole = whole.dup
353
+ if places.zero?
354
+ new_frac = ''
355
+ elsif places.positive? && places < frac.length
356
+ new_frac = frac[0...places - 1]
357
+ new_frac[places - 1] =
358
+ if frac[places].to_i >= 5
359
+ (frac[places - 1].to_i + 1).to_s
360
+ else
361
+ frac[places - 1]
362
+ end
363
+ elsif places >= frac.length
364
+ new_frac = frac + '0' * (places - frac.length)
365
+ else
366
+ # Negative places, round whole to places.abs from decimal
367
+ places = places.abs
368
+ if places > whole.length
369
+ lead = whole[0].to_i >= 5 ? '1' : '0'
370
+ new_whole[0] = places == whole.length + 1 ? lead : '0'
371
+ new_whole[1..-1] = '0' * (places - 1)
372
+ new_frac = ''
373
+ elsif places > 1
374
+ target = whole.length - places
375
+ new_whole[target] =
376
+ if whole[target + 1].to_i >= 5
377
+ (whole[target].to_i + 1).to_s
378
+ else
379
+ whole[target]
380
+ end
381
+ new_whole[target + 1..whole.length - 1] =
382
+ '0' * (whole.length - target - 1)
383
+ new_frac = ''
384
+ else
385
+ # Rounding to 1 place, therefore, no rounding
386
+ new_frac = ''
387
+ new_whole = whole
388
+ end
389
+ end
390
+ frac = new_frac
391
+ whole = new_whole
392
+ end
393
+
338
394
  # Place the commas in the whole part only
339
395
  whole = whole.reverse
340
396
  whole.gsub!(/([0-9]{3})/, '\\1,')
@@ -1,6 +1,6 @@
1
1
  module FatCore
2
2
  MAJOR = 4
3
- MINOR = 0
3
+ MINOR = 1
4
4
  PATCH = 0
5
5
 
6
6
  # FatCore version number
@@ -1,3 +1,4 @@
1
+ require 'spec_helper'
1
2
  require 'fat_core/string'
2
3
  require 'fat_core/date'
3
4
 
@@ -76,8 +77,34 @@ the people, for the people, shall not perish from the earth."
76
77
  expect('20140521.556'.commas).to eq '20,140,521.556'
77
78
  expect('20140521.556'.commas(2)).to eq '20,140,521.56'
78
79
  expect('-20140521.556'.commas).to eq '-20,140,521.556'
79
- expect('+20140521.556'.commas).to eq '20,140,521.556'
80
- expect('+20140521.556e3'.commas).to eq '20,140,521,556'
80
+ expect('+20140521.556'.commas).to eq '+20,140,521.556'
81
+ expect('+20140521.556e3'.commas).to eq '+20,140,521.556e3'
82
+ expect('3.141591828'.commas(11)).to eq '3.14159182800'
83
+ expect('3.141591828'.commas(10)).to eq '3.1415918280'
84
+ expect('3.141591828'.commas(9)).to eq '3.141591828'
85
+ expect('3.141591828'.commas(8)).to eq '3.14159183'
86
+ expect('3.141591828'.commas(7)).to eq '3.1415918'
87
+ expect('3.141591828'.commas(6)).to eq '3.141592'
88
+ expect('3.141591828'.commas(5)).to eq '3.14159'
89
+ expect('3.141591828'.commas(4)).to eq '3.1416'
90
+ expect('3.141591828'.commas(3)).to eq '3.142'
91
+ expect('3.141591828'.commas(2)).to eq '3.14'
92
+ expect('3.141591828'.commas(1)).to eq '3.1'
93
+ expect('3.141591828'.commas(0)).to eq '3'
94
+ expect('3.14'.commas(5)).to eq '3.14000'
95
+ expect('6789345612.14'.commas).to eq '6,789,345,612.14'
96
+ expect('6789345612.14'.commas(-1)).to eq '6,789,345,612'
97
+ expect('6789345612.14'.commas(-2)).to eq '6,789,345,610'
98
+ expect('6789345612.14'.commas(-3)).to eq '6,789,345,600'
99
+ expect('6789345612.14'.commas(-4)).to eq '6,789,346,000'
100
+ expect('6789345612.14'.commas(-5)).to eq '6,789,350,000'
101
+ expect('6789345612.14'.commas(-6)).to eq '6,789,300,000'
102
+ expect('6789345612.14'.commas(-7)).to eq '6,789,000,000'
103
+ expect('6789345612.14'.commas(-8)).to eq '6,790,000,000'
104
+ expect('6789345612.14'.commas(-9)).to eq '6,800,000,000'
105
+ expect('6789345612.14'.commas(-10)).to eq '7,000,000,000'
106
+ expect('6789345612.14'.commas(-11)).to eq '10,000,000,000'
107
+ expect('6789345612.14'.commas(-12)).to eq '000,000,000,000'
81
108
  end
82
109
 
83
110
  it 'should be able to convert a digital date to a Date' do
@@ -168,12 +195,14 @@ the people, for the people, shall not perish from the earth."
168
195
  # Don't capitalize ordinals
169
196
  expect('22nd of september'.entitle).to eq('22nd of September')
170
197
  # Capitalize common abbrevs
171
- expect('US BANK'.entitle).to eq('US BANK')
198
+ expect('Us Bank'.entitle).to eq('US Bank')
172
199
  expect('nw territory'.entitle).to eq('NW Territory')
173
200
  # Leave word starting with numbers alone
174
201
  expect('apartment 33-B'.entitle).to eq('Apartment 33-B')
175
- # Assume all consanants is an acronym
176
- expect('the cbs network'.entitle).to eq('The CBS Network')
202
+ # Assume all uppercase is an acronym
203
+ expect('the ABC network'.entitle).to eq('The ABC Network')
204
+ # But not if the whole string is uppercase
205
+ expect('THE ABC NETWORK'.entitle).to eq('The Abc Network')
177
206
  # Capitalize both parts of a hyphenated word
178
207
  expect('the hitler-stalin pact'.entitle).to eq('The Hitler-Stalin Pact')
179
208
  end
data/spec/spec_helper.rb CHANGED
@@ -5,9 +5,8 @@ SimpleCov.start
5
5
  require 'bundler/setup'
6
6
  Bundler.setup
7
7
 
8
- # Note: I now include these in my global ~/.rspec file, so no need for them
9
- # here.
10
- # require 'pry' require 'byebug'
8
+ require 'pry'
9
+ require 'pry-byebug'
11
10
 
12
11
  # This file was generated by the `rspec --init` command. Conventionally, all
13
12
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel E. Doherty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-18 00:00:00.000000000 Z
11
+ date: 2017-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simplecov
@@ -150,20 +150,6 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
- - !ruby/object:Gem::Dependency
154
- name: erubis
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- version: '0'
160
- type: :runtime
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- version: '0'
167
153
  - !ruby/object:Gem::Dependency
168
154
  name: damerau-levenshtein
169
155
  requirement: !ruby/object:Gem::Requirement