alphadecimal 1.0.1
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/History.txt +10 -0
- data/Manifest.txt +8 -0
- data/README.txt +49 -0
- data/Rakefile +13 -0
- data/bin/alphadecimal +0 -0
- data/init.rb +1 -0
- data/lib/alphadecimal.rb +64 -0
- data/test/test_alphadecimal.rb +69 -0
- metadata +74 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
= alphadecimal
|
|
2
|
+
|
|
3
|
+
* http://github.com/JackDanger/alphadecimal
|
|
4
|
+
|
|
5
|
+
== DESCRIPTION:
|
|
6
|
+
|
|
7
|
+
Convert integers to base62 strings (A-Za-z0-9) and back. A handy
|
|
8
|
+
way to shorten long numbers.
|
|
9
|
+
|
|
10
|
+
== SYNOPSIS:
|
|
11
|
+
|
|
12
|
+
# 45.alphadecimal
|
|
13
|
+
# => 'j'
|
|
14
|
+
# 5234233.alphadecimal
|
|
15
|
+
# 'Lxf7'
|
|
16
|
+
# 'yH783Xx3Ajz99'.alphadecimal
|
|
17
|
+
# => 194466611442340388130115
|
|
18
|
+
# 194466611442340388130115.alphadecimal
|
|
19
|
+
# => 'yH783Xx3Ajz99'
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
== INSTALL:
|
|
23
|
+
|
|
24
|
+
* sudo gem install alphadecimal
|
|
25
|
+
|
|
26
|
+
== LICENSE:
|
|
27
|
+
|
|
28
|
+
(The MIT License)
|
|
29
|
+
|
|
30
|
+
Copyright (c) 2008 Jack Danger Canty, Mike Mondragon
|
|
31
|
+
|
|
32
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
33
|
+
a copy of this software and associated documentation files (the
|
|
34
|
+
'Software'), to deal in the Software without restriction, including
|
|
35
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
36
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
37
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
38
|
+
the following conditions:
|
|
39
|
+
|
|
40
|
+
The above copyright notice and this permission notice shall be
|
|
41
|
+
included in all copies or substantial portions of the Software.
|
|
42
|
+
|
|
43
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
44
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
45
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
46
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
47
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
48
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
49
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# -*- ruby -*-
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'hoe'
|
|
5
|
+
require './lib/alphadecimal.rb'
|
|
6
|
+
|
|
7
|
+
Hoe.new('alphadecimal', Alphadecimal::VERSION) do |p|
|
|
8
|
+
# p.rubyforge_name = 'alphadecimal' # if different than lowercase project name
|
|
9
|
+
p.developer('Mike Mondragon', 'mikemondragon@gmail.com')
|
|
10
|
+
p.developer('Jack Danger Canty', 'jdanger@adpickles.com')
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# vim: syntax=Ruby
|
data/bin/alphadecimal
ADDED
|
File without changes
|
data/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'alphadecimal'
|
data/lib/alphadecimal.rb
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module Alphadecimal
|
|
2
|
+
VERSION = '1.0.1'
|
|
3
|
+
|
|
4
|
+
B62_0, B62_9 = '0'[0], '9'[0]
|
|
5
|
+
B62_A, B62_Z = 'A'[0], 'Z'[0]
|
|
6
|
+
B62_a, B62_z = 'a'[0], 'z'[0]
|
|
7
|
+
B62_CHRS = [(B62_0..B62_9).map, (B62_A..B62_Z).map, (B62_a..B62_z).map].flatten
|
|
8
|
+
|
|
9
|
+
module Number
|
|
10
|
+
def alphadecimal
|
|
11
|
+
return self if nil? || self < 0
|
|
12
|
+
return to_s if self == 0
|
|
13
|
+
string = ""
|
|
14
|
+
value = self
|
|
15
|
+
until value == 0
|
|
16
|
+
case mod = value % 62
|
|
17
|
+
when 0..9 then string << (mod + B62_0)
|
|
18
|
+
when 10..35 then string << (mod - 10 + B62_A)
|
|
19
|
+
when 36...62 then string << (mod - 36 + B62_a)
|
|
20
|
+
end
|
|
21
|
+
value = value / 62
|
|
22
|
+
end
|
|
23
|
+
string.reverse
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
module String
|
|
28
|
+
def alphadecimal
|
|
29
|
+
return self unless is_alphadecimal?
|
|
30
|
+
val = 0
|
|
31
|
+
key = reverse
|
|
32
|
+
(0...key.size).each do |i|
|
|
33
|
+
char = key[i]
|
|
34
|
+
case
|
|
35
|
+
when (B62_0..B62_9).include?(char) then norm = char - B62_0
|
|
36
|
+
when (B62_A..B62_Z).include?(char) then norm = char - B62_A + 10
|
|
37
|
+
when (B62_a..B62_z).include?(char) then norm = char - B62_a + 36
|
|
38
|
+
end
|
|
39
|
+
val = val + (norm * (62 ** i))
|
|
40
|
+
end
|
|
41
|
+
val
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def is_alphadecimal?
|
|
45
|
+
return false if nil?
|
|
46
|
+
string = self.dup
|
|
47
|
+
return false if string.length <= 0
|
|
48
|
+
(0...string.size).each do |i|
|
|
49
|
+
return false unless B62_CHRS.include?(string[i])
|
|
50
|
+
end
|
|
51
|
+
true
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
class String
|
|
57
|
+
include Alphadecimal::String
|
|
58
|
+
end
|
|
59
|
+
class Fixnum
|
|
60
|
+
include Alphadecimal::Number
|
|
61
|
+
end
|
|
62
|
+
class Bignum
|
|
63
|
+
include Alphadecimal::Number
|
|
64
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
require File.dirname(__FILE__) + "/../lib/alphadecimal"
|
|
3
|
+
|
|
4
|
+
class TestAlphadecimal < Test::Unit::TestCase
|
|
5
|
+
|
|
6
|
+
include Alphadecimal
|
|
7
|
+
|
|
8
|
+
def test_fixnum_becomes_string
|
|
9
|
+
assert_kind_of String, 45.alphadecimal
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_bignum_becomes_string
|
|
13
|
+
assert_kind_of String, 231087934571049032.alphadecimal
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_string_becomes_bignum
|
|
17
|
+
assert_kind_of Bignum, 'aHealk43JZy'.alphadecimal
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_string_becomes_fixnum
|
|
21
|
+
assert_kind_of Fixnum, 'j'.alphadecimal
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_invalid_string_returns_self
|
|
25
|
+
string = 'Whoah there!'
|
|
26
|
+
assert_equal string, string.alphadecimal
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_identities
|
|
30
|
+
[123, 90000008, 733, '12331', 'abYzj', '0', 'A', 'z',
|
|
31
|
+
79823898923232, 'ZZZZZZZZZZZZZZZZZZZ', 'Yz82J3Ng5Nj9M1',
|
|
32
|
+
'!!!', '@$%!^', 'abc ', 'HP', 4, 'ǟ'].each do |object|
|
|
33
|
+
assert_equal object, object.alphadecimal.alphadecimal
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_alphabet_characters_should_only_validate
|
|
38
|
+
alphabet = (('0'[0]..'9'[0]).collect <<
|
|
39
|
+
('A'[0]..'Z'[0]).collect <<
|
|
40
|
+
('a'[0]..'z'[0]).collect).flatten
|
|
41
|
+
(0..255).each do |i|
|
|
42
|
+
case
|
|
43
|
+
when alphabet.include?(i)
|
|
44
|
+
assert i.chr.is_alphadecimal?, "char #{i} is not valid as string #{i.chr}"
|
|
45
|
+
else
|
|
46
|
+
assert !i.chr.is_alphadecimal?, "char #{i} is valid as string #{i.chr}"
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_edge_62_to_the_0_convertions_should_be_valid
|
|
52
|
+
(0...62).each do |i|
|
|
53
|
+
encode = i.alphadecimal
|
|
54
|
+
decode = encode.alphadecimal
|
|
55
|
+
assert_equal i, decode, "integer #{i} was encoded as #{encode} and was decoded to #{decode}"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def test_edge_62_to_the_n_convertions_should_be_valid
|
|
60
|
+
(0...3).each do |p|
|
|
61
|
+
n = 62 ** p
|
|
62
|
+
(0...62).each do |i|
|
|
63
|
+
encode = (i+n).alphadecimal
|
|
64
|
+
decode = encode.alphadecimal
|
|
65
|
+
assert_equal i+n, decode, "interger #{i+n} was encoded as #{encode} and was decoded to #{decode}"
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: alphadecimal
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mike Mondragon
|
|
8
|
+
- Jack Danger Canty
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2008-03-27 00:00:00 -07:00
|
|
14
|
+
default_executable:
|
|
15
|
+
dependencies:
|
|
16
|
+
- !ruby/object:Gem::Dependency
|
|
17
|
+
name: hoe
|
|
18
|
+
version_requirement:
|
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
- - ">="
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: 1.5.1
|
|
24
|
+
version:
|
|
25
|
+
description: Convert integers to base62 strings (A-Za-z0-9) and back. A handy way to shorten long numbers.
|
|
26
|
+
email:
|
|
27
|
+
- mikemondragon@gmail.com
|
|
28
|
+
- jdanger@adpickles.com
|
|
29
|
+
executables:
|
|
30
|
+
- alphadecimal
|
|
31
|
+
extensions: []
|
|
32
|
+
|
|
33
|
+
extra_rdoc_files:
|
|
34
|
+
- History.txt
|
|
35
|
+
- Manifest.txt
|
|
36
|
+
- README.txt
|
|
37
|
+
files:
|
|
38
|
+
- History.txt
|
|
39
|
+
- Manifest.txt
|
|
40
|
+
- README.txt
|
|
41
|
+
- Rakefile
|
|
42
|
+
- bin/alphadecimal
|
|
43
|
+
- init.rb
|
|
44
|
+
- lib/alphadecimal.rb
|
|
45
|
+
- test/test_alphadecimal.rb
|
|
46
|
+
has_rdoc: true
|
|
47
|
+
homepage: http://github.com/JackDanger/alphadecimal
|
|
48
|
+
post_install_message:
|
|
49
|
+
rdoc_options:
|
|
50
|
+
- --main
|
|
51
|
+
- README.txt
|
|
52
|
+
require_paths:
|
|
53
|
+
- lib
|
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: "0"
|
|
59
|
+
version:
|
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: "0"
|
|
65
|
+
version:
|
|
66
|
+
requirements: []
|
|
67
|
+
|
|
68
|
+
rubyforge_project: alphadecimal
|
|
69
|
+
rubygems_version: 1.0.1
|
|
70
|
+
signing_key:
|
|
71
|
+
specification_version: 2
|
|
72
|
+
summary: Convert integers to base62 strings (A-Za-z0-9) and back
|
|
73
|
+
test_files:
|
|
74
|
+
- test/test_alphadecimal.rb
|