shorten 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "yard", "~> 0.6.0"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.6.4"
13
+ gem "rcov", ">= 0"
14
+ end
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.6.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.9.2)
11
+ rcov (0.9.10)
12
+ rcov (0.9.10-java)
13
+ rspec (2.3.0)
14
+ rspec-core (~> 2.3.0)
15
+ rspec-expectations (~> 2.3.0)
16
+ rspec-mocks (~> 2.3.0)
17
+ rspec-core (2.3.1)
18
+ rspec-expectations (2.3.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.3.0)
21
+ yard (0.6.8)
22
+
23
+ PLATFORMS
24
+ java
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (~> 1.0.0)
29
+ jeweler (~> 1.6.4)
30
+ rcov
31
+ rspec (~> 2.3.0)
32
+ yard (~> 0.6.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Junegunn Choi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ = shorten
2
+
3
+ A number shortener.
4
+
5
+ == Example
6
+
7
+ # Default: Shorten::BASE62
8
+ 12345.shorten
9
+ 12345.shorten.unshorten
10
+
11
+ # Using Shorten::BASE36 for case-insensitivity
12
+ 12345.shorten(Shorten::BASE36)
13
+ 12345.shorten(Shorten::BASE36).unshorten(Shorten::BASE36)
14
+
15
+ # Using an arbitrary sequence of characters for shortening
16
+ 12345.shorten("ㄱㄴㄷㄹㅁㅂㅅㅇㅈㅊㅋㅌㅍㅎ")
17
+
18
+ == TODO
19
+
20
+ * Negative numbers.
21
+ * Float support.
22
+
23
+ == Contributing to shorten
24
+
25
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
26
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
27
+ * Fork the project
28
+ * Start a feature/bugfix branch
29
+ * Commit and push until you are happy with your contribution
30
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
31
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
32
+
33
+ == Copyright
34
+
35
+ Copyright (c) 2011 Junegunn Choi. See LICENSE.txt for
36
+ further details.
37
+
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "shorten"
18
+ gem.homepage = "http://github.com/junegunn/shorten"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Number shortener}
21
+ gem.description = %Q{Number shortener}
22
+ gem.email = "junegunn.c@gmail.com"
23
+ gem.authors = ["Junegunn Choi"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,56 @@
1
+ # encoding: UTF-8
2
+
3
+ module Shorten
4
+ BASE36 = ((0..9).to_a + ('a'..'z').to_a).map(&:to_s).join
5
+ BASE62 = BASE36 + ('A'..'Z').map(&:to_s).join
6
+
7
+ module Numeric
8
+ # @param [String] chars Sequence of characters for shortening
9
+ # @return [String] Shortened string
10
+ def shorten chars = Shorten::BASE62
11
+ raise ArgumentError.new('String required') unless chars.is_a? String
12
+ raise ArgumentError.new('Only non-negative integers can be shortened') if self < 0
13
+
14
+ num = self
15
+ len = chars.length
16
+ str = ''
17
+ while num > 0
18
+ mod = num % len
19
+ str = chars[mod, 1] + str
20
+ num /= len
21
+ end
22
+ str
23
+ end
24
+ end
25
+
26
+ module String
27
+ # @param [String] chars Sequence of characters for unshortening
28
+ # @return [Fixnum/Bignum] Unshortened number
29
+ def unshorten chars = Shorten::BASE62
30
+ raise ArgumentError.new('String required') unless chars.is_a? String
31
+
32
+ num = 0
33
+ len = chars.length
34
+ self.each_char do |c|
35
+ num *= len
36
+ char = chars.index(c)
37
+ raise ArgumentError.new('Cannot unshorten: invalid characters') if char.nil?
38
+ num += char
39
+ end
40
+ num
41
+ end
42
+ end
43
+ end
44
+
45
+ class Fixnum
46
+ include Shorten::Numeric
47
+ end
48
+
49
+ class Bignum
50
+ include Shorten::Numeric
51
+ end
52
+
53
+ class String
54
+ include Shorten::String
55
+ end
56
+
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env rspec
2
+ # encoding: UTF-8
3
+
4
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
5
+
6
+ CHEETSHEET = {
7
+ :base62 => {
8
+ 1 => '1',
9
+ 9 => '9',
10
+ 10 => 'a',
11
+ 36 => 'A',
12
+ 37 => 'B',
13
+ 62 => '10',
14
+ 63 => '11',
15
+ 100000000000000000000000000000000 => 'xPEhw0uJHtT7g9cK7C'
16
+ },
17
+ :base36 => {
18
+ 1 => '1',
19
+ 9 => '9',
20
+ 10 => 'a',
21
+ 36 => '10',
22
+ 37 => '11'
23
+ }
24
+ }
25
+
26
+ describe "Shorten" do
27
+ it "shortens a number" do
28
+ CHEETSHEET[:base62].each do |k, v|
29
+ k.shorten.should eq v
30
+ k.shorten(Shorten::BASE62).should eq v
31
+ end
32
+
33
+ CHEETSHEET[:base36].each do |k, v|
34
+ k.shorten(Shorten::BASE36).should eq v
35
+ k.shorten(Shorten::BASE36).should eq k.to_s(36)
36
+ end
37
+
38
+ (1..10000).each do |i|
39
+ i.shorten.unshorten.should eq i
40
+ end
41
+
42
+ (9999999999999999999989999..9999999999999999999999999).each do |i|
43
+ i.shorten.unshorten.should eq i
44
+ end
45
+ end
46
+
47
+ it "shortens a number with any given characters" do
48
+ chars = Shorten::BASE62
49
+ my_chars = chars.each_char.to_a.shuffle.join
50
+ map = {}
51
+
52
+ chars.each_char.each_with_index do |e, idx|
53
+ map[e] = my_chars[idx]
54
+ end
55
+
56
+ CHEETSHEET[:base62].each do |k, v|
57
+ k.shorten(my_chars).should eq(v.each_char.map { |e| map[e] }.join)
58
+ k.shorten(my_chars).unshorten(my_chars).should eq(k)
59
+ end
60
+
61
+ 12345.shorten("ㄱㄴㄷㄹㅁㅂㅅㅇㅈㅊㅋㅌㅍㅎ").should eq "ㅁㅅㅎㅌ"
62
+ "ㅁㅅㅎㅌ".unshorten("ㄱㄴㄷㄹㅁㅂㅅㅇㅈㅊㅋㅌㅍㅎ").should eq 12345
63
+ end
64
+
65
+ it "shortens Bignums" do
66
+ bn = 1000000000000000000000000000000000
67
+ bn.shorten(Shorten::BASE36).should eq bn.to_s(36)
68
+ end
69
+
70
+ it "unshortens a shortened number" do
71
+ CHEETSHEET[:base62].each do |k, v|
72
+ k.shorten.unshorten.should eq k
73
+ k.shorten(Shorten::BASE62).unshorten(Shorten::BASE62).should eq k
74
+ end
75
+
76
+ CHEETSHEET[:base36].each do |k, v|
77
+ k.shorten(Shorten::BASE36).unshorten(Shorten::BASE36).should eq k
78
+ k.shorten(Shorten::BASE36).unshorten(Shorten::BASE36).should eq k
79
+ end
80
+ end
81
+
82
+ it "throws error on unshorten-able string" do
83
+ expect { "-_-;".unshorten }.to raise_error(ArgumentError)
84
+ end
85
+
86
+ it "cannot shorten negative integers" do
87
+ expect { -5.shorten }.to raise_error(ArgumentError)
88
+ end
89
+
90
+ it "does not support Float" do
91
+ expect { 8.27.shorten }.to raise_error(NoMethodError)
92
+ end
93
+
94
+ it "throws error on invalid parameter" do
95
+ [
96
+ 1234,
97
+ 'a'..'z',
98
+ {:a => 1},
99
+ ["hello"]
100
+ ].each do |inv|
101
+ expect { 1234.shorten(inv) }.to raise_error(ArgumentError)
102
+ expect { "1234".unshorten(inv) }.to raise_error(ArgumentError)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'shorten'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shorten
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Junegunn Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-15 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2152905400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2152905400
25
+ - !ruby/object:Gem::Dependency
26
+ name: yard
27
+ requirement: &2152904880 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.6.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2152904880
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &2152904400 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152904400
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &2152903900 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.4
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2152903900
58
+ - !ruby/object:Gem::Dependency
59
+ name: rcov
60
+ requirement: &2152903380 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2152903380
69
+ description: Number shortener
70
+ email: junegunn.c@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ files:
77
+ - .document
78
+ - .rspec
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.rdoc
83
+ - Rakefile
84
+ - VERSION
85
+ - lib/shorten.rb
86
+ - spec/shorten_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: http://github.com/junegunn/shorten
89
+ licenses:
90
+ - MIT
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: -1233340932936789572
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.6
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Number shortener
116
+ test_files: []