strings 0.1.5 → 0.2.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 +4 -4
- data/CHANGELOG.md +40 -0
- data/README.md +17 -12
- data/lib/strings.rb +9 -9
- data/lib/strings/align.rb +23 -16
- data/lib/strings/extensions.rb +15 -11
- data/lib/strings/fold.rb +2 -2
- data/lib/strings/pad.rb +13 -11
- data/lib/strings/padder.rb +4 -4
- data/lib/strings/truncate.rb +9 -8
- data/lib/strings/version.rb +1 -1
- data/lib/strings/wrap.rb +63 -53
- metadata +28 -52
- data/Rakefile +0 -8
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/spec/spec_helper.rb +0 -37
- data/spec/unit/align/align_left_spec.rb +0 -62
- data/spec/unit/align/align_right_spec.rb +0 -62
- data/spec/unit/align/align_spec.rb +0 -77
- data/spec/unit/align_spec.rb +0 -18
- data/spec/unit/ansi_spec.rb +0 -7
- data/spec/unit/extensions_spec.rb +0 -51
- data/spec/unit/fold/fold_spec.rb +0 -28
- data/spec/unit/fold_spec.rb +0 -7
- data/spec/unit/pad/pad_spec.rb +0 -63
- data/spec/unit/pad_spec.rb +0 -12
- data/spec/unit/padder/parse_spec.rb +0 -35
- data/spec/unit/sanitize_spec.rb +0 -7
- data/spec/unit/truncate/truncate_spec.rb +0 -70
- data/spec/unit/truncate_spec.rb +0 -8
- data/spec/unit/wrap/wrap_spec.rb +0 -155
- data/spec/unit/wrap_spec.rb +0 -15
- data/strings.gemspec +0 -28
- data/tasks/console.rake +0 -11
- data/tasks/coverage.rake +0 -11
- data/tasks/spec.rake +0 -29
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Strings::Align, '#align_left' do
|
4
|
-
it "aligns line to left" do
|
5
|
-
text = "the madness of men"
|
6
|
-
expect(Strings::Align.align_left(text, 22)).to eq("the madness of men ")
|
7
|
-
end
|
8
|
-
|
9
|
-
it "fills empty" do
|
10
|
-
expect(Strings::Align.align_left('', 22)).to eq(" ")
|
11
|
-
end
|
12
|
-
|
13
|
-
it "left justifies string with unicode characters" do
|
14
|
-
text = "こんにちは"
|
15
|
-
expect(Strings::Align.align(text, 20, direction: :left)).to eq("こんにちは ")
|
16
|
-
end
|
17
|
-
|
18
|
-
it "left justifies string with ansi codes" do
|
19
|
-
text = "\e[32mthe madness of men\e[0m"
|
20
|
-
expect(Strings::Align.align(text, 22, direction: :left)).to eq("\e[32mthe madness of men\e[0m ")
|
21
|
-
end
|
22
|
-
|
23
|
-
it "aligns multiline text to left" do
|
24
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
25
|
-
expect(Strings::Align.align_left(text, 40)).to eq([
|
26
|
-
"for there is no folly of the beast \n",
|
27
|
-
"of the earth which \n",
|
28
|
-
"is not infinitely \n",
|
29
|
-
"outdone by the madness of men "
|
30
|
-
].join)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "left justifies multiline utf text" do
|
34
|
-
text = "ラドクリフ\n、マラソン五輪\n代表に1万m出\n場にも含み"
|
35
|
-
expect(Strings::Align.align_left(text, 20)).to eq([
|
36
|
-
"ラドクリフ \n",
|
37
|
-
"、マラソン五輪 \n",
|
38
|
-
"代表に1万m出 \n",
|
39
|
-
"場にも含み "
|
40
|
-
].join)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "left justifies ansi text" do
|
44
|
-
text = "for \e[35mthere\e[0m is no folly of the beast\nof the \e[33mearth\e0m which\nis \e[34mnot infinitely\e[0m\n\e[33moutdone\e[0m by the madness of men"
|
45
|
-
expect(Strings::Align.align_left(text, 40)).to eq([
|
46
|
-
"for \e[35mthere\e[0m is no folly of the beast \n",
|
47
|
-
"of the \e[33mearth\e0m which \n",
|
48
|
-
"is \e[34mnot infinitely\e[0m \n",
|
49
|
-
"\e[33moutdone\e[0m by the madness of men "
|
50
|
-
].join)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "left justifies multiline text with fill of '*'" do
|
54
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
55
|
-
expect(Strings::Align.align_left(text, 40, fill: '*')).to eq([
|
56
|
-
"for there is no folly of the beast******\n",
|
57
|
-
"of the earth which**********************\n",
|
58
|
-
"is not infinitely***********************\n",
|
59
|
-
"outdone by the madness of men***********"
|
60
|
-
].join)
|
61
|
-
end
|
62
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Strings::Align, '#align_right' do
|
4
|
-
it "aligns line to right" do
|
5
|
-
text = "the madness of men"
|
6
|
-
expect(Strings::Align.align_right(text, 22)).to eq(" the madness of men")
|
7
|
-
end
|
8
|
-
|
9
|
-
it "fills empty" do
|
10
|
-
expect(Strings::Align.align_right('', 22)).to eq(" ")
|
11
|
-
end
|
12
|
-
|
13
|
-
it "aligns string to the right with unicode characters" do
|
14
|
-
text = "こんにちは"
|
15
|
-
expect(Strings::Align.align(text, 20, direction: :right)).to eq(" こんにちは")
|
16
|
-
end
|
17
|
-
|
18
|
-
it "aligns string to the right with ansi codees" do
|
19
|
-
text = "\e[32mthe madness of men\e[0m"
|
20
|
-
expect(Strings::Align.align(text, 22, direction: :right)).to eq(" \e[32mthe madness of men\e[0m")
|
21
|
-
end
|
22
|
-
|
23
|
-
it "aligns multiline text to the right" do
|
24
|
-
text = "for there is no folly of the beast\n of the earth which\n is not infinitely\n outdone by the madness of men"
|
25
|
-
expect(Strings::Align.align_right(text, 40)).to eq([
|
26
|
-
" for there is no folly of the beast\n",
|
27
|
-
" of the earth which\n",
|
28
|
-
" is not infinitely\n",
|
29
|
-
" outdone by the madness of men"
|
30
|
-
].join)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "aligns multiline text to right with unicode characters" do
|
34
|
-
text = "ラドクリフ\n、マラソン五輪\n代表に1万m出\n場にも含み"
|
35
|
-
expect(Strings::Align.align_right(text, 20)).to eq([
|
36
|
-
" ラドクリフ\n",
|
37
|
-
" 、マラソン五輪\n",
|
38
|
-
" 代表に1万m出\n",
|
39
|
-
" 場にも含み"
|
40
|
-
].join)
|
41
|
-
end
|
42
|
-
|
43
|
-
it "right justfies ansi text" do
|
44
|
-
text = "for \e[35mthere\e[0m is no folly of the beast\nof the \e[33mearth\e0m which\nis \e[34mnot infinitely\e[0m\n\e[33moutdone\e[0m by the madness of men"
|
45
|
-
expect(Strings::Align.align_right(text, 40)).to eq([
|
46
|
-
" for \e[35mthere\e[0m is no folly of the beast\n",
|
47
|
-
" of the \e[33mearth\e0m which\n",
|
48
|
-
" is \e[34mnot infinitely\e[0m\n",
|
49
|
-
" \e[33moutdone\e[0m by the madness of men"
|
50
|
-
].join)
|
51
|
-
end
|
52
|
-
|
53
|
-
it "right justifies multiline text with fill of '*'" do
|
54
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
55
|
-
expect(Strings::Align.align_right(text, 40, fill: '*')).to eq([
|
56
|
-
"******for there is no folly of the beast\n",
|
57
|
-
"**********************of the earth which\n",
|
58
|
-
"***********************is not infinitely\n",
|
59
|
-
"***********outdone by the madness of men"
|
60
|
-
].join)
|
61
|
-
end
|
62
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Strings::Align, '#align' do
|
4
|
-
it "doesn't align unrecognized direction" do
|
5
|
-
text = "the madness of men"
|
6
|
-
expect {
|
7
|
-
Strings::Align.align(text, 22, direction: :unknown)
|
8
|
-
}.to raise_error(ArgumentError, /Unknown alignment/)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "fills empty" do
|
12
|
-
expect(Strings::Align.align('', 22, direction: :center)).to eq(" ")
|
13
|
-
end
|
14
|
-
|
15
|
-
it "centers line" do
|
16
|
-
text = "the madness of men"
|
17
|
-
expect(Strings::Align.align_center(text, 22)).to eq(" the madness of men ")
|
18
|
-
end
|
19
|
-
|
20
|
-
it "centers unicode characters" do
|
21
|
-
text = "こんにちは"
|
22
|
-
expect(Strings::Align.align_center(text, 20)).to eq(" こんにちは ")
|
23
|
-
end
|
24
|
-
|
25
|
-
it "centers string with ansi codes" do
|
26
|
-
text = "\e[32mthe madness of men\e[0m"
|
27
|
-
expect(Strings::Align.align_center(text, 22)).to eq(" \e[32mthe madness of men\e[0m ")
|
28
|
-
end
|
29
|
-
|
30
|
-
it "centers multiline text" do
|
31
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
32
|
-
expect(Strings::Align.align_center(text, 40)).to eq([
|
33
|
-
" for there is no folly of the beast \n",
|
34
|
-
" of the earth which \n",
|
35
|
-
" is not infinitely \n",
|
36
|
-
" outdone by the madness of men "
|
37
|
-
].join)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "centers multiline text with exact width" do
|
41
|
-
text = "the madness \nof men"
|
42
|
-
expect(Strings::Align.align_center(text, 12)).to eq([
|
43
|
-
"the madness \n",
|
44
|
-
" of men "
|
45
|
-
].join)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "centers multiline unicode text" do
|
49
|
-
text = "ラドクリフ\n、マラソン五輪\n代表に1万m出\n場にも含み"
|
50
|
-
expect(Strings::Align.align_center(text, 20)).to eq([
|
51
|
-
" ラドクリフ \n",
|
52
|
-
" 、マラソン五輪 \n",
|
53
|
-
" 代表に1万m出 \n",
|
54
|
-
" 場にも含み "
|
55
|
-
].join)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "centers text with ansi codes" do
|
59
|
-
text = "for \e[35mthere\e[0m is no folly of the beast\nof the \e[33mearth\e0m which\nis \e[34mnot infinitely\e[0m\n\e[33moutdone\e[0m by the madness of men"
|
60
|
-
expect(Strings::Align.align_center(text, 40)).to eq([
|
61
|
-
" for \e[35mthere\e[0m is no folly of the beast \n",
|
62
|
-
" of the \e[33mearth\e0m which \n",
|
63
|
-
" is \e[34mnot infinitely\e[0m \n",
|
64
|
-
" \e[33moutdone\e[0m by the madness of men "
|
65
|
-
].join)
|
66
|
-
end
|
67
|
-
|
68
|
-
it "centers multiline text with fill character '*'" do
|
69
|
-
text = "for there is no folly of the beast\nof the earth which\nis not infinitely\noutdone by the madness of men"
|
70
|
-
expect(Strings::Align.align(text, 40, direction: :center, fill: '*')).to eq([
|
71
|
-
"***for there is no folly of the beast***\n",
|
72
|
-
"***********of the earth which***********\n",
|
73
|
-
"***********is not infinitely************\n",
|
74
|
-
"*****outdone by the madness of men******"
|
75
|
-
].join)
|
76
|
-
end
|
77
|
-
end
|
data/spec/unit/align_spec.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Strings, '#align' do
|
4
|
-
it "aligns text" do
|
5
|
-
text = "the madness of men"
|
6
|
-
expect(Strings.align(text, 22, direction: :center)).to eq(" the madness of men ")
|
7
|
-
end
|
8
|
-
|
9
|
-
it "aligns text to left" do
|
10
|
-
text = "the madness of men"
|
11
|
-
expect(Strings.align_left(text, 22)).to eq("the madness of men ")
|
12
|
-
end
|
13
|
-
|
14
|
-
it "aligns text to right" do
|
15
|
-
text = "the madness of men"
|
16
|
-
expect(Strings.align_right(text, 22)).to eq(" the madness of men")
|
17
|
-
end
|
18
|
-
end
|
data/spec/unit/ansi_spec.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'strings/extensions'
|
4
|
-
|
5
|
-
using Strings::Extensions
|
6
|
-
|
7
|
-
RSpec.describe Strings::Extensions do
|
8
|
-
let(:text) { "the madness of men" }
|
9
|
-
|
10
|
-
it "aligns a line in the center" do
|
11
|
-
expect(text.align(22, direction: :center)).to eq(" the madness of men ")
|
12
|
-
end
|
13
|
-
|
14
|
-
it "aligns a line to the left" do
|
15
|
-
expect(text.align_left(22)).to eq("the madness of men ")
|
16
|
-
end
|
17
|
-
|
18
|
-
it "aligns a line to the right" do
|
19
|
-
expect(text.align_right(22)).to eq(" the madness of men")
|
20
|
-
end
|
21
|
-
|
22
|
-
it "checks for ansi" do
|
23
|
-
expect("\e[33mfoo\e[0m".ansi?).to eq(true)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "removes ansi codes" do
|
27
|
-
expect("\e[33mfoo\e[0m".sanitize).to eq('foo')
|
28
|
-
end
|
29
|
-
|
30
|
-
it "folds a line" do
|
31
|
-
expect("foo\n\nbar\n".fold).to eq('foo bar ')
|
32
|
-
end
|
33
|
-
|
34
|
-
it "pads a line" do
|
35
|
-
expect(text.pad(1)).to eq([
|
36
|
-
" " * (text.size + 2),
|
37
|
-
" #{text} ",
|
38
|
-
" " * (text.size + 2),
|
39
|
-
].join("\n"))
|
40
|
-
end
|
41
|
-
|
42
|
-
it "truncates a line" do
|
43
|
-
text = "the madness of men"
|
44
|
-
expect(text.truncate(10)).to eq('the madn…')
|
45
|
-
end
|
46
|
-
|
47
|
-
it "wraps a line" do
|
48
|
-
expect('foobar1'.wrap(3)).to eq("foo\nbar\n1")
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
data/spec/unit/fold/fold_spec.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# unicode: utf-8
|
2
|
-
# frozen_string_literals: true
|
3
|
-
|
4
|
-
RSpec.describe Strings, '#fold' do
|
5
|
-
{
|
6
|
-
" \n" => ' ',
|
7
|
-
"\n " => ' ',
|
8
|
-
"\n" => ' ',
|
9
|
-
"\n\n\n" => ' ',
|
10
|
-
" \n " => ' ',
|
11
|
-
" \n \n \n" => ' '
|
12
|
-
}.each do |actual, expected|
|
13
|
-
it "removes newline '#{actual.gsub(/\n/, '\\n')}' to '#{expected}'" do
|
14
|
-
expect(Strings::Fold.fold(actual)).to eq(expected)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
{
|
19
|
-
" \r\n" => ' ',
|
20
|
-
"\r\n " => ' ',
|
21
|
-
"\r\n" => ' ',
|
22
|
-
" \r\n " => ' ',
|
23
|
-
}.each do |actual, expected|
|
24
|
-
it "squashes '#{actual.gsub(/\r\n/, '\\r\\n')}' to '#{expected}'" do
|
25
|
-
expect(Strings::Fold.fold(actual, "\r\n")).to eq(expected)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
data/spec/unit/fold_spec.rb
DELETED
data/spec/unit/pad/pad_spec.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
RSpec.describe Strings::Pad, '#pad' do
|
4
|
-
it "pads content with padding as a single value" do
|
5
|
-
text = "Ignorance is the parent of fear."
|
6
|
-
expect(Strings::Pad.pad(text, 1)).to eq([
|
7
|
-
" ",
|
8
|
-
" Ignorance is the parent of fear. ",
|
9
|
-
" ",
|
10
|
-
].join("\n"))
|
11
|
-
end
|
12
|
-
|
13
|
-
it "pads content with specific padding as an array" do
|
14
|
-
text = "Ignorance is the parent of fear."
|
15
|
-
expect(Strings::Pad.pad(text, [1,1,1,1])).to eq([
|
16
|
-
" ",
|
17
|
-
" Ignorance is the parent of fear. ",
|
18
|
-
" ",
|
19
|
-
].join("\n"))
|
20
|
-
end
|
21
|
-
|
22
|
-
it "pads with custom character" do
|
23
|
-
text = "Ignorance is the parent of fear."
|
24
|
-
expect(Strings::Pad.pad(text, [1, 2], fill: "*")).to eq([
|
25
|
-
"************************************",
|
26
|
-
"**Ignorance is the parent of fear.**",
|
27
|
-
"************************************",
|
28
|
-
].join("\n"))
|
29
|
-
end
|
30
|
-
|
31
|
-
it "pads unicode content" do
|
32
|
-
text = "ラドクリフ、マラソン"
|
33
|
-
expect(Strings::Pad.pad(text, [1,1,1,1])).to eq([
|
34
|
-
" ",
|
35
|
-
" ラドクリフ、マラソン ",
|
36
|
-
" "
|
37
|
-
].join("\n"))
|
38
|
-
end
|
39
|
-
|
40
|
-
it "pads multiline content" do
|
41
|
-
text = "It is the easiest thing\nin the world for a man\nto look as if he had \na great secret in him."
|
42
|
-
expect(Strings::Pad.pad(text, [1,2])).to eq([
|
43
|
-
" ",
|
44
|
-
" It is the easiest thing ",
|
45
|
-
" in the world for a man ",
|
46
|
-
" to look as if he had ",
|
47
|
-
" a great secret in him. ",
|
48
|
-
" ",
|
49
|
-
].join("\n"))
|
50
|
-
end
|
51
|
-
|
52
|
-
it "pads ANSI codes inside content" do
|
53
|
-
text = "It is \e[35mthe easiest\e[0m thing\nin the \e[34mworld\e[0m for a man\nto look as if he had \na great \e[33msecret\e[0m in him."
|
54
|
-
expect(Strings::Pad.pad(text, [1,1,1,1])).to eq([
|
55
|
-
" ",
|
56
|
-
" It is \e[35mthe easiest\e[0m thing ",
|
57
|
-
" in the \e[34mworld\e[0m for a man ",
|
58
|
-
" to look as if he had ",
|
59
|
-
" a great \e[33msecret\e[0m in him. ",
|
60
|
-
" ",
|
61
|
-
].join("\n"))
|
62
|
-
end
|
63
|
-
end
|
data/spec/unit/pad_spec.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
# frozen_string_literal:true
|
2
|
-
|
3
|
-
RSpec.describe Strings::Padder, '#parse' do
|
4
|
-
it "parses nil" do
|
5
|
-
instance = Strings::Padder.parse(nil)
|
6
|
-
expect(instance.padding).to eq([])
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'parses self' do
|
10
|
-
value = Strings::Padder.new([])
|
11
|
-
instance = Strings::Padder.parse(value)
|
12
|
-
expect(instance.padding).to eq([])
|
13
|
-
end
|
14
|
-
|
15
|
-
it "parses digit" do
|
16
|
-
instance = Strings::Padder.parse(5)
|
17
|
-
expect(instance.padding).to eq([5,5,5,5])
|
18
|
-
end
|
19
|
-
|
20
|
-
it "parses 2-element array" do
|
21
|
-
instance = Strings::Padder.parse([2,3])
|
22
|
-
expect(instance.padding).to eq([2,3,2,3])
|
23
|
-
end
|
24
|
-
|
25
|
-
it "parses 4-element array" do
|
26
|
-
instance = Strings::Padder.parse([1,2,3,4])
|
27
|
-
expect(instance.padding).to eq([1,2,3,4])
|
28
|
-
end
|
29
|
-
|
30
|
-
it "fails to parse unknown value" do
|
31
|
-
expect {
|
32
|
-
Strings::Padder.parse(:unknown)
|
33
|
-
}.to raise_error(Strings::Padder::ParseError)
|
34
|
-
end
|
35
|
-
end
|