crystal_goodies 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +5 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +34 -0
- data/Rakefile +8 -0
- data/lib/crystal_goodies/array/extensions.rb +7 -0
- data/lib/crystal_goodies/array.rb +45 -0
- data/lib/crystal_goodies/enumerable/extensions.rb +7 -0
- data/lib/crystal_goodies/enumerable.rb +158 -0
- data/lib/crystal_goodies/integer/extensions.rb +7 -0
- data/lib/crystal_goodies/integer.rb +21 -0
- data/lib/crystal_goodies/object/extensions.rb +7 -0
- data/lib/crystal_goodies/object.rb +16 -0
- data/lib/crystal_goodies/string/extensions.rb +7 -0
- data/lib/crystal_goodies/string.rb +188 -0
- data/lib/crystal_goodies/version.rb +5 -0
- data/lib/crystal_goodies.rb +9 -0
- data/sig/array.rbs +10 -0
- data/sig/enumerable.rbs +28 -0
- data/sig/integer.rbs +8 -0
- data/sig/object.rbs +5 -0
- data/sig/string.rbs +24 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 100fad08a9578a26ae53785926cc01fc7d81735a7afe55c62d1a3d74d9312924
|
4
|
+
data.tar.gz: c0bf1b1c3d290359d270659de2e888fa9486473eae55e517af9c38358c4367af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1eebd55c21f807d34ae9be8bacd3c12d645ff9eacc721314bdd19cd3c880091a39d873dee0f3d5e1e5cbd407a0574a695cdc25d5da7ee4fda01f2c5d19c32122
|
7
|
+
data.tar.gz: f2d138d8a5b01fc8dc89e5913909e0c768332e43c9f1e07ffd2329b98238bc9f84b24316a270698ddff48f89bcd7b54ed2aeba32d334518ab4b253cc49510b69
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at nnbnh@protonmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 NNB
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
<img width="100%" src="https://capsule-render.vercel.app/api?type=waving§ion=header&color=DC2626&fontColor=FEF2F2&height=256&text=Crystal%20Goodies&desc=Crystal%20goodies%20port%20to%20Ruby&fontAlignY=40" />
|
2
|
+
|
3
|
+
## 💡 About
|
4
|
+
|
5
|
+
Class's methods port from Crystal.
|
6
|
+
|
7
|
+
## 📥 Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
```sh
|
12
|
+
bundle add crystal_goodies
|
13
|
+
```
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
```sh
|
18
|
+
gem install crystal_goodies
|
19
|
+
```
|
20
|
+
|
21
|
+
## ⌨️ Usage
|
22
|
+
|
23
|
+
In Ruby do:
|
24
|
+
|
25
|
+
```rb
|
26
|
+
require 'crystal_goodies'
|
27
|
+
```
|
28
|
+
TODO
|
29
|
+
|
30
|
+
## 💌 Credits
|
31
|
+
|
32
|
+
- [**Crystal**](https://crystal-lang.org) by [it's contributors](https://github.com/crystal-lang/crystal/graphs/contributors)
|
33
|
+
|
34
|
+
<a href="https://codeberg.org/NNB"><img width="100%" src="https://capsule-render.vercel.app/api?type=waving§ion=footer&color=DC2626&fontColor=FEF2F2&height=128&desc=Made%20with%20%26lt;3%20by%20NNB&descAlignY=80" /></a>
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CrystalGoodies
|
4
|
+
class Array
|
5
|
+
# Returns an `Array` with the first *count* elements removed
|
6
|
+
# from the original array.
|
7
|
+
#
|
8
|
+
# If *count* is bigger than the number of elements in the array, returns an empty array.
|
9
|
+
#
|
10
|
+
# ```
|
11
|
+
# [1, 2, 3, 4, 5, 6].skip(3) # => [4, 5, 6]
|
12
|
+
# ```
|
13
|
+
def skip(count)
|
14
|
+
raise ArgumentError, 'attempt to skip negative size' if count.negative?
|
15
|
+
|
16
|
+
self[count..]
|
17
|
+
end
|
18
|
+
|
19
|
+
# Removes all elements except the *count* or less (if there aren't enough)
|
20
|
+
# elements starting at the given *start* index. Returns `self`.
|
21
|
+
#
|
22
|
+
# Negative values of *start* count from the end of the array.
|
23
|
+
#
|
24
|
+
# Raises `IndexError` if the *start* index is out of range.
|
25
|
+
#
|
26
|
+
# Raises `ArgumentError` if *count* is negative.
|
27
|
+
#
|
28
|
+
# ```
|
29
|
+
# a = [0, 1, 4, 9, 16, 25]
|
30
|
+
# a.truncate(2, 3) # => [4, 9, 16]
|
31
|
+
# a # => [4, 9, 16]
|
32
|
+
# ```
|
33
|
+
#
|
34
|
+
# Or removes all elements except those within the given *range*. Returns `self`.
|
35
|
+
#
|
36
|
+
# ```
|
37
|
+
# a = [0, 1, 4, 9, 16, 25]
|
38
|
+
# a.truncate(1..-3) # => [1, 4, 9]
|
39
|
+
# a # => [1, 4, 9]
|
40
|
+
# ```
|
41
|
+
def truncate(...)
|
42
|
+
self.[](...)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,158 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CrystalGoodies
|
4
|
+
module Enumerable
|
5
|
+
# Returns `true` if `self` is empty, `false` otherwise.
|
6
|
+
#
|
7
|
+
# ```
|
8
|
+
# [].empty? # => true
|
9
|
+
# [1].empty? # => false
|
10
|
+
# ```
|
11
|
+
def empty?
|
12
|
+
each { return false }
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns an `Array` with the results of running the block against each element
|
17
|
+
# of the collection, removing `nil` values.
|
18
|
+
#
|
19
|
+
# ```
|
20
|
+
# ["Alice", "Bob"].map { |name| name.match(/^A./) } # => [#<MatchData "Al">, nil]
|
21
|
+
# ["Alice", "Bob"].compact_map { |name| name.match(/^A./) } # => [#<MatchData "Al">]
|
22
|
+
# ```
|
23
|
+
def compact_map(&)
|
24
|
+
# FIXME
|
25
|
+
map(&).compact
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns an `Array` with chunks in the given size, eventually filled up
|
29
|
+
# with given value or `nil`.
|
30
|
+
#
|
31
|
+
# ```
|
32
|
+
# [1, 2, 3].in_groups_of(2, 0) # => [[1, 2], [3, 0]]
|
33
|
+
# [1, 2, 3].in_groups_of(2) # => [[1, 2], [3, nil]]
|
34
|
+
# ```
|
35
|
+
def in_groups_of(size, filled_up_with = nil)
|
36
|
+
groups = each_slice(size).to_a
|
37
|
+
groups.last.append(*[filled_up_with] * (size - groups.last.size))
|
38
|
+
groups
|
39
|
+
end
|
40
|
+
|
41
|
+
# Skips elements up to, but not including, the first element for which
|
42
|
+
# the block is falsey, and returns an `Array`
|
43
|
+
# containing the remaining elements.
|
44
|
+
#
|
45
|
+
# ```
|
46
|
+
# [1, 2, 3, 4, 5, 0].skip_while { _1 < 3 } # => [3, 4, 5, 0]
|
47
|
+
# ```
|
48
|
+
def skip_while(&)
|
49
|
+
result = []
|
50
|
+
block_returned_false = false
|
51
|
+
each do |element|
|
52
|
+
block_returned_false = true unless block_returned_false || yield(element)
|
53
|
+
result.append element if block_returned_false
|
54
|
+
end
|
55
|
+
result
|
56
|
+
end
|
57
|
+
|
58
|
+
# Converts an `Enumerable` to a `Hash` by using the value returned by the block
|
59
|
+
# as the hash key.
|
60
|
+
# Be aware, if two elements return the same value as a key one will override
|
61
|
+
# the other. If you want to keep all values, then you should probably use
|
62
|
+
# `group_by` instead.
|
63
|
+
#
|
64
|
+
# ```
|
65
|
+
# ["Anna", "Ary", "Alice"].index_by(&:size)
|
66
|
+
# # => {4 => "Anna", 3 => "Ary", 5 => "Alice"}
|
67
|
+
# ["Anna", "Ary", "Alice", "Bob"].index_by(&:size)
|
68
|
+
# # => {4 => "Anna", 3 => "Bob", 5 => "Alice"}
|
69
|
+
# ```
|
70
|
+
def index_by(&)
|
71
|
+
hash = {}
|
72
|
+
each do |element|
|
73
|
+
hash[yield element] = element
|
74
|
+
end
|
75
|
+
hash
|
76
|
+
end
|
77
|
+
|
78
|
+
# Tallies the collection. Accepts a *hash* to count occurrences.
|
79
|
+
# The value corresponding to each element must be an integer.
|
80
|
+
# Returns *hash* where the keys are the
|
81
|
+
# elements and the values are numbers of elements in the collection
|
82
|
+
# that correspond to the key after transformation by the given block.
|
83
|
+
#
|
84
|
+
# ```
|
85
|
+
# hash = {}
|
86
|
+
# words = ["Crystal", "Ruby"]
|
87
|
+
# words.each { |word| word.chars.tally_by(hash, &:downcase) }
|
88
|
+
# hash # => {"c" => 1, "r" => 2, "y" => 2, "s" => 1, "t" => 1, "a" => 1, "l" => 1, "u" => 1, "b" => 1}
|
89
|
+
# ```
|
90
|
+
def tally_by(hash = {}, &)
|
91
|
+
each_with_object(hash) do |element, object_hash|
|
92
|
+
value = yield element
|
93
|
+
|
94
|
+
object_hash[value] = object_hash.fetch(value, 0) + 1
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Returns the element for which the passed block returns with the maximum value.
|
99
|
+
#
|
100
|
+
# It compares using `>` so the block must return a type that supports that method
|
101
|
+
#
|
102
|
+
# ```
|
103
|
+
# ["Alice", "Bob"].max_by(&:size) # => "Alice"
|
104
|
+
# ```
|
105
|
+
#
|
106
|
+
# Return `nil` if the collection is empty.
|
107
|
+
def min_of?(&)
|
108
|
+
min = nil
|
109
|
+
|
110
|
+
each_with_index do |element, index|
|
111
|
+
value = yield element
|
112
|
+
min = value if index.zero? || value < min
|
113
|
+
end
|
114
|
+
|
115
|
+
min
|
116
|
+
end
|
117
|
+
|
118
|
+
# Returns the element for which the passed block returns with the minimum value.
|
119
|
+
#
|
120
|
+
# It compares using `<` so the block must return a type that supports that method
|
121
|
+
#
|
122
|
+
# ```
|
123
|
+
# ["Alice", "Bob"].min_by(&:size) # => "Bob"
|
124
|
+
# ```
|
125
|
+
#
|
126
|
+
# Return `nil` if the collection is empty.
|
127
|
+
def max_of?(&)
|
128
|
+
max = nil
|
129
|
+
|
130
|
+
each_with_index do |element, index|
|
131
|
+
value = yield element
|
132
|
+
max = value if index.zero? || value > max
|
133
|
+
end
|
134
|
+
|
135
|
+
max
|
136
|
+
end
|
137
|
+
|
138
|
+
# Returns a `Array` with both the minimum and maximum values according to the passed block.
|
139
|
+
#
|
140
|
+
# ```
|
141
|
+
# ["Alice", "Bob", "Carl"].minmax_by(&:size) # => ["Bob", "Alice"]
|
142
|
+
# ```
|
143
|
+
#
|
144
|
+
# Return `[nil, nil]` if the collection is empty.
|
145
|
+
def minmax_of?(&)
|
146
|
+
min = nil
|
147
|
+
max = nil
|
148
|
+
|
149
|
+
each_with_index do |element, index|
|
150
|
+
value = yield element
|
151
|
+
min = value if index.zero? || value < min
|
152
|
+
max = value if index.zero? || value > max
|
153
|
+
end
|
154
|
+
|
155
|
+
[min, max]
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CrystalGoodies
|
4
|
+
class Integer
|
5
|
+
# Returns `true` if `self` is divisible by *num*.
|
6
|
+
#
|
7
|
+
# ```
|
8
|
+
# 6.divisible_by? 3 # => true
|
9
|
+
# 6.divisible_by? 2 # => true
|
10
|
+
# 5.divisible_by? 3 # => false
|
11
|
+
# ```
|
12
|
+
def divisible_by?(num)
|
13
|
+
(self % num).zero?
|
14
|
+
end
|
15
|
+
|
16
|
+
# Call `upto` or `downto` depend on the *limit*.
|
17
|
+
def to(limit, &block)
|
18
|
+
self <= limit ? upto(limit, &block) : downto(limit, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CrystalGoodies
|
4
|
+
class Object
|
5
|
+
# Returns `true` if `self` is included in the *collection* argument.
|
6
|
+
#
|
7
|
+
# ```
|
8
|
+
# 10.in?(0..100) # => true
|
9
|
+
# 10.in?(0, 1, 10) # => true
|
10
|
+
# 10.in?(:foo, :bar) # => false
|
11
|
+
# ```
|
12
|
+
def in?(*collection)
|
13
|
+
(collection.one? ? collection.first : collection).include? self
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CrystalGoodies
|
4
|
+
class String
|
5
|
+
# Converts underscores to camelcase boundaries.
|
6
|
+
#
|
7
|
+
# If *lower* is true, lower camelcase will be returned (the first letter is downcased).
|
8
|
+
#
|
9
|
+
# ```
|
10
|
+
# "eiffel_tower".camelcase # => "EiffelTower"
|
11
|
+
# "empire_state_building".camelcase(lower: true) # => "empireStateBuilding"
|
12
|
+
# ```
|
13
|
+
def camelcase(*options, lower: false)
|
14
|
+
first = true
|
15
|
+
last_is_underscore = false
|
16
|
+
|
17
|
+
chars.map do |char|
|
18
|
+
if first
|
19
|
+
first = false
|
20
|
+
next lower ? char.downcase(*options) : char.upcase(*options)
|
21
|
+
end
|
22
|
+
|
23
|
+
if %w[_ -].include? char
|
24
|
+
last_is_underscore = true
|
25
|
+
next nil
|
26
|
+
end
|
27
|
+
|
28
|
+
if last_is_underscore
|
29
|
+
last_is_underscore = false
|
30
|
+
next char.upcase(*options)
|
31
|
+
end
|
32
|
+
|
33
|
+
char
|
34
|
+
end.compact.join
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns a new `String` with the first letter after any space converted to uppercase and every
|
38
|
+
# other letter converted to lowercase.
|
39
|
+
#
|
40
|
+
# ```
|
41
|
+
# "hEllO tAb\tworld".titleize # => "Hello Tab\tWorld"
|
42
|
+
# " spaces before".titleize # => " Spaces Before"
|
43
|
+
# "x-men: the last stand".titleize # => "X-men: The Last Stand"
|
44
|
+
# ```
|
45
|
+
def titleize(*options)
|
46
|
+
upcase_next = true
|
47
|
+
|
48
|
+
chars.map do |char|
|
49
|
+
titleized_char = upcase_next ? char.upcase(*options) : char.downcase(*options)
|
50
|
+
upcase_next = char.match(/\s/)
|
51
|
+
titleized_char
|
52
|
+
end.join
|
53
|
+
end
|
54
|
+
|
55
|
+
# Converts camelcase boundaries to underscores.
|
56
|
+
#
|
57
|
+
# ```
|
58
|
+
# "DoesWhatItSaysOnTheTin".underscore # => "does_what_it_says_on_the_tin"
|
59
|
+
# "PartyInTheUSA".underscore # => "party_in_the_usa"
|
60
|
+
# "HTTP_CLIENT".underscore # => "http_client"
|
61
|
+
# "3.14IsPi".underscore # => "3.14_is_pi"
|
62
|
+
# ```
|
63
|
+
def underscore(*options)
|
64
|
+
<<-FIXME
|
65
|
+
|
66
|
+
string = ''
|
67
|
+
first = true
|
68
|
+
last_is_downcase = false
|
69
|
+
last_is_upcase = false
|
70
|
+
last_is_digit = false
|
71
|
+
mem = nil
|
72
|
+
|
73
|
+
each_char do |char|
|
74
|
+
if options.ascii?
|
75
|
+
digit = char.ascii_number?
|
76
|
+
downcase = digit || char.ascii_lowercase?
|
77
|
+
upcase = char.ascii_uppercase?
|
78
|
+
else
|
79
|
+
digit = char.number?
|
80
|
+
downcase = digit || char.lowercase?
|
81
|
+
upcase = char.uppercase?
|
82
|
+
end
|
83
|
+
|
84
|
+
if first
|
85
|
+
char.downcase(options) { |c| string << c }
|
86
|
+
elsif last_is_downcase && upcase
|
87
|
+
if mem
|
88
|
+
# This is the case of A1Bcd, we need to put 'mem' (not to need to convert as downcase
|
89
|
+
# ^
|
90
|
+
# because 'mem' is digit surely) before putting this char as downcase.
|
91
|
+
string << mem
|
92
|
+
mem = nil
|
93
|
+
end
|
94
|
+
# This is the case of AbcDe, we need to put an underscore before the 'D'
|
95
|
+
# ^
|
96
|
+
string << '_'
|
97
|
+
char.downcase(options) { |c| string << c }
|
98
|
+
elsif (last_is_upcase || last_is_digit) && (upcase || digit)
|
99
|
+
# This is the case of 1) A1Bcd, 2) A1BCd or 3) A1B_cd:if the next char is upcase (case 1) we need
|
100
|
+
# ^ ^ ^
|
101
|
+
# 1) we need to append this char as downcase
|
102
|
+
# 2) we need to append an underscore and then the char as downcase, so we save this char
|
103
|
+
# in 'mem' and decide later
|
104
|
+
# 3) we need to append this char as downcase and then a single underscore
|
105
|
+
if mem
|
106
|
+
# case 2
|
107
|
+
mem.downcase(options) { |c| string << c }
|
108
|
+
end
|
109
|
+
mem = char
|
110
|
+
else
|
111
|
+
if mem
|
112
|
+
if char == '_'
|
113
|
+
# case 3
|
114
|
+
elsif last_is_upcase && downcase
|
115
|
+
# case 1
|
116
|
+
string << '_'
|
117
|
+
end
|
118
|
+
mem.downcase(options) { |c| string << c }
|
119
|
+
mem = nil
|
120
|
+
end
|
121
|
+
|
122
|
+
char.downcase(options) { |c| string << c }
|
123
|
+
end
|
124
|
+
|
125
|
+
last_is_downcase = downcase
|
126
|
+
last_is_upcase = upcase
|
127
|
+
last_is_digit = digit
|
128
|
+
first = false
|
129
|
+
end
|
130
|
+
|
131
|
+
mem&.downcase(options) { |c| string << c }
|
132
|
+
|
133
|
+
FIXME
|
134
|
+
|
135
|
+
self
|
136
|
+
end
|
137
|
+
|
138
|
+
# Converts camelcase boundaries to kebabcase.
|
139
|
+
#
|
140
|
+
# ```
|
141
|
+
# "DoesWhatItSaysOnTheTin".dasherize # => "does-what-it-says-on-the-tin"
|
142
|
+
# "PartyInTheUSA".dasherize # => "party-in-the-usa"
|
143
|
+
# "HTTP_CLIENT".dasherize # => "http-client"
|
144
|
+
# "3.14IsPi".dasherize # => "3.14-is-pi"
|
145
|
+
# ```
|
146
|
+
def dasherize
|
147
|
+
underscore.tr('_', '-')
|
148
|
+
end
|
149
|
+
|
150
|
+
# Returns `true` if this string consists exclusively of unicode whitespace.
|
151
|
+
#
|
152
|
+
# ```
|
153
|
+
# "".blank? # => true
|
154
|
+
# " ".blank? # => true
|
155
|
+
# " a ".blank? # => false
|
156
|
+
# ```
|
157
|
+
def blank?
|
158
|
+
each_char do |char|
|
159
|
+
return false unless char.match(/\s/)
|
160
|
+
end
|
161
|
+
true
|
162
|
+
end
|
163
|
+
|
164
|
+
# Returns `self` unless `#blank?` is `true` in which case it returns `nil`.
|
165
|
+
#
|
166
|
+
# ```
|
167
|
+
# "a".presence # => "a"
|
168
|
+
# "".presence # => nil
|
169
|
+
# " ".presence # => nil
|
170
|
+
# " a ".presence # => " a "
|
171
|
+
# ```
|
172
|
+
def presence
|
173
|
+
self unless blank?
|
174
|
+
end
|
175
|
+
|
176
|
+
# Returns a new string that results from deleting characters with `slice`.
|
177
|
+
def delete_at(...)
|
178
|
+
deleted_string = self
|
179
|
+
deleted_string.slice!(...)
|
180
|
+
deleted_string
|
181
|
+
end
|
182
|
+
|
183
|
+
# Call `upto` or `downto` depend on the *limit*.
|
184
|
+
def to(limit, exclusive = false, &block)
|
185
|
+
self <= limit ? upto(limit, exclusive, &block) : downto(limit, exclusive, &block)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'crystal_goodies/version'
|
4
|
+
|
5
|
+
require_relative 'crystal_goodies/array/extensions'
|
6
|
+
require_relative 'crystal_goodies/enumerable/extensions'
|
7
|
+
require_relative 'crystal_goodies/integer/extensions'
|
8
|
+
require_relative 'crystal_goodies/object/extensions'
|
9
|
+
require_relative 'crystal_goodies/string/extensions'
|
data/sig/array.rbs
ADDED
data/sig/enumerable.rbs
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module CrystalGoodies
|
2
|
+
module Enumerable
|
3
|
+
def empty?: () -> bool
|
4
|
+
|
5
|
+
def compact_map: () -> Enumerator
|
6
|
+
| () { () -> void } -> Array
|
7
|
+
|
8
|
+
def in_groups_of: (Integer size, ?untyped? filled_up_with) -> Array
|
9
|
+
|
10
|
+
def skip_while: () -> Enumerator
|
11
|
+
| () { (untyped) -> bool } -> Array
|
12
|
+
|
13
|
+
def index_by: () -> Enumerator
|
14
|
+
| () { (untyped) -> untyped } -> Hash
|
15
|
+
|
16
|
+
def tally_by: (?Hash[untyped, Integer] hash) -> Enumerator
|
17
|
+
| (?Hash[untyped, Integer] hash) { (untyped) -> untyped } -> Hash
|
18
|
+
|
19
|
+
def min_of?: () -> Enumerator
|
20
|
+
| () { (untyped) -> untyped } -> untyped
|
21
|
+
|
22
|
+
def max_of?: () -> Enumerator
|
23
|
+
| () { (untyped) -> untyped } -> untyped
|
24
|
+
|
25
|
+
def minmax_of?: () -> Enumerator
|
26
|
+
| () { (untyped) -> untyped } -> Array[untyped]
|
27
|
+
end
|
28
|
+
end
|
data/sig/integer.rbs
ADDED
data/sig/object.rbs
ADDED
data/sig/string.rbs
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module CrystalGoodies
|
2
|
+
class String
|
3
|
+
def camelcase: (*Symbol options, ?lower: bool) -> String
|
4
|
+
|
5
|
+
def titleize: (*Symbol options) -> String
|
6
|
+
|
7
|
+
def underscore: (*Symbol options) -> String
|
8
|
+
|
9
|
+
def dasherize: () -> String
|
10
|
+
|
11
|
+
def blank?: () -> bool
|
12
|
+
|
13
|
+
def presence: () -> (self | nil)
|
14
|
+
|
15
|
+
def delete_at: (Integer index) -> String
|
16
|
+
| (Integer start, Integer length) -> String
|
17
|
+
| (Range range) -> String
|
18
|
+
| (Regexp regexp, ?Integer capture) -> String
|
19
|
+
| (String substring) -> String
|
20
|
+
|
21
|
+
def to: (String limit, ?bool exclusive) -> Enumerator
|
22
|
+
| (String limit, ?bool exclusive) { (String) -> void } -> self
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crystal_goodies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- NNB
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Class's methods port from Crystal.
|
14
|
+
email:
|
15
|
+
- nnbnh@protonmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.md
|
20
|
+
- CHANGELOG.md
|
21
|
+
- LICENSE.txt
|
22
|
+
files:
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- CHANGELOG.md
|
25
|
+
- CODE_OF_CONDUCT.md
|
26
|
+
- Gemfile
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- lib/crystal_goodies.rb
|
31
|
+
- lib/crystal_goodies/array.rb
|
32
|
+
- lib/crystal_goodies/array/extensions.rb
|
33
|
+
- lib/crystal_goodies/enumerable.rb
|
34
|
+
- lib/crystal_goodies/enumerable/extensions.rb
|
35
|
+
- lib/crystal_goodies/integer.rb
|
36
|
+
- lib/crystal_goodies/integer/extensions.rb
|
37
|
+
- lib/crystal_goodies/object.rb
|
38
|
+
- lib/crystal_goodies/object/extensions.rb
|
39
|
+
- lib/crystal_goodies/string.rb
|
40
|
+
- lib/crystal_goodies/string/extensions.rb
|
41
|
+
- lib/crystal_goodies/version.rb
|
42
|
+
- sig/array.rbs
|
43
|
+
- sig/enumerable.rbs
|
44
|
+
- sig/integer.rbs
|
45
|
+
- sig/object.rbs
|
46
|
+
- sig/string.rbs
|
47
|
+
homepage: https://codeberg.org/NNB/crystal_goodies.rb
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata:
|
51
|
+
homepage_uri: https://codeberg.org/NNB/crystal_goodies.rb
|
52
|
+
source_code_uri: https://codeberg.org/NNB/crystal_goodies.rb
|
53
|
+
changelog_uri: https://codeberg.org/NNB/crystal_goodies.rb/src/branch/main/CHANGELOG.md
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '3.1'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.4.10
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Crystal goodies port to Ruby.
|
73
|
+
test_files: []
|