naturally 2.2.2 → 2.3.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 +4 -4
- data/CHANGELOG.md +31 -0
- data/README.md +33 -0
- data/lib/naturally/version.rb +1 -1
- data/lib/naturally.rb +10 -0
- data/spec/naturally_spec.rb +12 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9438825196cf2910c3580ac61f37ff9a38faa29df8c113268ccf2835ecc0736b
|
4
|
+
data.tar.gz: 7ea180458e34813435963b22fbed22e68a28c0d8ff025bf471116dc576e5eb88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f74680fdecc119d32b2ad624f819972086961dd9d394b86f5f0897ec414a0fa12d1eec9902c0a207106423a011f7fedf8f277c90f91ffb053c2afeaa952925cc
|
7
|
+
data.tar.gz: f5f5979bb76c14ed2e5494043090d091eb17b5684930006291dfe7fb19c43544b629c0f84ec003d412c20e540075b7100f51309fd2c6113fb99f38f34bfa20ba
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [2.3.0] - 2025-06-11
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- New `#sort_filenames` method for sorting filenames with underscores and dots as separators
|
12
|
+
- Dots are given higher priority than underscores in filename sorting
|
13
|
+
- Comprehensive test coverage for filename sorting scenarios
|
14
|
+
|
15
|
+
### Changed
|
16
|
+
- Updated README with documentation and examples for the new `#sort_filenames` method
|
17
|
+
|
18
|
+
## [2.2.2] - Previous Release
|
19
|
+
|
20
|
+
### Notes
|
21
|
+
- Previous stable release
|
22
|
+
- Included support for natural sorting with legal numbering, course codes, and Unicode
|
23
|
+
- Maintained compatibility with Ruby 2.0+
|
24
|
+
|
25
|
+
## Historical Releases
|
26
|
+
|
27
|
+
For releases prior to 2.2.2, please refer to the git commit history.
|
28
|
+
|
29
|
+
---
|
30
|
+
|
31
|
+
**Note**: This changelog was introduced in version 2.3.0. Future releases will document all changes here.
|
data/README.md
CHANGED
@@ -61,6 +61,39 @@ expect(sorted.map(&:name)).to eq [
|
|
61
61
|
[More examples are in the specs](https://github.com/public-law/naturally/blob/master/spec/naturally_spec.rb).
|
62
62
|
|
63
63
|
|
64
|
+
## `#sort_filenames`
|
65
|
+
|
66
|
+
Sorts filenames naturally, treating underscores and dots as separators. Useful for sorting files like images or documents that use numbers, underscores, and dots in their names.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
files = [
|
70
|
+
'abc_2.tif',
|
71
|
+
'abc_1_a.tif',
|
72
|
+
'abc_1.zzz',
|
73
|
+
'abc_1_xyz.abc',
|
74
|
+
'abc_2a.tif',
|
75
|
+
'abc.2.tif',
|
76
|
+
'abc.1_a.tif',
|
77
|
+
'abc_2.abc',
|
78
|
+
'abc_1_xyz.abc'
|
79
|
+
]
|
80
|
+
Naturally.sort_filenames(files)
|
81
|
+
# => [
|
82
|
+
# "abc.1_a.tif",
|
83
|
+
# "abc.2.tif",
|
84
|
+
# "abc_1.zzz",
|
85
|
+
# "abc_1_a.tif",
|
86
|
+
# "abc_1_xyz.abc",
|
87
|
+
# "abc_1_xyz.abc",
|
88
|
+
# "abc_2.abc",
|
89
|
+
# "abc_2.tif",
|
90
|
+
# "abc_2a.tif"
|
91
|
+
# ]
|
92
|
+
```
|
93
|
+
|
94
|
+
This method gives higher priority to dots than underscores, so files like `abc.1_a.tif` will come before `abc.2.tif`.
|
95
|
+
|
96
|
+
|
64
97
|
## Implementation Notes
|
65
98
|
|
66
99
|
The algorithm capitalizes on Ruby's array comparison behavior:
|
data/lib/naturally/version.rb
CHANGED
data/lib/naturally.rb
CHANGED
@@ -49,6 +49,16 @@ module Naturally
|
|
49
49
|
tokens.map { |t| Segment.new(t) }
|
50
50
|
end
|
51
51
|
|
52
|
+
# Natural sort for filenames, treating underscores and dots as separators.
|
53
|
+
# Dots are given higher priority by inserting a '0' segment, as in user workaround.
|
54
|
+
# See Issue #24.
|
55
|
+
def self.sort_filenames(array)
|
56
|
+
array.sort_by do |filename|
|
57
|
+
# Replace '.' with '.0.' to give dot higher priority, then split on dot or underscore
|
58
|
+
filename.to_s.gsub('.', '.0.').split(/[\._]/).map { |t| Segment.new(t) }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
52
62
|
private
|
53
63
|
# Sort an array of objects "naturally", yielding each object
|
54
64
|
# to the block to obtain the sort key.
|
data/spec/naturally_spec.rb
CHANGED
@@ -208,4 +208,16 @@ describe Naturally do
|
|
208
208
|
]
|
209
209
|
end
|
210
210
|
end
|
211
|
+
|
212
|
+
describe '#sort_filenames' do
|
213
|
+
it 'sorts filenames with underscores and dots as separators' do
|
214
|
+
expect(Naturally.sort_filenames(['abc_2.tif', 'abc_1_a.tif'])).to eq(["abc_1_a.tif", "abc_2.tif"])
|
215
|
+
expect(Naturally.sort_filenames(['abc_1.zzz', 'abc_1_xyz.abc'])).to eq(["abc_1.zzz", "abc_1_xyz.abc"])
|
216
|
+
expect(Naturally.sort_filenames(['abc_2a.tif', 'abc_1_a.tif'])).to eq(["abc_1_a.tif", "abc_2a.tif"])
|
217
|
+
expect(Naturally.sort_filenames(['abc.2.tif', 'abc.1_a.tif'])).to eq(["abc.1_a.tif", "abc.2.tif"])
|
218
|
+
expect(Naturally.sort_filenames(['abc_2.tif', 'abc_1.tif'])).to eq(["abc_1.tif", "abc_2.tif"])
|
219
|
+
expect(Naturally.sort_filenames(['abc_2.tif', 'abc_1_a.tif'])).to eq(["abc_1_a.tif", "abc_2.tif"])
|
220
|
+
expect(Naturally.sort_filenames(['abc_2.abc', 'abc_1_xyz.abc'])).to eq(["abc_1_xyz.abc", "abc_2.abc"])
|
221
|
+
end
|
222
|
+
end
|
211
223
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: naturally
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robb Shecter
|
@@ -19,6 +19,7 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- ".gitignore"
|
21
21
|
- ".travis.yml"
|
22
|
+
- CHANGELOG.md
|
22
23
|
- Gemfile
|
23
24
|
- LICENSE.txt
|
24
25
|
- README.md
|