polyfill 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/.travis.yml +3 -2
  4. data/CHANGELOG.md +26 -10
  5. data/CONTRIBUTING.md +0 -2
  6. data/README.md +107 -28
  7. data/lib/polyfill.rb +168 -74
  8. data/lib/polyfill/utils.rb +8 -3
  9. data/lib/polyfill/v2_2.rb +9 -0
  10. data/lib/polyfill/v2_2/enumerable.rb +34 -0
  11. data/lib/polyfill/v2_2/kernel.rb +9 -0
  12. data/lib/polyfill/v2_3.rb +2 -0
  13. data/lib/polyfill/v2_3/array.rb +22 -3
  14. data/lib/polyfill/v2_3/enumerable.rb +48 -3
  15. data/lib/polyfill/v2_3/enumerator/lazy.rb +7 -3
  16. data/lib/polyfill/v2_3/hash.rb +39 -3
  17. data/lib/polyfill/v2_3/kernel.rb +17 -0
  18. data/lib/polyfill/v2_3/string.rb +23 -5
  19. data/lib/polyfill/v2_3/struct.rb +22 -3
  20. data/lib/polyfill/v2_4/array.rb +25 -3
  21. data/lib/polyfill/v2_4/comparable.rb +9 -3
  22. data/lib/polyfill/v2_4/dir.rb +5 -3
  23. data/lib/polyfill/v2_4/enumerable.rb +28 -3
  24. data/lib/polyfill/v2_4/enumerator/lazy.rb +15 -3
  25. data/lib/polyfill/v2_4/file.rb +5 -3
  26. data/lib/polyfill/v2_4/float.rb +38 -3
  27. data/lib/polyfill/v2_4/hash.rb +35 -3
  28. data/lib/polyfill/v2_4/i_p_addr.rb +11 -3
  29. data/lib/polyfill/v2_4/integer.rb +53 -3
  30. data/lib/polyfill/v2_4/io.rb +166 -4
  31. data/lib/polyfill/v2_4/match_data.rb +11 -3
  32. data/lib/polyfill/v2_4/numeric.rb +15 -3
  33. data/lib/polyfill/v2_4/object.rb +9 -3
  34. data/lib/polyfill/v2_4/pathname.rb +7 -3
  35. data/lib/polyfill/v2_4/regexp.rb +3 -3
  36. data/lib/polyfill/v2_4/string.rb +110 -4
  37. data/lib/polyfill/v2_4/string_io.rb +166 -2
  38. data/lib/polyfill/v2_4/symbol.rb +17 -3
  39. data/lib/polyfill/version.rb +1 -1
  40. data/polyfill.gemspec +1 -1
  41. metadata +7 -98
  42. data/lib/polyfill/v2_3/array/instance.rb +0 -11
  43. data/lib/polyfill/v2_3/array/instance/dig.rb +0 -42
  44. data/lib/polyfill/v2_3/enumerable/instance.rb +0 -15
  45. data/lib/polyfill/v2_3/enumerable/instance/chunk_while.rb +0 -70
  46. data/lib/polyfill/v2_3/enumerable/instance/grep_v.rb +0 -48
  47. data/lib/polyfill/v2_3/enumerable/instance/slice_before.rb +0 -48
  48. data/lib/polyfill/v2_3/enumerator/lazy/instance.rb +0 -13
  49. data/lib/polyfill/v2_3/enumerator/lazy/instance/grep_v.rb +0 -31
  50. data/lib/polyfill/v2_3/hash/instance.rb +0 -15
  51. data/lib/polyfill/v2_3/hash/instance/dig.rb +0 -17
  52. data/lib/polyfill/v2_3/hash/instance/fetch_values.rb +0 -32
  53. data/lib/polyfill/v2_3/hash/instance/to_proc.rb +0 -23
  54. data/lib/polyfill/v2_3/string/class.rb +0 -11
  55. data/lib/polyfill/v2_3/string/class/new.rb +0 -33
  56. data/lib/polyfill/v2_3/string/instance.rb +0 -13
  57. data/lib/polyfill/v2_3/string/instance/minus_unary.rb +0 -23
  58. data/lib/polyfill/v2_3/string/instance/plus_unary.rb +0 -23
  59. data/lib/polyfill/v2_3/struct/instance.rb +0 -11
  60. data/lib/polyfill/v2_3/struct/instance/dig.rb +0 -17
  61. data/lib/polyfill/v2_4/array/instance.rb +0 -13
  62. data/lib/polyfill/v2_4/array/instance/concat.rb +0 -30
  63. data/lib/polyfill/v2_4/array/instance/sum.rb +0 -17
  64. data/lib/polyfill/v2_4/comparable/instance.rb +0 -11
  65. data/lib/polyfill/v2_4/comparable/instance/clamp.rb +0 -35
  66. data/lib/polyfill/v2_4/dir/class.rb +0 -11
  67. data/lib/polyfill/v2_4/dir/class/empty_q.rb +0 -23
  68. data/lib/polyfill/v2_4/enumerable/instance.rb +0 -15
  69. data/lib/polyfill/v2_4/enumerable/instance/chunk.rb +0 -48
  70. data/lib/polyfill/v2_4/enumerable/instance/sum.rb +0 -52
  71. data/lib/polyfill/v2_4/enumerable/instance/uniq.rb +0 -45
  72. data/lib/polyfill/v2_4/enumerator/lazy/instance.rb +0 -15
  73. data/lib/polyfill/v2_4/enumerator/lazy/instance/chunk_while.rb +0 -29
  74. data/lib/polyfill/v2_4/enumerator/lazy/instance/uniq.rb +0 -33
  75. data/lib/polyfill/v2_4/file/class.rb +0 -11
  76. data/lib/polyfill/v2_4/file/class/empty_q.rb +0 -23
  77. data/lib/polyfill/v2_4/float/instance.rb +0 -15
  78. data/lib/polyfill/v2_4/float/instance/ceil.rb +0 -32
  79. data/lib/polyfill/v2_4/float/instance/floor.rb +0 -32
  80. data/lib/polyfill/v2_4/float/instance/truncate.rb +0 -32
  81. data/lib/polyfill/v2_4/hash/instance.rb +0 -17
  82. data/lib/polyfill/v2_4/hash/instance/compact.rb +0 -23
  83. data/lib/polyfill/v2_4/hash/instance/compact_e.rb +0 -23
  84. data/lib/polyfill/v2_4/hash/instance/transform_values.rb +0 -33
  85. data/lib/polyfill/v2_4/hash/instance/transform_values_e.rb +0 -33
  86. data/lib/polyfill/v2_4/i_p_addr/instance.rb +0 -13
  87. data/lib/polyfill/v2_4/i_p_addr/instance/equalequal.rb +0 -27
  88. data/lib/polyfill/v2_4/i_p_addr/instance/lessthanequalgreaterthan.rb +0 -27
  89. data/lib/polyfill/v2_4/integer/instance.rb +0 -19
  90. data/lib/polyfill/v2_4/integer/instance/ceil.rb +0 -28
  91. data/lib/polyfill/v2_4/integer/instance/digits.rb +0 -34
  92. data/lib/polyfill/v2_4/integer/instance/floor.rb +0 -28
  93. data/lib/polyfill/v2_4/integer/instance/round.rb +0 -31
  94. data/lib/polyfill/v2_4/integer/instance/truncate.rb +0 -28
  95. data/lib/polyfill/v2_4/io/class.rb +0 -13
  96. data/lib/polyfill/v2_4/io/class/foreach.rb +0 -55
  97. data/lib/polyfill/v2_4/io/class/readlines.rb +0 -37
  98. data/lib/polyfill/v2_4/io/instance.rb +0 -19
  99. data/lib/polyfill/v2_4/io/instance/each_line.rb +0 -54
  100. data/lib/polyfill/v2_4/io/instance/gets.rb +0 -37
  101. data/lib/polyfill/v2_4/io/instance/lines.rb +0 -54
  102. data/lib/polyfill/v2_4/io/instance/readline.rb +0 -37
  103. data/lib/polyfill/v2_4/io/instance/readlines.rb +0 -37
  104. data/lib/polyfill/v2_4/match_data/instance.rb +0 -13
  105. data/lib/polyfill/v2_4/match_data/instance/named_captures.rb +0 -25
  106. data/lib/polyfill/v2_4/match_data/instance/values_at.rb +0 -25
  107. data/lib/polyfill/v2_4/numeric/instance.rb +0 -17
  108. data/lib/polyfill/v2_4/numeric/instance/clone.rb +0 -23
  109. data/lib/polyfill/v2_4/numeric/instance/dup.rb +0 -23
  110. data/lib/polyfill/v2_4/numeric/instance/finite_q.rb +0 -23
  111. data/lib/polyfill/v2_4/numeric/instance/infinite_q.rb +0 -23
  112. data/lib/polyfill/v2_4/object/instance.rb +0 -11
  113. data/lib/polyfill/v2_4/object/instance/clone.rb +0 -29
  114. data/lib/polyfill/v2_4/pathname/instance.rb +0 -11
  115. data/lib/polyfill/v2_4/pathname/instance/empty_q.rb +0 -29
  116. data/lib/polyfill/v2_4/regexp/instance.rb +0 -11
  117. data/lib/polyfill/v2_4/regexp/instance/match_q.rb +0 -23
  118. data/lib/polyfill/v2_4/string/class.rb +0 -11
  119. data/lib/polyfill/v2_4/string/class/new.rb +0 -26
  120. data/lib/polyfill/v2_4/string/instance.rb +0 -23
  121. data/lib/polyfill/v2_4/string/instance/casecmp_q.rb +0 -23
  122. data/lib/polyfill/v2_4/string/instance/concat.rb +0 -30
  123. data/lib/polyfill/v2_4/string/instance/each_line.rb +0 -17
  124. data/lib/polyfill/v2_4/string/instance/lines.rb +0 -55
  125. data/lib/polyfill/v2_4/string/instance/match_q.rb +0 -23
  126. data/lib/polyfill/v2_4/string/instance/prepend.rb +0 -30
  127. data/lib/polyfill/v2_4/string/instance/unpack1.rb +0 -23
  128. data/lib/polyfill/v2_4/string_io/instance.rb +0 -17
  129. data/lib/polyfill/v2_4/string_io/instance/each_line.rb +0 -17
  130. data/lib/polyfill/v2_4/string_io/instance/gets.rb +0 -17
  131. data/lib/polyfill/v2_4/string_io/instance/readline.rb +0 -17
  132. data/lib/polyfill/v2_4/string_io/instance/readlines.rb +0 -17
  133. data/lib/polyfill/v2_4/symbol/instance.rb +0 -15
  134. data/lib/polyfill/v2_4/symbol/instance/casecmp_q.rb +0 -25
  135. data/lib/polyfill/v2_4/symbol/instance/match.rb +0 -27
  136. data/lib/polyfill/v2_4/symbol/instance/match_q.rb +0 -17
@@ -1,8 +1,13 @@
1
1
  module Polyfill
2
2
  module Utils
3
- def when_ruby_below(version)
4
- yield if RUBY_VERSION[/\A(\d+\.\d+)/, 1] < version
3
+ def ignore_warnings
4
+ orig = $VERBOSE
5
+ $VERBOSE = nil
6
+
7
+ yield
8
+
9
+ $VERBOSE = orig
5
10
  end
6
- module_function :when_ruby_below
11
+ module_function :ignore_warnings
7
12
  end
8
13
  end
@@ -0,0 +1,9 @@
1
+ require_relative 'v2_2/enumerable'
2
+ require_relative 'v2_2/kernel'
3
+
4
+ module Polyfill
5
+ module V2_2
6
+ include Enumerable
7
+ include Kernel
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ module Polyfill
2
+ module V2_2
3
+ module Enumerable
4
+ def slice_after(pattern = nil)
5
+ raise ArgumentError, 'both pattern and block are given' if pattern && block_given?
6
+ raise ArgumentError, 'wrong number of arguments (given 0, expected 1)' if !pattern && !block_given?
7
+
8
+ matcher = pattern || ::Proc.new
9
+ enum_count =
10
+ begin
11
+ size
12
+ rescue NameError
13
+ count
14
+ end
15
+
16
+ ::Enumerator.new do |yielder|
17
+ output = []
18
+ run = 1
19
+ each do |element, *rest|
20
+ elements = rest.any? ? [element, *rest] : element
21
+
22
+ output.push(elements)
23
+ if matcher === elements || run == enum_count # rubocop:disable Style/CaseEquality
24
+ yielder << output
25
+ output = []
26
+ end
27
+
28
+ run += 1
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ module Polyfill
2
+ module V2_2
3
+ module Kernel
4
+ def itself
5
+ self
6
+ end
7
+ end
8
+ end
9
+ end
@@ -2,6 +2,7 @@ require_relative 'v2_3/array'
2
2
  require_relative 'v2_3/hash'
3
3
  require_relative 'v2_3/enumerable'
4
4
  require_relative 'v2_3/enumerator/lazy'
5
+ require_relative 'v2_3/kernel'
5
6
  require_relative 'v2_3/string'
6
7
  require_relative 'v2_3/struct'
7
8
 
@@ -11,6 +12,7 @@ module Polyfill
11
12
  include Hash
12
13
  include Enumerable
13
14
  include Enumerator::Lazy
15
+ include Kernel
14
16
  include String
15
17
  include Struct
16
18
  end
@@ -1,9 +1,28 @@
1
- require_relative 'array/instance'
2
-
3
1
  module Polyfill
4
2
  module V2_3
5
3
  module Array
6
- include Instance
4
+ def dig(head, *rest)
5
+ [head, *rest].reduce(self) do |value, accessor|
6
+ next_value =
7
+ case value
8
+ when ::Array
9
+ value.at(accessor)
10
+ when ::Hash
11
+ value[accessor]
12
+ when ::Struct
13
+ value[accessor] if value.members.include?(accessor)
14
+ else
15
+ begin
16
+ break value.dig(*rest)
17
+ rescue NoMethodError
18
+ raise TypeError, "#{value.class} does not have a #dig method"
19
+ end
20
+ end
21
+
22
+ break nil if next_value.nil?
23
+ next_value
24
+ end
25
+ end
7
26
  end
8
27
  end
9
28
  end
@@ -1,9 +1,54 @@
1
- require_relative 'enumerable/instance'
2
-
3
1
  module Polyfill
4
2
  module V2_3
5
3
  module Enumerable
6
- include Instance
4
+ def chunk_while
5
+ block = ::Proc.new
6
+
7
+ enum_count =
8
+ begin
9
+ size
10
+ rescue NameError
11
+ count
12
+ end
13
+
14
+ return [self] if enum_count == 1
15
+
16
+ ::Enumerator.new do |yielder|
17
+ output = []
18
+ each_cons(2).with_index(1) do |(a, b), run|
19
+ if run == enum_count - 1
20
+ if block.call(a, b)
21
+ output.push(a, b)
22
+ yielder << output
23
+ else
24
+ output.push(a)
25
+ yielder << output
26
+ yielder << [b]
27
+ end
28
+ else
29
+ output.push(a)
30
+ unless block.call(a, b)
31
+ yielder << output
32
+ output = []
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def grep_v(pattern)
40
+ output = to_a - grep(pattern)
41
+ output.map!(&::Proc.new) if block_given?
42
+ output
43
+ end
44
+
45
+ def slice_before(*args)
46
+ if !args.empty? && block_given?
47
+ raise ArgumentError, 'wrong number of arguments (given 1, expected 0)'
48
+ end
49
+
50
+ super
51
+ end
7
52
  end
8
53
  end
9
54
  end
@@ -1,10 +1,14 @@
1
- require_relative 'lazy/instance'
2
-
3
1
  module Polyfill
4
2
  module V2_3
5
3
  module Enumerator
6
4
  module Lazy
7
- include Instance
5
+ def grep_v(pattern)
6
+ ::Enumerator::Lazy.new(self) do |yielder, element|
7
+ next if pattern === element # rubocop:disable Style/CaseEquality
8
+
9
+ yielder << (block_given? ? yield(element) : element)
10
+ end
11
+ end
8
12
  end
9
13
  end
10
14
  end
@@ -1,9 +1,45 @@
1
- require_relative 'hash/instance'
2
-
3
1
  module Polyfill
4
2
  module V2_3
5
3
  module Hash
6
- include Instance
4
+ def dig(head, *rest)
5
+ [head, *rest].reduce(self) do |value, accessor|
6
+ next_value =
7
+ case value
8
+ when ::Array
9
+ value.at(accessor)
10
+ when ::Hash
11
+ value[accessor]
12
+ when ::Struct
13
+ value[accessor] if value.members.include?(accessor)
14
+ else
15
+ begin
16
+ break value.dig(*rest)
17
+ rescue NoMethodError
18
+ raise TypeError, "#{value.class} does not have a #dig method"
19
+ end
20
+ end
21
+
22
+ break nil if next_value.nil?
23
+ next_value
24
+ end
25
+ end
26
+
27
+ def fetch_values(*keys)
28
+ keys.each_with_object([]) do |key, values|
29
+ value =
30
+ if block_given?
31
+ fetch(key, &::Proc.new)
32
+ else
33
+ fetch(key)
34
+ end
35
+
36
+ values << value
37
+ end
38
+ end
39
+
40
+ def to_proc
41
+ method(:[]).to_proc
42
+ end
7
43
  end
8
44
  end
9
45
  end
@@ -0,0 +1,17 @@
1
+ module Polyfill
2
+ module V2_3
3
+ module Kernel
4
+ def loop
5
+ return super unless block_given?
6
+
7
+ super do
8
+ begin
9
+ yield
10
+ rescue StopIteration => enum
11
+ break enum.result
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,11 +1,29 @@
1
- require_relative 'string/class'
2
- require_relative 'string/instance'
3
-
4
1
  module Polyfill
5
2
  module V2_3
6
3
  module String
7
- include Class
8
- include Instance
4
+ module ClassMethods
5
+ def new(*args)
6
+ hash, others = args.partition { |arg| arg.is_a?(::Hash) }
7
+ hash = hash.first
8
+ encoding = hash && hash.delete(:encoding)
9
+
10
+ if hash && !hash.keys.empty?
11
+ raise ArgumentError, "unknown keyword: #{hash.keys.first}"
12
+ end
13
+
14
+ str = super(*others)
15
+ str.encode!(encoding) if encoding
16
+ str
17
+ end
18
+ end
19
+
20
+ def +@
21
+ frozen? ? dup : self
22
+ end
23
+
24
+ def -@
25
+ frozen? ? self : dup.freeze
26
+ end
9
27
  end
10
28
  end
11
29
  end
@@ -1,9 +1,28 @@
1
- require_relative 'struct/instance'
2
-
3
1
  module Polyfill
4
2
  module V2_3
5
3
  module Struct
6
- include Instance
4
+ def dig(head, *rest)
5
+ [head, *rest].reduce(self) do |value, accessor|
6
+ next_value =
7
+ case value
8
+ when ::Array
9
+ value.at(accessor)
10
+ when ::Hash
11
+ value[accessor]
12
+ when ::Struct
13
+ value[accessor] if value.members.include?(accessor)
14
+ else
15
+ begin
16
+ break value.dig(*rest)
17
+ rescue NoMethodError
18
+ raise TypeError, "#{value.class} does not have a #dig method"
19
+ end
20
+ end
21
+
22
+ break nil if next_value.nil?
23
+ next_value
24
+ end
25
+ end
7
26
  end
8
27
  end
9
28
  end
@@ -1,9 +1,31 @@
1
- require_relative 'array/instance'
2
-
3
1
  module Polyfill
4
2
  module V2_4
5
3
  module Array
6
- include Instance
4
+ def concat(*others)
5
+ return super if others.length == 1
6
+
7
+ acc = [].concat(self)
8
+ others.each do |other|
9
+ acc.concat(other)
10
+ end
11
+
12
+ replace(acc)
13
+ end
14
+
15
+ def sum(init = 0)
16
+ acc =
17
+ begin
18
+ init.dup
19
+ rescue TypeError
20
+ init
21
+ end
22
+
23
+ each do |elem|
24
+ acc += block_given? ? yield(elem) : elem
25
+ end
26
+
27
+ acc
28
+ end
7
29
  end
8
30
  end
9
31
  end
@@ -1,9 +1,15 @@
1
- require_relative 'comparable/instance'
2
-
3
1
  module Polyfill
4
2
  module V2_4
5
3
  module Comparable
6
- include Instance
4
+ def clamp(min, max)
5
+ if min > max
6
+ raise ArgumentError, 'min argument must be smaller than max argument'
7
+ end
8
+
9
+ return min if min > self
10
+ return max if max < self
11
+ self
12
+ end
7
13
  end
8
14
  end
9
15
  end
@@ -1,9 +1,11 @@
1
- require_relative 'dir/class'
2
-
3
1
  module Polyfill
4
2
  module V2_4
5
3
  module Dir
6
- include Class
4
+ module ClassMethods
5
+ def empty?(path_name)
6
+ exist?(path_name) && (entries(path_name) - ['.', '..']).empty?
7
+ end
8
+ end
7
9
  end
8
10
  end
9
11
  end
@@ -1,9 +1,34 @@
1
- require_relative 'enumerable/instance'
2
-
3
1
  module Polyfill
4
2
  module V2_4
5
3
  module Enumerable
6
- include Instance
4
+ def chunk(*)
5
+ return enum_for(:chunk) unless block_given?
6
+
7
+ super
8
+ end
9
+
10
+ def sum(init = 0)
11
+ acc =
12
+ begin
13
+ init.dup
14
+ rescue TypeError
15
+ init
16
+ end
17
+
18
+ each do |elem|
19
+ acc += block_given? ? yield(elem) : elem
20
+ end
21
+
22
+ acc
23
+ end
24
+
25
+ def uniq
26
+ if block_given?
27
+ to_a.uniq(&::Proc.new)
28
+ else
29
+ to_a.uniq
30
+ end
31
+ end
7
32
  end
8
33
  end
9
34
  end
@@ -1,10 +1,22 @@
1
- require_relative 'lazy/instance'
2
-
3
1
  module Polyfill
4
2
  module V2_4
5
3
  module Enumerator
6
4
  module Lazy
7
- include Instance
5
+ using Polyfill(Enumerable: %w[#chunk_while])
6
+
7
+ def chunk_while
8
+ super.lazy
9
+ end
10
+
11
+ def uniq
12
+ seen = Set.new
13
+
14
+ ::Enumerator::Lazy.new(self) do |yielder, *values|
15
+ result = block_given? ? yield(*values) : values
16
+
17
+ yielder.<<(*values) if seen.add?(result)
18
+ end
19
+ end
8
20
  end
9
21
  end
10
22
  end