string-format 0.1.0
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 +7 -0
- checksums.yaml.gz.sig +0 -0
- data/lib/string/format/count.rb +37 -0
- data/lib/string/format/decimal.rb +16 -0
- data/lib/string/format/ratio.rb +16 -0
- data/lib/string/format/snake_case.rb +20 -0
- data/lib/string/format/statistics.rb +24 -0
- data/lib/string/format/title_case.rb +18 -0
- data/lib/string/format/version.rb +12 -0
- data/lib/string/format.rb +15 -0
- data/license.md +21 -0
- data/readme.md +35 -0
- data/releases.md +3 -0
- data.tar.gz.sig +0 -0
- metadata +79 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b29841a689a4ec3e6fba4c7aad34fd683db3ed2b0b012ddd6e63d16b009a1ec4
|
4
|
+
data.tar.gz: a1b5eba4869899ce137f364b0508d6d4606b13f2d3a940646887272b8080a3c6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd7c94eb13f3809660790154f460c869d21c760d33f941b2e812d22cb66fc037ef182d5eb74e640d64c2fa26476d8ec08e2e7793095b08f867dfa4ea0c775c23
|
7
|
+
data.tar.gz: 6cbf6163f08194f0b56df5ba03f63d6a2c4aaaaf0b4d614de8626e8cb8ce9a58e9df6046fa4fe6572c86009711d823620ba87cea3ee6d1479518f1a23822b723
|
checksums.yaml.gz.sig
ADDED
Binary file
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
5
|
+
|
6
|
+
module String::Format
|
7
|
+
UNITS = [nil, "K", "M", "B", "T", "P", "E", "Z", "Y"]
|
8
|
+
|
9
|
+
# Format a count into a human-readable string.
|
10
|
+
# @parameter value [Numeric] The count to format.
|
11
|
+
# @parameter units [Array] The units to use for formatting (default: UNITS).
|
12
|
+
# @parameter scale [Numeric] The scaling factor between units (default: 1000).
|
13
|
+
# @returns [String] A formatted string representing the count.
|
14
|
+
def count(value, units = UNITS, scale: 1000)
|
15
|
+
value = value
|
16
|
+
index = 0
|
17
|
+
limit = units.size - 1
|
18
|
+
|
19
|
+
# Handle negative numbers by working with absolute value:
|
20
|
+
negative = value < 0
|
21
|
+
value = value.abs
|
22
|
+
|
23
|
+
while value >= scale and index < limit
|
24
|
+
value = value / scale.to_f
|
25
|
+
index += 1
|
26
|
+
end
|
27
|
+
|
28
|
+
result = String.new
|
29
|
+
result << "-" if negative
|
30
|
+
result << value.round(2).to_s
|
31
|
+
result << units[index].to_s if units[index]
|
32
|
+
|
33
|
+
return result
|
34
|
+
end
|
35
|
+
|
36
|
+
module_function :count
|
37
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
5
|
+
|
6
|
+
module String::Format
|
7
|
+
# Format a numeric value as a decimal with specified precision.
|
8
|
+
# @parameter value [Numeric] The numeric value to format.
|
9
|
+
# @parameter precision [Integer] Number of decimal places (default: 2).
|
10
|
+
# @returns [String] A formatted decimal string.
|
11
|
+
def decimal(value, precision: 2)
|
12
|
+
value.round(precision).to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
module_function :decimal
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
5
|
+
|
6
|
+
module String::Format
|
7
|
+
# Format a ratio as "current/total" with human-readable counts.
|
8
|
+
# @parameter current [Numeric] The current value.
|
9
|
+
# @parameter total [Numeric] The total value.
|
10
|
+
# @returns [String] A formatted ratio string.
|
11
|
+
def ratio(current, total)
|
12
|
+
"#{count(current)}/#{count(total)}"
|
13
|
+
end
|
14
|
+
|
15
|
+
module_function :ratio
|
16
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
5
|
+
|
6
|
+
module String::Format
|
7
|
+
# Convert a string to `snake_case`.
|
8
|
+
# @parameter string [String] The string to convert.
|
9
|
+
# @returns [String] A snake_cased string.
|
10
|
+
def snake_case(string)
|
11
|
+
string = string.gsub("::", "")
|
12
|
+
string.gsub!(/(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/, "_")
|
13
|
+
string.downcase!
|
14
|
+
string.sub!(/^_+/, "")
|
15
|
+
|
16
|
+
return string
|
17
|
+
end
|
18
|
+
|
19
|
+
module_function :snake_case
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
5
|
+
|
6
|
+
module String::Format
|
7
|
+
# Format multiple statistics into a compact string.
|
8
|
+
# @parameter pairs [Hash] Hash of statistic names to values or [current, total] arrays.
|
9
|
+
# @returns [String] A formatted statistics string.
|
10
|
+
def statistics(**pairs)
|
11
|
+
pairs.map do |key, value|
|
12
|
+
case value
|
13
|
+
when Array
|
14
|
+
value = value.map(&method(:count)).join("/")
|
15
|
+
when Numeric
|
16
|
+
value = count(value)
|
17
|
+
end
|
18
|
+
|
19
|
+
"#{key.to_s.upcase}=#{value}"
|
20
|
+
end.join(" ")
|
21
|
+
end
|
22
|
+
|
23
|
+
module_function :statistics
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
5
|
+
|
6
|
+
module String::Format
|
7
|
+
# Convert a string to title case.
|
8
|
+
# @parameter string [String] The string to convert.
|
9
|
+
# @returns [String] A title-cased string.
|
10
|
+
def title_case(string)
|
11
|
+
string = string.gsub(/(^|[ \-_])(.)/){" " + $2.upcase}
|
12
|
+
string.strip!
|
13
|
+
|
14
|
+
return string
|
15
|
+
end
|
16
|
+
|
17
|
+
module_function :title_case
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
5
|
+
|
6
|
+
require_relative "format/version"
|
7
|
+
require_relative "format/count"
|
8
|
+
require_relative "format/ratio"
|
9
|
+
require_relative "format/decimal"
|
10
|
+
require_relative "format/title_case"
|
11
|
+
require_relative "format/snake_case"
|
12
|
+
require_relative "format/statistics"
|
13
|
+
|
14
|
+
module String::Format
|
15
|
+
end
|
data/license.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# MIT License
|
2
|
+
|
3
|
+
Copyright, 2025, by Samuel Williams.
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/readme.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# String::Format
|
2
|
+
|
3
|
+
Formatting utilities for strings, providing consistent human-readable formatting for numbers, counts, ratios, and statistics.
|
4
|
+
|
5
|
+
[](https://github.com/socketry/string-format/actions?workflow=Test)
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
Please see the [project documentation](https://socketry.github.io/string-format/) for more details.
|
10
|
+
|
11
|
+
- [Getting Started](https://socketry.github.io/string-format/guides/getting-started/index) - This guide explains how to get started with `string-format` to format numbers, ratios, and strings for display purposes.
|
12
|
+
|
13
|
+
## Releases
|
14
|
+
|
15
|
+
Please see the [project releases](https://socketry.github.io/string-format/releases/index) for all releases.
|
16
|
+
|
17
|
+
### v0.1.0
|
18
|
+
|
19
|
+
## Contributing
|
20
|
+
|
21
|
+
We welcome contributions to this project.
|
22
|
+
|
23
|
+
1. Fork it.
|
24
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
25
|
+
3. Commit your changes (`git commit -am 'Add some feature'`).
|
26
|
+
4. Push to the branch (`git push origin my-new-feature`).
|
27
|
+
5. Create new Pull Request.
|
28
|
+
|
29
|
+
### Developer Certificate of Origin
|
30
|
+
|
31
|
+
In order to protect users of this project, we require all contributors to comply with the [Developer Certificate of Origin](https://developercertificate.org/). This ensures that all contributions are properly licensed and attributed.
|
32
|
+
|
33
|
+
### Community Guidelines
|
34
|
+
|
35
|
+
This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
|
data/releases.md
ADDED
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string-format
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Williams
|
8
|
+
bindir: bin
|
9
|
+
cert_chain:
|
10
|
+
- |
|
11
|
+
-----BEGIN CERTIFICATE-----
|
12
|
+
MIIE2DCCA0CgAwIBAgIBATANBgkqhkiG9w0BAQsFADBhMRgwFgYDVQQDDA9zYW11
|
13
|
+
ZWwud2lsbGlhbXMxHTAbBgoJkiaJk/IsZAEZFg1vcmlvbnRyYW5zZmVyMRIwEAYK
|
14
|
+
CZImiZPyLGQBGRYCY28xEjAQBgoJkiaJk/IsZAEZFgJuejAeFw0yMjA4MDYwNDUz
|
15
|
+
MjRaFw0zMjA4MDMwNDUzMjRaMGExGDAWBgNVBAMMD3NhbXVlbC53aWxsaWFtczEd
|
16
|
+
MBsGCgmSJomT8ixkARkWDW9yaW9udHJhbnNmZXIxEjAQBgoJkiaJk/IsZAEZFgJj
|
17
|
+
bzESMBAGCgmSJomT8ixkARkWAm56MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIB
|
18
|
+
igKCAYEAomvSopQXQ24+9DBB6I6jxRI2auu3VVb4nOjmmHq7XWM4u3HL+pni63X2
|
19
|
+
9qZdoq9xt7H+RPbwL28LDpDNflYQXoOhoVhQ37Pjn9YDjl8/4/9xa9+NUpl9XDIW
|
20
|
+
sGkaOY0eqsQm1pEWkHJr3zn/fxoKPZPfaJOglovdxf7dgsHz67Xgd/ka+Wo1YqoE
|
21
|
+
e5AUKRwUuvaUaumAKgPH+4E4oiLXI4T1Ff5Q7xxv6yXvHuYtlMHhYfgNn8iiW8WN
|
22
|
+
XibYXPNP7NtieSQqwR/xM6IRSoyXKuS+ZNGDPUUGk8RoiV/xvVN4LrVm9upSc0ss
|
23
|
+
RZ6qwOQmXCo/lLcDUxJAgG95cPw//sI00tZan75VgsGzSWAOdjQpFM0l4dxvKwHn
|
24
|
+
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Z
|
40
|
+
dependencies: []
|
41
|
+
executables: []
|
42
|
+
extensions: []
|
43
|
+
extra_rdoc_files: []
|
44
|
+
files:
|
45
|
+
- lib/string/format.rb
|
46
|
+
- lib/string/format/count.rb
|
47
|
+
- lib/string/format/decimal.rb
|
48
|
+
- lib/string/format/ratio.rb
|
49
|
+
- lib/string/format/snake_case.rb
|
50
|
+
- lib/string/format/statistics.rb
|
51
|
+
- lib/string/format/title_case.rb
|
52
|
+
- lib/string/format/version.rb
|
53
|
+
- license.md
|
54
|
+
- readme.md
|
55
|
+
- releases.md
|
56
|
+
homepage: https://github.com/socketry/string-format
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata:
|
60
|
+
documentation_uri: https://socketry.github.io/string-format/
|
61
|
+
source_code_uri: https://github.com/socketry/string-format.git
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.2'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubygems_version: 3.6.9
|
77
|
+
specification_version: 4
|
78
|
+
summary: Formatting utilities for strings.
|
79
|
+
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|