use crate::StringCharAt; pub trait StringCharCodeAt { /// `String.prototype.charCodeAt ( pos )` /// fn char_code_at(&self, index: Option) -> Option; } impl StringCharCodeAt for &str { fn char_code_at(&self, index: Option) -> Option { self.char_at(index).map(|c| c as u32) } } #[cfg(test)] mod test { #[test] fn test_evaluate_char_code_at() { use crate::StringCharCodeAt; assert_eq!("abcde".char_code_at(Some(0.0)), Some(97)); assert_eq!("abcde".char_code_at(Some(1.0)), Some(98)); assert_eq!("abcde".char_code_at(Some(2.0)), Some(99)); assert_eq!("abcde".char_code_at(Some(3.0)), Some(100)); assert_eq!("abcde".char_code_at(Some(4.0)), Some(101)); assert_eq!("abcde".char_code_at(Some(5.0)), None); assert_eq!("abcde".char_code_at(Some(-1.0)), None); assert_eq!("abcde".char_code_at(None), Some(97)); assert_eq!("abcde".char_code_at(Some(0.0)), Some(97)); } }