caseconverter 0.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.
- data/README +59 -0
- data/lib/case_converter.rb +75 -0
- data/lib/caseconverter.rb +2 -0
- data/test/tc_case_converter.rb +61 -0
- data/test/ts_case_converter.rb +2 -0
- metadata +48 -0
data/README
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
Case Converter is copyrighted free software by Brian Takita <brian.takita@gmail.com>.
|
2
|
+
You can redistribute it and/or modify it under either the terms of the GPL, or the conditions below:
|
3
|
+
|
4
|
+
1. You may make and give away verbatim copies of the source form of the
|
5
|
+
software without restriction, provided that you duplicate all of the
|
6
|
+
original copyright notices and associated disclaimers.
|
7
|
+
|
8
|
+
2. You may modify your copy of the software in any way, provided that
|
9
|
+
you do at least ONE of the following:
|
10
|
+
|
11
|
+
a) place your modifications in the Public Domain or otherwise
|
12
|
+
make them Freely Available, such as by posting said
|
13
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
14
|
+
the author to include your modifications in the software.
|
15
|
+
|
16
|
+
b) use the modified software only within your corporation or
|
17
|
+
organization.
|
18
|
+
|
19
|
+
c) rename any non-standard executables so the names do not conflict
|
20
|
+
with standard executables, which must also be provided.
|
21
|
+
|
22
|
+
d) make other distribution arrangements with the author.
|
23
|
+
|
24
|
+
3. You may distribute the software in object code or executable
|
25
|
+
form, provided that you do at least ONE of the following:
|
26
|
+
|
27
|
+
a) distribute the executables and library files of the software,
|
28
|
+
together with instructions (in the manual page or equivalent)
|
29
|
+
on where to get the original distribution.
|
30
|
+
|
31
|
+
b) accompany the distribution with the machine-readable source of
|
32
|
+
the software.
|
33
|
+
|
34
|
+
c) give non-standard executables non-standard names, with
|
35
|
+
instructions on where to get the original software distribution.
|
36
|
+
|
37
|
+
d) make other distribution arrangements with the author.
|
38
|
+
|
39
|
+
4. You may modify and include the part of the software into any other
|
40
|
+
software (possibly commercial). But some files in the distribution
|
41
|
+
are not written by the author, so that they are not under this terms.
|
42
|
+
|
43
|
+
They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
|
44
|
+
files under the ./missing directory. See each file for the copying
|
45
|
+
condition.
|
46
|
+
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
48
|
+
output from the software do not automatically fall under the
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
51
|
+
software.
|
52
|
+
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
56
|
+
PURPOSE.
|
57
|
+
|
58
|
+
|
59
|
+
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module CaseConverter
|
2
|
+
class << self
|
3
|
+
public
|
4
|
+
|
5
|
+
def to_upper_camel_case(str)
|
6
|
+
tokens = tokenize(str)
|
7
|
+
tokens.each { |t| t.capitalize! }
|
8
|
+
tokens.join('')
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_lower_camel_case(str)
|
12
|
+
tokens = tokenize(str)
|
13
|
+
tokens.each_with_index { |t, i| t.capitalize! unless i == 0 }
|
14
|
+
tokens.join('')
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_underscore_case(str)
|
18
|
+
tokens = tokenize(str)
|
19
|
+
tokens.join('_')
|
20
|
+
end
|
21
|
+
|
22
|
+
def tokenize(str)
|
23
|
+
str.squeeze(' ')
|
24
|
+
tokens = str.split(' ')
|
25
|
+
|
26
|
+
tokenize_non_alphanumeric_characters(tokens)
|
27
|
+
tokenize_camel_casing(tokens)
|
28
|
+
tokens
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def tokenize_non_alphanumeric_characters(tokens)
|
34
|
+
tokens.each_with_index do |t, i|
|
35
|
+
tokens[i] = t.split(/[\W_]+/)
|
36
|
+
end
|
37
|
+
|
38
|
+
tokens.flatten!
|
39
|
+
end
|
40
|
+
|
41
|
+
def tokenize_camel_casing(tokens)
|
42
|
+
tokens.each_with_index do |t, i|
|
43
|
+
tokenized_array = []
|
44
|
+
|
45
|
+
cur_str = t
|
46
|
+
loop do
|
47
|
+
matches = cur_str.scan(/^(.[^A-Z]*)(.*)/).flatten
|
48
|
+
tokenized_array << matches[0].downcase
|
49
|
+
cur_str = matches[1]
|
50
|
+
break if cur_str.nil? || cur_str == ''
|
51
|
+
end
|
52
|
+
tokens[i] = tokenized_array
|
53
|
+
end
|
54
|
+
|
55
|
+
tokens.flatten!
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_upper_camel_case
|
60
|
+
CaseConverter.to_upper_camel_case(self.to_s)
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_lower_camel_case(str)
|
64
|
+
CaseConverter.to_lower_camel_case(self.to_s)
|
65
|
+
end
|
66
|
+
|
67
|
+
def to_underscore_case(str)
|
68
|
+
CaseConverter.to_underscore_case(self.to_s)
|
69
|
+
end
|
70
|
+
|
71
|
+
def tokenize
|
72
|
+
CaseConverter.tokenize(self.to_s)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
dir = File.dirname(__FILE__)
|
2
|
+
require "#{dir}/../lib/case_converter"
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class TestCaseConverter < Test::Unit::TestCase
|
6
|
+
public
|
7
|
+
|
8
|
+
def test_to_upper_camel_case
|
9
|
+
strings_to_test.each do |s|
|
10
|
+
assert_equal(
|
11
|
+
'OneTwoThreeFour',
|
12
|
+
CaseConverter.to_upper_camel_case(s)
|
13
|
+
)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_to_lower_camel_case
|
18
|
+
strings_to_test.each do |s|
|
19
|
+
assert_equal(
|
20
|
+
'oneTwoThreeFour',
|
21
|
+
CaseConverter.to_lower_camel_case(s)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_to_underscore_case
|
27
|
+
strings_to_test.each do |s|
|
28
|
+
assert_equal(
|
29
|
+
'one_two_three_four',
|
30
|
+
CaseConverter.to_underscore_case(s)
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_tokenize
|
36
|
+
strings_to_test.each do |s|
|
37
|
+
tokens = CaseConverter.tokenize(s)
|
38
|
+
assert_equal('one', tokens[0])
|
39
|
+
assert_equal('two', tokens[1])
|
40
|
+
assert_equal('three', tokens[2])
|
41
|
+
assert_equal('four', tokens[3])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
include(CaseConverter)
|
48
|
+
|
49
|
+
def strings_to_test
|
50
|
+
[
|
51
|
+
'one two three four',
|
52
|
+
'one_two_three_four',
|
53
|
+
'oneTwoThreeFour',
|
54
|
+
'OneTwoThreeFour',
|
55
|
+
'one two_threeFour',
|
56
|
+
'one_TwoThree Four',
|
57
|
+
'one,two, three, four',
|
58
|
+
'One-two_threeFour'
|
59
|
+
]
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: caseconverter
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2005-09-06 00:00:00 -07:00
|
8
|
+
summary: Operations that change the casing of a string.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: brian.takita@gmail.com
|
12
|
+
homepage: http://caseconverter.rubyforge.org
|
13
|
+
rubyforge_project:
|
14
|
+
description: "Converts to and from underscore case, upper camel case, lower camel case, and
|
15
|
+
custom casing."
|
16
|
+
autorequire:
|
17
|
+
default_executable:
|
18
|
+
bindir: bin
|
19
|
+
has_rdoc: true
|
20
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
21
|
+
requirements:
|
22
|
+
-
|
23
|
+
- ">"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 0.0.0
|
26
|
+
version:
|
27
|
+
platform: ruby
|
28
|
+
signing_key:
|
29
|
+
cert_chain:
|
30
|
+
authors:
|
31
|
+
- Brian Takita
|
32
|
+
files:
|
33
|
+
- test/tc_case_converter.rb
|
34
|
+
- test/ts_case_converter.rb
|
35
|
+
- lib/caseconverter.rb
|
36
|
+
- lib/case_converter.rb
|
37
|
+
- README
|
38
|
+
test_files:
|
39
|
+
- test/ts_case_converter.rb
|
40
|
+
rdoc_options:
|
41
|
+
- "--main"
|
42
|
+
- lib/case_converter.rb
|
43
|
+
extra_rdoc_files:
|
44
|
+
- README
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
requirements: []
|
48
|
+
dependencies: []
|