├── .formatter.exs ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── lib ├── inflex.ex └── inflex │ ├── camelize.ex │ ├── ordinalize.ex │ ├── parameterize.ex │ ├── pluralize.ex │ └── underscore.ex ├── mix.exs ├── mix.lock └── test ├── inflex_test.exs └── test_helper.exs /.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build/ 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover/ 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps/ 9 | 10 | # Where third-party dependencies like ExDoc output generated docs. 11 | /doc/ 12 | 13 | # Ignore .fetch files in case you like to edit your project deps locally. 14 | /.fetch 15 | 16 | # If the VM crashes, it generates a dump, let's ignore it too. 17 | erl_crash.dump 18 | 19 | # Also ignore archive artifacts (built via "mix archive.build"). 20 | *.ez 21 | 22 | # Ignore package tarball (built via "mix hex.build"). 23 | inflex-*.tar 24 | 25 | # Temporary files for e.g. tests 26 | /tmp 27 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: elixir 3 | elixir: 4 | - 1.10.4 5 | otp_release: 6 | - 22.3.4.10 7 | 8 | matrix: 9 | include: 10 | - elixir: 1.6.6 11 | otp_release: 20.3.8.22 12 | - elixir: 1.7.4 13 | otp_release: 20.3.8.22 14 | - elixir: 1.8.2 15 | otp_release: 20.3.8.22 16 | - elixir: 1.9.4 17 | otp_release: 20.3.8.22 18 | - elixir: 1.10.4 19 | otp_release: 21.3.8.1 20 | - elixir: 1.10.4 21 | otp_release: 23.0.3 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Inflex 2 | 3 | 4 | 5 | [![Build Status](https://travis-ci.org/nurugger07/inflex.svg?branch=master)](https://travis-ci.org/nurugger07/inflex) 6 | [![Module Version](https://img.shields.io/hexpm/v/inflex.svg)](https://hex.pm/packages/inflex) 7 | [![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/inflex/) 8 | [![Total Download](https://img.shields.io/hexpm/dt/inflex.svg)](https://hex.pm/packages/inflex) 9 | [![License](https://img.shields.io/hexpm/l/inflex.svg)](https://github.com/nurugger07/inflex/blob/master/LICENSE) 10 | [![Last Updated](https://img.shields.io/github/last-commit/nurugger07/inflex.svg)](https://github.com/nurugger07/inflex/commits/master) 11 | 12 | 13 | An Elixir library for handling word inflections. 14 | 15 | ## Getting Started 16 | 17 | You can add Inflex as a dependency in your `mix.exs` file. Since it only requires Elixir and Erlang there are no other dependencies. 18 | 19 | ```elixir 20 | def deps do 21 | [ 22 | {:inflex, "~> 2.0.0"} 23 | ] 24 | end 25 | ``` 26 | 27 | If you are not using [hex](http://hex.pm) you can add the dependency using the GitHub repository. 28 | 29 | ```elixir 30 | def deps do 31 | [ 32 | {:inflex, github: "nurugger07/inflex"} 33 | ] 34 | end 35 | ``` 36 | 37 | Then run `mix deps.get` in the shell to fetch and compile the dependencies. 38 | 39 | ### Requirements 40 | 41 | Although Inflex supports Elixir 1.6, which is [compatible](https://hexdocs.pm/elixir/compatibility-and-deprecations.html#compatibility-between-elixir-and-erlang-otp) with Erlang/OTP 19–20, [Inflex requires Erlang/OTP 20+](https://github.com/nurugger07/inflex/blob/master/lib/inflex/parameterize.ex#L21). 42 | 43 | 44 | To incorporate Inflex in your modules, use `import`. 45 | 46 | ```elixir 47 | defmodule Blog do 48 | import Inflex 49 | 50 | def greeting(count), do: "You are the #{ordinalize(count)} visitor!" 51 | 52 | end 53 | ``` 54 | 55 | ## Examples 56 | 57 | ### Singularize & Pluralize 58 | 59 | Here are some basic examples from `iex`: 60 | 61 | ```elixir 62 | 63 | iex(1)> Inflex.singularize("dogs") 64 | "dog" 65 | 66 | iex(2)> Inflex.pluralize("dog") 67 | "dogs" 68 | 69 | iex(3)> Inflex.singularize("people") 70 | "person" 71 | 72 | iex(4)> Inflex.pluralize("person") 73 | "people" 74 | ``` 75 | 76 | Some other special cases are handled for nouns ending in -o and -y 77 | 78 | ```elixir 79 | iex(1)> Inflex.pluralize("piano") 80 | "pianos" 81 | 82 | iex(2)> Inflex.pluralize("hero") 83 | "heroes" 84 | 85 | iex(3)> Inflex.pluralize("butterfly") 86 | "butterflies" 87 | 88 | iex(4)> Inflex.pluralize("monkey") 89 | "monkeys" 90 | ``` 91 | 92 | ### Inflect 93 | 94 | ```elixir 95 | iex(1)> Inflex.inflect("child", 1) 96 | "child" 97 | 98 | iex(2)> Inflex.inflect("child", 2) 99 | "children" 100 | ``` 101 | 102 | ### Camelize & Pascalize 103 | 104 | Inflex also camelizes or pascalizes strings and atoms. 105 | 106 | ```elixir 107 | iex(1)> Inflex.camelize(:upper_camel_case) 108 | "UpperCamelCase" 109 | 110 | iex(2)> Inflex.camelize("pascal-case", :lower) 111 | "pascalCase" 112 | ``` 113 | 114 | ### Parameterize 115 | 116 | Strings can be parameterized easily. 117 | 118 | ```elixir 119 | iex(1)> Inflex.parameterize("String for parameter") 120 | "string-for-parameter" 121 | 122 | iex(2)> Inflex.parameterize("String with underscore", "_") 123 | "string_with_underscore" 124 | ``` 125 | 126 | ### Underscore 127 | 128 | Makes an underscored, lowercase form from a string or atom. 129 | 130 | ```elixir 131 | iex(1)> Inflex.underscore("UpperCamelCase") 132 | "upper_camel_case" 133 | 134 | iex(2)> Inflex.underscore("pascalCase") 135 | "pascal_case" 136 | 137 | iex(3)> Inflex.underscore(UpperCamelCase) 138 | "upper_camel_case" 139 | 140 | iex(4)> Inflex.underscore(:pascalCase) 141 | "pascal_case" 142 | ``` 143 | 144 | ## Contributing 145 | 146 | All pull requests will be reviewed for inclusion but must include tests. 147 | 148 | ## License 149 | 150 | Copyright (c) 2013 Johnny Winn 151 | 152 | Licensed under the Apache License, Version 2.0 (the "License"); 153 | you may not use this file except in compliance with the License. 154 | You may obtain a copy of the License at [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) 155 | 156 | Unless required by applicable law or agreed to in writing, software 157 | distributed under the License is distributed on an "AS IS" BASIS, 158 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 159 | See the License for the specific language governing permissions and 160 | limitations under the License. 161 | -------------------------------------------------------------------------------- /lib/inflex.ex: -------------------------------------------------------------------------------- 1 | defmodule Inflex do 2 | @external_resource "README.md" 3 | @moduledoc "README.md" 4 | |> File.read!() 5 | |> String.split("") 6 | |> Enum.fetch!(1) 7 | 8 | @doc """ 9 | Camelizes or pascalizes strings and atoms to upper-case CamelCase. 10 | 11 | ## Examples 12 | 13 | iex> Inflex.camelize(:upper_camel_case) 14 | "UpperCamelCase" 15 | 16 | """ 17 | defdelegate camelize(word), to: Inflex.Camelize 18 | 19 | @doc """ 20 | Camelizes or pascalizes strings and atoms. 21 | 22 | ## Options 23 | 24 | * `:lower` - Lower-cases the first letter. 25 | 26 | ## Examples 27 | 28 | iex> Inflex.camelize("pascal-case", :lower) 29 | "pascalCase" 30 | 31 | """ 32 | defdelegate camelize(word, option), to: Inflex.Camelize 33 | 34 | @doc """ 35 | Singularize a word. 36 | 37 | ## Examples 38 | 39 | iex> Inflex.singularize("dogs") 40 | "dog" 41 | 42 | iex> Inflex.singularize("people") 43 | "person" 44 | 45 | """ 46 | defdelegate singularize(word), to: Inflex.Pluralize 47 | 48 | @doc """ 49 | Pluralize a word. 50 | 51 | ## Examples 52 | 53 | iex> Inflex.pluralize("dog") 54 | "dogs" 55 | 56 | iex> Inflex.pluralize("person") 57 | "people" 58 | 59 | """ 60 | defdelegate pluralize(word), to: Inflex.Pluralize 61 | 62 | @doc """ 63 | Inflect on the plurality of a word given some count. 64 | 65 | ## Examples 66 | 67 | iex> Inflex.inflect("child", 1) 68 | "child" 69 | 70 | iex> Inflex.inflect("child", 2) 71 | "children" 72 | 73 | """ 74 | defdelegate inflect(word, count), to: Inflex.Pluralize 75 | 76 | @doc """ 77 | Parameterize a string using a hyphen (`-`) separator. If you want to return 78 | as only ASCII characters, use `parameterize_to_ascii/2` 79 | 80 | ## Examples 81 | 82 | iex> Inflex.parameterize("String for parameter") 83 | "string-for-parameter" 84 | 85 | """ 86 | defdelegate parameterize(word), to: Inflex.Parameterize 87 | 88 | @doc """ 89 | Parameterize a string given some separator. If you want to return 90 | as only ASCII characters, use `parameterize_to_ascii/2` 91 | 92 | The `option` argument is a string representing the character that 93 | will be used as the separator. 94 | 95 | ## Examples 96 | 97 | iex> Inflex.parameterize("String with underscore", "_") 98 | "string_with_underscore" 99 | 100 | """ 101 | defdelegate parameterize(word, option), to: Inflex.Parameterize 102 | 103 | @doc """ 104 | Parameterize a string using a hyphen (`-`) separator, returning 105 | only ASCII characters. 106 | 107 | ## Examples 108 | 109 | iex> Inflex.parameterize_to_ascii("String for parameter 😎") 110 | "string-for-parameter-" 111 | 112 | """ 113 | defdelegate parameterize_to_ascii(word), to: Inflex.Parameterize 114 | 115 | @doc """ 116 | Parameterize a string given some separator, returning only ASCII 117 | characters. 118 | 119 | The `option` argument is a string representing the character that 120 | will be used as the separator. 121 | 122 | ## Examples 123 | 124 | iex> Inflex.parameterize_to_ascii("String with underscore 😎", "_") 125 | "string_with_underscore_" 126 | 127 | """ 128 | defdelegate parameterize_to_ascii(word, option), to: Inflex.Parameterize 129 | 130 | @doc """ 131 | Underscore and lowercase a string. 132 | 133 | ## Examples 134 | 135 | iex> Inflex.underscore("UpperCamelCase") 136 | "upper_camel_case" 137 | 138 | iex> Inflex.underscore(:pascalCase) 139 | "pascal_case" 140 | 141 | """ 142 | defdelegate underscore(word), to: Inflex.Underscore 143 | 144 | @doc """ 145 | Converts an integer to a ordinal value. 146 | 147 | ## Examples 148 | 149 | iex> Inflex.ordinalize(1) 150 | "1st" 151 | 152 | iex> Inflex.ordinalize(11) 153 | "11th" 154 | 155 | """ 156 | defdelegate ordinalize(number), to: Inflex.Ordinalize 157 | end 158 | -------------------------------------------------------------------------------- /lib/inflex/camelize.ex: -------------------------------------------------------------------------------- 1 | defmodule Inflex.Camelize do 2 | @moduledoc false 3 | 4 | @camelize_regex ~r/(?:^|[-_])|(?=[A-Z][a-z])/ 5 | 6 | def camelize(word, option \\ :upper) do 7 | case Regex.split(@camelize_regex, to_string(word)) do 8 | words -> 9 | words 10 | |> Enum.filter(&(&1 != "")) 11 | |> camelize_list(option) 12 | |> Enum.join() 13 | end 14 | end 15 | 16 | defp camelize_list([], _), do: [] 17 | 18 | defp camelize_list([h | tail], :lower) do 19 | [lowercase(h)] ++ camelize_list(tail, :upper) 20 | end 21 | 22 | defp camelize_list([h | tail], :upper) do 23 | [capitalize(h)] ++ camelize_list(tail, :upper) 24 | end 25 | 26 | def capitalize(word), do: String.capitalize(word) 27 | def lowercase(word), do: String.downcase(word) 28 | end 29 | -------------------------------------------------------------------------------- /lib/inflex/ordinalize.ex: -------------------------------------------------------------------------------- 1 | defmodule Inflex.Ordinalize do 2 | @moduledoc false 3 | 4 | def ordinalize(number) when is_number(number) do 5 | abs_number = abs(number) 6 | 7 | cond do 8 | rem(abs_number, 100) in 11..13 -> 9 | Integer.to_string(number) <> "th" 10 | 11 | rem(abs_number, 10) == 1 -> 12 | Integer.to_string(number) <> "st" 13 | 14 | rem(abs_number, 10) == 2 -> 15 | Integer.to_string(number) <> "nd" 16 | 17 | rem(abs_number, 10) == 3 -> 18 | Integer.to_string(number) <> "rd" 19 | 20 | true -> 21 | Integer.to_string(number) <> "th" 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/inflex/parameterize.ex: -------------------------------------------------------------------------------- 1 | defmodule Inflex.Parameterize do 2 | @moduledoc false 3 | 4 | def parameterize(string, option \\ "-") do 5 | string 6 | |> clean_split() 7 | |> Stream.map(&String.trim/1) 8 | |> Enum.join(option) 9 | end 10 | 11 | def parameterize_to_ascii(string, option \\ "-") do 12 | string 13 | |> clean_split() 14 | |> Stream.map(&trim_replace/1) 15 | |> Enum.join(option) 16 | end 17 | 18 | defp clean_split(string) do 19 | string 20 | |> String.downcase() 21 | |> :unicode.characters_to_nfd_binary() 22 | |> String.split(~r/\s|\%20/) 23 | end 24 | 25 | defp trim_replace(string) do 26 | string 27 | |> String.trim() 28 | |> String.replace(~r/[^A-z\s]/u, "") 29 | end 30 | end 31 | -------------------------------------------------------------------------------- /lib/inflex/pluralize.ex: -------------------------------------------------------------------------------- 1 | defmodule Inflex.Pluralize do 2 | @moduledoc false 3 | @default true 4 | 5 | @uncountable [ 6 | "aircraft", 7 | "bellows", 8 | "bison", 9 | "deer", 10 | "equipment", 11 | "fish", 12 | "hovercraft", 13 | "information", 14 | "jeans", 15 | "means", 16 | "measles", 17 | "money", 18 | "moose", 19 | "news", 20 | "pants", 21 | "police", 22 | "rice", 23 | "series", 24 | "sheep", 25 | "spacecraft", 26 | "species", 27 | "swine", 28 | "tights", 29 | "tongs", 30 | "trousers" 31 | ] 32 | 33 | @irregular [ 34 | {~r/(alumn|cact|fung|radi|stimul|syllab)i/i, "\\1us"}, 35 | {~r/(alg|antenn|amoeb|larv|vertebr)ae/i, "\\1a"}, 36 | {~r/^(gen)era$/i, "\\1us"}, 37 | {~r/(pe)ople/i, "\\1rson"}, 38 | {~r/^(zombie)s$/i, "\\1"}, 39 | {~r/(g)eese/i, "\\1oose"}, 40 | {~r/(criteri)a/i, "\\1on"}, 41 | {~r/^(m)en$/i, "\\1an"}, 42 | {~r/^(echo)es/i, "\\1"}, 43 | {~r/^(hero)es/i, "\\1"}, 44 | {~r/^(potato)es/i, "\\1"}, 45 | {~r/^(tomato)es/i, "\\1"}, 46 | {~r/^(t)eeth/i, "\\1ooth"}, 47 | {~r/^(l)ice$/i, "\\1ouse"}, 48 | {~r/^(addend|bacteri|curricul|dat|memorand|quant)a$/i, "\\1um"}, 49 | {~r/^(di)ce/i, "\\1e"}, 50 | {~r/^(f)eet/i, "\\1oot"}, 51 | {~r/^(phenomen)a/i, "\\1on"} 52 | ] 53 | 54 | @plural_irregular [ 55 | {~r/(alumn|cact|fung|radi|stimul|syllab)us/i, "\\1i"}, 56 | {~r/(alg|antenn|amoeb|larv|vertebr)a/i, "\\1ae"}, 57 | {~r/^(gen)us$/i, "\\1era"}, 58 | {~r/(pe)rson$/i, "\\1ople"}, 59 | {~r/^(zombie)s$/i, "\\1"}, 60 | {~r/(g)oose$/i, "\\1eese"}, 61 | {~r/(criteri)on/i, "\\1a"}, 62 | {~r/^(men)$/i, "\\1"}, 63 | {~r/^(women)/i, "\\1"}, 64 | {~r/^(echo)$/i, "\\1es"}, 65 | {~r/^(hero)$/i, "\\1es"}, 66 | {~r/^(potato)/i, "\\1es"}, 67 | {~r/^(tomato)/i, "\\1es"}, 68 | {~r/^(t)ooth$/i, "\\1eeth"}, 69 | {~r/^(l)ouse$/i, "\\1ice"}, 70 | {~r/^(addend|bacteri|curricul|dat|memorand|quant)um$/i, "\\1a"}, 71 | {~r/^(di)e$/i, "\\1ce"}, 72 | {~r/^(f)oot$/i, "\\1eet"}, 73 | {~r/^(phenomen)on/i, "\\1a"} 74 | ] 75 | 76 | @singular @irregular ++ 77 | [ 78 | {~r/(child)ren/i, "\\1"}, 79 | {~r/(wo|sea)men$/i, "\\1man"}, 80 | {~r/^(m|l)ice$/i, "\\1ouse"}, 81 | {~r/(bus|canvas|status|alias)(es)?$/i, "\\1"}, 82 | {~r/(ss)$/i, "\\1"}, 83 | {~r/(database)s$/i, "\\1"}, 84 | {~r/([ti])a$/i, "\\1um"}, 85 | {~r/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, 86 | "\\1sis"}, 87 | {~r/(analy)(sis|ses)$/i, "\\1sis"}, 88 | {~r/(octop|vir)i$/i, "\\1us"}, 89 | {~r/(hive)s$/i, "\\1"}, 90 | {~r/(tive)s$/i, "\\1"}, 91 | {~r/(er)ves$/i, "\\1ve"}, 92 | {~r/([lora])ves$/i, "\\1f"}, 93 | {~r/([^f])ves$/i, "\\1fe"}, 94 | {~r/([^aeiouy]|qu)ies$/i, "\\1y"}, 95 | {~r/(m)ovies$/i, "\\1ovie"}, 96 | {~r/(x|ch|ss|sh)es$/i, "\\1"}, 97 | {~r/(shoe)s$/i, "\\1"}, 98 | {~r/(o)es$/i, "\\1"}, 99 | {~r/s$/i, ""} 100 | ] 101 | 102 | @plural @plural_irregular ++ 103 | [ 104 | {~r/(child)$/i, "\\1ren"}, 105 | {~r/(m)an$/i, "\\1en"}, 106 | {~r/(m|l)ouse/i, "\\1ice"}, 107 | {~r/(database)s$/i, "\\1"}, 108 | {~r/(quiz)$/i, "\\1zes"}, 109 | {~r/^(ox)$/i, "\\1en"}, 110 | {~r/(matr|vert|ind)ix|ex$/i, "\\1ices"}, 111 | {~r/(x|ch|ss|sh)$/i, "\\1es"}, 112 | {~r/([^aeiouy]|qu)y$/i, "\\1ies"}, 113 | {~r/(hive)$/i, "\\1s"}, 114 | {~r/(sc[au]rf)$/i, "\\1s"}, 115 | {~r/(?:([^f])fe|((hoo)|([lra]))f)$/i, "\\2\\1ves"}, 116 | {~r/sis$/i, "ses"}, 117 | {~r/([ti])um$/i, "\\1a"}, 118 | {~r/(buffal|tomat)o$/i, "\\1oes"}, 119 | {~r/(octop|vir)us$/i, "\\1i"}, 120 | {~r/(bus|alias|status|canvas)$/i, "\\1es"}, 121 | {~r/(ax|test)is$/i, "\\1es"}, 122 | {~r/s$/i, "s"}, 123 | {~r/data$/i, "data"}, 124 | {~r/$/i, "s"} 125 | ] 126 | 127 | @singular @irregular ++ 128 | [ 129 | {~r/(child)ren/i, "\\1"}, 130 | {~r/(wo|sea)men$/i, "\\1man"}, 131 | {~r/^(m|l)ice$/i, "\\1ouse"}, 132 | {~r/(bus|canvas|status|alias)(es)?$/i, "\\1"}, 133 | {~r/(ss)$/i, "\\1"}, 134 | {~r/(database)s$/i, "\\1"}, 135 | {~r/([ti])a$/i, "\\1um"}, 136 | {~r/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, 137 | "\\1sis"}, 138 | {~r/(analy)(sis|ses)$/i, "\\1sis"}, 139 | {~r/(octop|vir)i$/i, "\\1us"}, 140 | {~r/(hive)s$/i, "\\1"}, 141 | {~r/(tive)s$/i, "\\1"}, 142 | {~r/(er)ves$/i, "\\1ve"}, 143 | {~r/([lora])ves$/i, "\\1f"}, 144 | {~r/([^f])ves$/i, "\\1fe"}, 145 | {~r/([^aeiouy]|qu)ies$/i, "\\1y"}, 146 | {~r/(m)ovies$/i, "\\1ovie"}, 147 | {~r/(x|ch|ss|sh)es$/i, "\\1"}, 148 | {~r/(shoe)s$/i, "\\1"}, 149 | {~r/(o)es$/i, "\\1"}, 150 | {~r/s$/i, ""} 151 | ] 152 | 153 | @plural @plural_irregular ++ 154 | [ 155 | {~r/(child)$/i, "\\1ren"}, 156 | {~r/(m)an$/i, "\\1en"}, 157 | {~r/(m|l)ouse/i, "\\1ice"}, 158 | {~r/(database)s$/i, "\\1"}, 159 | {~r/(quiz)$/i, "\\1zes"}, 160 | {~r/^(ox)$/i, "\\1en"}, 161 | {~r/(matr|vert|ind)ix|ex$/i, "\\1ices"}, 162 | {~r/(x|ch|ss|sh)$/i, "\\1es"}, 163 | {~r/([^aeiouy]|qu)y$/i, "\\1ies"}, 164 | {~r/(hive)$/i, "\\1s"}, 165 | {~r/(sc[au]rf)$/i, "\\1s"}, 166 | {~r/(?:([^f])fe|((hoo)|([lra]))f)$/i, "\\2\\1ves"}, 167 | {~r/sis$/i, "ses"}, 168 | {~r/([ti])um$/i, "\\1a"}, 169 | {~r/(buffal|tomat)o$/i, "\\1oes"}, 170 | {~r/(octop|vir)us$/i, "\\1i"}, 171 | {~r/(bus|alias|status|canvas)$/i, "\\1es"}, 172 | {~r/(ax|test)is$/i, "\\1es"}, 173 | {~r/s$/i, "s"}, 174 | {~r/data$/i, "data"}, 175 | {~r/$/i, "s"} 176 | ] 177 | 178 | def singularize(word) when is_atom(word) do 179 | find_match(@singular, to_string(word)) 180 | end 181 | 182 | def singularize(word), do: find_match(@singular, word) 183 | 184 | def pluralize(word) when is_atom(word) do 185 | find_match(@plural, to_string(word)) 186 | end 187 | 188 | def pluralize(word), do: find_match(@plural, word) 189 | 190 | def inflect(word, n) when n == 1, do: singularize(word) 191 | def inflect(word, n) when is_number(n), do: pluralize(word) 192 | 193 | defp find_match(set, word) do 194 | cond do 195 | uncountable?(word) -> word 196 | @default -> replace_match(set, word) 197 | end 198 | end 199 | 200 | defp replace_match(set, word) do 201 | find_in_set(set, word) |> replace(word) 202 | end 203 | 204 | defp find_in_set(set, word) do 205 | Enum.find(set, fn {reg, _} -> Regex.match?(reg, word) end) 206 | end 207 | 208 | defp replace({regex, replacement}, word) do 209 | Regex.replace(regex, word, replacement) 210 | end 211 | 212 | defp replace(_, word), do: word 213 | 214 | defp uncountable?(word), do: Enum.member?(@uncountable, word) 215 | end 216 | -------------------------------------------------------------------------------- /lib/inflex/underscore.ex: -------------------------------------------------------------------------------- 1 | defmodule Inflex.Underscore do 2 | @moduledoc false 3 | 4 | def underscore(atom) when is_atom(atom) do 5 | case Atom.to_string(atom) do 6 | "Elixir." <> rest -> underscore(rest) 7 | word -> underscore(word) 8 | end 9 | end 10 | 11 | def underscore(word) when is_binary(word) do 12 | word 13 | |> String.replace(~r/([A-Z]+)([A-Z][a-z])/, "\\1_\\2") 14 | |> String.replace(~r/([a-z\d])([A-Z])/, "\\1_\\2") 15 | |> String.replace(~r/-/, "_") 16 | |> String.downcase() 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | Code.ensure_loaded?(Hex) and Hex.start() 2 | 3 | defmodule Inflex.Mixfile do 4 | use Mix.Project 5 | 6 | @source_url "https://github.com/nurugger07/inflex" 7 | 8 | def project do 9 | [ 10 | app: :inflex, 11 | version: "2.0.0", 12 | elixir: ">= 1.0.0", 13 | deps: deps(), 14 | package: [ 15 | files: ["lib", "mix.exs", "README*", "LICENSE*"], 16 | contributors: ["Johnny Winn"], 17 | licenses: ["Apache-2.0"], 18 | links: %{"GitHub" => @source_url}, 19 | maintainers: ["Johnny Winn"] 20 | ], 21 | name: "Inflex", 22 | docs: [ 23 | extras: ["README.md"], 24 | main: "Inflex", 25 | source_url: @source_url, 26 | api_references: false, 27 | extra_section: [] 28 | ], 29 | description: """ 30 | An Elixir library for handling word inflections. 31 | """ 32 | ] 33 | end 34 | 35 | def application do 36 | [] 37 | end 38 | 39 | def deps do 40 | [{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}] 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "earmark": {:hex, :earmark, "1.4.5", "62ffd3bd7722fb7a7b1ecd2419ea0b458c356e7168c1f5d65caf09b4fbdd13c8", [:mix], [], "hexpm", "b7d0e6263d83dc27141a523467799a685965bf8b13b6743413f19a7079843f4f"}, 3 | "earmark_parser": {:hex, :earmark_parser, "1.4.12", "b245e875ec0a311a342320da0551da407d9d2b65d98f7a9597ae078615af3449", [:mix], [], "hexpm", "711e2cc4d64abb7d566d43f54b78f7dc129308a63bc103fbd88550d2174b3160"}, 4 | "ex_doc": {:hex, :ex_doc, "0.23.0", "a069bc9b0bf8efe323ecde8c0d62afc13d308b1fa3d228b65bca5cf8703a529d", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f5e2c4702468b2fd11b10d39416ddadd2fcdd173ba2a0285ebd92c39827a5a16"}, 5 | "makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"}, 6 | "makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"}, 7 | "nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"}, 8 | } 9 | -------------------------------------------------------------------------------- /test/inflex_test.exs: -------------------------------------------------------------------------------- 1 | defmodule InflexTest do 2 | use ExUnit.Case 3 | 4 | import Inflex 5 | 6 | test :singularize do 7 | assert "addendum" == singularize("addenda") 8 | assert "alga" == singularize("algae") 9 | assert "alumnus" == singularize("alumni") 10 | assert "amoeba" == singularize("amoebae") 11 | assert "antenna" == singularize("antennae") 12 | assert "bacterium" == singularize("bacteria") 13 | assert "cactus" == singularize("cacti") 14 | assert "curriculum" == singularize("curricula") 15 | assert "datum" == singularize("data") 16 | assert "fungus" == singularize("fungi") 17 | assert "genus" == singularize("genera") 18 | assert "larva" == singularize("larvae") 19 | assert "memorandum" == singularize("memoranda") 20 | assert "menu" == singularize("menus") 21 | assert "stimulus" == singularize("stimuli") 22 | assert "syllabus" == singularize("syllabi") 23 | assert "vertebra" == singularize("vertebrae") 24 | 25 | assert "user" == singularize("users") 26 | assert "sheep" == singularize("sheep") 27 | assert "mouse" == singularize("mice") 28 | assert "child" == singularize("children") 29 | 30 | assert "bus" == singularize("buses") 31 | assert "database" == singularize("databases") 32 | assert "shoe" == singularize("shoes") 33 | assert "zombie" == singularize("zombies") 34 | assert "virus" == singularize("viri") 35 | assert "goose" == singularize("geese") 36 | assert "criterion" == singularize("criteria") 37 | assert "radius" == singularize("radii") 38 | assert "class" == singularize("classes") 39 | assert "woman" == singularize("women") 40 | assert "seaman" == singularize("seamen") 41 | assert "man" == singularize("men") 42 | assert "abdomen" == singularize("abdomens") 43 | assert "abdomen" == singularize("abdomen") 44 | assert "specimen" == singularize("specimens") 45 | assert "specimen" == singularize("specimen") 46 | assert "manager" == singularize("manager") 47 | assert "manager" == singularize("managers") 48 | assert "Person" == singularize("People") 49 | assert "louse" == singularize("lice") 50 | assert "quantum" == singularize("quanta") 51 | assert "datum" == singularize("data") 52 | assert "foot" == singularize("feet") 53 | assert "die" == singularize("dice") 54 | assert "phenomenon" == singularize("phenomena") 55 | assert "license" == singularize("licenses") 56 | 57 | assert "bus" == singularize(:buses) 58 | assert "reserve" == singularize("reserves") 59 | assert "nerve" == singularize("nerves") 60 | assert "representative" == singularize("representatives") 61 | end 62 | 63 | test :pluralize do 64 | assert "addenda" == pluralize("addendum") 65 | assert "algae" == pluralize("alga") 66 | assert "alumni" == pluralize("alumnus") 67 | assert "amoebae" == pluralize("amoeba") 68 | assert "antennae" == pluralize("antenna") 69 | assert "bacteria" == pluralize("bacterium") 70 | assert "cacti" == pluralize("cactus") 71 | assert "curricula" == pluralize("curriculum") 72 | assert "data" == pluralize("datum") 73 | assert "fungi" == pluralize("fungus") 74 | assert "genera" == pluralize("genus") 75 | assert "larvae" == pluralize("larva") 76 | assert "memoranda" == pluralize("memorandum") 77 | assert "menus" == pluralize("menu") 78 | assert "stimuli" == pluralize("stimulus") 79 | assert "syllabi" == pluralize("syllabus") 80 | assert "vertebrae" == pluralize("vertebra") 81 | 82 | assert "users" == pluralize("user") 83 | assert "sheep" == pluralize("sheep") 84 | assert "mice" == pluralize("mouse") 85 | assert "children" == pluralize("child") 86 | 87 | assert "buses" == pluralize("bus") 88 | assert "databases" == pluralize("database") 89 | assert "shoes" == pluralize("shoe") 90 | assert "viri" == pluralize("virus") 91 | assert "geese" == pluralize("goose") 92 | assert "criteria" == pluralize("criterion") 93 | assert "radii" == pluralize("radius") 94 | assert "classes" == pluralize("class") 95 | assert "women" == pluralize("woman") 96 | assert "seamen" == pluralize("seaman") 97 | assert "men" == pluralize("man") 98 | assert "men" == pluralize("men") 99 | assert "women" == pluralize("women") 100 | assert "abdomens" == pluralize("abdomen") 101 | assert "specimens" == pluralize("specimen") 102 | assert "managers" == pluralize("manager") 103 | assert "lice" == pluralize("louse") 104 | assert "quanta" == pluralize("quantum") 105 | assert "data" == pluralize("datum") 106 | assert "data" == pluralize("data") 107 | assert "feet" == pluralize("foot") 108 | assert "dice" == pluralize("die") 109 | assert "phenomena" == pluralize("phenomenon") 110 | 111 | assert "buses" == pluralize(:bus) 112 | assert "reserves" == pluralize("reserve") 113 | assert "nerves" == pluralize("nerve") 114 | end 115 | 116 | test :special_case_nouns_ending_in_f_or_fe do 117 | assert "life" == singularize("lives") 118 | assert "knife" == singularize("knives") 119 | assert "hoof" == singularize("hooves") 120 | assert "roof" == singularize("roofs") 121 | assert "loof" == singularize("loofs") 122 | assert "wolf" == singularize("wolves") 123 | assert "calf" == singularize("calves") 124 | assert "scarf" == singularize("scarfs") 125 | assert "scurf" == singularize("scurfs") 126 | assert "wharf" == singularize("wharves") 127 | assert "loaf" == singularize("loaves") 128 | assert "leaf" == singularize("leaves") 129 | 130 | assert "lives" == pluralize("life") 131 | assert "knives" == pluralize("knife") 132 | assert "hooves" == pluralize("hoof") 133 | assert "roofs" == pluralize("roof") 134 | assert "loofs" == pluralize("loof") 135 | assert "wolves" == pluralize("wolf") 136 | assert "calves" == pluralize("calf") 137 | assert "scarfs" == pluralize("scarf") 138 | assert "scurfs" == pluralize("scurf") 139 | assert "wharves" == pluralize("wharf") 140 | assert "loaves" == pluralize("loaf") 141 | assert "leaves" == pluralize("leaf") 142 | end 143 | 144 | test :special_case_nouns_ending_in_o do 145 | assert "echo" == singularize("echoes") 146 | assert "hero" == singularize("heroes") 147 | assert "potato" == singularize("potatoes") 148 | assert "tomato" == singularize("tomatoes") 149 | assert "tooth" == singularize("teeth") 150 | 151 | assert "echoes" == pluralize("echo") 152 | assert "heroes" == pluralize("hero") 153 | assert "potatoes" == pluralize("potato") 154 | assert "tomatoes" == pluralize("tomato") 155 | assert "teeth" == pluralize("tooth") 156 | 157 | assert "statuses" == pluralize("status") 158 | assert "aliases" == pluralize("alias") 159 | assert "canvases" == pluralize("canvas") 160 | end 161 | 162 | test :pluralize_only_at_the_end_of_the_word do 163 | assert "leaflets" == pluralize("leaflet") 164 | assert "personalities" == pluralize("personality") 165 | assert "diets" == pluralize("diet") 166 | assert "footers" == pluralize("footer") 167 | assert "toothpicks" == pluralize("toothpick") 168 | assert "echograms" == pluralize("echogram") 169 | assert "herons" == pluralize("heron") 170 | assert "gooseberries" == pluralize("gooseberry") 171 | assert "womanizers" == pluralize("womanizer") 172 | end 173 | 174 | test :singularize_only_at_the_end_of_the_word do 175 | assert "general" == singularize("generals") 176 | end 177 | 178 | test :skip_singularize do 179 | assert "dog" == singularize("dog") 180 | 181 | assert "status" == singularize("status") 182 | assert "alias" == singularize("alias") 183 | assert "canvas" == singularize("canvas") 184 | end 185 | 186 | test :skip_pluralize do 187 | assert "deer" == pluralize("deer") 188 | assert "aircraft" == pluralize("aircraft") 189 | assert "dogs" == pluralize("dogs") 190 | end 191 | 192 | test :camelize_upper do 193 | assert "Upper" == camelize("upper") 194 | assert "UpperCamelCase" == camelize("upper_camel_case") 195 | assert "UpperCamelCase" == camelize("UpperCamelCase") 196 | assert "UpperCamelCase" == camelize("UPPER_CAMEL_CASE") 197 | assert "" == camelize("") 198 | refute "UpperCamelCase" == camelize("upper_camel_case", :lower) 199 | end 200 | 201 | test :camelize_lower do 202 | assert "lower" == camelize("lower", :lower) 203 | assert "lowerCamelCase" == camelize("lower_camel_case", :lower) 204 | assert "lowerCamelCase" == camelize("Lower_camel_case", :lower) 205 | assert "lowerCamelCase" == camelize("lowerCamelCase", :lower) 206 | assert "lowerCamelCase" == camelize("LOWER_CAMEL_CASE", :lower) 207 | assert "" == camelize("", :lower) 208 | refute "lowerCamelCase" == camelize("lower_camel_case") 209 | end 210 | 211 | test :parameterize do 212 | assert "elixir-inflex" == parameterize("elixir inflex") 213 | assert "elixir-inflex" == parameterize("Elixir Inflex") 214 | assert "elixir_inflex" == parameterize("elixir inflex", "_") 215 | assert "elixir-inflex" == parameterize("elixir%20inflex") 216 | end 217 | 218 | test :parameterize_to_ascii do 219 | assert "elixir-inflex" == parameterize_to_ascii("elixir inflex") 220 | assert "elixir-inflex" == parameterize_to_ascii("Elixir Inflex") 221 | assert "elixir_inflex" == parameterize_to_ascii("elixir inflex", "_") 222 | assert "elixir-inflex" == parameterize_to_ascii("elixir%20inflex") 223 | end 224 | 225 | test :underscore do 226 | assert "upper_camel_case" == underscore("UpperCamelCase") 227 | assert "lower_camel_case" == underscore("lowerCamelCase") 228 | assert "upper_camel_case" == underscore(UpperCamelCase) 229 | assert "lower_camel_case" == underscore(:lowerCamelCase) 230 | assert "elixir_inflex" == underscore("elixir-inflex") 231 | assert "elixir_inflex" == underscore("Elixir-Inflex") 232 | end 233 | 234 | test :ordinalize do 235 | assert "-113th" == ordinalize(-113) 236 | assert "-112th" == ordinalize(-112) 237 | assert "-111th" == ordinalize(-111) 238 | assert "-3rd" == ordinalize(-3) 239 | assert "-2nd" == ordinalize(-2) 240 | assert "-1st" == ordinalize(-1) 241 | assert "1st" == ordinalize(1) 242 | assert "2nd" == ordinalize(2) 243 | assert "3rd" == ordinalize(3) 244 | assert "4th" == ordinalize(4) 245 | assert "7th" == ordinalize(7) 246 | assert "8th" == ordinalize(8) 247 | assert "10th" == ordinalize(10) 248 | assert "11th" == ordinalize(11) 249 | assert "12th" == ordinalize(12) 250 | assert "15th" == ordinalize(15) 251 | assert "17th" == ordinalize(17) 252 | assert "20th" == ordinalize(20) 253 | assert "21st" == ordinalize(21) 254 | assert "22nd" == ordinalize(22) 255 | assert "23rd" == ordinalize(23) 256 | assert "26th" == ordinalize(26) 257 | assert "33rd" == ordinalize(33) 258 | assert "34th" == ordinalize(34) 259 | assert "35th" == ordinalize(35) 260 | assert "41st" == ordinalize(41) 261 | assert "85th" == ordinalize(85) 262 | assert "90th" == ordinalize(90) 263 | assert "92nd" == ordinalize(92) 264 | assert "111th" == ordinalize(111) 265 | assert "112th" == ordinalize(112) 266 | assert "113th" == ordinalize(113) 267 | end 268 | 269 | test :inflect do 270 | assert "child" == inflect("children", 1) 271 | assert "child" == inflect("children", 1.0) 272 | assert "people" == inflect("person", 2) 273 | assert "People" == inflect("Person", 2) 274 | assert "dogs" == inflect("dog", 3.2) 275 | assert "slice" == inflect("slice", 1) 276 | assert "slice" == inflect("slice", 1.0) 277 | assert "accomplice" == inflect("accomplice", 1) 278 | assert "accomplice" == inflect("accomplice", 1.0) 279 | end 280 | 281 | end 282 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start 2 | --------------------------------------------------------------------------------