strings 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Strings, '#pad' do
4
- it "pads text" do
5
- text = 'ラドクリフ、マラソン五輪代表に1万m出場にも含み'
6
- expect(Strings.pad(text, [1,1,1,1])).to eql([
7
- ' ',
8
- ' ラドクリフ、マラソン五輪代表に1万m出場にも含み ',
9
- ' '
10
- ].join("\n"))
11
- end
12
- end
@@ -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
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Strings, '#sanitize' do
4
- it "removes ansi codes" do
5
- expect(Strings.sanitize("\e[33mfoo\e[0m")).to eq('foo')
6
- end
7
- end
@@ -1,70 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Strings::Truncate, '#truncate' do
4
- let(:text) { 'ラドクリフ、マラソン五輪代表に1万m出場にも含み' }
5
-
6
- it "doesn't change text for 0 length" do
7
- expect(Strings::Truncate.truncate(text, 0)).to eq(text)
8
- end
9
-
10
- it "doensn't change text for nil length" do
11
- expect(Strings::Truncate.truncate(text, nil)).to eq(text)
12
- end
13
-
14
- it "doesn't change text for equal length" do
15
- truncation = Strings::Truncate.truncate(text, text.length * 2)
16
- expect(truncation).to eq(text)
17
- end
18
-
19
- it 'truncates text and displays omission' do
20
- trailing = '…'
21
- expect(Strings::Truncate.truncate(text, 12)).to eq("ラドクリフ#{trailing}")
22
- end
23
-
24
- it "estimates total width correctly " do
25
- text = '太丸ゴシック体'
26
- trailing = '…'
27
- expect(Strings::Truncate.truncate(text, 8)).to eq("太丸ゴ#{trailing}")
28
- end
29
-
30
- it "doesn't truncate text when length exceeds content" do
31
- expect(Strings::Truncate.truncate(text, 100)).to eq(text)
32
- end
33
-
34
- it "doesn't truncate whole words" do
35
- text = "I know not all that may be coming, but be it what it will, I'll go to it laughing."
36
- trailing = '…'
37
- truncation = Strings::Truncate.truncate(text, separator: ' ')
38
- expect(truncation).to eq("I know not all that may be#{trailing}")
39
- end
40
-
41
- it 'truncates text with string separator' do
42
- trailing = '…'
43
- truncation = Strings::Truncate.truncate(text, 12, separator: '')
44
- expect(truncation).to eq("ラドクリフ#{trailing}")
45
- end
46
-
47
- it 'truncates text with regex separator' do
48
- trailing = '…'
49
- truncation = Strings::Truncate.truncate(text, 12, separator: /\s/)
50
- expect(truncation).to eq("ラドクリフ#{trailing}")
51
- end
52
-
53
- it 'truncates text with custom trailing' do
54
- trailing = '... (see more)'
55
- truncation = Strings::Truncate.truncate(text, 20, trailing: trailing)
56
- expect(truncation).to eq("ラド#{trailing}")
57
- end
58
-
59
- it 'correctly truncates with ANSI characters' do
60
- text = "I try \e[34mall things\e[0m, I achieve what I can"
61
- truncation = Strings::Truncate.truncate(text, 18)
62
- expect(truncation).to eq("I try \e[34mall things\e[0m…")
63
- end
64
-
65
- it "finishes on word boundary" do
66
- text = "for there is no folly of the beast of the earth"
67
- truncation = Strings::Truncate.truncate(text, 20, separator: ' ')
68
- expect(truncation).to eq('for there is no…')
69
- end
70
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Strings, '#truncate' do
4
- it "truncates text" do
5
- text = 'ラドクリフ、マラソン五輪代表に1万m出場にも含み'
6
- expect(Strings.truncate(text, 12)).to eq('ラドクリフ…')
7
- end
8
- end
@@ -1,155 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe Strings::Wrap, '.wrap' do
4
- context 'when unicode' do
5
- let(:text) { 'ラドクリフ、マラソン五輪代表に1万m出場にも含み' }
6
-
7
- it "doesn't wrap at zero length" do
8
- expect(Strings::Wrap.wrap(text, 0)).to eq(text)
9
- end
10
-
11
- it "doesn't wrap at nil length" do
12
- expect(Strings::Wrap.wrap(text, nil)).to eq(text)
13
- end
14
-
15
- it "doesn't wrap at length exceeding content length" do
16
- expect(Strings::Wrap.wrap(text, 100)).to eq(text)
17
- end
18
-
19
- it "wraps correctly unbreakable words" do
20
- expect(Strings::Wrap.wrap('foobar1', 3)).to eq([
21
- "foo",
22
- "bar",
23
- "1"
24
- ].join("\n"))
25
- end
26
-
27
- it "collapses multiple line breaks " do
28
- text = "some \r\n\n\n\nunbreakable\n\n\n\n \r\r\rcontent \t"
29
- expect(Strings::Wrap.wrap(text, 5)).to eq([
30
- "some ",
31
- "unbre",
32
- "akabl",
33
- "e",
34
- " ",
35
- "conte",
36
- "nt "
37
- ].join("\n"))
38
- end
39
-
40
- it "preserves newlines" do
41
- text = "It is not down\n on any map;\n true places never are."
42
- expect(Strings::Wrap.wrap(text, 10)).to eq([
43
- "It is not ",
44
- "down",
45
- " on any ",
46
- "map;",
47
- " true ",
48
- "places ",
49
- "never are."
50
- ].join("\n"))
51
- end
52
-
53
- it "wraps ascii text" do
54
- text = "for there is no folly of the beast of the earth which is not infinitely outdone by the madness of men "
55
- expect(Strings::Wrap.wrap(text, 16)).to eq([
56
- "for there is no ",
57
- "folly of the ",
58
- "beast of the ",
59
- "earth which is ",
60
- "not infinitely ",
61
- "outdone by the ",
62
- "madness of men "
63
- ].join("\n"))
64
- end
65
-
66
- it 'wraps at 8 characters' do
67
- expect(Strings::Wrap.wrap(text, 8)).to eq([
68
- "ラドクリ",
69
- "フ、マラ",
70
- "ソン五輪",
71
- "代表に1",
72
- "万m出場",
73
- "にも含み"
74
- ].join("\n"))
75
- end
76
-
77
- it 'preserves whitespace' do
78
- text = " As for me, I am tormented with an everlasting itch for things remote. "
79
- expect(Strings::Wrap.wrap(text, 10)).to eq([
80
- " As for ",
81
- "me, I ",
82
- "am ",
83
- "tormented ",
84
- "with an ",
85
- "e",
86
- "verlasting",
87
- " itch ",
88
- "for ",
89
- "things ",
90
- "remote. "
91
- ].join("\n"))
92
- end
93
- end
94
-
95
- context 'when long text' do
96
- it "wraps long text at 45 characters" do
97
- text =
98
- "What of it, if some old hunks of a sea-captain orders me to get a broom and sweep down the decks? What does that indignity amount to, weighed, I mean, in the scales of the New Testament? Do you think the archangel Gabriel thinks anything the less of me, because I promptly and respectfully obey that old hunks in that particular instance? Who ain't a slave? Tell me that. Well, then, however the old sea-captains may order me about--however they may thump and punch me about, I have the satisfaction of knowing that it is all right;"
99
- expect(Strings::Wrap.wrap(text, 45)).to eq unindent <<-EOS
100
- What of it, if some old hunks of a \nsea-captain orders me to get a broom and \n sweep down the decks? What does that \nindignity amount to, weighed, I mean, in the \n scales of the New Testament? Do you think \nthe archangel Gabriel thinks anything the \nless of me, because I promptly and \nrespectfully obey that old hunks in that \nparticular instance? Who ain't a slave? Tell \nme that. Well, then, however the old \nsea-captains may order me about--however \nthey may thump and punch me about, I have \nthe satisfaction of knowing that it is all \nright;
101
- EOS
102
- end
103
- end
104
-
105
- context 'with newlines' do
106
- it "preserves newlines for both prefix and postfix" do
107
- text = "\n\nラドクリフ、マラソン五輪代表に1万m出場にも含み\n\n\n"
108
- expect(Strings::Wrap.wrap(text, 10)).to eq([
109
- "\nラドクリフ",
110
- "、マラソン",
111
- "五輪代表に",
112
- "1万m出場に",
113
- "も含み\n"
114
- ].join("\n"))
115
- end
116
- end
117
-
118
- context 'with ANSI codes' do
119
- it "wraps ANSI chars" do
120
- text = "\e[32;44mIgnorance is the parent of fear.\e[0m"
121
- expect(Strings::Wrap.wrap(text, 14)).to eq([
122
- "\e[32;44mIgnorance is \e[0m",
123
- "\e[32;44mthe parent of \e[0m",
124
- "\e[32;44mfear.\e[0m",
125
- ].join("\n"))
126
- end
127
-
128
- it "wraps ANSI in the middle of text" do
129
- text = "Ignorance is the \e[32mparent\e[0m of fear."
130
- expect(Strings::Wrap.wrap(text, 14)).to eq([
131
- "Ignorance is ",
132
- "the \e[32mparent\e[0m of ",
133
- "fear.",
134
- ].join("\n"))
135
- end
136
-
137
- it "wraps multline ANSI codes" do
138
- text = "\e32;44mMulti\nLine\nContent.\e[0m"
139
- expect(Strings::Wrap.wrap(text, 14)).to eq([
140
- "\e32;44mMulti\e[0m",
141
- "\e32;44mLine\e[0m",
142
- "\e32;44mContent.\e[0m",
143
- ].join("\n"))
144
- end
145
-
146
- it "wraps multiple ANSI codes in a single line" do
147
- text = "Talk \e[32mnot\e[0m to me of \e[33mblasphemy\e[0m, man; I'd \e[35mstrike the sun\e[0m if it insulted me."
148
- expect(Strings::Wrap.wrap(text, 30)).to eq([
149
- "Talk \e[32mnot\e[0m to me of \e[33mblasphemy\e[0m, ",
150
- "man; I'd \e[35mstrike the sun\e[0m if it ",
151
- "insulted me."
152
- ].join("\n"))
153
- end
154
- end
155
- end
@@ -1,15 +0,0 @@
1
- # encoding: utf-8
2
-
3
- RSpec.describe Strings, '#wrap' do
4
- it "wraps text" do
5
- text = 'ラドクリフ、マラソン五輪代表に1万m出場にも含み'
6
- expect(Strings.wrap(text, 8)).to eql([
7
- "ラドクリ",
8
- "フ、マラ",
9
- "ソン五輪",
10
- "代表に1",
11
- "万m出場",
12
- "にも含み"
13
- ].join("\n"))
14
- end
15
- end
@@ -1,30 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "strings/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "strings"
8
- spec.version = Strings::VERSION
9
- spec.authors = ["Piotr Murach"]
10
- spec.email = [""]
11
-
12
- spec.summary = %q{A set of useful functions for transforming strings.}
13
- spec.description = %q{A set of useful functions such as fold, truncate, wrap and more for transoforming strings.}
14
- spec.homepage = ""
15
- spec.license = "MIT"
16
-
17
- spec.files = Dir['{lib,spec}/**/*.rb', '{bin,tasks}/*', 'strings.gemspec']
18
- spec.files += Dir['README.md', 'CHANGELOG.md', 'LICENSE.txt', 'Rakefile']
19
- spec.bindir = "exe"
20
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
- spec.require_paths = ["lib"]
22
-
23
- spec.add_dependency 'strings-ansi', '~> 0.1.0'
24
- spec.add_dependency 'unicode_utils', '~> 1.4.0'
25
- spec.add_dependency 'unicode-display_width','~> 1.4.0'
26
-
27
- spec.add_development_dependency "bundler", "~> 1.15"
28
- spec.add_development_dependency "rake", "~> 10.0"
29
- spec.add_development_dependency "rspec", "~> 3.0"
30
- end
@@ -1,11 +0,0 @@
1
- # encoding: utf-8
2
-
3
- desc 'Load gem inside irb console'
4
- task :console do
5
- require 'irb'
6
- require 'irb/completion'
7
- require File.join(__FILE__, '../../lib/tty-reader')
8
- ARGV.clear
9
- IRB.start
10
- end
11
- task c: %w[ console ]
@@ -1,11 +0,0 @@
1
- # encoding: utf-8
2
-
3
- desc 'Measure code coverage'
4
- task :coverage do
5
- begin
6
- original, ENV['COVERAGE'] = ENV['COVERAGE'], 'true'
7
- Rake::Task['spec'].invoke
8
- ensure
9
- ENV['COVERAGE'] = original
10
- end
11
- end
@@ -1,29 +0,0 @@
1
- # encoding: utf-8
2
-
3
- begin
4
- require 'rspec/core/rake_task'
5
-
6
- desc 'Run all specs'
7
- RSpec::Core::RakeTask.new(:spec) do |task|
8
- task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
9
- end
10
-
11
- namespace :spec do
12
- desc 'Run unit specs'
13
- RSpec::Core::RakeTask.new(:unit) do |task|
14
- task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
15
- end
16
-
17
- desc 'Run integration specs'
18
- RSpec::Core::RakeTask.new(:integration) do |task|
19
- task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
20
- end
21
- end
22
-
23
- rescue LoadError
24
- %w[spec spec:unit spec:integration].each do |name|
25
- task name do
26
- $stderr.puts "In order to run #{name}, do `gem install rspec`"
27
- end
28
- end
29
- end