Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (53 sloc) 13.2 KB
<!DOCTYPE html> <html> <head> <title>index.js</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> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> index.js </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> <pre><code>Splearch 0.0.1
(c) 2012 Stephen Belanger
Splearch may be freely distributed under the MIT license.
</code></pre> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>Splearch provides a simplified abstraction of regex searching to locate
patterns in a string. It works very similar to how Dir.glob does in ruby
in that * matches word characters, ** matches all characters, and braces
can be used to expand lists to multiple matching possibilities.</p>
<h2>Usage</h2>
<pre><code>Splearch('foo.{bar,baz*}.buz{1..5}')
Splearch('foo.*.bux', {
braces: false // Can be broken down to 'ranges' and 'lists'
, double_splats: false
})
</code></pre> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">Splearch</span> <span class="p">(</span><span class="nx">search</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">options</span> <span class="o">||</span> <span class="p">(</span><span class="nx">options</span> <span class="o">=</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>First of all, we need to convert the string to lowercase and escape
regex characters.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">str</span> <span class="o">=</span> <span class="nx">search</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">().</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/[-[\]()+?\\^$|#\s]/g</span><span class="p">,</span> <span class="s2">&quot;\\$&amp;&quot;</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>Brace expansion and multi-splat support are both optional, but are
enabled by default. Ranges are parsed separately from lists and
before them, so we can nest ranges within lists like {a,{1..3},c}</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">braces</span> <span class="o">!==</span> <span class="kc">false</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">ranges</span> <span class="o">!==</span> <span class="kc">false</span><span class="p">)</span> <span class="p">{</span> <span class="nx">str</span> <span class="o">=</span> <span class="nx">Splearch</span><span class="p">.</span><span class="nx">replaceRanges</span><span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">lists</span> <span class="o">!==</span> <span class="kc">false</span><span class="p">)</span> <span class="p">{</span> <span class="nx">str</span> <span class="o">=</span> <span class="nx">Splearch</span><span class="p">.</span><span class="nx">replaceLists</span><span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">}</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">double_splats</span> <span class="o">!==</span> <span class="kc">false</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">str</span> <span class="o">=</span> <span class="nx">str</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\*\*/g</span><span class="p">,</span> <span class="s1">&#39;.+&#39;</span><span class="p">)</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>Lastly, we replace single splats and then build and return a regex object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="k">new</span> <span class="nb">RegExp</span><span class="p">(</span><span class="s1">&#39;^&#39;</span> <span class="o">+</span> <span class="nx">str</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\*/g</span><span class="p">,</span> <span class="s1">&#39;\\w*&#39;</span><span class="p">),</span> <span class="s1">&#39;i&#39;</span><span class="p">)</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>You can use the range replacer directly, if you so desire; though it's
not recommended. Note than number ordering is preserved in the final
string, so {0..9} becomes [0-9], which matches any number.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Splearch</span><span class="p">.</span><span class="nx">replaceRanges</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">new_str</span> <span class="o">=</span> <span class="nx">str</span><span class="p">,</span> <span class="nx">ranges</span> <span class="o">=</span> <span class="nx">new_str</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/{([^\{\.]*\.\.[^\}]*)}/g</span><span class="p">)</span> <span class="o">||</span> <span class="p">[]</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">ranges</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">block</span> <span class="o">=</span> <span class="nx">ranges</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span> <span class="nx">rep</span> <span class="o">=</span> <span class="nx">block</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/([^{\.])\.\./</span><span class="p">,</span> <span class="s1">&#39;\$1-&#39;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/{([^\-]*\-[^}])}/</span><span class="p">,</span> <span class="s1">&#39;[\$1]&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">block</span> <span class="o">!==</span> <span class="nx">rep</span><span class="p">)</span> <span class="p">{</span> <span class="nx">new_str</span> <span class="o">=</span> <span class="nx">new_str</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">block</span><span class="p">,</span> <span class="nx">rep</span><span class="p">)</span> <span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">new_str</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>You can also use the list replacer directly, if you so desire; though,
again, it's not recommended. Lists should be replaced after ranges,
if you want to support nesting ranges within lists. A list can contain
any number of values, separated by commas, including ranges and single
or double splats.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Splearch</span><span class="p">.</span><span class="nx">replaceLists</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">new_str</span> <span class="o">=</span> <span class="nx">str</span><span class="p">,</span> <span class="nx">lists</span> <span class="o">=</span> <span class="nx">new_str</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/{([^\{\}]*)}/g</span><span class="p">)</span> <span class="o">||</span> <span class="p">[]</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">lists</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">block</span> <span class="o">=</span> <span class="nx">lists</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span> <span class="nx">rep</span> <span class="o">=</span> <span class="nx">block</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/([^{,]*)*,/g</span><span class="p">,</span> <span class="s1">&#39;\$1|&#39;</span><span class="p">)</span>
<span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/{([^}]*)}/</span><span class="p">,</span> <span class="s1">&#39;(\$1)&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">block</span> <span class="o">!==</span> <span class="nx">rep</span><span class="p">)</span> <span class="p">{</span> <span class="nx">new_str</span> <span class="o">=</span> <span class="nx">new_str</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">block</span><span class="p">,</span> <span class="nx">rep</span><span class="p">)</span> <span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">new_str</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>Splearch also supports CommonJS, so it works in browsers, and in node.js!</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">module</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span> <span class="k">typeof</span> <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">Splearch</span>
<span class="p">}</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
Jump to Line
Something went wrong with that request. Please try again.