array_utility 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66035a12a194d47cd799f876ff4570ed66949a95
4
- data.tar.gz: b959f21f2dfb3fafe72c06a111c78ae5029ba8c5
3
+ metadata.gz: d3923c019b192cd8768cc54b5ea6f88ed15e4183
4
+ data.tar.gz: 67ec1a1527660a1f6109429b39ee48e8989bba46
5
5
  SHA512:
6
- metadata.gz: cf392b49fdfe79f83fa3797b36698ae2b22b720bd48ee860b1210254413e93c22f8954d155e2ab4591f12ae8137bd19c914aed6c40f696a4a5c54cf6df4c3bc7
7
- data.tar.gz: 0e52a964c661d96ba4958dc79ffac520bbdcc8ea17ee7696be3e22f87ef5735e98aa5e064e3a941b30ad2a7762d8fb6443171d8dc8d852f903c2f8d922a31986
6
+ metadata.gz: e7bd835df1b692ecc4f9f17dc72794bee69d25b16a750cabc33c3b66deb96af53c821aaff832480628b9e9e8e2060cdd7f9346732b30bc22b3afb77631aed97c
7
+ data.tar.gz: df392144b06ea2838c2f548114c1f1fdc0c0485d14f1595826a2c13477dfda3622943b8cd7d643d3032a4028cff81ef88df32ecddf432d5a64cfbca3b80fd19a
@@ -0,0 +1,24 @@
1
+ require_relative '../lib/array_utility'
2
+
3
+ using ArrayUtility
4
+
5
+ [1, 2, 3].next(2)
6
+ # => 3
7
+
8
+ [1, 2, 3].next(3)
9
+ # => nil
10
+
11
+ [1, 2, 3].before(2)
12
+ # => 1
13
+
14
+ [1, 2, 3].before(1)
15
+ # => 3
16
+
17
+ [1, 2, 3].prepend_capped(4, 3)
18
+ # => [4, 1, 2]
19
+
20
+ [1, 2, 3].prepend_capped(70, 4)
21
+ # => [70, 1, 2, 3]
22
+
23
+ [1, 2, 3].prepend_capped(6, 2)
24
+ # => [6, 1]
data/lib/array_utility.rb CHANGED
@@ -15,5 +15,19 @@ module ArrayUtility
15
15
  def before(single)
16
16
  self[index(single) - 1]
17
17
  end
18
+
19
+ # Puts a value at the start of an array, and deletes all values greater than
20
+ # the cap.
21
+ # @param value [Any] The value to prepend.
22
+ # @param cap [Int] The maximum number of values in the array.
23
+ def prepend_capped(value, cap)
24
+ unshift(value)
25
+ if size > cap
26
+ drop(cap).each do |val|
27
+ delete(val)
28
+ end
29
+ end
30
+ self
31
+ end
18
32
  end
19
33
  end
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require_relative '../lib/array_utility'
3
+
4
+ class TestPrependCapped < Test::Unit::TestCase
5
+ using ArrayUtility
6
+ def test_prepend_capped
7
+ assert_equal([4, 1, 2], [1, 2, 3].prepend_capped(4, 3))
8
+ assert_equal([4, 1, 2], [1, 2].prepend_capped(4, 3))
9
+ assert_equal([4], [1, 2].prepend_capped(4, 1))
10
+ end
11
+ end
metadata CHANGED
@@ -1,26 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array_utility
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-20 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Some simple but very useful utilities for Array objects. It primarily
14
- focuses on using the most efficient methods to get certain values from Arrays. Eventually
15
- it may have methods for manipulating Arrays.
14
+ focuses on using the most efficient methods to get and modify certain values from
15
+ Arrays.
16
16
  email: elifosterwy@gmail.com
17
17
  executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - examples/examples.rb
21
22
  - lib/array_utility.rb
22
23
  - spec/test_before.rb
23
24
  - spec/test_next.rb
25
+ - spec/test_prepend_capped.rb
24
26
  homepage: https://github.com/elifoster/array_utility
25
27
  licenses: []
26
28
  metadata: