basecustom 0.1.2 → 0.1.3
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.
- data/README +2 -0
- data/lib/basecustom/version.rb +7 -3
- data/lib/basecustom.rb +18 -8
- data/test/test_bc.rb +22 -0
- metadata +4 -4
data/README
CHANGED
@@ -4,6 +4,8 @@ by Daniel P. Clark
|
|
4
4
|
Inspired by:
|
5
5
|
* base62 by "JT Zemp" and contributors "Saadiq Rodgers-King", "Derrick Camerino"
|
6
6
|
|
7
|
+
Version 0.1.3 created from assistance and advice by my father Robert Clark.
|
8
|
+
|
7
9
|
Description
|
8
10
|
* Define any base conversion with any identifier for each value.
|
9
11
|
|
data/lib/basecustom/version.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
class BaseCustom
|
2
|
-
VERSION = "0.1.
|
2
|
+
VERSION = "0.1.3"
|
3
3
|
end
|
4
4
|
|
5
|
+
# Version 0.1.3
|
6
|
+
# * Fixed multicharacter numbering units
|
7
|
+
# * Added delimiter for multicharacter numbering units
|
8
|
+
# * Safe base type creation. Each situation checked for and proper error messages.
|
5
9
|
# Version 0.1.2
|
6
|
-
# * Added unique to string init
|
7
|
-
# * Added unique to array as well as all elements are
|
10
|
+
# * Added unique to string init declaration
|
11
|
+
# * Added unique to array as well as checking that all array elements are strings
|
8
12
|
# Version 0.1.1
|
9
13
|
# * Flatten array's on input initialization
|
10
14
|
# Version 0.1.0
|
data/lib/basecustom.rb
CHANGED
@@ -6,27 +6,37 @@ $: << File.join(File.dirname(__FILE__), "/basecustom")
|
|
6
6
|
require 'basecustom/version'
|
7
7
|
|
8
8
|
class BaseCustom
|
9
|
-
def initialize(array_in)
|
9
|
+
def initialize(array_in, delim = '')
|
10
10
|
if array_in.is_a?(String)
|
11
|
-
|
11
|
+
if not delim.empty?
|
12
|
+
raise "You must use an Array when defining with a delimiter."
|
13
|
+
end
|
14
|
+
array_in = array_in.split('')
|
12
15
|
end
|
13
16
|
if not array_in.is_a?(Array)
|
14
|
-
raise "Invalid type
|
17
|
+
raise "Invalid type! Please provide a String or an Array."
|
15
18
|
end
|
19
|
+
array_in = array_in.flatten
|
16
20
|
if array_in.any? { |i| not i.is_a?(String) }
|
17
|
-
raise "Invalid type
|
21
|
+
raise "Invalid type! Each array element must be a String."
|
22
|
+
end
|
23
|
+
if array_in.any? { |i| i.length > 1 }
|
24
|
+
if delim.empty?
|
25
|
+
raise "Error! You must define a delimiter when using multiple characters for a base."
|
26
|
+
end
|
18
27
|
end
|
19
|
-
@BASE_PRIMITIVES_ARRAY = array_in.
|
28
|
+
@BASE_PRIMITIVES_ARRAY = array_in.uniq
|
20
29
|
@BASE_PRIMITIVES_HASH = Hash[@BASE_PRIMITIVES_ARRAY.each_with_index.map {|x,idx| [x, idx]}]
|
30
|
+
@delim = delim
|
21
31
|
end # initialize
|
22
32
|
|
23
33
|
def base(input_val)
|
24
34
|
if input_val.is_a?(String)
|
25
|
-
if input_val.split(
|
35
|
+
if input_val.split(@delim).any? { |i| not @BASE_PRIMITIVES_ARRAY.include?(i) }
|
26
36
|
raise "Characters used are not in predefined base!"
|
27
37
|
end
|
28
38
|
i, i_out = 0, 0
|
29
|
-
input_val.split(
|
39
|
+
input_val.split(@delim).reverse.each do |c|
|
30
40
|
place = @BASE_PRIMITIVES_HASH.size ** i
|
31
41
|
i_out += @BASE_PRIMITIVES_HASH[c] * place
|
32
42
|
i += 1
|
@@ -38,7 +48,7 @@ class BaseCustom
|
|
38
48
|
number = input_val
|
39
49
|
result = ''
|
40
50
|
while(number != 0)
|
41
|
-
result = @BASE_PRIMITIVES_ARRAY[number % @BASE_PRIMITIVES_ARRAY.size ].to_s + result
|
51
|
+
result = @BASE_PRIMITIVES_ARRAY[number % @BASE_PRIMITIVES_ARRAY.size ].to_s + @delim + result
|
42
52
|
number /= @BASE_PRIMITIVES_ARRAY.size
|
43
53
|
end
|
44
54
|
result
|
data/test/test_bc.rb
CHANGED
@@ -30,5 +30,27 @@ class TestBaseCustom < Test::Unit::TestCase
|
|
30
30
|
assert_raise RuntimeError, LoadError do base2.base(:nonexistant); end
|
31
31
|
assert_raise RuntimeError, LoadError do base2.base('abc'); end
|
32
32
|
assert_raise RuntimeError, LoadError do base2.base(4.5); end
|
33
|
+
assert_raise RuntimeError, LoadError do baseFail = BaseCustom.new(%w[:a :b :c]); end
|
34
|
+
assert_raise RuntimeError, LoadError do baseFail = BaseCustom.new(":a:b:c", ":"); end
|
35
|
+
assert_raise RuntimeError, LoadError do baseFail = BaseCustom.new(0..9); end
|
36
|
+
assert_raise RuntimeError, LoadError do baseFail = BaseCustom.new(['0','1',2]); end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_delim
|
40
|
+
base = BaseCustom.new([ 'a', 'bb', 'ccc', 'dddd' ], ' ' )
|
41
|
+
assert base.base( 20 ) == 'bb bb a '
|
42
|
+
assert base.base( 'bb bb a ' ) == 20
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_delim_music
|
46
|
+
baseMusic = BaseCustom.new( %w[A A# B C C# D D# E F F# G G#], ' ' )
|
47
|
+
baseMusic.base( (Math::PI * 100000000).to_i ) == "F F# B D# D A# D# F# "
|
48
|
+
baseMusic.base( "F F# B D# D A# D# F# " ) == (Math::PI * 100000000).to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_multi_with_delim
|
52
|
+
baseMND = BaseCustom.new(%w(aa bb cc), ':')
|
53
|
+
baseMND.base(12) == "bb:bb:aa:"
|
54
|
+
baseMND.base("bb:bb:aa:") == 12
|
33
55
|
end
|
34
56
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: basecustom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Daniel P. Clark / 6ftDan(TM)
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-11-22 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: Define any base conversion with any identifier for each value. Let the fun begin!
|