Permalink
Fetching contributors…
Cannot retrieve contributors at this time
79 lines (67 sloc) 21.7 KB
<!DOCTYPE html> <html> <head> <title>inflections.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="default_inflections.html"> default_inflections.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="inflections.html"> inflections.coffee </a> <a class="source" href="methods.html"> methods.coffee </a> <a class="source" href="number_extensions.html"> number_extensions.coffee </a> <a class="source" href="string_extensions.html"> string_extensions.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> inflections.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="k">class</span> <span class="nx">Inflections</span>
<span class="k">this</span><span class="p">.</span><span class="nv">instance = </span><span class="o">-&gt;</span>
<span class="nx">@__instance__</span> <span class="o">||=</span> <span class="k">new</span> <span class="k">this</span>
<span class="nv">constructor: </span><span class="o">-&gt;</span>
<span class="vi">@plurals = </span><span class="p">[]</span>
<span class="vi">@singulars = </span><span class="p">[]</span>
<span class="vi">@uncountables = </span><span class="p">[]</span>
<span class="vi">@humans = </span><span class="p">[]</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
The replacement should always be a string that may include references to the matched data from the rule.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">plural: </span><span class="nf">(rule, replacement) -&gt;</span>
<span class="k">if</span> <span class="k">typeof</span> <span class="nx">rule</span> <span class="o">==</span> <span class="s">&#39;string&#39;</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nv">index = </span><span class="nx">@uncountables</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">rule</span><span class="p">))</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span>
<span class="nx">@uncountables</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">index = </span><span class="nx">@uncountables</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">replacement</span><span class="p">))</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span>
<span class="nx">@uncountables</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="nx">@plurals</span><span class="p">.</span><span class="nx">unshift</span><span class="p">([</span><span class="nx">rule</span><span class="p">,</span> <span class="nx">replacement</span><span class="p">])</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
The replacement should always be a string that may include references to the matched data from the rule.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">singular: </span><span class="nf">(rule, replacement) -&gt;</span>
<span class="k">if</span> <span class="k">typeof</span> <span class="nx">rule</span> <span class="o">==</span> <span class="s">&#39;string&#39;</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="nv">index = </span><span class="nx">@uncountables</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">rule</span><span class="p">))</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span>
<span class="nx">@uncountables</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">index = </span><span class="nx">@uncountables</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">replacement</span><span class="p">))</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span>
<span class="nx">@uncountables</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="nx">@singulars</span><span class="p">.</span><span class="nx">unshift</span><span class="p">([</span><span class="nx">rule</span><span class="p">,</span> <span class="nx">replacement</span><span class="p">])</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
for strings, not regular expressions. You simply pass the irregular in singular and plural form.</p>
<h3>Examples</h3>
<pre><code>irregular 'octopus', 'octopi'
irregular 'person', 'people'
</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">irregular: </span><span class="nf">(singular, plural) -&gt;</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">index = </span><span class="nx">@uncountables</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">singular</span><span class="p">))</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span>
<span class="nx">@uncountables</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">index = </span><span class="nx">@uncountables</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">plural</span><span class="p">))</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">1</span>
<span class="nx">@uncountables</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">singular</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">==</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toUpperCase</span><span class="p">()</span>
<span class="k">this</span><span class="p">.</span><span class="nx">plural</span><span class="p">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">&quot;(</span><span class="si">#{</span><span class="nx">singular</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="si">}</span><span class="s">)</span><span class="si">#{</span><span class="nx">singular</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">$&quot;</span><span class="p">,</span> <span class="s">&quot;i&quot;</span><span class="p">),</span> <span class="s">&#39;$1&#39;</span> <span class="o">+</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">this</span><span class="p">.</span><span class="nx">plural</span><span class="p">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">&quot;(</span><span class="si">#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="si">}</span><span class="s">)</span><span class="si">#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">$&quot;</span><span class="p">,</span> <span class="s">&quot;i&quot;</span><span class="p">),</span> <span class="s">&#39;$1&#39;</span> <span class="o">+</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">this</span><span class="p">.</span><span class="nx">singular</span><span class="p">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">&quot;(</span><span class="si">#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="si">}</span><span class="s">)</span><span class="si">#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">$&quot;</span><span class="p">,</span> <span class="s">&quot;i&quot;</span><span class="p">),</span> <span class="s">&#39;$1&#39;</span> <span class="o">+</span> <span class="nx">singular</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">else</span>
<span class="k">this</span><span class="p">.</span><span class="nx">plural</span><span class="p">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">&quot;</span><span class="si">#{</span><span class="nx">singular</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toUpperCase</span><span class="p">()</span><span class="si">}#{</span><span class="nx">singular</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">$&quot;</span><span class="p">),</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">this</span><span class="p">.</span><span class="nx">plural</span><span class="p">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">&quot;</span><span class="si">#{</span><span class="nx">singular</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toLowerCase</span><span class="p">()</span><span class="si">}#{</span><span class="nx">singular</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">$&quot;</span><span class="p">),</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toLowerCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">this</span><span class="p">.</span><span class="nx">plural</span><span class="p">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">&quot;</span><span class="si">#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toUpperCase</span><span class="p">()</span><span class="si">}#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">$&quot;</span><span class="p">),</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">this</span><span class="p">.</span><span class="nx">plural</span><span class="p">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">&quot;</span><span class="si">#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toLowerCase</span><span class="p">()</span><span class="si">}#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">$&quot;</span><span class="p">),</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toLowerCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">this</span><span class="p">.</span><span class="nx">singular</span><span class="p">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">&quot;</span><span class="si">#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toUpperCase</span><span class="p">()</span><span class="si">}#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">$&quot;</span><span class="p">),</span> <span class="nx">singular</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toUpperCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">singular</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="k">this</span><span class="p">.</span><span class="nx">singular</span><span class="p">(</span><span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s">&quot;</span><span class="si">#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toLowerCase</span><span class="p">()</span><span class="si">}#{</span><span class="nx">plural</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s">$&quot;</span><span class="p">),</span> <span class="nx">singular</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">toLowerCase</span><span class="p">()</span> <span class="o">+</span> <span class="nx">singular</span><span class="p">[</span><span class="mi">1</span><span class="p">..</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Add uncountable words that shouldn't be attempted inflected.</p>
<h3>Examples</h3>
<pre><code>uncountable "money"
uncountable "money", "information"
</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">uncountable: </span><span class="nf">(words...) -&gt;</span>
<span class="vi">@uncountables = </span><span class="nx">@uncountables</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">words</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Specifies a humanized form of a string by a regular expression rule or by a string mapping.
When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')</p>
<h3>Examples</h3>
<pre><code>human /_cnt$/i, '$1_count'
human "legacy_col_person_name", "Name"
</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">human: </span><span class="nf">(rule, replacement) -&gt;</span>
<span class="nx">@humans</span><span class="p">.</span><span class="nx">unshift</span><span class="p">([</span><span class="nx">rule</span><span class="p">,</span> <span class="nx">replacement</span><span class="p">])</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Clears the loaded inflections within a given scope (default is <tt>:all</tt>).
Give the scope as a symbol of the inflection type, the options are: <tt>plurals</tt>,
<tt>singulars</tt>, <tt>uncountables</tt>, <tt>humans</tt>.</p>
<h3>Examples</h3>
<pre><code>clear 'all'
clear 'plurals'
</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">clear: </span><span class="nf">(scope = &#39;all&#39;) -&gt;</span>
<span class="k">if</span> <span class="nx">scope</span> <span class="o">==</span> <span class="s">&#39;all&#39;</span>
<span class="vi">@plurals = </span><span class="p">[]</span>
<span class="vi">@singulars = </span><span class="p">[]</span>
<span class="vi">@uncountables = </span><span class="p">[]</span>
<span class="vi">@humans = </span><span class="p">[]</span>
<span class="k">else</span>
<span class="k">this</span><span class="p">[</span><span class="nx">scope</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="nv">exports.Inflections = </span><span class="nx">Inflections</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>