fat_core 4.12.0 → 4.12.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +21 -15
- data/fat_core.gemspec +9 -5
- data/lib/fat_core/string.rb +6 -67
- data/lib/fat_core/version.rb +1 -1
- data/spec/lib/array_spec.rb +1 -1
- data/spec/lib/bigdecimal_spec.rb +1 -1
- data/spec/lib/date_spec.rb +708 -709
- data/spec/lib/enumerable_spec.rb +8 -8
- data/spec/lib/hash_spec.rb +3 -3
- data/spec/lib/kernel_spec.rb +1 -1
- data/spec/lib/nil_spec.rb +3 -3
- data/spec/lib/numeric_spec.rb +10 -10
- data/spec/lib/range_spec.rb +39 -39
- data/spec/lib/string_spec.rb +137 -129
- data/spec/lib/symbol_spec.rb +3 -3
- metadata +10 -5
data/spec/lib/string_spec.rb
CHANGED
@@ -1,11 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'fat_core/string'
|
3
5
|
require 'fat_core/date'
|
4
6
|
|
5
7
|
describe String do
|
6
|
-
|
7
|
-
|
8
|
-
|
8
|
+
describe 'class methods' do
|
9
|
+
it 'generates a random string' do
|
10
|
+
ss = described_class.random(100)
|
11
|
+
expect(ss.class).to eq String
|
12
|
+
expect(ss.size).to eq 100
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'instance methods' do
|
17
|
+
describe 'wrapping' do
|
18
|
+
#23456789012345678901234567890123456789012345678901234567890123456789|
|
19
|
+
let(:getty) {"\
|
9
20
|
Four score and seven years ago our fathers brought forth on this conti\
|
10
21
|
nent a new nation, conceived in liberty, and dedicated to the proposit\
|
11
22
|
ion that all men are created equal. Now we are engaged in a great civ\
|
@@ -26,88 +37,114 @@ red dead we take increased devotion to that cause for which they gave \
|
|
26
37
|
the last full measure of devotion--that we here highly resolve that th\
|
27
38
|
ese dead shall not have died in vain--that this nation, under God, sha\
|
28
39
|
ll have a new birth of freedom--and that government of the people, by \
|
29
|
-
the people, for the people, shall not perish from the earth."
|
40
|
+
the people, for the people, shall not perish from the earth."}
|
30
41
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
42
|
+
# 0000000000111111111122222222223333333333444444444455555555556666666666
|
43
|
+
# 0123456789012345678901234567890123456789012345678901234567890123456789|
|
44
|
+
# Four score and seven years ago our fathers brought forth on
|
45
|
+
# this continent a new nation, conceived in liberty, and
|
46
|
+
# dedicated to the proposition that all men are created
|
47
|
+
# equal. Now we are engaged in a great civil war, testing
|
48
|
+
# whether that nation, or any nation, so conceived and
|
49
|
+
# so dedicated, can long endure. We are met on a great
|
50
|
+
# battle-field of that war. We have come to dedicate a
|
51
|
+
# portion of that field, as a final resting place for
|
52
|
+
# those who here gave their lives that that nation might
|
53
|
+
# live. It is altogether fitting and proper that we should
|
54
|
+
# do this. But, in a larger sense, we can not dedicate,
|
55
|
+
# we can not consecrate, we can not hallow this ground.
|
56
|
+
# The brave men, living and dead, who struggled here,
|
57
|
+
# have consecrated it, far above our poor power to add
|
58
|
+
# or detract. The world will little note, nor long remember
|
59
|
+
# what we say here, but it can never forget what they
|
60
|
+
# did here. It is for us the living, rather, to be dedicated
|
61
|
+
# here to the unfinished work which they who fought here
|
62
|
+
# have thus far so nobly advanced. It is rather for us
|
63
|
+
# to be here dedicated to the great task remaining before
|
64
|
+
# us--that from these honored dead we take increased devotion
|
65
|
+
# to that cause for which they gave the last full measure
|
66
|
+
# of devotion--that we here highly resolve that these dead
|
67
|
+
# shall not have died in vain--that this nation, under
|
68
|
+
# God, shall have a new birth of freedom--and that government
|
69
|
+
# of the people, by the people, for the people, shall
|
70
|
+
# not perish from the earth.
|
61
71
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
72
|
+
it 'wraps a short string' do
|
73
|
+
expect('hello, world'.wrap).to eq 'hello, world'
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'wraps a long string' do
|
77
|
+
str = getty.wrap
|
78
|
+
str.split("\n").each { |l| expect(l.length).to be <= 70 }
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'wraps a long string with a hangining indent' do
|
82
|
+
str = getty.wrap(70, 10)
|
83
|
+
str.split("\n").each { |l| expect(l.length).to be <= 70 }
|
84
|
+
str.split("\n")[1..-1].each do |l|
|
85
|
+
expect(l).to match(/^ /)
|
86
|
+
end
|
87
|
+
second_line = ' ' * 10 + 'this continent a new nation'
|
88
|
+
third_line = ' ' * 10 + 'dedicated to the proposition'
|
89
|
+
twenty_fourth_line = ' ' * 10 + 'shall not have died in vain'
|
90
|
+
expect(str.split("\n")[1]).to match(/^#{second_line}/)
|
91
|
+
expect(str.split("\n")[2]).to match(/^#{third_line}/)
|
92
|
+
expect(str.split("\n")[23]).to match(/^#{twenty_fourth_line}/)
|
93
|
+
end
|
67
94
|
end
|
68
|
-
end
|
69
95
|
|
70
|
-
|
71
|
-
it 'should be able to clean up white space in a string' do
|
96
|
+
it 'cleans up white space in a string' do
|
72
97
|
expect(' string here '.clean).to eq 'string here'
|
73
98
|
end
|
74
99
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
100
|
+
describe 'numeric strings' do
|
101
|
+
it 'converts a numeric string to a commified form' do
|
102
|
+
expect('20140521'.commas).to eq '20,140,521'
|
103
|
+
expect('20140521.556'.commas).to eq '20,140,521.556'
|
104
|
+
expect('20140521.556'.commas(2)).to eq '20,140,521.56'
|
105
|
+
expect('-20140521.556'.commas).to eq '-20,140,521.556'
|
106
|
+
expect('+20140521.556'.commas).to eq '20,140,521.556'
|
107
|
+
expect('+20140521.556e3'.commas).to eq '20,140,521,556'
|
108
|
+
expect('3.141591828'.commas(11)).to eq '3.14159182800'
|
109
|
+
expect('3.141591828'.commas(10)).to eq '3.1415918280'
|
110
|
+
expect('3.141591828'.commas(9)).to eq '3.141591828'
|
111
|
+
expect('3.141591828'.commas(8)).to eq '3.14159183'
|
112
|
+
expect('3.141591828'.commas(7)).to eq '3.1415918'
|
113
|
+
expect('3.141591828'.commas(6)).to eq '3.141592'
|
114
|
+
expect('3.141591828'.commas(5)).to eq '3.14159'
|
115
|
+
expect('3.141591828'.commas(4)).to eq '3.1416'
|
116
|
+
expect('3.141591828'.commas(3)).to eq '3.142'
|
117
|
+
expect('3.141591828'.commas(2)).to eq '3.14'
|
118
|
+
expect('3.141591828'.commas(1)).to eq '3.1'
|
119
|
+
expect('3.141591828'.commas(0)).to eq '3'
|
120
|
+
expect('3.14'.commas(5)).to eq '3.14000'
|
121
|
+
expect('6789345612.14'.commas).to eq '6,789,345,612.14'
|
122
|
+
expect('6789345612.14'.commas(-1)).to eq '6,789,345,610'
|
123
|
+
expect('6789345612.14'.commas(-2)).to eq '6,789,345,600'
|
124
|
+
expect('6789345612.14'.commas(-3)).to eq '6,789,346,000'
|
125
|
+
expect('6789345612.14'.commas(-4)).to eq '6,789,350,000'
|
126
|
+
expect('6789345612.14'.commas(-5)).to eq '6,789,300,000'
|
127
|
+
expect('6789345612.14'.commas(-6)).to eq '6,789,000,000'
|
128
|
+
expect('6789345612.14'.commas(-7)).to eq '6,790,000,000'
|
129
|
+
expect('6789345612.14'.commas(-8)).to eq '6,800,000,000'
|
130
|
+
expect('6789345612.14'.commas(-9)).to eq '7,000,000,000'
|
131
|
+
expect('6789345612.14'.commas(-10)).to eq '10,000,000,000'
|
132
|
+
expect('6789345612.14'.commas(-11)).to eq '0'
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'determines if it\'s a valid number' do
|
136
|
+
expect('88'.number?).to be true
|
137
|
+
expect('-88'.number?).to be true
|
138
|
+
expect('8.008'.number?).to be true
|
139
|
+
expect('-8.008'.number?).to be true
|
140
|
+
expect('8.008e33'.number?).to be true
|
141
|
+
expect('-8.008e33'.number?).to be true
|
142
|
+
expect('0x8.008'.number?).to be false
|
143
|
+
expect('hello world'.number?).to be false
|
144
|
+
end
|
108
145
|
end
|
109
146
|
|
110
|
-
it '
|
147
|
+
it 'converts a digital date to a Date' do
|
111
148
|
expect('20140521'.as_date.iso).to eq '2014-05-21'
|
112
149
|
expect('2014-05-21'.as_date.iso).to eq '2014-05-21'
|
113
150
|
expect('2014/05/21'.as_date.iso).to eq '2014-05-21'
|
@@ -115,41 +152,7 @@ the people, for the people, shall not perish from the earth."
|
|
115
152
|
expect('201X/5/21'.as_date).to be_nil
|
116
153
|
end
|
117
154
|
|
118
|
-
it '
|
119
|
-
expect('hello, world'.wrap).to eq 'hello, world'
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'should wrap a long string' do
|
123
|
-
str = @getty.wrap
|
124
|
-
str.split("\n").each { |l| expect(l.length).to be <= 70 }
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'should wrap a long string with a hangining indent' do
|
128
|
-
str = @getty.wrap(70, 10)
|
129
|
-
str.split("\n").each { |l| expect(l.length).to be <= 70 }
|
130
|
-
str.split("\n")[1..-1].each do |l|
|
131
|
-
expect(l).to match(/^ /)
|
132
|
-
end
|
133
|
-
second_line = ' ' * 10 + 'this continent a new nation'
|
134
|
-
third_line = ' ' * 10 + 'dedicated to the proposition'
|
135
|
-
twenty_fourth_line = ' ' * 10 + 'shall not have died in vain'
|
136
|
-
expect(str.split("\n")[1]).to match(/^#{second_line}/)
|
137
|
-
expect(str.split("\n")[2]).to match(/^#{third_line}/)
|
138
|
-
expect(str.split("\n")[23]).to match(/^#{twenty_fourth_line}/)
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'should be able to determine is it\'s a valid number' do
|
142
|
-
expect('88'.number?).to be true
|
143
|
-
expect('-88'.number?).to be true
|
144
|
-
expect('8.008'.number?).to be true
|
145
|
-
expect('-8.008'.number?).to be true
|
146
|
-
expect('8.008e33'.number?).to be true
|
147
|
-
expect('-8.008e33'.number?).to be true
|
148
|
-
expect('0x8.008'.number?).to be false
|
149
|
-
expect('hello world'.number?).to be false
|
150
|
-
end
|
151
|
-
|
152
|
-
it 'should be able to convert a string to a regular expression' do
|
155
|
+
it 'converts a string to a regular expression' do
|
153
156
|
# Ignores case by default
|
154
157
|
re = "/hello((\s+)(world))?/".as_regexp
|
155
158
|
expect(re.class).to eq Regexp
|
@@ -172,11 +175,11 @@ the people, for the people, shall not perish from the earth."
|
|
172
175
|
expect(re.multiline?).to be false
|
173
176
|
expect(re.match?('hello world')).to be false
|
174
177
|
expect(re.match?(str)).to be true
|
175
|
-
expect('Hello\b'.as_regexp).to eq
|
176
|
-
expect('Hello'.as_regexp).to eq
|
178
|
+
expect('Hello\b'.as_regexp).to eq(/Hello\\b/)
|
179
|
+
expect('Hello'.as_regexp).to eq(/Hello/)
|
177
180
|
end
|
178
181
|
|
179
|
-
it '
|
182
|
+
it 'converts metacharacters into Regexp' do
|
180
183
|
"\\$()*+.<>?[]^{|}".chars.each do |c|
|
181
184
|
re = c.as_regexp
|
182
185
|
expect(re.class).to eq Regexp
|
@@ -184,21 +187,20 @@ the people, for the people, shall not perish from the earth."
|
|
184
187
|
end
|
185
188
|
end
|
186
189
|
|
187
|
-
it '
|
190
|
+
it 'converts itself to a sym' do
|
188
191
|
expect('joke'.as_sym).to eq :joke
|
189
192
|
expect('hello world'.as_sym).to eq :hello_world
|
190
193
|
expect("hello world it's me".as_sym).to eq :hello_world_its_me
|
191
194
|
expect('Street1'.as_sym).to eq :street1
|
192
195
|
end
|
193
196
|
|
194
|
-
it '
|
197
|
+
it 'does nothing in response to as_string' do
|
195
198
|
expect('joke'.as_string).to eq 'joke'
|
196
199
|
expect('hello world'.as_string).to eq 'hello world'
|
197
|
-
expect("hello world it's me".as_string)
|
198
|
-
.to eq "hello world it's me"
|
200
|
+
expect("hello world it's me".as_string).to eq "hello world it's me"
|
199
201
|
end
|
200
202
|
|
201
|
-
it '
|
203
|
+
it 'properly capitalizes a string as a title' do
|
202
204
|
# Capitalize little words only at beginning and end
|
203
205
|
expect('the cat in the hat'.entitle).to eq('The Cat in the Hat')
|
204
206
|
expect('dr'.entitle).to eq('Dr')
|
@@ -225,7 +227,7 @@ the people, for the people, shall not perish from the earth."
|
|
225
227
|
expect('the hitler-stalin pact'.entitle).to eq('The Hitler-Stalin Pact')
|
226
228
|
end
|
227
229
|
|
228
|
-
it '
|
230
|
+
it 'computes its Levenshtein distance from another string' do
|
229
231
|
expect('Something'.distance('Smoething')).to eq 1
|
230
232
|
expect('Something'.distance('meSothing')).to eq 4
|
231
233
|
expect('SomethingElse'.distance('Encyclopedia')).to eq 11
|
@@ -235,7 +237,7 @@ the people, for the people, shall not perish from the earth."
|
|
235
237
|
end
|
236
238
|
|
237
239
|
describe 'Quoting' do
|
238
|
-
it '
|
240
|
+
it 'quotes special TeX characters' do
|
239
241
|
expect('$10,000'.tex_quote).to eq('\\$10,000')
|
240
242
|
expect('would~~have'.tex_quote).to eq('would\\textasciitilde{}\\textasciitilde{}have')
|
241
243
|
expect('<hello>'.tex_quote).to eq('\\textless{}hello\\textgreater{}')
|
@@ -243,32 +245,38 @@ the people, for the people, shall not perish from the earth."
|
|
243
245
|
end
|
244
246
|
end
|
245
247
|
|
246
|
-
describe 'Matching' do
|
247
|
-
it '
|
248
|
+
describe 'Fuzzy Matching' do
|
249
|
+
it 'fuzzy matches with another string' do
|
248
250
|
expect('Hello, world'.fuzzy_match('wor')).to be_truthy
|
249
251
|
expect('Hello, world'.fuzzy_match('ox')).to be_falsy
|
250
252
|
end
|
251
253
|
|
252
|
-
it '
|
254
|
+
it 'fuzzy matches with another string containing re' do
|
253
255
|
expect('Hello, world'.matches_with('/or/')).to be_truthy
|
254
256
|
expect('Hello, world'.matches_with('/ox/')).to be_falsy
|
255
257
|
end
|
256
258
|
|
257
|
-
it '
|
259
|
+
it 'fuzzy matches space-separated parts' do
|
258
260
|
expect('Hello world'.fuzzy_match('hel wor')).to be_truthy
|
259
261
|
expect('Hello, world'.fuzzy_match('hel ox')).to be_falsy
|
260
262
|
end
|
261
263
|
|
262
|
-
it '
|
264
|
+
it 'fuzzy matches colon-separated parts' do
|
263
265
|
expect('Hello:world'.fuzzy_match('hel:wor')).to be_truthy
|
264
266
|
expect('Hello:world'.fuzzy_match('hel:ox')).to be_falsy
|
265
267
|
end
|
266
268
|
|
267
|
-
it '
|
269
|
+
it 'fuzzy matches colon-separated parts' do
|
268
270
|
expect('Hello:world'.fuzzy_match('hel:wor')).to be_truthy
|
269
271
|
expect('Hello:world'.fuzzy_match('hel:ox')).to be_falsy
|
270
272
|
end
|
271
273
|
|
274
|
+
it 'treats an internal `:stuff in the matcher as \bstuff.*?\b' do
|
275
|
+
expect('Hello, what is with the world?'.fuzzy_match('wha:wi:wor')).to be_truthy
|
276
|
+
expect('Hello:world'.fuzzy_match('what:or')).to be_falsy
|
277
|
+
expect('Hello, what=+&is (with) the world?'.fuzzy_match('wha:wi:wor')).to be_truthy
|
278
|
+
end
|
279
|
+
|
272
280
|
it 'requires end-anchor for ending colon' do
|
273
281
|
expect('Hello, to the world'.fuzzy_match('hel:world:')).to eq('Hello to the world')
|
274
282
|
expect('Hello, to the world today'.fuzzy_match('to:world:')).to be_nil
|
@@ -284,13 +292,13 @@ the people, for the people, shall not perish from the earth."
|
|
284
292
|
expect('Hello, to the world today'.fuzzy_match('hel:world:')).to be_falsy
|
285
293
|
end
|
286
294
|
|
287
|
-
it '
|
295
|
+
it 'returns the matched text after match' do
|
288
296
|
expect('Hello:world'.fuzzy_match('hel')).to eq('Hel')
|
289
297
|
expect('Hello:world'.fuzzy_match('hel:wor')).to eq('Hello:wor')
|
290
298
|
expect('Hello:world'.matches_with('/^h.*r/')).to eq('Hello:wor')
|
291
299
|
end
|
292
300
|
|
293
|
-
it '
|
301
|
+
it 'ignores periods, commas, and apostrophes when matching' do
|
294
302
|
expect("St. Luke's".fuzzy_match('st lukes')).to eq('St Lukes')
|
295
303
|
expect('St Lukes'.fuzzy_match('st. luke\'s')).to eq('St Lukes')
|
296
304
|
expect('St Lukes, Inc.'.fuzzy_match('st luke inc')).to eq('St Lukes Inc')
|
data/spec/lib/symbol_spec.rb
CHANGED
@@ -2,16 +2,16 @@ require 'spec_helper'
|
|
2
2
|
require 'fat_core/symbol'
|
3
3
|
|
4
4
|
describe Symbol do
|
5
|
-
it '
|
5
|
+
it 'converts to a capitalized string' do
|
6
6
|
expect(:i_am_a_symbol.entitle).to eq 'I Am a Symbol'
|
7
7
|
expect(:i_am_a_symbol.as_string).to eq 'I Am a Symbol'
|
8
8
|
end
|
9
9
|
|
10
|
-
it '
|
10
|
+
it 'responds to tex_quote' do
|
11
11
|
expect(:i_am_a_symbol.tex_quote).to eq 'i\\_am\\_a\\_symbol'
|
12
12
|
end
|
13
13
|
|
14
|
-
it '
|
14
|
+
it 'responds to :as_sym as identity' do
|
15
15
|
expect(:i_am_a_symbol.as_sym).to eq :i_am_a_symbol
|
16
16
|
end
|
17
17
|
end
|
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.12.
|
4
|
+
version: 4.12.1
|
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: 2022-
|
11
|
+
date: 2022-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simplecov
|
@@ -164,7 +164,12 @@ dependencies:
|
|
164
164
|
- - ">="
|
165
165
|
- !ruby/object:Gem::Version
|
166
166
|
version: '0'
|
167
|
-
description:
|
167
|
+
description: |
|
168
|
+
Useful extensions to Date, String, Range and other classes
|
169
|
+
including useful Date extensions for dealing with US Federal
|
170
|
+
and New York Stock Exchange holidays and working days, a useful
|
171
|
+
Enumerable#each_with_flags for flagging first and last items in the
|
172
|
+
iteration, (also for Hash), set operations on Ranges
|
168
173
|
email:
|
169
174
|
- ded@ddoherty.net
|
170
175
|
executables:
|
@@ -234,10 +239,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
234
239
|
- !ruby/object:Gem::Version
|
235
240
|
version: '0'
|
236
241
|
requirements: []
|
237
|
-
rubygems_version: 3.3.
|
242
|
+
rubygems_version: 3.3.22
|
238
243
|
signing_key:
|
239
244
|
specification_version: 4
|
240
|
-
summary:
|
245
|
+
summary: some useful core extensions
|
241
246
|
test_files:
|
242
247
|
- spec/lib/array_spec.rb
|
243
248
|
- spec/lib/bigdecimal_spec.rb
|