natural_sort_jp 0.2.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6423ce38fe5c382b916dcbab85f70e7866cc55c22679f62b8d5d0384a40b8a0e
4
- data.tar.gz: e135ba990b7a7ea8bcde936e68b35a789b0259b43a6dc9c1cf4d8b702301a87a
3
+ metadata.gz: '06468e39e925af897dbcf27ee6c04ec4e8941d89d3d3ce0bd17021414568f2c6'
4
+ data.tar.gz: 1c5845917fdabd93bbb3dfa51f875d04256999d903967ec1270ebac00316087c
5
5
  SHA512:
6
- metadata.gz: d0b7099de4c68efbfca4ff8a8b12c387cf36a91ffc32998922bdbfe1c89c8bccd9f45c2dd45af034d56c1d1ccbcabeb46b1c66a2a703f51ee7a3ef5cb9ecfa3b
7
- data.tar.gz: f868b9928ad667576badcfa8dfce189172729a7bbdc1a1269316ea2f8eebb7f184d4acc186b009dd7b54e6cab2de19e40b8536d935fc946b9529f365c20d52d2
6
+ metadata.gz: e1ad6d01f561694ebc8836424a9ebbb32205199da71d4a8436c385db100fb0dbc8d26bb794b0215b51ad08feb34cc6bd1f10f09384a493fbb96b908bf0fe271f
7
+ data.tar.gz: f304379728764ea4a660b339a198f0a34b5bd3ee189fb79852f4b397b4c6a1cd9dc4d91c650b58bf1ded07efa5923ec0c42b0a8c4cf105ee66c21a1858c8a884
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- natural_sort_jp (0.1.0)
4
+ natural_sort_jp (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # NaturalSortJp
2
2
 
3
- **natural_sort_jp** can discriminate between full-width and half-width numbers (zenkaku and hankaku), which are unique to the Japanese language, and perform natural sorting.
3
+ **natural_sort_jp** can discriminate between full-width and half-width characters (zenkaku and hankaku), which are unique to the Japanese language, and perform natural sorting.
4
4
 
5
5
  By using this gem, you can realize the special sorting (natural sort) used in file explorers such as google drive.
6
6
 
@@ -15,7 +15,7 @@ $ gem install natural_sort_jp
15
15
  ```ruby
16
16
  require 'natural_sort_jp'
17
17
 
18
- # Can use natural sort even if mix of zenkaku and hankaku.
18
+ # Can use natural sort even if mix of zenkaku and hankaku number.
19
19
 
20
20
  NaturalSortJp.sort(
21
21
  ["第1回", "第2回", "第3回", "第10回", "第20回", "第21回"]
@@ -23,6 +23,12 @@ NaturalSortJp.sort(
23
23
  # => ["第1回", "第2回", "第3回", "第10回", "第20回", "第21回"]
24
24
 
25
25
 
26
+ # even if mix of zenkaku and hankaku alphabet.
27
+
28
+ NaturalSortJp.sort(["a11", "a2", "a10", "a1"])
29
+ # => ["a1", "a2", "a10", "a11"]
30
+
31
+
26
32
  # It is not affected by the presence or absence of 0-filling.
27
33
 
28
34
  NaturalSortJp.sort(
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NaturalSortJp
4
- VERSION = "0.2.0"
4
+ VERSION = '0.4.0'
5
5
  end
@@ -4,12 +4,26 @@ require 'natural_sort_jp/element'
4
4
 
5
5
  module NaturalSortJp
6
6
 
7
- def self.sort(array)
8
- array.sort_by { |x| convert(x) }
7
+ def self.sort(array, by: nil, desc: false)
8
+ array = by ? self.sort_by(array, by) : array.sort_by { |x| convert(x) }
9
+ return array.reverse if desc
10
+ array
11
+ end
12
+
13
+ def self.sort_by(array, attribute)
14
+ array.sort_by { |obj| convert(referenced_by_attribute(obj, attribute)) }
9
15
  end
10
16
 
11
17
  def self.convert(title)
12
18
  elements = title.to_s.gsub(/[0-90-9]+/, ',\&,').split(',')
13
19
  elements.map { |t| Element.new(t) }
14
20
  end
21
+
22
+ def self.referenced_by_attribute(obj, attribute)
23
+ if obj.class == Hash
24
+ obj[attribute]
25
+ else
26
+ obj.send(attribute)
27
+ end
28
+ end
15
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: natural_sort_jp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thehighhigh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-07 00:00:00.000000000 Z
11
+ date: 2023-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler