ramda-ruby 0.12.0 → 0.14.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.
@@ -7,7 +7,7 @@ module Ramda
7
7
  module Object
8
8
  extend ::Ramda::Internal::CurriedMethod
9
9
 
10
- Functors = ::Ramda::Internals::Functors
10
+ Functors = ::Ramda::Internal::Functors
11
11
 
12
12
  # Makes a shallow clone of an object, setting or overriding the specified
13
13
  # property with the given value. Note that this copies and flattens
@@ -429,5 +429,18 @@ module Ramda
429
429
  curried_method(:where) do |spec, test|
430
430
  spec.all? { |k, v| v.call(test[k]) }
431
431
  end
432
+
433
+ # Takes a spec object and a test object; returns true if the test satisfies
434
+ # the spec, false otherwise. An object satisfies the spec if, for each of
435
+ # the spec's own properties, accessing that property of the object gives
436
+ # the same value (in R.equals terms) as accessing that property of the spec.
437
+ #
438
+ # whereEq is a specialization of where.)
439
+ #
440
+ # {String: *} -> {String: *} -> Boolean
441
+ #
442
+ curried_method(:where_eq) do |spec, test|
443
+ spec.all? { |k, v| test[k] && test[k] == v }
444
+ end
432
445
  end
433
446
  end
@@ -1,9 +1,11 @@
1
1
  require_relative 'internal/curried_method'
2
+ require_relative 'internal/dispatchable'
2
3
 
3
4
  module Ramda
4
5
  # String functions
5
6
  module String
6
7
  extend ::Ramda::Internal::CurriedMethod
8
+ extend ::Ramda::Internal::Dispatchable
7
9
 
8
10
  # Tests a regular expression against a String. Note that this function will
9
11
  # return an empty array when there are no matches. This differs from
@@ -45,17 +47,34 @@ module Ramda
45
47
  !str.match(rx).nil?
46
48
  end
47
49
 
48
- # The upper case version of a string.
50
+ # The lower case version of a string.
49
51
  #
50
52
  # String -> String
51
53
  #
52
- curried_method(:to_upper, &:upcase)
54
+ curried_method(:to_lower, &:downcase)
53
55
 
54
- # The lower case version of a string.
56
+ # Returns the string representation of the given value.
57
+ # eval'ing the output should result in a value equivalent to the input value.
58
+ # Many of the built-in to_string methods do not satisfy this requirement.
59
+ #
60
+ # * -> String
61
+ #
62
+ curried_method(:to_string, &dispatchable([:inspect], []) do |x|
63
+ # case x
64
+ # when Array
65
+ # when TrueClass, FalseClass
66
+ # when DateTime
67
+ # when NilClass
68
+ # when Number
69
+ # when String
70
+ # end
71
+ end)
72
+
73
+ # The upper case version of a string.
55
74
  #
56
75
  # String -> String
57
76
  #
58
- curried_method(:to_lower, &:downcase)
77
+ curried_method(:to_upper, &:upcase)
59
78
 
60
79
  # Removes (strips) whitespace from both ends of the string.
61
80
  #
@@ -1,3 +1,3 @@
1
1
  module Ramda
2
- VERSION = '0.12.0'.freeze
2
+ VERSION = '0.14.0'.freeze
3
3
  end
@@ -44,9 +44,18 @@ describe Ramda::Function do
44
44
  .to eq(['tasty pizza', 'tasty salad', 'PIZZA', 'SALAD'])
45
45
  end
46
46
 
47
- xit 'with monads' do
48
- res = r.ap(r.ap([R.multiply], M::Maybe(10)), M::Maybe(20))
49
- expect(res).to eq(M::Maybe::Some.new(200))
47
+ it 'with monads' do
48
+ expect(Maybe.of(R.multiply).ap(Maybe.of(10)).ap(Maybe.of(20))).to eq(Maybe.of(200))
49
+
50
+ res = R.ap(R.ap(Maybe.of(R.multiply), Maybe.of(10)), Maybe.of(20))
51
+ expect(res).to eq(Maybe.of(200))
52
+ end
53
+
54
+ context 'supports gem' do
55
+ it 'kleisli' do
56
+ result = R.ap(KleisliMaybe::Some.new(R.add(5)), KleisliMaybe::Some.new(10))
57
+ expect(result).to eq(KleisliMaybe::Some.new(15))
58
+ end
50
59
  end
51
60
  end
52
61
 
@@ -325,10 +334,9 @@ describe Ramda::Function do
325
334
  ).to eq([3, 4, 5, 4, 5, 6, 5, 6, 7])
326
335
  end
327
336
 
328
- xit 'with monads' do
329
- addm = r.lift(R.add)
330
- expect(addm.call(M::Maybe(3), M::Maybe(5))).to eq(M::Maybe(8))
331
- # expect(addM.call(M::Maybe(3), M::Maybe(nil))).to eq(M::Maybe::None.new)
337
+ it 'with monads' do
338
+ add_m = r.lift(R.add)
339
+ expect(add_m.call(Maybe.of(3), Maybe.of(5))).to eq(Maybe.of(8))
332
340
  end
333
341
  end
334
342
 
@@ -337,6 +345,11 @@ describe Ramda::Function do
337
345
  madd3 = r.lift_n(3, ->(*args) { R.sum(args) })
338
346
  expect(madd3.call([1, 2, 3], [1, 2, 3], [1])).to eq([3, 4, 5, 4, 5, 6, 5, 6, 7])
339
347
  end
348
+
349
+ it 'works with other functors such as "Maybe"' do
350
+ add_m = R.lift_n(3, ->(a, b, c) { a + b + c })
351
+ expect(add_m.call(Maybe.of(3), Maybe.of(5), Maybe.of(10))).to eq(Maybe.of(18))
352
+ end
340
353
  end
341
354
 
342
355
  context '#memoize' do
@@ -3,6 +3,16 @@ require 'spec_helper'
3
3
  describe Ramda::List do
4
4
  let(:r) { Ramda }
5
5
 
6
+ context '#adjust' do
7
+ it 'from docs' do
8
+ expect(R.adjust(R.add(10), 1, [1, 2, 3])).to eq([1, 12, 3])
9
+ end
10
+
11
+ it 'is curried' do
12
+ expect(R.adjust(R.add(10)).call(1).call([1, 2, 3])).to eq([1, 12, 3])
13
+ end
14
+ end
15
+
6
16
  context '#all' do
7
17
  it 'test' do
8
18
  equals3 = R.equals(3)
@@ -64,26 +74,6 @@ describe Ramda::List do
64
74
  expect(r.chain(duplicate, [1, 2, 3])).to eq([1, 1, 2, 2, 3, 3])
65
75
  end
66
76
 
67
- it 'monad with chain' do
68
- monad = Class.new do
69
- def chain(fn)
70
- fn.call(10)
71
- end
72
- end.new
73
-
74
- expect(r.chain(R.add(5), monad)).to eq(15)
75
- end
76
-
77
- it 'monad with bind' do
78
- monad = Class.new do
79
- def bind(fn)
80
- fn.call(10)
81
- end
82
- end.new
83
-
84
- expect(r.chain(R.add(5), monad)).to eq(15)
85
- end
86
-
87
77
  # it 'multiple args' do
88
78
  # expect(r.chain(R.compose(R.append, R.head)).call([1, 2, 3])).to eq([1, 2, 3, 1])
89
79
  # end
@@ -92,6 +82,23 @@ describe Ramda::List do
92
82
  times2 = ->(x) { [x * 2] }
93
83
  expect(R.chain(times2).call([1, 2, 3, 4])).to eq([2, 4, 6, 8])
94
84
  end
85
+
86
+ context 'supports gem' do
87
+ it 'fantasy-ruby' do
88
+ result = R.chain(->(x) { Maybe.of(R.add(5, x)) }, Maybe.of(10))
89
+ expect(result).to eq(Maybe.of(15))
90
+ end
91
+
92
+ it 'dry-monads', market: :dry_monads do
93
+ result = R.chain(->(x) { DryMaybe.pure(R.add(5, x)) }, DryMaybe.pure(10))
94
+ expect(result).to eq(DryMaybe.pure(15))
95
+ end
96
+
97
+ it 'kleisli' do
98
+ result = R.chain(->(x) { KleisliMaybe::Some.new(R.add(5, x)) }, KleisliMaybe::Some.new(10))
99
+ expect(result).to eq(KleisliMaybe::Some.new(15))
100
+ end
101
+ end
95
102
  end
96
103
 
97
104
  context '#concat' do
@@ -133,6 +140,38 @@ describe Ramda::List do
133
140
  end
134
141
  end
135
142
 
143
+ context '#drop_repeats' do
144
+ let(:x1s) { [1, 2, 3, 4, 5, 3, 2] }
145
+ let(:x2s) { [1, 2, 2, 2, 3, 4, 4, 5, 5, 3, 2, 2] }
146
+
147
+ it 'removes repeated elements' do
148
+ expect(R.drop_repeats(x2s)).to eql(x1s)
149
+ expect(R.drop_repeats(x1s)).to eql(x1s)
150
+ end
151
+
152
+ it 'returns an empty array for an empty array' do
153
+ expect(R.drop_repeats([])).to eq([])
154
+ end
155
+
156
+ it 'can act as a transducer' do
157
+ expect(R.into([], R.drop_repeats, x2s)).to eq(x1s)
158
+ end
159
+ end
160
+
161
+ context '#drop_repeats_with' do
162
+ let(:drop_repeats_with_abs) { R.drop_repeats_with(R.eq_by(:abs.to_proc)) }
163
+
164
+ it 'removes repeated elements' do
165
+ xs = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3]
166
+ expect(drop_repeats_with_abs.call(xs)).to eq([1, 3, 4, -5, 3])
167
+ end
168
+
169
+ it 'can act as a transducer' do
170
+ xs = [1, -1, 1, 3, 4, -4, -4, -5, 5, 3, 3]
171
+ expect(R.into([], drop_repeats_with_abs, xs)).to eq([1, 3, 4, -5, 3])
172
+ end
173
+ end
174
+
136
175
  context '#drop_while' do
137
176
  it 'from docs' do
138
177
  lte_two = ->(x) { x <= 2 }
@@ -308,6 +347,12 @@ describe Ramda::List do
308
347
  end
309
348
  end
310
349
 
350
+ context '#intersperse' do
351
+ it 'from docs' do
352
+ expect(R.intersperse('n', ['ba', 'a', 'a'])).to eq(['ba', 'n', 'a', 'n', 'a'])
353
+ end
354
+ end
355
+
311
356
  context '#into' do
312
357
  it 'transduces into arrays' do
313
358
  expect(R.into([], R.map(R.add(1)), [1, 2, 3, 4])).to eq([2, 3, 4, 5])
@@ -382,6 +427,23 @@ describe Ramda::List do
382
427
 
383
428
  expect(r.map(double_fn).call([1, 2, 3])).to eq([2, 4, 6])
384
429
  end
430
+
431
+ context 'supports gem' do
432
+ it 'fantasy-ruby' do
433
+ result = R.map(R.add(5), Maybe.of(10))
434
+ expect(result).to eq(Maybe.of(15))
435
+ end
436
+
437
+ it 'dry-monads', market: :dry_monads do
438
+ result = R.map(R.add(5), DryMaybe.pure(10))
439
+ expect(result).to eq(DryMaybe.pure(15))
440
+ end
441
+
442
+ it 'kleisli' do
443
+ result = R.map(R.add(5), KleisliMaybe::Some.new(10))
444
+ expect(result).to eq(KleisliMaybe::Some.new(15))
445
+ end
446
+ end
385
447
  end
386
448
 
387
449
  # context '#map_accum' do
@@ -449,6 +511,10 @@ describe Ramda::List do
449
511
  expect(r.pluck(:a).call([{ a: 1 }, { a: 2 }])).to eq([1, 2])
450
512
  expect(r.pluck(0).call([[1, 2], [3, 4]])).to eq([1, 3])
451
513
  end
514
+
515
+ it 'works with functor' do
516
+ expect(R.pluck(:a, Maybe.of(a: 100))).to eq(Maybe.of(100))
517
+ end
452
518
  end
453
519
 
454
520
  context '#prepend' do
@@ -44,6 +44,16 @@ describe Ramda::Logic do
44
44
  expect(f.call(15)).to be_truthy
45
45
  expect(f.call(30)).to be_falsey
46
46
  end
47
+
48
+ it 'accepts fantasy-land applicative functors' do
49
+ some = Maybe::Some
50
+ none = Maybe::None
51
+ expect(R.both(some.new(true), some.new(true))). to eq(some.new(true))
52
+ expect(R.both(some.new(true), some.new(false))).to eq(some.new(false))
53
+ expect(R.both(some.new(true), none.new)).to eq(none.new)
54
+ expect(R.both(none.new, some.new(false))).to eq(none.new)
55
+ expect(R.both(none.new, none.new)).to eq(none.new)
56
+ end
47
57
  end
48
58
 
49
59
  context '#complement' do
@@ -54,6 +64,15 @@ describe Ramda::Logic do
54
64
  expect(R.is_nil(7)).to be_falsey
55
65
  expect(is_not_nil.call(7)).to be_truthy
56
66
  end
67
+
68
+ it 'accepts fantasy-land functors' do
69
+ some = Maybe::Some
70
+ none = Maybe::None
71
+
72
+ expect(R.complement(some.new(true))).to eq(some.new(false))
73
+ expect(R.complement(some.new(false))).to eq(some.new(true))
74
+ expect(R.complement(none.new)).to eq(none.new)
75
+ end
57
76
  end
58
77
 
59
78
  context '#cond' do
@@ -86,6 +105,17 @@ describe Ramda::Logic do
86
105
  expect(f.call(101)).to be_truthy
87
106
  expect(f.call(8)).to be_truthy
88
107
  end
108
+
109
+ it 'accepts fantasy-land applicative functors' do
110
+ some = Maybe::Some
111
+ none = Maybe::None
112
+ expect(R.either(some.new(true), some.new(true))).to eq(some.new(true))
113
+ expect(R.either(some.new(true), some.new(false))).to eq(some.new(true))
114
+ expect(R.either(some.new(false), some.new(false))).to eq(some.new(false))
115
+ expect(R.either(some.new(true), none.new)).to eq(none.new)
116
+ expect(R.either(none.new, some.new(false))).to eq(none.new)
117
+ expect(R.either(none.new, none.new)).to eq(none.new)
118
+ end
89
119
  end
90
120
 
91
121
  context '#if_else' do
@@ -56,6 +56,35 @@ describe Ramda::Math do
56
56
  end
57
57
  end
58
58
 
59
+ context '#mean' do
60
+ it 'returns mean of a nonempty list' do
61
+ expect(R.mean([2])).to eq(2)
62
+ expect(R.mean([2, 7])).to eq(4.5)
63
+ expect(R.mean([2, 7, 9])).to eq(6)
64
+ expect(R.mean([2, 7, 9, 10])).to eq(7)
65
+ end
66
+
67
+ it 'returns Nil for an empty list' do
68
+ expect(R.mean([])).to be_nan
69
+ end
70
+ end
71
+
72
+ context '#median' do
73
+ it 'returns middle value of an odd-length list' do
74
+ expect(R.median([2])).to eq(2)
75
+ expect(R.median([2, 9, 7])).to eq(7)
76
+ end
77
+
78
+ it 'returns mean of two middle values of a nonempty even-length list' do
79
+ expect(R.median([7, 2])).to eq(4.5)
80
+ expect(R.median([7, 2, 10, 9])).to eq(8)
81
+ end
82
+
83
+ it 'returns NaN for an empty list' do
84
+ expect(R.median([])).to be_nan
85
+ end
86
+ end
87
+
59
88
  context '#modulo' do
60
89
  it 'from docs' do
61
90
  expect(r.modulo(0, 3)).to be(0)
@@ -546,4 +546,15 @@ describe Ramda::Object do
546
546
  expect(pred.call(a: 'foo', b: 'xxx', x: 11, y: 20)).to be_falsey
547
547
  end
548
548
  end
549
+
550
+ context '#where_eq' do
551
+ it 'from docs' do
552
+ pred = R.where_eq(a: 1, b: 2)
553
+
554
+ expect(pred.call(a: 1)).to be_falsey
555
+ expect(pred.call(a: 1, b: 2)).to be_truthy
556
+ expect(pred.call(a: 1, b: 2, c: 3)).to be_truthy
557
+ expect(pred.call(a: 1, b: 1)).to be_falsey
558
+ end
559
+ end
549
560
  end
@@ -36,6 +36,49 @@ describe Ramda::String do
36
36
  end
37
37
  end
38
38
 
39
+ context '#to_string' do
40
+ it 'returns the string representation of null' do
41
+ expect(R.to_string(nil)).to eq('nil')
42
+ end
43
+
44
+ it 'returns the string representation of a Boolean primitive' do
45
+ expect(R.to_string(true)).to eq('true')
46
+ expect(R.to_string(false)).to eq('false')
47
+ end
48
+
49
+ it 'returns the string representation of a number primitive' do
50
+ expect(R.to_string(0)).to eq('0')
51
+ expect(R.to_string(1.23)).to eq('1.23')
52
+ expect(R.to_string(-1.23)).to eq('-1.23')
53
+ end
54
+
55
+ it 'returns the string representation of a string primitive' do
56
+ expect(R.to_string('abc')).to eq('"abc"')
57
+ expect(R.to_string('x "y" z')).to eq('"x \\"y\\" z"')
58
+ expect(R.to_string("' '")).to eq('"\' \'"')
59
+ expect(R.to_string('" "')).to eq('"\\" \\""')
60
+ expect(R.to_string('\b \b')).to eq('"\\\\b \\\\b"')
61
+ expect(R.to_string('\f \f')).to eq('"\\\\f \\\\f"')
62
+ expect(R.to_string('\n \n')).to eq('"\\\\n \\\\n"')
63
+ expect(R.to_string('\r \r')).to eq('"\\\\r \\\\r"')
64
+ expect(R.to_string('\t \t')).to eq('"\\\\t \\\\t"')
65
+ expect(R.to_string('\v \v')).to eq('"\\\\v \\\\v"')
66
+ expect(R.to_string('\0 \0')).to eq('"\\\\0 \\\\0"')
67
+ expect(R.to_string('\\ \\')).to eq('"\\\\ \\\\"')
68
+ end
69
+
70
+ # it 'returns the string representation of a Date object' do
71
+ # expect(R.to_string(DateTime.parse('2001-02-03T04:05:06.000Z')))
72
+ # .to eq('DateTime.parse("2001-02-03T04:05:06.000Z")');
73
+ # expect(R.to_string(DateTime.parse('XXX'))).to eq('new Date(NaN)');
74
+ # end
75
+
76
+ it 'returns the string representation of a RegExp object' do
77
+ expect(R.to_string(/(?:)/)).to eq('/(?:)/')
78
+ expect(R.to_string(%r{/}i)).to eq('/\\//i')
79
+ end
80
+ end
81
+
39
82
  context '#to_upper' do
40
83
  it 'from docs' do
41
84
  expect(r.to_upper('abc')).to eq('ABC')
@@ -17,6 +17,7 @@ describe Ramda do
17
17
  r(:T)
18
18
  r(:__)
19
19
  r(:add)
20
+ r(:adjust)
20
21
  r(:all)
21
22
  r(:all_pass)
22
23
  r(:always)
@@ -55,6 +56,8 @@ describe Ramda do
55
56
  r(:dissoc_path)
56
57
  r(:divide)
57
58
  r(:drop)
59
+ r(:drop_repeats)
60
+ r(:drop_repeats_with)
58
61
  r(:drop_while)
59
62
  r(:empty)
60
63
  r(:either)
@@ -85,6 +88,7 @@ describe Ramda do
85
88
  r(:insert)
86
89
  r(:insert_all)
87
90
  r(:intersection)
91
+ r(:intersperse)
88
92
  r(:into)
89
93
  r(:invert)
90
94
  r(:invert_obj)
@@ -111,6 +115,8 @@ describe Ramda do
111
115
  r(:match)
112
116
  r(:max)
113
117
  r(:max_by)
118
+ r(:mean)
119
+ r(:median)
114
120
  r(:memoize)
115
121
  r(:merge)
116
122
  r(:merge_all)
@@ -170,6 +176,7 @@ describe Ramda do
170
176
  r(:times)
171
177
  r(:to_lower)
172
178
  r(:to_pairs)
179
+ r(:to_string)
173
180
  r(:to_upper)
174
181
  r(:transduce)
175
182
  r(:trim)
@@ -187,6 +194,7 @@ describe Ramda do
187
194
  r(:values)
188
195
  r(:view)
189
196
  r(:where)
197
+ r(:where_eq)
190
198
  r(:xprod)
191
199
  r(:zip)
192
200
  r(:zip_obj)