From d203b3e6623b071582eaebdb5b0e432db4fe7c8d Mon Sep 17 00:00:00 2001 From: deltreey Date: Tue, 4 Aug 2015 00:55:01 -0400 Subject: [PATCH] rename index.js to wildstring.js semver requires a major version bump --- Gruntfile.js | 2 +- README.md | 1 - bower.json | 2 +- .../F__Git_wildstring_wildstring.js.html | 336 ++++++++++++++++++ documentation/index.html | 3 +- documentation/wildstring.html | 16 +- package.json | 4 +- index.js => wildstring.js | 0 8 files changed, 349 insertions(+), 15 deletions(-) create mode 100644 documentation/F__Git_wildstring_wildstring.js.html rename index.js => wildstring.js (100%) diff --git a/Gruntfile.js b/Gruntfile.js index a8fdd98..dd810d0 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -23,7 +23,7 @@ module.exports = function (grunt) { }, jsdoc: { dist: { - src: ['index.js', 'test/*.js', 'README.md'], + src: ['wildstring.js', 'test/*.js', 'README.md'], options: { destination: 'documentation', template : 'node_modules/grunt-jsdoc/node_modules/ink-docstrap/template', diff --git a/README.md b/README.md index 3af92e6..3750a82 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,6 @@ bower install wildstring #### html -make sure you rename the `index.js` file to `wildstring.js` when you add it to your project ``` html ``` diff --git a/bower.json b/bower.json index b557f56..045dfe6 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "wildstring", - "main": "index.js", + "main": "wildstring.js", "version": "0.0.2", "authors": [ "deltreey " diff --git a/documentation/F__Git_wildstring_wildstring.js.html b/documentation/F__Git_wildstring_wildstring.js.html new file mode 100644 index 0000000..f932efd --- /dev/null +++ b/documentation/F__Git_wildstring_wildstring.js.html @@ -0,0 +1,336 @@ + + + + + + wildstring Source: F:/Git/wildstring/wildstring.js + + + + + + + + + + + + + +
+
+ + +
+ +
+

Source: F:/Git/wildstring/wildstring.js

+ + +

Source: F:/Git/wildstring/wildstring.js

+ +
+
+
'use strict';
+
+var wildstring = (function(module) {
+
+/**
+* @namespace wildstring
+* @property {string} wildcard the wildcard to use in your strings, defaults to '*'
+* @property {boolean} caseSensitive whether matches should care about case, defaults to true
+*/
+wildstring = {
+	wildcard: '*',
+	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 < rollbackStrings.length; ++s) {
+		var currentString = rollbackStrings[s].string;	// starting with the rolled back string
+		var patternIndex = rollbackStrings[s].index;
+
+		while (patternIndex < 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] !== '' &&
+					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
+*/
+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 && string.indexOf(patternSubstrings[0]) !== 0) {
+		// not starting with a wildcard
+		return false;
+	}
+
+	var rollbackStrings = [];
+
+	while (patternIndex < 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();
+		}
+
+		currentString = currentString.substr(
+			currentString.indexOf(patternSubstrings[patternIndex]) + patternSubstrings[patternIndex].length
+		);
+
+		patternIndex++;
+		while (patternSubstrings[patternIndex] === '') {
+			patternIndex++;
+		}
+	}
+
+	if (patternIndex >= patternSubstrings.length &&
+			patternSubstrings[patternSubstrings.length - 1] !== '' &&
+			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 checkRollbackStrings(rollbackStrings, patternSubstrings);
+	}
+
+	return true;
+};
+
+/**
+* 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 < strings.length; ++s) {
+		result += patternSubstrings[s] + strings[s];
+	}
+
+	return result;
+};
+
+module.exports = wildstring;
+return wildstring;
+}(module || {}));
+
+
+
+ + + + + +
+
+ +
+ + + +
+
+ + +
+ + + + DocStrap Copyright © 2012-2014 The contributors to the JSDoc3 and DocStrap projects. + +
+ + + Documentation generated by JSDoc 3.3.2 + on Tue Aug 4th 2015 using the DocStrap template. + +
+ + + + + + + + + + + + + + + + + + diff --git a/documentation/index.html b/documentation/index.html index 2961e52..f109fef 100644 --- a/documentation/index.html +++ b/documentation/index.html @@ -79,8 +79,7 @@ Join the chat at https://gitter.im/deltreey/wildstring

Shake it shake it

Installing wildstring is a snap. wildstring has no dependencies, so you don't need anything else to run it. If you want to use tools though, here's some tips on how to install it with popular installers.

node.js

npm install wildstring

then:

-
var wildstring = require('wildstring');

bower

bower install wildstring

html

make sure you rename the index.js file to wildstring.js when you add it to your project

-
<script src="wildstring.js"></script>

Hold me tight

Especially with something that does something new, it's important to see how it works. Below are some examples, but here's a brief explanation as well.

+
var wildstring = require('wildstring');

bower

bower install wildstring

html

<script src="wildstring.js"></script>

Hold me tight

Especially with something that does something new, it's important to see how it works. Below are some examples, but here's a brief explanation as well.

In this explanation, I'll use * as my wildcard for simplicity. If you put a wildcard at the beginning, for example *Thing then you can match anything or nothing before your string. So your string could be Wild Thing or just Thing and it would match fine. The same is true for the end. Wild* would match Wild Thing or just Wild. If you want to match text in the middle of the string, it works the same way. Wild*Thing matches both WildThing and Wild and crazy Thing.

wildstring.match('Test*', 'Testing');                 // true, wildcard matches 'ing'
 wildstring.match('*ing', 'testing');                  // true, wildcard matches 'test'
diff --git a/documentation/wildstring.html b/documentation/wildstring.html
index f3c9b29..eff86ee 100644
--- a/documentation/wildstring.html
+++ b/documentation/wildstring.html
@@ -175,8 +175,8 @@
 	
@@ -338,8 +338,8 @@
@@ -497,8 +497,8 @@
@@ -661,8 +661,8 @@
diff --git a/package.json b/package.json index 03cf73a..d0e3185 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "wildstring", - "version": "0.0.3", + "version": "1.0.0", "description": "Simple String Wildcard Handling", - "main": "index.js", + "main": "wildstring.js", "scripts": { "test": "grunt" }, diff --git a/index.js b/wildstring.js similarity index 100% rename from index.js rename to wildstring.js