indefinite-0.1.8/.cargo_vcs_info.json0000644000000001360000000000100131600ustar { "git": { "sha1": "9e41fe0c8b815c7ec0b316709a1947c391c35d61" }, "path_in_vcs": "" }indefinite-0.1.8/Cargo.toml0000644000000021550000000000100111610ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "indefinite" version = "0.1.8" authors = ["Adrian Tombu "] include = [ "/src", "LICENSE.md", "README.md", ] description = "Prefix a noun with an indefinite article - a or an - based on whether it begins with a vowel" documentation = "https://docs.rs/indefinite/latest/indefinite/index.html" readme = "README.md" keywords = [ "article", "a", "an", "grammar", "noun", ] categories = ["text-processing"] license = "MIT" repository = "https://github.com/adriantombu/indefinite" [package.metadata.docs.rs] all-features = true [dependencies.regex] version = "1.9" [features] macros = [] indefinite-0.1.8/Cargo.toml.orig000064400000000000000000000011271046102023000146400ustar 00000000000000[package] name = "indefinite" version = "0.1.8" edition = "2021" authors = ["Adrian Tombu "] keywords = ["article", "a", "an", "grammar", "noun"] categories = ["text-processing"] description = "Prefix a noun with an indefinite article - a or an - based on whether it begins with a vowel" license = "MIT" repository = "https://github.com/adriantombu/indefinite" documentation = "https://docs.rs/indefinite/latest/indefinite/index.html" include = ["/src", "LICENSE.md", "README.md"] [dependencies] regex = "1.9" [features] macros = [] [package.metadata.docs.rs] all-features = true indefinite-0.1.8/LICENSE.md000064400000000000000000000021351046102023000133550ustar 00000000000000The MIT License (MIT) - Copyright (c) 2022 Adrian Tombu - Copyright (c) 2015 Andrew Nichols Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. indefinite-0.1.8/README.md000064400000000000000000000035241046102023000132330ustar 00000000000000[![crates.io](https://img.shields.io/crates/v/indefinite.svg)](https://crates.io/crates/indefinite) [![docs.rs](https://img.shields.io/docsrs/indefinite/latest.svg)](https://docs.rs/indefinite/latest/indefinite/index.html) # indefinite > This crate is a port of the [JavaScript library indefinite](https://github.com/tandrewnichols/indefinite), thanks to the original authors of this library! Prefix a noun with an indefinite article - a or an - based on whether it begins with a vowel. ## Installation `cargo add indefinite` ## Usage ```rust use indefinite::*; fn main () { assert_eq!(indefinite("honor"), "an honor"); assert_eq!(indefinite("ouija board"), "a ouija board"); assert_eq!(indefinite_capitalized("apple"), "An apple"); assert_eq!(indefinite_capitalized("banana"), "A banana"); assert_eq!(indefinite_article_only("apple"), "an"); assert_eq!(indefinite_article_only("pear"), "a"); assert_eq!(indefinite_article_only_capitalized("apple"), "An"); assert_eq!(indefinite_article_only_capitalized("pear"), "A"); } ``` ## Macros Macros are available as a feature. They are working the same as the functions, except they also accept a list of inputs. ```toml indefinite = { version = "0.1", features = [ "macros" ] } ``` ```rust use indefinite::*; fn main () { assert_eq!(indefinite!("honor"), "an honor"); assert_eq!(indefinite!("honor", "euro"), ["an honor", "a euro"]); assert_eq!(indefinite_capitalized!("apple"), "An apple"); assert_eq!(indefinite_capitalized!("banana", "pear"), ["A banana", "A pear"]); assert_eq!(indefinite_article_only!("apple"), "an"); assert_eq!(indefinite_article_only!("apple","pear"), ["an", "a"]); assert_eq!(indefinite_article_only_capitalized!("apple"), "An"); assert_eq!(indefinite_article_only_capitalized!("apple", "pear"), ["An", "A"]); } ``` indefinite-0.1.8/src/helper.rs000064400000000000000000000050241046102023000143650ustar 00000000000000use regex::Regex; pub struct Helper; impl Helper { // TODO: Explore & benchmark other solutions pub fn starts_with_vowel(word: &str) -> bool { let re = Regex::new(r"^[aeiouAEIOU]").unwrap(); re.is_match(word) } pub fn capitalize(word: &str) -> String { let mut c = word.chars(); match c.next() { None => String::new(), Some(f) => f.to_uppercase().collect::() + c.as_str(), } } /// Returns the first word of a string pub fn get_first_word(sentence: &str) -> String { let re = Regex::new(r"[\s'-]").unwrap(); let splits = re.split(sentence).take(1).collect::>(); splits.first().unwrap().to_string() } } #[cfg(test)] mod tests { use super::*; #[test] fn test_starts_with_vowel_lowercase() { assert!(Helper::starts_with_vowel("accept")); assert!(Helper::starts_with_vowel("empathetic")); assert!(Helper::starts_with_vowel("invulnerable")); assert!(Helper::starts_with_vowel("outdoor")); assert!(Helper::starts_with_vowel("utopia")); } #[test] fn test_starts_with_vowel_uppercase() { assert!(Helper::starts_with_vowel("ACCEPT")); assert!(Helper::starts_with_vowel("EMPATHETIC")); assert!(Helper::starts_with_vowel("INVULNERABLE")); assert!(Helper::starts_with_vowel("OUTDOOR")); assert!(Helper::starts_with_vowel("UTOPIA")); } #[test] fn test_starts_with_not_vowel_lowercase() { assert!(!Helper::starts_with_vowel("")); assert!(!Helper::starts_with_vowel("zombie")); assert!(!Helper::starts_with_vowel("kindness")); assert!(!Helper::starts_with_vowel("glamorous")); assert!(!Helper::starts_with_vowel("041231232")); assert!(!Helper::starts_with_vowel("!:&éàè_qsdf")); } #[test] fn test_starts_with_not_vowel_uppercase() { assert!(!Helper::starts_with_vowel("ZOMBIE")); assert!(!Helper::starts_with_vowel("KINDNESS")); assert!(!Helper::starts_with_vowel("GLAMOROUS")); } #[test] fn test_capitalize() { assert_eq!(Helper::capitalize(""), ""); assert_eq!(Helper::capitalize("accept"), "Accept"); assert_eq!(Helper::capitalize("zombie"), "Zombie"); } #[test] fn test_get_first() { assert_eq!(&Helper::get_first_word("heir's"), "heir"); assert_eq!(&Helper::get_first_word("honor-bound"), "honor"); assert_eq!(&Helper::get_first_word("ouija board"), "ouija"); } } indefinite-0.1.8/src/irregular.rs000064400000000000000000000056401046102023000151060ustar 00000000000000pub const LIST: [&str; 119] = [ // Nouns: eu like y "eunuch", "eucalyptus", "eugenics", "eulogy", "euphemism", "euphony", "euphoria", "eureka", // Adjectives: eu like y "euro", "european", "euphemistic", "euphonic", "euphoric", // Adverbs: eu like y "euphemistically", "euphonically", "euphorically", // Nouns: silent h "heir", "heiress", "herb", "homage", "honesty", "honor", "honour", "honoree", "hour", // Adjectives: silent h "honest", "honorous", // Adverbs: silent h "honestly", "hourly", // Nouns: o like w "one", "ouija", // Adjectives: o like w "once", // Nouns: u like y "ubiquity", "udometer", "ufo", "uke", "ukulele", "ululate", "unicorn", "unicycle", "uniform", "unify", "union", "unison", "unit", "unity", "universe", "university", "upas", "ural", "uranium", "urea", "ureter", "urethra", "urine", "urologist", "urology", "urus", "usage", "use", "user", "usual", "usurp", "usurper", "usury", "utensil", "uterus", "utility", "utopia", "utricle", "uvarovite", "uvea", "uvula", "utah", "utahn", // Adjectives: u like y "ubiquitous", "ugandan", "ukrainian", "unanimous", "unicameral", "unified", "unique", "unisex", "universal", "urinal", "urological", "useful", "useless", "usurious", "utilitarian", "utopic", // Adverbs: u like y "ubiquitously", "unanimously", "unicamerally", "uniquely", "universally", "urologically", "usefully", "uselessly", "usuriously", // Nouns: y like i "yttria", "yggdrasil", "ylem", "yperite", "ytterbia", "ytterbium", "yttrium", // Adjectives: y like i "ytterbous", "ytterbic", "yttric", // Single letters "f", "h", "l", "m", "n", "r", "s", "u", "x", ]; pub struct Irregular; impl Irregular { /// Checks if the word is irregular pub fn check(word: &str) -> bool { let mut word = word.to_string(); if word.len() > 1 { for end in ["es", "ed", "s"] { if word.ends_with(end) { word = word.replace(end, ""); if word.len() as isize - end.len() as isize <= 1 { return false; } } } } LIST.contains(&word.as_str()) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_check() { assert!(Irregular::check("honor")); assert!(Irregular::check("euro")); assert!(Irregular::check("ukulele")); assert!(Irregular::check("ouija")); assert!(!Irregular::check("red")); } } indefinite-0.1.8/src/lib.rs000064400000000000000000000121021046102023000136470ustar 00000000000000//! Prefix a noun with an indefinite article - a or an - based on whether it begins with a vowel. //! //! # Examples //! //! Returns the input string with the article related to the first word //! ``` //! use indefinite::indefinite; //! //! assert_eq!(indefinite("honor"), "an honor"); //! assert_eq!(indefinite("euro"), "a euro"); //! ``` //! //! Returns the input string with the article related to the first word with the first letter capitalised //! ``` //! use indefinite::indefinite_capitalized; //! //! assert_eq!(indefinite_capitalized("apple"), "An apple"); //! assert_eq!(indefinite_capitalized("banana"), "A banana"); //! ``` //! //! Returns only the article related to the first word //! ``` //! use indefinite::indefinite_article_only; //! //! assert_eq!(indefinite_article_only("apple"), "an"); //! assert_eq!(indefinite_article_only("pear"), "a"); //! ``` //! //! Returns only the article related to the first word with the first letter capitalised //! ``` //! use indefinite::indefinite_article_only_capitalized; //! //! assert_eq!(indefinite_article_only_capitalized("apple"), "An"); //! assert_eq!(indefinite_article_only_capitalized("pear"), "A"); //! ``` mod helper; mod irregular; #[cfg(feature = "macros")] #[macro_use] mod macros; mod methods; mod rules; /// Returns the input string with the article related to the first word /// /// # Examples /// /// ``` /// use indefinite::indefinite; /// /// assert_eq!(indefinite("honor"), "an honor"); /// assert_eq!(indefinite("euro"), "a euro"); /// assert_eq!(indefinite("ukulele"), "a ukulele"); /// assert_eq!(indefinite("ouija board"), "a ouija board"); /// ``` pub fn indefinite(sentence: &str) -> String { methods::indefinite(sentence) } /// Returns the input string with the article related to the first word with the first letter capitalised /// /// # Examples /// /// ``` /// use indefinite::indefinite_capitalized; /// /// assert_eq!(indefinite_capitalized("apple"), "An apple"); /// assert_eq!(indefinite_capitalized("banana"), "A banana"); /// ``` pub fn indefinite_capitalized(sentence: &str) -> String { methods::indefinite_capitalized(sentence) } /// Returns only the article related to the first word /// /// # Examples /// /// ``` /// use indefinite::indefinite_article_only; /// /// assert_eq!(indefinite_article_only("apple"), "an"); /// assert_eq!(indefinite_article_only("pear"), "a"); /// ``` pub fn indefinite_article_only(sentence: &str) -> String { methods::indefinite_article_only(sentence) } /// Returns only the article related to the first word with the first letter capitalised /// /// # Examples /// /// ``` /// use indefinite::indefinite_article_only_capitalized; /// /// assert_eq!(indefinite_article_only_capitalized("apple"), "An"); /// assert_eq!(indefinite_article_only_capitalized("pear"), "A"); /// ``` pub fn indefinite_article_only_capitalized(sentence: &str) -> String { methods::indefinite_article_only_capitalized(sentence) } #[cfg(test)] mod tests { use super::*; #[test] fn test_indefinite() { assert_eq!(indefinite("Umbrella"), "an Umbrella"); assert_eq!(indefinite("FFA prodigy"), "an FFA prodigy"); assert_eq!(indefinite("honor"), "an honor"); assert_eq!(indefinite("euro"), "a euro"); assert_eq!(indefinite("ukulele"), "a ukulele"); assert_eq!(indefinite("ouija board"), "a ouija board"); assert_eq!(indefinite("honor-bound"), "an honor-bound"); assert_eq!(indefinite("heir's"), "an heir's"); assert_eq!(indefinite("red rum"), "a red rum"); assert_eq!(indefinite("Hour"), "an Hour"); assert_eq!(indefinite("hours"), "an hours"); assert_eq!(indefinite("heiresses"), "an heiresses"); assert_eq!(indefinite("honored"), "an honored"); assert_eq!(indefinite("11000000000000"), "an 11000000000000"); assert_eq!(indefinite("110000000000000"), "a 110000000000000"); assert_eq!(indefinite("18000000"), "an 18000000"); assert_eq!(indefinite("180000000"), "a 180000000"); assert_eq!(indefinite("800000"), "an 800000"); assert_eq!(indefinite("u"), "a u"); assert_eq!(indefinite("f"), "an f"); assert_eq!(indefinite("h"), "an h"); assert_eq!(indefinite("l"), "an l"); assert_eq!(indefinite("m"), "an m"); assert_eq!(indefinite("n"), "an n"); assert_eq!(indefinite("r"), "an r"); assert_eq!(indefinite("s"), "an s"); assert_eq!(indefinite("x"), "an x"); assert_eq!(indefinite("a"), "an a"); assert_eq!(indefinite("b"), "a b"); } #[test] fn test_indefinite_capitalized() { assert_eq!(indefinite_capitalized("apple"), "An apple"); assert_eq!(indefinite_capitalized("banana"), "A banana"); } #[test] fn test_indefinite_article_only() { assert_eq!(indefinite_article_only("apple"), "an"); assert_eq!(indefinite_article_only("pear"), "a"); } #[test] fn test_indefinite_article_only_capitalized() { assert_eq!(indefinite_article_only_capitalized("apple"), "An"); assert_eq!(indefinite_article_only_capitalized("pear"), "A"); } } indefinite-0.1.8/src/macros.rs000064400000000000000000000043571046102023000144020ustar 00000000000000/// Returns the input string with the article related to the first word (also accepts a list of input string) /// /// # Examples /// /// ``` /// use indefinite::indefinite; /// /// assert_eq!(indefinite!("honor"), "an honor"); /// assert_eq!(indefinite!("honor", "euro"), ["an honor", "a euro"]); /// ``` #[macro_export] macro_rules! indefinite { ($word:literal) => { $crate::indefinite($word) }; ($($word:literal),+) => {{ [$($crate::indefinite($word),)+] }}; } /// Returns the input string with the article related to the first word with the first letter capitalised (also accepts a list of input string) /// /// # Examples /// /// ``` /// use indefinite::indefinite_capitalized; /// /// assert_eq!(indefinite_capitalized!("apple"), "An apple"); /// assert_eq!(indefinite_capitalized!("banana", "pear"), ["A banana", "A pear"]); /// ``` #[macro_export] macro_rules! indefinite_capitalized { ($word:literal) => { $crate::indefinite_capitalized($word) }; ($($word:literal),+) => {{ [$($crate::indefinite_capitalized($word),)+] }}; } /// Returns only the article related to the first word (also accepts a list of input string) /// /// # Examples /// /// ``` /// use indefinite::indefinite_article_only; /// /// assert_eq!(indefinite_article_only!("apple"), "an"); /// assert_eq!(indefinite_article_only!("apple","pear"), ["an", "a"]); /// ``` #[macro_export] macro_rules! indefinite_article_only { ($word:literal) => { $crate::indefinite_article_only($word) }; ($($word:literal),+) => {{ [$($crate::indefinite_article_only($word),)+] }}; } /// Returns only the article related to the first word with the first letter capitalised (also accepts a list of input string) /// /// # Examples /// /// ``` /// use indefinite::indefinite_article_only_capitalized; /// /// assert_eq!(indefinite_article_only_capitalized!("apple"), "An"); /// assert_eq!(indefinite_article_only_capitalized!("apple", "pear"), ["An", "A"]); /// ``` #[macro_export] macro_rules! indefinite_article_only_capitalized { ($word:literal) => { $crate::indefinite_article_only_capitalized($word) }; ($($word:literal),+) => {{ [$($crate::indefinite_article_only_capitalized($word),)+] }}; } indefinite-0.1.8/src/methods.rs000064400000000000000000000014751046102023000145570ustar 00000000000000use crate::helper::Helper; use crate::rules; pub fn indefinite(sentence: &str) -> String { format!("{} {}", apply_rules(sentence), sentence) } pub fn indefinite_capitalized(sentence: &str) -> String { Helper::capitalize(&format!("{} {}", apply_rules(sentence), sentence)) } pub fn indefinite_article_only(sentence: &str) -> String { apply_rules(sentence) } pub fn indefinite_article_only_capitalized(sentence: &str) -> String { Helper::capitalize(&apply_rules(sentence)) } fn apply_rules(sentence: &str) -> String { let word = &Helper::get_first_word(sentence); if rules::number::Number::check(word) { rules::number::Number::run(word) } else if rules::acronym::Acronym::check(word) { rules::acronym::Acronym::run(word) } else { rules::other::Other::run(word) } } indefinite-0.1.8/src/rules/acronym.rs000064400000000000000000000034551046102023000157160ustar 00000000000000use crate::helper::Helper; use regex::Regex; pub struct Acronym; impl Acronym { /// Checks if the word is an acronym pub fn check(word: &str) -> bool { word.to_uppercase().eq(word) } /// If it starts with U: "a" /// If it starts with any other vowel: "an" /// If it starts with F, H, L, M, N, R, S, or X: "an" /// If it starts with any other consonant: "a" pub fn run(word: &str) -> String { if Self::is_irregular(word) == Helper::starts_with_vowel(word) { "a".to_string() } else { "an".to_string() } } /// Checks if the acronym is irregular fn is_irregular(word: &str) -> bool { let re = Regex::new(r"^[UFHLMNRSX]+").unwrap(); re.is_match(word) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_check() { assert!(Acronym::check("NHL")); assert!(Acronym::check("SSI")); } #[test] fn test_is_not_acronym() { assert!(!Acronym::check("Umbrella")); assert!(!Acronym::check("nhl")); assert!(!Acronym::check("ssi")); } #[test] fn test_is_irregular() { assert!(Acronym::is_irregular("URSSAF")); assert!(Acronym::is_irregular("FIFA")); assert!(Acronym::is_irregular("NHL")); assert!(Acronym::is_irregular("NASA")); } #[test] fn test_is_not_irregular() { assert!(!Acronym::is_irregular("WHO")); } #[test] fn test_run() { assert_eq!(&Acronym::run("URSSAF"), "a"); assert_eq!(&Acronym::run("EULA"), "an"); assert_eq!(&Acronym::run("XLM"), "an"); assert_eq!(&Acronym::run("CMYK"), "a"); assert_eq!(&Acronym::run("IOU"), "an"); assert_eq!(&Acronym::run("UFO"), "a"); assert_eq!(&Acronym::run("CEO"), "a"); } } indefinite-0.1.8/src/rules/mod.rs000064400000000000000000000000601046102023000150120ustar 00000000000000pub mod acronym; pub mod number; pub mod other; indefinite-0.1.8/src/rules/number.rs000064400000000000000000000116161046102023000155340ustar 00000000000000use regex::Regex; pub struct Number; impl Number { /// Checks if the word is a number pub fn check(word: &str) -> bool { let re = Regex::new(r"^([\d,.]+)").unwrap(); re.is_match(word) } pub fn run(word: &str) -> String { let re = Regex::new(r"^[,.]").unwrap(); let number = re.replace_all(word, ""); if Self::starts_with_height_eleven_or_eighteen(word) && (Self::starts_with_eleven_or_eighteen(word) && (number.len() - 2) % 3 == 0 || word.starts_with('8')) { "an".to_string() } else { "a".to_string() } } /// Checks if the word starts with 8, 11 or 18 fn starts_with_height_eleven_or_eighteen(word: &str) -> bool { let re = Regex::new(r"^(8|11|18)").unwrap(); re.is_match(word) } /// Checks if the word starts with 11 or 18 fn starts_with_eleven_or_eighteen(word: &str) -> bool { let re = Regex::new(r"^(11|18)").unwrap(); re.is_match(word) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_check() { assert!(Number::check("213 horses were running in the wild")); assert!(Number::check("4232,96 euro")); assert!(Number::check("10231.27 dollars")); } #[test] fn test_not_check() { assert!(!Number::check("lorem ipsum is NOT a valid number")); } #[test] fn test_starts_with_height_eleven_or_eighteen() { assert!(Number::starts_with_height_eleven_or_eighteen("8123")); assert!(Number::starts_with_height_eleven_or_eighteen("11023")); assert!(Number::starts_with_height_eleven_or_eighteen("18")); } #[test] fn test_not_starts_with_height_eleven_or_eighteen() { assert!(!Number::starts_with_height_eleven_or_eighteen("9123")); assert!(!Number::starts_with_height_eleven_or_eighteen("1231")); assert!(!Number::starts_with_height_eleven_or_eighteen("745")); } #[test] fn test_starts_with_eleven_or_eighteen() { assert!(Number::starts_with_eleven_or_eighteen("11023")); assert!(Number::starts_with_eleven_or_eighteen("1831")); } #[test] fn test_not_starts_with_eleven_or_eighteen() { assert!(!Number::starts_with_eleven_or_eighteen("8123")); assert!(!Number::starts_with_eleven_or_eighteen("9123")); assert!(!Number::starts_with_eleven_or_eighteen("1231")); assert!(!Number::starts_with_eleven_or_eighteen("745")); } #[test] fn test_run_starts_with_11() { assert_eq!(Number::run("11"), "an"); assert_eq!(Number::run("110"), "a"); assert_eq!(Number::run("11000"), "an"); assert_eq!(Number::run("110000"), "a"); assert_eq!(Number::run("1100000"), "a"); assert_eq!(Number::run("11000000"), "an"); assert_eq!(Number::run("110000000"), "a"); assert_eq!(Number::run("1100000000"), "a"); assert_eq!(Number::run("11000000000"), "an"); assert_eq!(Number::run("110000000000"), "a"); assert_eq!(Number::run("1100000000000"), "a"); assert_eq!(Number::run("11000000000000"), "an"); assert_eq!(Number::run("110000000000000"), "a"); assert_eq!(Number::run("1100000000000000"), "a"); assert_eq!(Number::run("11000000000000000"), "an"); } #[test] fn test_run_starts_with_18() { assert_eq!(Number::run("18"), "an"); assert_eq!(Number::run("180"), "a"); assert_eq!(Number::run("18000"), "an"); assert_eq!(Number::run("180000"), "a"); assert_eq!(Number::run("1800000"), "a"); assert_eq!(Number::run("18000000"), "an"); assert_eq!(Number::run("180000000"), "a"); assert_eq!(Number::run("1800000000"), "a"); assert_eq!(Number::run("18000000000"), "an"); assert_eq!(Number::run("180000000000"), "a"); assert_eq!(Number::run("1800000000000"), "a"); assert_eq!(Number::run("18000000000000"), "an"); assert_eq!(Number::run("180000000000000"), "a"); assert_eq!(Number::run("1800000000000000"), "a"); assert_eq!(Number::run("18000000000000000"), "an"); } #[test] fn test_run_starts_with_8() { assert_eq!(Number::run("8"), "an"); assert_eq!(Number::run("80"), "an"); assert_eq!(Number::run("8000"), "an"); assert_eq!(Number::run("80000"), "an"); assert_eq!(Number::run("800000"), "an"); assert_eq!(Number::run("8000000"), "an"); assert_eq!(Number::run("80000000"), "an"); assert_eq!(Number::run("800000000"), "an"); assert_eq!(Number::run("8000000000"), "an"); } #[test] fn test_run_starts_with_1100_1800() { assert_eq!(Number::run("1100"), "a"); assert_eq!(Number::run("1800"), "a"); } #[test] fn test_run_starts_with_other_numbers() { assert_eq!(Number::run("17"), "a"); assert_eq!(Number::run("14"), "a"); } } indefinite-0.1.8/src/rules/other.rs000064400000000000000000000017541046102023000153670ustar 00000000000000use crate::helper::Helper; use crate::irregular::Irregular; pub struct Other; impl Other { pub fn run(word: &str) -> String { let word = &word.to_lowercase(); let is_irregular = Irregular::check(word); let starts_with_a_vowel = Helper::starts_with_vowel(word); // If it starts with a vowel and isn't irregular: "an" // If it starts with a vowel and IS irregular: "a" // If it starts with a consonant and isn't irregular: "a" // If it starts with a consonant and IS irregular: "an" if (starts_with_a_vowel || is_irregular) && !(starts_with_a_vowel && is_irregular) { "an".to_string() } else { "a".to_string() } } } #[cfg(test)] mod tests { use super::*; #[test] fn test_check() { assert_eq!(Other::run("apple"), "an"); assert_eq!(Other::run("banana"), "a"); assert_eq!(Other::run("Apple"), "an"); assert_eq!(Other::run("Banana"), "a"); } }