yano 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8d51546596fce988638ab9a5abf4682237fe561
4
+ data.tar.gz: c945f255763ac0bdcafef060f28d01ccd9f01a7e
5
+ SHA512:
6
+ metadata.gz: e70d89d755189269f73762e9001f8fe5343ea6c6023e284bce982920d39ee84b78ae9c2d3dbbd1e7bfe7e086dd502100b48230c2b28b0acc49c2f529ad571274
7
+ data.tar.gz: c5a150c71b27ad02c36b19922e695bd8447deb1b75173781d2099445bfe9ba182357727d778d0d4d75f213e4c6444a4066f30995a35b4d3f3ff67edde9a4f009
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nick Palaniuk
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Yano
2
+ **This is a ruby port of [yn](https://github.com/sindresorhus/yn) by [sindresorhus](https://github.com/sindresorhus)**
3
+
4
+ > Parse yes/no like values
5
+
6
+ Useful for validating answers to a CLI prompt.
7
+
8
+ -
9
+
10
+ The following case-insensitive values are recognized:
11
+
12
+ ```ruby
13
+ 'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0
14
+ ```
15
+
16
+ ## Install
17
+
18
+ ```ruby
19
+ $ gem install yano
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ Yano.parse('y');
26
+ #=> true
27
+
28
+ Yano.parse('NO');
29
+ #=> false
30
+
31
+ Yano.parse(true);
32
+ #=> true
33
+
34
+ Yano.parse('abomasum');
35
+ #=> nil
36
+
37
+ # lenient mode will use a key distance-based score
38
+ # to leniently accept typos of "yes" and "no"
39
+ Yano.parse('mo', lenient: true);
40
+ #=> false
41
+ ```
42
+
43
+ Unrecognized values return `nil`
44
+
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,84 @@
1
+ module Yano
2
+ module Lenient
3
+ YES_MATCH_SCORE_THRESHOLD = 2
4
+ NO_MATCH_SCORE_THRESHOLD = 1.25
5
+
6
+ Y_MATCH = {
7
+ '5' => 0.25,
8
+ '6' => 0.25,
9
+ '7' => 0.25,
10
+ 't' => 0.75,
11
+ 'y' => 1,
12
+ 'u' => 0.75,
13
+ 'g' => 0.25,
14
+ 'h' => 0.25,
15
+ 'k' => 0.25
16
+ }
17
+
18
+ E_MATCH = {
19
+ '2' => 0.25,
20
+ '3' => 0.25,
21
+ '4' => 0.25,
22
+ 'w' => 0.75,
23
+ 'e' => 1,
24
+ 'r' => 0.75,
25
+ 's' => 0.25,
26
+ 'd' => 0.25,
27
+ 'f' => 0.25
28
+ }
29
+
30
+ S_MATCH = {
31
+ 'q' => 0.25,
32
+ 'w' => 0.25,
33
+ 'e' => 0.25,
34
+ 'a' => 0.75,
35
+ 's' => 1,
36
+ 'd' => 0.75,
37
+ 'z' => 0.25,
38
+ 'x' => 0.25,
39
+ 'c' => 0.25
40
+ }
41
+
42
+ N_MATCH = {
43
+ 'h' => 0.25,
44
+ 'j' => 0.25,
45
+ 'k' => 0.25,
46
+ 'b' => 0.75,
47
+ 'n' => 1,
48
+ 'm' => 0.75
49
+ }
50
+
51
+ O_MATCH = {
52
+ '9' => 0.25,
53
+ '0' => 0.25,
54
+ 'i' => 0.75,
55
+ 'o' => 1,
56
+ 'p' => 0.75,
57
+ 'k' => 0.25,
58
+ 'l' => 0.25
59
+ }
60
+
61
+ def self.get_yes_match_score(val)
62
+ y, e, s = val[0], val[1], val[2]
63
+ [].tap do |score|
64
+ score << (Y_MATCH.key?(y) ? Y_MATCH[y] : 0)
65
+ score << (E_MATCH.key?(e) ? E_MATCH[e] : 0)
66
+ score << (S_MATCH.key?(s) ? S_MATCH[s] : 0)
67
+ end.reduce(:+)
68
+ end
69
+
70
+ def self.get_no_match_score(val)
71
+ n, o = val[0], val[1]
72
+
73
+ [].tap do |score|
74
+ score << (N_MATCH.key?(n) ? N_MATCH[n] : 0)
75
+ score << (O_MATCH.key?(o) ? O_MATCH[o] : 0)
76
+ end.reduce(:+)
77
+ end
78
+
79
+ def self.check_lenient_values(val)
80
+ return true if get_yes_match_score(val) >= YES_MATCH_SCORE_THRESHOLD
81
+ return false if get_no_match_score(val) >= NO_MATCH_SCORE_THRESHOLD
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module Yano
2
+ VERSION = "0.1.0"
3
+ end
data/lib/yano.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'yano/version'
2
+ require 'yano/lenient'
3
+
4
+ module Yano
5
+ VALID_YES_VALUE = /^(y|yes|true|1)$/i
6
+ VALID_NO_VALUE = /^(n|no|false|0)$/i
7
+
8
+ def self.parse(val, opts = {})
9
+ val = val.to_s.strip
10
+ return true if !!(val =~ VALID_YES_VALUE)
11
+ return false if !!(val =~ VALID_NO_VALUE)
12
+ return Lenient.check_lenient_values(val) if opts.delete(:lenient)
13
+ end
14
+ end
data/yano.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yano/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yano"
8
+ spec.version = Yano::VERSION
9
+ spec.authors = ["Nick Palaniuk"]
10
+ spec.email = ["npalaniuk@gmail.com"]
11
+
12
+ spec.summary = %q{Parse yes/no values, useful for CLI prompts}
13
+ spec.description = %q{Parse yes/no values}
14
+ spec.homepage = "https://github.com/nikkypx/yano"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yano
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nick Palaniuk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: Parse yes/no values
28
+ email:
29
+ - npalaniuk@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".travis.yml"
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - lib/yano.rb
41
+ - lib/yano/lenient.rb
42
+ - lib/yano/version.rb
43
+ - yano.gemspec
44
+ homepage: https://github.com/nikkypx/yano
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.5.1
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Parse yes/no values, useful for CLI prompts
68
+ test_files: []