duffy 1.0.5 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rspec.yml +17 -0
- data/README.md +19 -23
- data/doc/markdown.png +0 -0
- data/lib/duffy/array.rb +32 -0
- data/lib/duffy/string.rb +6 -3
- data/lib/duffy/version.rb +3 -1
- data/spec/array_spec.rb +29 -0
- data/spec/string_spec.rb +48 -0
- metadata +8 -7
- data/.travis.yml +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb1c4a1481cef7c25c9126237c88660ca2f9953133937348e238e3112bbfde3f
|
4
|
+
data.tar.gz: fc41ae17543a60627d4470deeb0677df595876bd33171212e22a42f3cc17bb93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e10dc97feccbeb7a5e9b5bfd6ed5bd0bb857f57ba0be4181595d2a30e4178db32deab797b4796a9bd3485ddea482354db8015e714796bf3234b2aed156cff7be
|
7
|
+
data.tar.gz: '08764e0a764d564c26389d53f42704ad1f646bab2b7614205eb358cc1d11cfd177e213b0499d869f8ad0a156f9eeed82c72cf2748fd64168b5217b61c30447fe'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
name: RSpec
|
2
|
+
on: push
|
3
|
+
jobs:
|
4
|
+
test:
|
5
|
+
strategy:
|
6
|
+
fail-fast: false
|
7
|
+
matrix:
|
8
|
+
os: [ubuntu-latest, macos-latest]
|
9
|
+
ruby: [2.6, 2.7, '3.0', 3.1]
|
10
|
+
runs-on: ${{ matrix.os }}
|
11
|
+
steps:
|
12
|
+
- uses: actions/checkout@v2
|
13
|
+
- uses: ruby/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: ${{ matrix.ruby }}
|
16
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
17
|
+
- run: bundle exec rake
|
data/README.md
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
# Duffy
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/duffyjp/duffy.svg?branch=master)](https://travis-ci.org/duffyjp/duffy)
|
4
|
-
[![Code Climate](https://codeclimate.com/github/duffyjp/duffy/badges/gpa.svg)](https://codeclimate.com/github/duffyjp/duffy)
|
5
|
-
[![Test Coverage](https://codeclimate.com/github/duffyjp/duffy/badges/coverage.svg)](https://codeclimate.com/github/duffyjp/duffy/coverage)
|
6
|
-
|
7
3
|
This is a collection of reusable things I don't want to keep duplicating in tons of projects.
|
8
4
|
|
9
5
|
## Installation
|
@@ -21,28 +17,28 @@ $ bundle
|
|
21
17
|
|
22
18
|
## String Patches:
|
23
19
|
|
24
|
-
Method | Example
|
25
|
-
|
26
|
-
gender_human | "F"
|
27
|
-
md5 | "duffy"
|
28
|
-
md5sum | "duffy"
|
29
|
-
nl2br | "space\nout"
|
30
|
-
pretty_phone | "1234567890"
|
31
|
-
pretty_committer | "bart"
|
32
|
-
smart_titlecase | "this and that"
|
33
|
-
space2nbsp | "space
|
34
|
-
to_alpha | "abc123"
|
35
|
-
to_alpha_numeric | "abc#$%123"
|
36
|
-
to_box | "abc".to_box
|
37
|
-
to_numeric | "abc123"
|
38
|
-
to_ssn | "123456789"
|
20
|
+
Method | Example | Output | Notes
|
21
|
+
------------------|------------------|------------------------------------|------
|
22
|
+
gender_human | "F" | "Female" |
|
23
|
+
md5 | "duffy" | "904bc6e21e4799ce276200117215b88b" |
|
24
|
+
md5sum | "duffy" | "904bc6e21e4799ce276200117215b88b" |
|
25
|
+
nl2br | "space\nout" | "space<br/>out" | various OS newline permutations
|
26
|
+
pretty_phone | "1234567890" | "(123) 456-7890" | US phone number with or without area code
|
27
|
+
pretty_committer | "bart" | "Bartholomew Simpson" | See config
|
28
|
+
smart_titlecase | "this and that" | "This and That" | Has config options. See lib/duffy.rb
|
29
|
+
space2nbsp | "space out" | "space out" | Double space holdouts rejoice.
|
30
|
+
to_alpha | "abc123" | "abc" |
|
31
|
+
to_alpha_numeric | "abc#$%123" | "abc123 |
|
32
|
+
to_box | "abc".to_box | ![Example](doc/abc.png) |
|
33
|
+
to_numeric | "abc123" | "123" |
|
34
|
+
to_ssn | "123456789" | "123-45-6789" |
|
39
35
|
|
40
36
|
|
41
37
|
## Array Patches:
|
42
|
-
Method | Example
|
43
|
-
|
44
|
-
to_box | ['abc', 123].to_box
|
45
|
-
|
38
|
+
Method | Example | Output
|
39
|
+
------------------|---------------------------|-------
|
40
|
+
to_box | ['abc', 123].to_box | ![Example](doc/abc123.png)
|
41
|
+
to_markdown | [[1,2],[3,4]].to_markdown | ![Example](doc/markdown.png)
|
46
42
|
|
47
43
|
## Date Patches:
|
48
44
|
* See config to set your organization's fiscal year start.
|
data/doc/markdown.png
ADDED
Binary file
|
data/lib/duffy/array.rb
CHANGED
@@ -14,4 +14,36 @@ class Array
|
|
14
14
|
out << (b[5,2] + self.join(b[4,3]) + b[4,2])[0,80] + "\n"
|
15
15
|
out << (b[7] + self.map{ |x| b[1] * (x.to_s.gsub(/\e\[([;\d]+)?m/, '').length + 2) }.join(b[8]) + b[9])[0,80]
|
16
16
|
end
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
# Create a markdown table from a 2D Array
|
21
|
+
#
|
22
|
+
# puts [["Car", "Boat", Date.today, 5],["Horse", "Car", nil, 0]].to_markdown
|
23
|
+
# String | String | Date | Integer
|
24
|
+
# -------|--------|------------|---------
|
25
|
+
# Car | Boat | 2022-01-12 | 5
|
26
|
+
# Horse | Car | | 0
|
27
|
+
#
|
28
|
+
def to_markdown
|
29
|
+
raise "expected 2D array" unless self.first.is_a?(Array)
|
30
|
+
|
31
|
+
# Find max widths of data/headers: eg => [10, 25, 18, 21, 6]
|
32
|
+
widths = self.transpose.map{ |col| ([col.first.class.to_s.length] + col.map{ |i| i.to_s.length }).max }
|
33
|
+
|
34
|
+
# Use the class of each cell in the first row for the table headers.
|
35
|
+
out = self.first.map.with_index{|c,i| c.class.to_s.ljust(widths[i])}.join(" | ") + "\n"
|
36
|
+
out << out.gsub(/[^|]/, "-") + "\n"
|
37
|
+
|
38
|
+
# Left justify each cell
|
39
|
+
self.each do |row|
|
40
|
+
out << row.map(&:to_s).map.with_index{|c,i| c.ljust(widths[i])}.join(" | ") + "\n"
|
41
|
+
end
|
42
|
+
out
|
43
|
+
|
44
|
+
rescue => e
|
45
|
+
warn "Unable to generate table: #{e}"
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
|
17
49
|
end
|
data/lib/duffy/string.rb
CHANGED
@@ -76,12 +76,15 @@ class String
|
|
76
76
|
end
|
77
77
|
|
78
78
|
def space2nbsp
|
79
|
-
# turns double space to double nbsp
|
80
|
-
(self.nil?)? "" : self.to_s.gsub(/ {2}/, " ")
|
79
|
+
# turns >= double space to double nbsp
|
80
|
+
string = (self.nil?)? "" : self.to_s.gsub(/ {2,}/, " ")
|
81
|
+
defined?(html_safe) ? string.html_safe : string
|
81
82
|
end
|
82
83
|
|
84
|
+
# Replace the various combinations of newline characters with HTML <br> tags.
|
83
85
|
def nl2br
|
84
|
-
(self.nil?)? "" : self.to_s.gsub(/(\r)
|
86
|
+
string = (self.nil?)? "" : self.to_s.gsub(/(\r\n)|(\n\r)|\n|\v/, "<br>")
|
87
|
+
defined?(html_safe) ? string.html_safe : string
|
85
88
|
end
|
86
89
|
|
87
90
|
|
data/lib/duffy/version.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
module Duffy
|
2
|
-
VERSION = '1.0.
|
2
|
+
VERSION = '1.0.7'
|
3
3
|
end
|
4
4
|
|
5
5
|
# History
|
6
|
+
# 1.0.7 - Added vertical tab to list of characters replace with <br> in nl2br.
|
7
|
+
# 1.0.6 - Add to_markdown Array method.
|
6
8
|
# 1.0.5 - Update smart_titlecase to better accommodate names.
|
7
9
|
# 1.0.4 - Added battery_percent method.
|
8
10
|
# 1.0.3 - to_box methods now accept ANSI colored strings.
|
data/spec/array_spec.rb
CHANGED
@@ -21,4 +21,33 @@ describe Array do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
end
|
24
|
+
|
25
|
+
describe "to_markdown" do
|
26
|
+
|
27
|
+
it "creates a markdown compatible table" do
|
28
|
+
array = [[1,2,3],[4,5,6],[7,8,9]]
|
29
|
+
expect(array.to_markdown).to eq "Integer | Integer | Integer\n--------|---------|---------\n1 | 2 | 3 \n4 | 5 | 6 \n7 | 8 | 9 \n"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "uses the first row for column header names" do
|
33
|
+
array = [[1, "a", Date.today]]
|
34
|
+
expect(array.to_markdown.lines.first).to eq "Integer | String | Date \n"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "arrays with very short data format correctly" do
|
38
|
+
array = [[1,2,3],[4,5,6],[7,8,9]]
|
39
|
+
expect(array.to_markdown).to eq "Integer | Integer | Integer\n--------|---------|---------\n1 | 2 | 3 \n4 | 5 | 6 \n7 | 8 | 9 \n"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "rejects non-2D arrays" do
|
43
|
+
array = ["Wisconsin", "cheese"]
|
44
|
+
expect{array.to_markdown}.to output("Unable to generate table: expected 2D array\n").to_stderr
|
45
|
+
end
|
46
|
+
|
47
|
+
it "rejects arrays with inconsistent dimensions" do
|
48
|
+
array = [[1],[2,3],[4,5,6]]
|
49
|
+
expect{array.to_markdown}.to output("Unable to generate table: element size differs (2 should be 1)\n").to_stderr
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
24
53
|
end
|
data/spec/string_spec.rb
CHANGED
@@ -8,6 +8,39 @@ describe String do
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
+
describe "nl2br" do
|
12
|
+
it "converts linux newlines" do
|
13
|
+
expect("This\nand\nthat.".nl2br).to eq "This<br>and<br>that."
|
14
|
+
end
|
15
|
+
it "converts windows newlines" do
|
16
|
+
expect("This\r\nand\r\nthat.".nl2br).to eq "This<br>and<br>that."
|
17
|
+
end
|
18
|
+
it "converts backwards windows newlines" do
|
19
|
+
expect("This\n\rand\n\rthat.".nl2br).to eq "This<br>and<br>that."
|
20
|
+
end
|
21
|
+
|
22
|
+
it "converts vertical tabs from dubious software newlines" do
|
23
|
+
expect("This\vand\vthat.".nl2br).to eq "This<br>and<br>that."
|
24
|
+
end
|
25
|
+
|
26
|
+
it "slash R slash N slash N becomes <br><br>" do
|
27
|
+
expect("This\r\n\nthat.".nl2br).to eq "This<br><br>that."
|
28
|
+
end
|
29
|
+
|
30
|
+
it "slash N slash R slash N becomes <br><br>" do
|
31
|
+
expect("This\n\r\nthat.".nl2br).to eq "This<br><br>that."
|
32
|
+
end
|
33
|
+
|
34
|
+
it "slash N slash N slash R becomes <br><br>" do
|
35
|
+
expect("This\n\n\rthat.".nl2br).to eq "This<br><br>that."
|
36
|
+
end
|
37
|
+
|
38
|
+
it "slash N slash N slash N becomes <br><br><br>" do
|
39
|
+
expect("This\n\n\nthat.".nl2br).to eq "This<br><br><br>that."
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
11
44
|
describe "pretty_phone" do
|
12
45
|
it "formats an American phone number with Area Code" do
|
13
46
|
expect("1234567890".pretty_phone).to eq "(123) 456-7890"
|
@@ -26,6 +59,21 @@ describe String do
|
|
26
59
|
end
|
27
60
|
end
|
28
61
|
|
62
|
+
describe "space2nbsp" do
|
63
|
+
|
64
|
+
it "leaves single space alone" do
|
65
|
+
expect("1 1".space2nbsp).to eq "1 1"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "turns double space to double nbsp" do
|
69
|
+
expect("1 2".space2nbsp).to eq "1 2"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "turns triple space to double nbsp" do
|
73
|
+
expect("1 3".space2nbsp).to eq "1 3"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
29
77
|
describe "smart_titlecase" do
|
30
78
|
{
|
31
79
|
"123 Main St." => "123 Main St.", # Numbers are allowed
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: duffy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Duffy
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -102,10 +102,10 @@ executables:
|
|
102
102
|
extensions: []
|
103
103
|
extra_rdoc_files: []
|
104
104
|
files:
|
105
|
+
- ".github/workflows/rspec.yml"
|
105
106
|
- ".gitignore"
|
106
107
|
- ".rspec"
|
107
108
|
- ".rubocop.yml"
|
108
|
-
- ".travis.yml"
|
109
109
|
- Gemfile
|
110
110
|
- LICENSE.txt
|
111
111
|
- README.md
|
@@ -113,6 +113,7 @@ files:
|
|
113
113
|
- bin/console
|
114
114
|
- doc/abc.png
|
115
115
|
- doc/abc123.png
|
116
|
+
- doc/markdown.png
|
116
117
|
- duffy.gemspec
|
117
118
|
- lib/duffy.rb
|
118
119
|
- lib/duffy/active_record.rb
|
@@ -139,7 +140,7 @@ homepage: https://github.com/duffyjp/duffy
|
|
139
140
|
licenses:
|
140
141
|
- MIT
|
141
142
|
metadata: {}
|
142
|
-
post_install_message:
|
143
|
+
post_install_message:
|
143
144
|
rdoc_options: []
|
144
145
|
require_paths:
|
145
146
|
- lib
|
@@ -154,8 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
155
|
- !ruby/object:Gem::Version
|
155
156
|
version: '0'
|
156
157
|
requirements: []
|
157
|
-
rubygems_version: 3.
|
158
|
-
signing_key:
|
158
|
+
rubygems_version: 3.4.10
|
159
|
+
signing_key:
|
159
160
|
specification_version: 4
|
160
161
|
summary: Library of things
|
161
162
|
test_files:
|
data/.travis.yml
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
env:
|
2
|
-
global:
|
3
|
-
- CC_TEST_REPORTER_ID=d77de389b3b98dc3b4f404875592cde5efbf22d5268880fc8e86c4c323871bb9
|
4
|
-
language: ruby
|
5
|
-
rvm:
|
6
|
-
- 2.6.1
|
7
|
-
|
8
|
-
before_install:
|
9
|
-
- gem update bundler
|
10
|
-
|
11
|
-
before_script:
|
12
|
-
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
13
|
-
- chmod +x ./cc-test-reporter
|
14
|
-
- ./cc-test-reporter before-build
|
15
|
-
script:
|
16
|
-
- bundle exec rspec
|
17
|
-
after_script:
|
18
|
-
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|