zen_to_i 0.1.0 → 0.2.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: 3839de0a246d873410c9acd82dc4d4317c08112c12c9e3593e847cc61b6fff80
4
- data.tar.gz: 79de8b4c92b8c373b3c0d10ae46e381cd265d5579630206302b59b571d5feeb8
3
+ metadata.gz: c17322f7e45062265d402a73f26fdb4204e39fd8385feb3dd69746956703e720
4
+ data.tar.gz: 8531336638b87ad8dcd182d7b8692ffe6403830c6c3203375bde8f64c34d4109
5
5
  SHA512:
6
- metadata.gz: 7ad359f8a19fc3ec4db4e9cb0be5d589a7405f2b7d35f234326991f0ea0a939525f1e31f6140748939d70b86a9928bac17e4ff3cec2a3c0a1f90b21dad32a52f
7
- data.tar.gz: 5253f8272185f802f772b5145923e842236690324e69c5623c468fed4420c0cdb85c5c432ea285b09c6b920ea1c3eee7229e200963ad09a193a289b30ddf833e
6
+ metadata.gz: 2db39986d5ce76e554a94945dabe40363d9c3fa6966aabbb605a25ba3409b4beadffbc90095173db3369e307bcc570186c9849c8de2d5cee74f4ce9c269bdb7b
7
+ data.tar.gz: 2d8c718b07f4d40aa1fc77ab901dd8ade5ed3a0d1c1709a0d47abc83efc3a3676f6ad0f394528800aae86fbaf12ac94555474a761a446158e74dc53f44909950
@@ -0,0 +1,23 @@
1
+ name: test
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ name: >-
8
+ Ruby: ${{ matrix.ruby }}
9
+ runs-on: ubuntu-latest
10
+ strategy:
11
+ matrix:
12
+ ruby:
13
+ - '3.4'
14
+ - '4.0'
15
+ steps:
16
+ - uses: actions/checkout@v7
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby }}
20
+ bundler-cache: true
21
+ - name: run rspec
22
+ run: |
23
+ bundle exec rspec
@@ -0,0 +1,70 @@
1
+ # For most projects, this workflow file will not need changing; you simply need
2
+ # to commit it to your repository.
3
+ #
4
+ # You may wish to alter this file to override the set of languages analyzed,
5
+ # or to provide custom queries or build logic.
6
+ #
7
+ # ******** NOTE ********
8
+ # We have attempted to detect the languages in your repository. Please check
9
+ # the `language` matrix defined below to confirm you have the correct set of
10
+ # supported CodeQL languages.
11
+ #
12
+ name: "CodeQL"
13
+
14
+ on:
15
+ push:
16
+ branches: [ master ]
17
+ pull_request:
18
+ # The branches below must be a subset of the branches above
19
+ branches: [ master ]
20
+ schedule:
21
+ - cron: '37 20 * * 2'
22
+
23
+ jobs:
24
+ analyze:
25
+ name: Analyze
26
+ runs-on: ubuntu-latest
27
+ permissions:
28
+ actions: read
29
+ contents: read
30
+ security-events: write
31
+
32
+ strategy:
33
+ fail-fast: false
34
+ matrix:
35
+ language: [ 'ruby' ]
36
+ # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37
+ # Learn more about CodeQL language support at https://git.io/codeql-language-support
38
+
39
+ steps:
40
+ - name: Checkout repository
41
+ uses: actions/checkout@v2
42
+
43
+ # Initializes the CodeQL tools for scanning.
44
+ - name: Initialize CodeQL
45
+ uses: github/codeql-action/init@v1
46
+ with:
47
+ languages: ${{ matrix.language }}
48
+ # If you wish to specify custom queries, you can do so here or in a config file.
49
+ # By default, queries listed here will override any specified in a config file.
50
+ # Prefix the list here with "+" to use these queries and those in the config file.
51
+ # queries: ./path/to/local/query, your-org/your-repo/queries@main
52
+
53
+ # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
54
+ # If this step fails, then you should remove it and run the build manually (see below)
55
+ - name: Autobuild
56
+ uses: github/codeql-action/autobuild@v1
57
+
58
+ # ℹ️ Command-line programs to run using the OS shell.
59
+ # 📚 https://git.io/JvXDl
60
+
61
+ # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
62
+ # and modify them (or add more) to build your code if your project
63
+ # uses a compiled language
64
+
65
+ #- run: |
66
+ # make bootstrap
67
+ # make release
68
+
69
+ - name: Perform CodeQL Analysis
70
+ uses: github/codeql-action/analyze@v1
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  兆までの漢数字を半角数字に変換します。
3
3
  漢数字が続いていたらそれぞれ変換します。
4
4
 
5
- [![Build Status](https://travis-ci.org/yoshitsugu/zen_to_i.svg)](https://travis-ci.org/yoshitsugu/zen_to_i)
5
+ [![Actions Status](https://github.com/yoshitsugu/zen_to_i/workflows/test/badge.svg)](https://github.com/yoshitsugu/zen_to_i/actions)
6
6
 
7
7
  ## Installation
8
8
 
@@ -61,10 +61,10 @@ require "zen_to_i/refine"
61
61
  using ZenToI::Refine
62
62
 
63
63
  "一二三".zen_to_i
64
- #=> "103"
64
+ #=> "123"
65
65
 
66
66
  "一二三".to_i
67
- #=> 103
67
+ #=> 123
68
68
  ```
69
69
 
70
70
  Also, you can implicitly overwrite `to_i` method by `require "zen_to_i/string_ext"`.
@@ -74,10 +74,10 @@ Of course it is very dangerous. Please be careful to use it.
74
74
  require "zen_to_i/string_ext"
75
75
 
76
76
  "一二三".zen_to_i
77
- #=> "103"
77
+ #=> "123"
78
78
 
79
79
  "一二三".to_i
80
- #=> 103
80
+ #=> 123
81
81
  ```
82
82
 
83
83
 
@@ -2,17 +2,21 @@
2
2
 
3
3
  module ZenToI
4
4
  module Constant
5
- KANNUM = { "〇" => 0, "" => 1, "一" => 1, "二" => 2, "三" => 3, "四" => 4,
6
- "五" => 5, "六" => 6, "七" => 7, "八" => 8, "九" => 9,
7
- "零" => 0, "壱" => 1, "弐" => 2, "参" => 3, "肆" => 4,
8
- "伍" => 5, "陸" => 6, "漆" => 7, "捌" => 8, "玖" => 9 }.freeze
9
- KANORDER = { "十" => 10, "百" => 100, "千" => 1000, "" => 1, "拾" => 10,
10
- "陌" => 100, "佰" => 100, "阡" => 1000, "仟" => 1000 }.freeze
11
- KANORDER_F = { "万" => 10**4, "億" => 10**8, "兆" => 10**12,
12
- "" => 1, "萬" => 10**4 }.freeze
13
- ALL_KANSUUJI = KANNUM.keys + KANORDER.keys + KANORDER_F.keys
14
- MAX_LOOP = 100
15
- KANORDER_F_KEYS_REGEX = /([^#{KANORDER_F.keys.join('')}]*)([#{KANORDER_F.keys.join('')}]?)/.freeze
16
- KANORDER_KEYS_REGEX = /([^#{KANORDER.keys.join('')}]*)([#{KANORDER.keys.join('')}]?)/.freeze
17
- end.freeze
5
+ unless const_defined?(:KANNUM, false)
6
+ KANNUM = { "〇" => 0, "" => 1, "一" => 1, "二" => 2, "三" => 3, "四" => 4,
7
+ "五" => 5, "六" => 6, "七" => 7, "八" => 8, "九" => 9,
8
+ "零" => 0, "壱" => 1, "弐" => 2, "参" => 3, "肆" => 4,
9
+ "伍" => 5, "陸" => 6, "漆" => 7, "捌" => 8, "玖" => 9 }.freeze
10
+ KANORDER = { "十" => 10, "百" => 100, "千" => 1000, "" => 1, "拾" => 10,
11
+ "陌" => 100, "佰" => 100, "阡" => 1000, "仟" => 1000 }.freeze
12
+ KANORDER_F = { "万" => 10**4, "億" => 10**8, "兆" => 10**12,
13
+ "" => 1, "萬" => 10**4 }.freeze
14
+ ALL_KANSUUJI = KANNUM.keys + KANORDER.keys + KANORDER_F.keys
15
+ MAX_LOOP = 100
16
+ KANORDER_F_KEYS_REGEX = /([^#{KANORDER_F.keys.join('')}]*)([#{KANORDER_F.keys.join('')}]?)/.freeze
17
+ KANORDER_KEYS_REGEX = /([^#{KANORDER.keys.join('')}]*)([#{KANORDER.keys.join('')}]?)/.freeze
18
+ end
19
+ end
20
+
21
+ Constant.freeze
18
22
  end
@@ -70,12 +70,5 @@ module ZenToI
70
70
  end
71
71
  end
72
72
  end
73
-
74
- def format_location(string)
75
- location_map.each do |key, value|
76
- string.gsub!(/#{key}/, value)
77
- end
78
- string
79
- end
80
73
  end
81
74
  end
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../zen_to_i"
4
+ require_relative "to_i"
4
5
 
5
- module ZenToI::Refine
6
- refine String do
7
- alias_method :_to_i, :to_i
8
-
9
- def to_i
10
- self.zen_to_i._to_i
6
+ module ZenToI
7
+ unless const_defined?(:Refine, false)
8
+ module Refine
9
+ refine String do
10
+ define_method(:_to_i, ZenToI::ToI.instance_method(:_to_i))
11
+ define_method(:to_i, ZenToI::ToI.instance_method(:to_i))
12
+ end
11
13
  end
12
14
  end
13
15
  end
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../zen_to_i"
4
+ require_relative "to_i"
4
5
 
5
6
  class String
6
- alias_method :_to_i, :to_i
7
-
8
- def to_i
9
- self.zen_to_i._to_i
7
+ unless String.instance_methods(false).include?(:_to_i)
8
+ alias_method :_to_i, :to_i
10
9
  end
10
+
11
+ define_method(:to_i, ZenToI::ToI.instance_method(:to_i))
11
12
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../zen_to_i"
4
+
5
+ module ZenToI
6
+ # String#to_i を漢数字変換に差し替えるための実装。
7
+ # string_ext.rb(グローバルに適用)と refine.rb(refinement 経由)で共有する。
8
+ module ToI
9
+ # 差し替え前の String#to_i を保持する。読み込み時点で String 自身が持つ
10
+ # ネイティブ実装を捕捉し、どのロード順でも素の to_i を参照できるようにする。
11
+ unless const_defined?(:ORIGINAL_TO_I, false)
12
+ ORIGINAL_TO_I = String.instance_method(:to_i)
13
+ end
14
+
15
+ def to_i
16
+ ORIGINAL_TO_I.bind(self.zen_to_i).call
17
+ end
18
+
19
+ def _to_i(*args)
20
+ ORIGINAL_TO_I.bind(self).call(*args)
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ZenToI
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zen_to_i"
4
+
5
+ describe ZenToI::Constant do
6
+ describe "複数回読み込んでも例外にならない" do
7
+ it "constantを再度読み込んでも定数が残る" do
8
+ expect { load File.expand_path("../lib/zen_to_i/constant.rb", __dir__) }.not_to raise_error
9
+
10
+ expect(ZenToI::Constant::KANNUM["一"]).to eq 1
11
+ expect("一二三".zen_to_i).to eq "123"
12
+ end
13
+
14
+ it "定数定義がスキップされModuleがfrozenのまま維持される" do
15
+ load File.expand_path("../lib/zen_to_i/constant.rb", __dir__)
16
+
17
+ expect(ZenToI::Constant).to be_frozen
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require "rbconfig"
5
+
6
+ RSpec.describe "ロード順" do
7
+ # refinement の定義は読み込み時点のグローバル String#to_i に影響されるため、
8
+ # ロード順によって挙動が変わらないことをサブプロセスで検証する。
9
+ def run_script(script)
10
+ stdout, stderr, status = Open3.capture3(
11
+ RbConfig.ruby, "-I", File.expand_path("../lib", __dir__), "-e", script
12
+ )
13
+ raise "script failed: #{stderr}" unless status.success?
14
+
15
+ stdout.strip
16
+ end
17
+
18
+ it "string_ext の後に refine を読み込んでも to_i が変換結果を返す" do
19
+ output = run_script(<<~RUBY)
20
+ require "zen_to_i/string_ext"
21
+ require "zen_to_i/refine"
22
+ using ZenToI::Refine
23
+ print "一二三".to_i
24
+ RUBY
25
+
26
+ expect(output).to eq "123"
27
+ end
28
+
29
+ it "refine の後に string_ext を読み込んでも to_i が変換結果を返す" do
30
+ output = run_script(<<~RUBY)
31
+ require "zen_to_i/refine"
32
+ require "zen_to_i/string_ext"
33
+ using ZenToI::Refine
34
+ print "一二三".to_i
35
+ RUBY
36
+
37
+ expect(output).to eq "123"
38
+ end
39
+
40
+ it "ロード順によらず _to_i は素の to_i と同じ結果を返す" do
41
+ outputs = [
42
+ <<~RUBY,
43
+ require "zen_to_i/string_ext"
44
+ require "zen_to_i/refine"
45
+ using ZenToI::Refine
46
+ print "一二三"._to_i
47
+ RUBY
48
+ <<~RUBY,
49
+ require "zen_to_i/refine"
50
+ require "zen_to_i/string_ext"
51
+ using ZenToI::Refine
52
+ print "一二三"._to_i
53
+ RUBY
54
+ ].map { |script| run_script(script) }
55
+
56
+ expect(outputs).to eq ["0", "0"]
57
+ end
58
+
59
+ it "先に定義された String#_to_i を上書きしない" do
60
+ output = run_script(<<~RUBY)
61
+ class String
62
+ def _to_i(*)
63
+ "third-party"
64
+ end
65
+ end
66
+ require "zen_to_i/string_ext"
67
+ print "一二三"._to_i, " ", "一二三".to_i
68
+ RUBY
69
+
70
+ expect(output).to eq "third-party 123"
71
+ end
72
+ end
data/spec/refine_spec.rb CHANGED
@@ -10,4 +10,21 @@ describe ZenToI::Refine do
10
10
  expect("一二三".to_i).to eq 123
11
11
  end
12
12
  end
13
+
14
+ describe "String#_to_i refined" do
15
+ it "_to_iは差し替え前のto_iと同じ結果を返す" do
16
+ expect("一二三"._to_i).to eq 0
17
+ expect("123"._to_i).to eq 123
18
+ end
19
+ end
20
+
21
+ describe "複数回読み込んでも無限再帰にならない" do
22
+ it "refineを再度読み込んでもto_iが動作する" do
23
+ load File.expand_path("../lib/zen_to_i/refine.rb", __dir__)
24
+
25
+ expect { "一二三".to_i }.not_to raise_error
26
+ expect("一二三".to_i).to eq 123
27
+ expect("四五六".to_i).to eq 456
28
+ end
29
+ end
13
30
  end
@@ -8,4 +8,21 @@ describe String do
8
8
  expect("一二三".to_i).to eq 123
9
9
  end
10
10
  end
11
+
12
+ describe "String#_to_i overwritten" do
13
+ it "_to_iは差し替え前のto_iと同じ結果を返す" do
14
+ expect("一二三"._to_i).to eq 0
15
+ expect("123"._to_i).to eq 123
16
+ end
17
+ end
18
+
19
+ describe "複数回読み込んでも無限再帰にならない" do
20
+ it "string_extを再度読み込んでもto_iが動作する" do
21
+ load File.expand_path("../lib/zen_to_i/string_ext.rb", __dir__)
22
+
23
+ expect { "一二三".to_i }.not_to raise_error
24
+ expect("一二三".to_i).to eq 123
25
+ expect("四五六".to_i).to eq 456
26
+ end
27
+ end
11
28
  end
data/zen_to_i.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["zeroinon1206bass@gmail.com"]
10
10
  spec.description = "全角文字に含まれる数字を半角の数値に変換。兆まで対応"
11
11
  spec.summary = "Japanese zenkaku to number"
12
- spec.homepage = ""
12
+ spec.homepage = "https://github.com/yoshitsugu/zen_to_i"
13
13
  spec.license = "MIT"
14
14
  spec.required_ruby_version = ">= 2.4.6"
15
15
 
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zen_to_i
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshitsugu
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2019-04-21 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: bundler
@@ -88,9 +87,10 @@ executables:
88
87
  extensions: []
89
88
  extra_rdoc_files: []
90
89
  files:
90
+ - ".github/workflows/ci.yaml"
91
+ - ".github/workflows/codeql-analysis.yml"
91
92
  - ".gitignore"
92
93
  - ".rubocop.yml"
93
- - ".travis.yml"
94
94
  - Gemfile
95
95
  - LICENSE.txt
96
96
  - README.md
@@ -101,16 +101,18 @@ files:
101
101
  - lib/zen_to_i/mixin.rb
102
102
  - lib/zen_to_i/refine.rb
103
103
  - lib/zen_to_i/string_ext.rb
104
+ - lib/zen_to_i/to_i.rb
104
105
  - lib/zen_to_i/version.rb
106
+ - spec/constant_spec.rb
107
+ - spec/load_order_spec.rb
105
108
  - spec/refine_spec.rb
106
109
  - spec/string_ext_spec.rb
107
110
  - spec/zen_to_i_spec.rb
108
111
  - zen_to_i.gemspec
109
- homepage: ''
112
+ homepage: https://github.com/yoshitsugu/zen_to_i
110
113
  licenses:
111
114
  - MIT
112
115
  metadata: {}
113
- post_install_message:
114
116
  rdoc_options: []
115
117
  require_paths:
116
118
  - lib
@@ -125,11 +127,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
127
  - !ruby/object:Gem::Version
126
128
  version: '0'
127
129
  requirements: []
128
- rubygems_version: 3.0.3
129
- signing_key:
130
+ rubygems_version: 4.0.3
130
131
  specification_version: 4
131
132
  summary: Japanese zenkaku to number
132
133
  test_files:
134
+ - spec/constant_spec.rb
135
+ - spec/load_order_spec.rb
133
136
  - spec/refine_spec.rb
134
137
  - spec/string_ext_spec.rb
135
138
  - spec/zen_to_i_spec.rb
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.6.3
4
- - 2.5.5
5
- - 2.4.6
6
- script: bundle exec rspec