bugfix and updated docs

This commit is contained in:
deltreey
2015-08-12 23:31:40 -04:00
parent 5bc4ed6a86
commit bd2902f7ed
5 changed files with 145 additions and 161 deletions

View File

@@ -54,21 +54,6 @@
<pre
class="sunlight-highlight-javascript linenums">'use strict';
var wildstring = (function (name, definition) {
if (typeof(module) !== 'undefined') {
module.exports = definition();
}
else if (typeof(define) === 'function' &amp;&amp; typeof(define.amd) === 'object') {
define(definition);
}
else if (typeof(this) !== 'undefined') {
this[name] = definition();
}
else {
return definition();
}
}('wildstring', function () {
/**
* @namespace wildstring
* @property {string} wildcard the wildcard to use in your strings, defaults to '*'
@@ -76,31 +61,103 @@ var wildstring = (function (name, definition) {
*/
var wildstring = {
wildcard: '*',
caseSensitive: true
};
caseSensitive: true,
/**
* When a match doesn't continue to the end of the string, this function rolls back to try again with the rest of the string
* @memberof wildstring
* @access private
* @param {string[]} rollbackStrings The list of substrings that appeared prior to the current match
* @param {string[]} patternSubstrings The matching list of pattens that need to be matched before the current pattern
*/
function checkRollbackStrings (rollbackStrings, patternSubstrings) {
for (var s = 0; s &lt; rollbackStrings.length; ++s) {
var currentString = rollbackStrings[s].string; // starting with the rolled back string
var patternIndex = rollbackStrings[s].index;
/**
* When a match doesn't continue to the end of the string, this function rolls back to try again with the rest of the string
* @memberof wildstring
* @access private
* @param {string[]} rollbackStrings The list of substrings that appeared prior to the current match
* @param {string[]} patternSubstrings The matching list of pattens that need to be matched before the current pattern
*/
checkRollbackStrings: function (rollbackStrings, patternSubstrings) {
for (var s = 0; s &lt; rollbackStrings.length; ++s) {
var currentString = rollbackStrings[s].string; // starting with the rolled back string
var patternIndex = rollbackStrings[s].index;
while (patternIndex &lt; patternSubstrings.length) {
if (currentString.indexOf(patternSubstrings[patternIndex]) === -1) {
break;
}
var testString = currentString.substr(1); //remove just one char to retest
rollbackStrings.push({ string: testString, index: patternIndex });
if (testString.indexOf(patternSubstrings[patternIndex]) === -1) {
rollbackStrings.pop();
break;
}
currentString = currentString.substr(
currentString.indexOf(patternSubstrings[patternIndex]) + patternSubstrings[patternIndex].length
);
patternIndex++;
while (patternSubstrings[patternIndex] === '') {
patternIndex++;
}
if (patternIndex >= patternSubstrings.length) {
if (patternSubstrings[patternSubstrings.length - 1] !== '' &amp;&amp;
currentString.length > 0) {
// not ending with a wildcard, we need to backtrack
break;
}
else {
return true;
}
}
}
}
return false;
},
/**
* Check if a string matches a pattern
* @memberof wildstring
* @param {string} pattern The pattern to match using the configured wildcard
* @param {string} string The string to test for a match
*/
match: function (pattern, string) {
// if there are no wildcards, must be exact
if (pattern.indexOf(wildstring.wildcard) === -1) {
return pattern === string;
}
if (!wildstring.caseSensitive) {
pattern = pattern.toLowerCase();
string = string.toLowerCase();
}
var patternSubstrings = pattern.split(wildstring.wildcard);
var patternIndex = 0;
var currentString = string;
// find pattern beginning
while (patternSubstrings[patternIndex] === '') {
patternIndex++;
// if the pattern is just wildcards, it matches
if (patternIndex === pattern.length) {
return true;
}
}
if (patternIndex === 0 &amp;&amp; string.indexOf(patternSubstrings[0]) !== 0) {
// not starting with a wildcard
return false;
}
var rollbackStrings = [];
while (patternIndex &lt; patternSubstrings.length) {
if (currentString.indexOf(patternSubstrings[patternIndex]) === -1) {
break;
return wildstring.checkRollbackStrings(rollbackStrings, patternSubstrings);
}
// create a queue of strings to roll back and try again if we fail later
var testString = currentString.substr(1); //remove just one char to retest
rollbackStrings.push({ string: testString, index: patternIndex });
if (testString.indexOf(patternSubstrings[patternIndex]) === -1) {
rollbackStrings.pop();
break;
}
currentString = currentString.substr(
@@ -111,138 +168,67 @@ function checkRollbackStrings (rollbackStrings, patternSubstrings) {
while (patternSubstrings[patternIndex] === '') {
patternIndex++;
}
}
if (patternIndex >= patternSubstrings.length) {
if (patternSubstrings[patternSubstrings.length - 1] !== '' &amp;&amp;
currentString.length > 0) {
// not ending with a wildcard, we need to backtrack
break;
}
else {
return true;
}
if (patternIndex >= patternSubstrings.length &amp;&amp;
patternSubstrings[patternSubstrings.length - 1] !== '' &amp;&amp;
currentString.length > 0) {
// not ending with a wildcard, we need to backtrack
if (currentString === string) { // this string doesn't even match a little
return false;
}
}
}
return false;
}
/**
* Check if a string matches a pattern
* @memberof wildstring
* @param {string} pattern The pattern to match using the configured wildcard
* @param {string} string The string to test for a match
*/
wildstring.match = function (pattern, string) {
// if there are no wildcards, must be exact
if (pattern.indexOf(wildstring.wildcard) === -1) {
return pattern === string;
}
if (!wildstring.caseSensitive) {
pattern = pattern.toLowerCase();
string = string.toLowerCase();
}
var patternSubstrings = pattern.split(wildstring.wildcard);
var patternIndex = 0;
var currentString = string;
// find pattern beginning
while (patternSubstrings[patternIndex] === '') {
patternIndex++;
// if the pattern is just wildcards, it matches
if (patternIndex === pattern.length) {
return true;
}
}
if (patternIndex === 0 &amp;&amp; string.indexOf(patternSubstrings[0]) !== 0) {
// not starting with a wildcard
return false;
}
var rollbackStrings = [];
while (patternIndex &lt; patternSubstrings.length) {
if (currentString.indexOf(patternSubstrings[patternIndex]) === -1) {
return checkRollbackStrings(rollbackStrings, patternSubstrings);
}
// create a queue of strings to roll back and try again if we fail later
var testString = currentString.substr(1); //remove just one char to retest
rollbackStrings.push({ string: testString, index: patternIndex });
if (testString.indexOf(patternSubstrings[patternIndex]) === -1) {
rollbackStrings.pop();
return wildstring.checkRollbackStrings(rollbackStrings, patternSubstrings);
}
currentString = currentString.substr(
currentString.indexOf(patternSubstrings[patternIndex]) + patternSubstrings[patternIndex].length
);
return true;
},
patternIndex++;
while (patternSubstrings[patternIndex] === '') {
patternIndex++;
/**
* Replace wildcards in a pattern with strings (string interpolation)
* @memberof wildstring
* @param {string} pattern The start string, using wildcards as placeholders
* @param {string|string[]} strings The string or strings to replace the wildcards in the pattern.
* If you pass a single string, it will replace all the wildcards with the string.
* If you pass an array of strings, they will replace the wildcards in order from left to right.
* @throws The number of items in the strings array (if you pass an array) must match the number of wildcards in the pattern string.
* @throws You need to pass both parameters
* @throws You need to pass the right types
*/
replace: function (pattern, strings) {
if (pattern === undefined || strings === undefined) {
throw new Error('wildstring.replace takes the pattern as one parameter and either a string or an array of strings as the second. You didn\'t pass enough parameters.');
}
}
if (patternIndex >= patternSubstrings.length &amp;&amp;
patternSubstrings[patternSubstrings.length - 1] !== '' &amp;&amp;
currentString.length > 0) {
// not ending with a wildcard, we need to backtrack
if (currentString === string) { // this string doesn't even match a little
return false;
if (typeof(strings) === typeof('')) {
return pattern.replace(wildstring.wildcard, strings);
}
if (!Array.isArray(strings) || typeof(pattern) !== typeof('')) {
throw new Error('wildstring.replace takes the pattern as one parameter and either a string or an array of strings as the second. Your parameter types are incorrect.');
}
if (pattern.indexOf(wildstring.wildcard) === -1) {
return pattern; // if there are no wildcards, just return the pattern
}
var patternSubstrings = pattern.split(wildstring.wildcard);
if (patternSubstrings.length - 1 !== strings.length) {
var message = 'There are a different number of wildcards than strings to replace them. You have ' +
wildstring.wildcard +' wildcards in "' + wildstring.wildcard + '" and ' + wildstring.wildcard +
' replacement strings.';
throw new Error(wildstring.replace(message, [ patternSubstrings.length - 1, pattern, strings.length ]));
}
return checkRollbackStrings(rollbackStrings, patternSubstrings);
}
var result = '';
return true;
for (var s = 0; s &lt; strings.length; ++s) {
result += patternSubstrings[s] + strings[s];
}
return result;
}
};
/**
* Replace wildcards in a pattern with strings (string interpolation)
* @memberof wildstring
* @param {string} pattern The start string, using wildcards as placeholders
* @param {string|string[]} strings The string or strings to replace the wildcards in the pattern.
* If you pass a single string, it will replace all the wildcards with the string.
* If you pass an array of strings, they will replace the wildcards in order from left to right.
* @throws The number of items in the strings array (if you pass an array) must match the number of wildcards in the pattern string.
* @throws You need to pass both parameters
* @throws You need to pass the right types
*/
wildstring.replace = function (pattern, strings) {
if (pattern === undefined || strings === undefined) {
throw new Error('wildstring.replace takes the pattern as one parameter and either a string or an array of strings as the second. You didn\'t pass enough parameters.');
}
if (typeof(strings) === typeof('')) {
return pattern.replace(wildstring.wildcard, strings);
}
if (!Array.isArray(strings) || typeof(pattern) !== typeof('')) {
throw new Error('wildstring.replace takes the pattern as one parameter and either a string or an array of strings as the second. Your parameter types are incorrect.');
}
if (pattern.indexOf(wildstring.wildcard) === -1) {
return pattern; // if there are no wildcards, just return the pattern
}
var patternSubstrings = pattern.split(wildstring.wildcard);
if (patternSubstrings.length - 1 !== strings.length) {
var message = 'There are a different number of wildcards than strings to replace them. You have ' +
wildstring.wildcard +' wildcards in "' + wildstring.wildcard + '" and ' + wildstring.wildcard +
' replacement strings.';
throw new Error(wildstring.replace(message, [ patternSubstrings.length - 1, pattern, strings.length ]));
}
var result = '';
for (var s = 0; s &lt; strings.length; ++s) {
result += patternSubstrings[s] + strings[s];
}
return result;
};
return wildstring;
}));
if (typeof(module) !== 'undefined') { module.exports = wildstring; }
if (typeof(angular) !== 'undefined') { angular.module('wildstring').factory = wildstring; }
if (typeof(define) !== 'undefined') { define([], wildstring); }
</pre>
</article>
</section>
@@ -272,7 +258,7 @@ return wildstring;
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Sun Aug 9th 2015 using the <a
on Wed Aug 12th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>

View File

@@ -138,7 +138,7 @@ grunt</code></pre><p>grunt will run all the tests and jshint, so just make sure
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Sun Aug 9th 2015 using the <a
on Wed Aug 12th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>

View File

@@ -165,7 +165,7 @@
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Sun Aug 9th 2015 using the <a
on Wed Aug 12th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>

View File

@@ -176,7 +176,7 @@
<ul class="dummy">
<li>
<a href="F__Git_wildstring_wildstring.js.html">F:/Git/wildstring/wildstring.js</a>,
<a href="F__Git_wildstring_wildstring.js.html#sunlight-1-line-18">line 18</a>
<a href="F__Git_wildstring_wildstring.js.html#sunlight-1-line-3">line 3</a>
</li>
</ul>
</dd>
@@ -339,7 +339,7 @@
<ul class="dummy">
<li>
<a href="F__Git_wildstring_wildstring.js.html">F:/Git/wildstring/wildstring.js</a>,
<a href="F__Git_wildstring_wildstring.js.html#sunlight-1-line-35">line 35</a>
<a href="F__Git_wildstring_wildstring.js.html#sunlight-1-line-19">line 19</a>
</li>
</ul>
</dd>
@@ -498,7 +498,7 @@
<ul class="dummy">
<li>
<a href="F__Git_wildstring_wildstring.js.html">F:/Git/wildstring/wildstring.js</a>,
<a href="F__Git_wildstring_wildstring.js.html#sunlight-1-line-83">line 83</a>
<a href="F__Git_wildstring_wildstring.js.html#sunlight-1-line-67">line 67</a>
</li>
</ul>
</dd>
@@ -662,7 +662,7 @@
<ul class="dummy">
<li>
<a href="F__Git_wildstring_wildstring.js.html">F:/Git/wildstring/wildstring.js</a>,
<a href="F__Git_wildstring_wildstring.js.html#sunlight-1-line-160">line 160</a>
<a href="F__Git_wildstring_wildstring.js.html#sunlight-1-line-144">line 144</a>
</li>
</ul>
</dd>
@@ -761,7 +761,7 @@
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Sun Aug 9th 2015 using the <a
on Wed Aug 12th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>

View File

@@ -175,5 +175,3 @@ var wildstring = {
if (typeof(module) !== 'undefined') { module.exports = wildstring; }
if (typeof(angular) !== 'undefined') { angular.module('wildstring').factory = wildstring; }
if (typeof(define) !== 'undefined') { define([], wildstring); }
return wildstring;