Skip to content

Commit

Permalink
update!: changed to treat digits as keeped marks (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Feb 25, 2024
1 parent 7b64a66 commit cb3dfc6
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 234 deletions.
63 changes: 37 additions & 26 deletions src/camel_case.rs
Expand Up @@ -26,7 +26,6 @@ pub fn camel_case(input: &str) -> String {
NextOfMark,
Others,
}

let mut flag = ChIs::FirstOfStr;

for ch in input.chars() {
Expand Down Expand Up @@ -66,10 +65,7 @@ pub fn camel_case(input: &str) -> String {
}
} else if ch.is_ascii_digit() {
result.push(ch);
match flag {
ChIs::NextOfMark => flag = ChIs::NextOfUpper,
_ => flag = ChIs::Others,
}
flag = ChIs::NextOfMark;
} else {
match flag {
ChIs::FirstOfStr => (),
Expand Down Expand Up @@ -148,12 +144,6 @@ pub fn camel_case_with_sep(input: &str, seps: &str) -> String {
flag = ChIs::Others;
}
}
} else if ch.is_ascii_digit() {
result.push(ch);
match flag {
ChIs::NextOfMark => flag = ChIs::NextOfUpper,
_ => flag = ChIs::Others,
}
} else {
result.push(ch);
flag = ChIs::NextOfMark;
Expand Down Expand Up @@ -226,13 +216,7 @@ pub fn camel_case_with_keep(input: &str, keeped: &str) -> String {
flag = ChIs::Others;
}
}
} else if ch.is_ascii_digit() {
result.push(ch);
match flag {
ChIs::NextOfMark => flag = ChIs::NextOfUpper,
_ => flag = ChIs::Others,
}
} else if keeped.contains(ch) {
} else if ch.is_ascii_digit() || keeped.contains(ch) {
result.push(ch);
flag = ChIs::NextOfMark;
} else {
Expand Down Expand Up @@ -295,11 +279,20 @@ mod tests_of_camel_case {
#[test]
fn it_should_keep_digits() {
let result = camel_case("abc123-456defG789HIJklMN12");
assert_eq!(result, "abc123456defG789HiJklMn12");
assert_eq!(result, "abc123456DefG789HiJklMn12");
}

#[test]
fn is_should_treat_marks_as_separators() {
fn it_should_convert_when_starting_with_digit() {
let result = camel_case("123abc456def");
assert_eq!(result, "123Abc456Def");

let result = camel_case("123ABC456DEF");
assert_eq!(result, "123Abc456Def");
}

#[test]
fn it_should_treat_marks_as_separators() {
let result = camel_case(":.abc~!@def#$ghi%&jk(lm)no/?");
assert_eq!(result, "abcDefGhiJkLmNo");
}
Expand Down Expand Up @@ -375,14 +368,23 @@ mod tests_of_camel_case_with_sep {
#[test]
fn it_should_keep_digits() {
let result = camel_case_with_sep("abc123-456defG789HIJklMN12", "_");
assert_eq!(result, "abc123-456defG789HiJklMn12");
assert_eq!(result, "abc123-456DefG789HiJklMn12");

let result = camel_case_with_sep("abc123-456defG789HIJklMN12", "-");
assert_eq!(result, "abc123456defG789HiJklMn12");
assert_eq!(result, "abc123456DefG789HiJklMn12");
}

#[test]
fn it_should_convert_when_starting_with_digit() {
let result = camel_case_with_sep("123abc456def", "-_");
assert_eq!(result, "123Abc456Def");

let result = camel_case_with_sep("123ABC456DEF", "-_");
assert_eq!(result, "123Abc456Def");
}

#[test]
fn is_should_treat_marks_as_separators() {
fn it_should_treat_marks_as_separators() {
let result = camel_case_with_sep(":.abc~!@def#$ghi%&jk(lm)no/?", ":@$&()/");
assert_eq!(result, ".Abc~!Def#Ghi%JkLmNo?");
}
Expand Down Expand Up @@ -458,14 +460,23 @@ mod tests_of_camel_case_with_keep {
#[test]
fn it_should_keep_digits() {
let result = camel_case_with_keep("abc123-456defG789HIJklMN12", "_");
assert_eq!(result, "abc123456defG789HiJklMn12");
assert_eq!(result, "abc123456DefG789HiJklMn12");

let result = camel_case_with_keep("abc123-456defG789HIJklMN12", "-");
assert_eq!(result, "abc123-456defG789HiJklMn12");
assert_eq!(result, "abc123-456DefG789HiJklMn12");
}

#[test]
fn it_should_convert_when_starting_with_digit() {
let result = camel_case_with_keep("123abc456def", "_");
assert_eq!(result, "123Abc456Def");

let result = camel_case_with_keep("123ABC456DEF", "-");
assert_eq!(result, "123Abc456Def");
}

#[test]
fn is_should_treat_marks_as_separators() {
fn it_should_treat_marks_as_separators() {
let result = camel_case_with_keep(":.abc~!@def#$ghi%&jk(lm)no/?", ".~!#%?");
assert_eq!(result, ".Abc~!Def#Ghi%JkLmNo?");
}
Expand Down
70 changes: 41 additions & 29 deletions src/cobol_case.rs
Expand Up @@ -23,7 +23,8 @@ pub fn cobol_case(input: &str) -> String {
FirstOfStr,
NextOfUpper,
NextOfContdUpper,
NextOfMark,
NextOfSepMark,
NextOfKeepedMark,
Others,
}
let mut flag = ChIs::FirstOfStr;
Expand Down Expand Up @@ -54,22 +55,24 @@ pub fn cobol_case(input: &str) -> String {
}
None => (), // impossible
},
ChIs::NextOfMark => result.push('-'),
ChIs::NextOfSepMark | ChIs::NextOfKeepedMark => {
result.push('-');
}
_ => (),
}
result.push(ch.to_ascii_uppercase());
flag = ChIs::Others;
} else if ch.is_ascii_digit() {
match flag {
ChIs::NextOfMark => result.push('-'),
ChIs::NextOfSepMark => result.push('-'),
_ => (),
}
result.push(ch);
flag = ChIs::Others;
flag = ChIs::NextOfKeepedMark;
} else {
match flag {
ChIs::FirstOfStr => (),
_ => flag = ChIs::NextOfMark,
_ => flag = ChIs::NextOfSepMark,
}
}
}
Expand Down Expand Up @@ -143,15 +146,6 @@ pub fn cobol_case_with_sep(input: &str, seps: &str) -> String {
}
result.push(ch.to_ascii_uppercase());
flag = ChIs::Others;
} else if ch.is_ascii_digit() {
match flag {
ChIs::NextOfSepMark | ChIs::NextOfKeepedMark => {
result.push('-');
}
_ => (),
}
result.push(ch);
flag = ChIs::Others;
} else {
match flag {
ChIs::NextOfSepMark => result.push('-'),
Expand Down Expand Up @@ -227,16 +221,7 @@ pub fn cobol_case_with_keep(input: &str, keeped: &str) -> String {
}
result.push(ch.to_ascii_uppercase());
flag = ChIs::Others;
} else if ch.is_ascii_digit() {
match flag {
ChIs::NextOfSepMark | ChIs::NextOfKeepedMark => {
result.push('-');
}
_ => (),
}
result.push(ch);
flag = ChIs::Others;
} else if keeped.contains(ch) {
} else if ch.is_ascii_digit() || keeped.contains(ch) {
match flag {
ChIs::NextOfSepMark => result.push('-'),
_ => (),
Expand Down Expand Up @@ -303,7 +288,16 @@ mod tests_of_cobol_case {
#[test]
fn it_should_keep_digits() {
let result = cobol_case("abc123-456defG789HIJklMN12");
assert_eq!(result, "ABC123-456DEF-G789-HI-JKL-MN12");
assert_eq!(result, "ABC123-456-DEF-G789-HI-JKL-MN12");
}

#[test]
fn it_should_convert_when_starting_with_digit() {
let result = cobol_case("123abc456def");
assert_eq!(result, "123-ABC456-DEF");

let result = cobol_case("123ABC456DEF");
assert_eq!(result, "123-ABC456-DEF");
}

#[test]
Expand Down Expand Up @@ -383,10 +377,19 @@ mod tests_of_cobol_case_with_sep {
#[test]
fn it_should_keep_digits() {
let result = cobol_case_with_sep("abc123-456defG789HIJklMN12", "-");
assert_eq!(result, "ABC123-456DEF-G789-HI-JKL-MN12");
assert_eq!(result, "ABC123-456-DEF-G789-HI-JKL-MN12");

let result = cobol_case_with_sep("abc123-456defG789HIJklMN12", "_");
assert_eq!(result, "ABC123--456DEF-G789-HI-JKL-MN12");
assert_eq!(result, "ABC123-456-DEF-G789-HI-JKL-MN12");
}

#[test]
fn it_should_convert_when_starting_with_digit() {
let result = cobol_case_with_sep("123abc456def", "-");
assert_eq!(result, "123-ABC456-DEF");

let result = cobol_case_with_sep("123ABC456DEF", "-");
assert_eq!(result, "123-ABC456-DEF");
}

#[test]
Expand Down Expand Up @@ -466,10 +469,19 @@ mod tests_of_cobol_case_with_keep {
#[test]
fn it_should_keep_digits() {
let result = cobol_case_with_keep("abc123-456defG789HIJklMN12", "_");
assert_eq!(result, "ABC123-456DEF-G789-HI-JKL-MN12");
assert_eq!(result, "ABC123-456-DEF-G789-HI-JKL-MN12");

let result = cobol_case_with_keep("abc123-456defG789HIJklMN12", "-");
assert_eq!(result, "ABC123--456DEF-G789-HI-JKL-MN12");
assert_eq!(result, "ABC123-456-DEF-G789-HI-JKL-MN12");
}

#[test]
fn it_should_convert_when_starting_with_digit() {
let result = cobol_case_with_keep("123abc456def", "-");
assert_eq!(result, "123-ABC456-DEF");

let result = cobol_case_with_keep("123ABC456DEF", "_");
assert_eq!(result, "123-ABC456-DEF");
}

#[test]
Expand Down
70 changes: 41 additions & 29 deletions src/kebab_case.rs
Expand Up @@ -23,7 +23,8 @@ pub fn kebab_case(input: &str) -> String {
FirstOfStr,
NextOfUpper,
NextOfContdUpper,
NextOfMark,
NextOfSepMark,
NextOfKeepedMark,
Others,
}
let mut flag = ChIs::FirstOfStr;
Expand Down Expand Up @@ -54,22 +55,24 @@ pub fn kebab_case(input: &str) -> String {
}
None => (), // impossible
},
ChIs::NextOfMark => result.push('-'),
ChIs::NextOfSepMark | ChIs::NextOfKeepedMark => {
result.push('-');
}
_ => (),
}
result.push(ch);
flag = ChIs::Others;
} else if ch.is_ascii_digit() {
match flag {
ChIs::NextOfMark => result.push('-'),
ChIs::NextOfSepMark => result.push('-'),
_ => (),
}
result.push(ch);
flag = ChIs::Others;
flag = ChIs::NextOfKeepedMark;
} else {
match flag {
ChIs::FirstOfStr => (),
_ => flag = ChIs::NextOfMark,
_ => flag = ChIs::NextOfSepMark,
}
}
}
Expand Down Expand Up @@ -143,15 +146,6 @@ pub fn kebab_case_with_sep(input: &str, seps: &str) -> String {
}
flag = ChIs::Others;
result.push(ch);
} else if ch.is_ascii_digit() {
match flag {
ChIs::NextOfSepMark | ChIs::NextOfKeepedMark => {
result.push('-');
}
_ => (),
}
flag = ChIs::Others;
result.push(ch);
} else {
match flag {
ChIs::NextOfSepMark => result.push('-'),
Expand Down Expand Up @@ -227,16 +221,7 @@ pub fn kebab_case_with_keep(input: &str, keeped: &str) -> String {
}
result.push(ch);
flag = ChIs::Others;
} else if ch.is_ascii_digit() {
match flag {
ChIs::NextOfSepMark | ChIs::NextOfKeepedMark => {
result.push('-');
}
_ => (),
}
result.push(ch);
flag = ChIs::Others;
} else if keeped.contains(ch) {
} else if ch.is_ascii_digit() || keeped.contains(ch) {
match flag {
ChIs::NextOfSepMark => result.push('-'),
_ => (),
Expand Down Expand Up @@ -303,7 +288,16 @@ mod tests_of_kebab_case {
#[test]
fn it_should_keep_digits() {
let result = kebab_case("abc123-456defG789HIJklMN12");
assert_eq!(result, "abc123-456def-g789-hi-jkl-mn12");
assert_eq!(result, "abc123-456-def-g789-hi-jkl-mn12");
}

#[test]
fn it_should_convert_when_starting_with_digit() {
let result = kebab_case("123abc456def");
assert_eq!(result, "123-abc456-def");

let result = kebab_case("123ABC456DEF");
assert_eq!(result, "123-abc456-def");
}

#[test]
Expand Down Expand Up @@ -383,10 +377,19 @@ mod tests_of_kebab_case_with_sep {
#[test]
fn it_should_keep_digits() {
let result = kebab_case_with_sep("abc123-456defG789HIJklMN12", "-");
assert_eq!(result, "abc123-456def-g789-hi-jkl-mn12");
assert_eq!(result, "abc123-456-def-g789-hi-jkl-mn12");

let result = kebab_case_with_sep("abc123-456defG789HIJklMN12", "_");
assert_eq!(result, "abc123--456def-g789-hi-jkl-mn12");
assert_eq!(result, "abc123-456-def-g789-hi-jkl-mn12");
}

#[test]
fn it_should_convert_when_starting_with_digit() {
let result = kebab_case_with_sep("123abc456def", "-");
assert_eq!(result, "123-abc456-def");

let result = kebab_case_with_sep("123ABC456DEF", "-");
assert_eq!(result, "123-abc456-def");
}

#[test]
Expand Down Expand Up @@ -466,10 +469,19 @@ mod tests_of_kebab_case_with_keep {
#[test]
fn it_should_keep_digits() {
let result = kebab_case_with_keep("abc123-456defG789HIJklMN12", "_");
assert_eq!(result, "abc123-456def-g789-hi-jkl-mn12");
assert_eq!(result, "abc123-456-def-g789-hi-jkl-mn12");

let result = kebab_case_with_keep("abc123-456defG789HIJklMN12", "-");
assert_eq!(result, "abc123--456def-g789-hi-jkl-mn12");
assert_eq!(result, "abc123-456-def-g789-hi-jkl-mn12");
}

#[test]
fn it_should_convert_when_starting_with_digit() {
let result = kebab_case_with_keep("123abc456def", "-");
assert_eq!(result, "123-abc456-def");

let result = kebab_case_with_keep("123ABC456DEF", "-");
assert_eq!(result, "123-abc456-def");
}

#[test]
Expand Down

0 comments on commit cb3dfc6

Please sign in to comment.