natural_sort_jp 0.1.0 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 576ae58c108d4b0db825ec54466d2bdd04ff4bea067d0232ed0a279d5f3b8680
4
- data.tar.gz: bdcc15aebf0562dfed95ac4807c66e16d10dbe8aebc3d850879e3c8adfdfbda5
3
+ metadata.gz: 517205030e6c52eda20e34cebbaab096422d48b623875bc5631c4a62f685db93
4
+ data.tar.gz: f69e154214ad3147a2aa34f9ff3bb49bf2d38a03f411ee2b7b41820313eeacf9
5
5
  SHA512:
6
- metadata.gz: 2b5d4da03645efd69312a451d35585553595046ef4f16ec2f4132887680e4e52b22ab7f4cc58cf03cea49eb2de7223214fa9402ee15235687aa035df6d113fb1
7
- data.tar.gz: c5bfd0fb25d3617aec1a23e2d871148f775105d3a2da17b48b5823a54e8112110786e042fa076188498e7104c3f7861b9539b9879cbd2b34369082e959ab090e
6
+ metadata.gz: 3a7c9e5437dcd875d69c1c2af89df68e6aea62a3f0dd5ec47964ae407356baf739a461de04e238a04c6932c39381e05de3bcfe484bd6e5c7ac51fc334dbbc897
7
+ data.tar.gz: 11d661797d8c26ec40ac3c7468b7d4ac02d747c87d5010811edb36cafa435c38245cc62629db9778b2238454246dbf4f870e4f9053bae875b6c48cc6cafe0835
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.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,25 +1,43 @@
1
1
  # NaturalSortJp
2
2
 
3
- natural_sort_jp can discriminate between full-width and half-width numbers, 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
 
7
7
  ## Installation
8
8
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
9
+ ```
10
+ $ gem install natural_sort_jp
11
+ ```
10
12
 
11
- Install the gem and add to the application's Gemfile by executing:
13
+ ## Usage
12
14
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
15
+ ```ruby
16
+ require 'natural_sort_jp'
14
17
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
18
+ # Can use natural sort even if mix of zenkaku and hankaku number.
16
19
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
20
+ NaturalSortJp.sort(
21
+ ["第1回", "第2回", "第3回", "第10回", "第20回", "第21回"]
22
+ )
23
+ # => ["第1回", "第2回", "第3回", "第10回", "第20回", "第21回"]
18
24
 
19
- ## Usage
20
25
 
21
- TODO: Write usage instructions here
26
+ # even if mix of zenkaku and hankaku alphabet.
27
+
28
+ NaturalSortJp.sort(["a11", "a2", "a10", "a1"])
29
+ # => ["a1", "a2", "a10", "a11"]
30
+
31
+
32
+ # It is not affected by the presence or absence of 0-filling.
33
+
34
+ NaturalSortJp.sort(
35
+ ["2022/1/1", "2022/01/02", "2022/01/03", "2022/1/10", "2023/1/1"]
36
+ )
37
+ # => ["2022/1/1", "2022/01/02", "2022/01/03", "2022/1/10", "2023/1/1"]
38
+
39
+ ```
22
40
 
23
41
  ## Contributing
24
42
 
25
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/natural_sort_jp.
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thehighhigh/natural_sort_jp.
@@ -7,8 +7,7 @@ module NaturalSortJp
7
7
  SORT_PRIORITY = { int: 1, str: 2 }.freeze
8
8
 
9
9
  def initialize(val)
10
- val.tr!('0-9', '0-9') if val =~ /[0-9]/
11
- @val = val
10
+ @val = zenkaku2hankaku(val)
12
11
  end
13
12
 
14
13
  def <=>(other)
@@ -22,5 +21,14 @@ module NaturalSortJp
22
21
  [SORT_PRIORITY[:str], @val]
23
22
  end
24
23
  end
24
+
25
+ private
26
+
27
+ def zenkaku2hankaku(val)
28
+ val.tr!('0-9', '0-9') if val =~ /[0-9]/
29
+ val.tr!('a-z', 'a-z') if val =~ /[a-z]/
30
+ val.tr!('A-Z', 'A-Z') if val =~ /[A-Z]/
31
+ val
32
+ end
25
33
  end
26
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NaturalSortJp
4
- VERSION = "0.1.0"
4
+ VERSION = '0.3.0'
5
5
  end
@@ -4,12 +4,28 @@ 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)
8
+ if by
9
+ self.sort_by(array, by)
10
+ else
11
+ array.sort_by { |x| convert(x) }
12
+ end
13
+ end
14
+
15
+ def self.sort_by(array, attribute)
16
+ array.sort_by { |obj| convert(referenced_by_attribute(obj, attribute)) }
9
17
  end
10
18
 
11
19
  def self.convert(title)
12
20
  elements = title.to_s.gsub(/[0-90-9]+/, ',\&,').split(',')
13
21
  elements.map { |t| Element.new(t) }
14
22
  end
23
+
24
+ def self.referenced_by_attribute(obj, attribute)
25
+ if obj.class == Hash
26
+ obj[attribute]
27
+ else
28
+ obj.send(attribute)
29
+ end
30
+ end
15
31
  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.1.0
4
+ version: 0.3.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