screw the module pattern

this needs to work when minified
http://snook.ca/archives/javascript/no-love-for-module-pattern
This commit is contained in:
deltreey
2015-08-12 23:30:51 -04:00
parent 54d1a9e180
commit 5bc4ed6a86
2 changed files with 138 additions and 150 deletions

View File

@@ -1,7 +1,7 @@
'use strict';
var assert = require('assert'),
wildstring = require('../');
wildstring = require('../wildstring');
describe('wildstring', function() {
it('should create an object and set a default wildcard', function() {

View File

@@ -1,20 +1,5 @@
'use strict';
var wildstring = (function (name, definition) {
if (typeof(module) !== 'undefined') {
module.exports = definition();
}
else if (typeof(define) === 'function' && 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 '*'
@@ -22,8 +7,7 @@ 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
@@ -32,7 +16,7 @@ var wildstring = {
* @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) {
checkRollbackStrings: function (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;
@@ -72,7 +56,7 @@ function checkRollbackStrings (rollbackStrings, patternSubstrings) {
}
return false;
}
},
/**
* Check if a string matches a pattern
@@ -80,7 +64,7 @@ function checkRollbackStrings (rollbackStrings, patternSubstrings) {
* @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) {
match: function (pattern, string) {
// if there are no wildcards, must be exact
if (pattern.indexOf(wildstring.wildcard) === -1) {
return pattern === string;
@@ -112,7 +96,7 @@ wildstring.match = function (pattern, string) {
while (patternIndex < patternSubstrings.length) {
if (currentString.indexOf(patternSubstrings[patternIndex]) === -1) {
return checkRollbackStrings(rollbackStrings, patternSubstrings);
return wildstring.checkRollbackStrings(rollbackStrings, patternSubstrings);
}
// create a queue of strings to roll back and try again if we fail later
@@ -140,11 +124,11 @@ wildstring.match = function (pattern, string) {
return false;
}
return checkRollbackStrings(rollbackStrings, patternSubstrings);
return wildstring.checkRollbackStrings(rollbackStrings, patternSubstrings);
}
return true;
};
},
/**
* Replace wildcards in a pattern with strings (string interpolation)
@@ -157,7 +141,7 @@ wildstring.match = function (pattern, string) {
* @throws You need to pass both parameters
* @throws You need to pass the right types
*/
wildstring.replace = function (pattern, strings) {
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.');
}
@@ -185,7 +169,11 @@ wildstring.replace = function (pattern, strings) {
}
return result;
}
};
if (typeof(module) !== 'undefined') { module.exports = wildstring; }
if (typeof(angular) !== 'undefined') { angular.module('wildstring').factory = wildstring; }
if (typeof(define) !== 'undefined') { define([], wildstring); }
return wildstring;
}));