array_utility 1.0.0 → 1.1.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 +4 -4
- data/examples/examples.rb +24 -0
- data/lib/array_utility.rb +14 -0
- data/spec/test_prepend_capped.rb +11 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3923c019b192cd8768cc54b5ea6f88ed15e4183
|
4
|
+
data.tar.gz: 67ec1a1527660a1f6109429b39ee48e8989bba46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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
|
+
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
|
15
|
-
|
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:
|