Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced some string literals with variables. #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions lib/sammy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
PATH_REPLACER = "([^\/]+)",
PATH_NAME_MATCHER = /:([\w\d]+)/g,
QUERY_STRING_MATCHER = /\?([^#]*)?$/,
UNDEFINED = "undefined",
GET = "get",
POST = "post",
PUT = "put",
DELETE = "delete",
// mainly for making `arguments` an Array
_makeArray = function(nonarray) { return Array.prototype.slice.call(nonarray); },
// borrowed from jQuery
Expand Down Expand Up @@ -106,7 +111,7 @@
});
};

if (typeof window.console != 'undefined') {
if (typeof window.console != UNDEFINED) {
if (typeof window.console.log === 'function' && _isFunction(window.console.log.apply)) {
Sammy.addLogger(function() {
window.console.log.apply(window.console, arguments);
Expand All @@ -116,7 +121,7 @@
window.console.log(arguments);
});
}
} else if (typeof console != 'undefined') {
} else if (typeof console != UNDEFINED) {
Sammy.addLogger(function() {
console.log.apply(console, arguments);
});
Expand Down Expand Up @@ -305,7 +310,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
}(this);

if (hostname == window.location.hostname &&
app.lookupRoute('get', full_path) &&
app.lookupRoute(GET, full_path) &&
Sammy.targetIsThisWindow(e, 'a')) {
e.preventDefault();
proxy.setLocation(full_path);
Expand Down Expand Up @@ -363,7 +368,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
if (!every) { every = 10; }
var hashCheck = function() {
var current_location = proxy.getLocation();
if (typeof Sammy.DefaultLocationProxy._last_location == 'undefined' ||
if (typeof Sammy.DefaultLocationProxy._last_location == UNDEFINED ||
current_location != Sammy.DefaultLocationProxy._last_location) {
window.setTimeout(function() {
$(window).trigger('hashchange', [true]);
Expand Down Expand Up @@ -410,7 +415,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
Sammy.Application.prototype = $.extend({}, Sammy.Object.prototype, {

// the four route verbs
ROUTE_VERBS: ['get','post','put','delete'],
ROUTE_VERBS: [GET,POST,PUT,DELETE],

// An array of the default events triggered by the
// application during its lifecycle
Expand Down Expand Up @@ -514,7 +519,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
}
plugin.apply(this, args);
} catch(e) {
if (typeof plugin === 'undefined') {
if (typeof plugin === UNDEFINED) {
this.error("Plugin Error: called use() but plugin (" + plugin_name.toString() + ") is not defined", e);
} else if (!_isFunction(plugin)) {
this.error("Plugin Error: called use() but '" + plugin_name.toString() + "' is not a function", e);
Expand Down Expand Up @@ -628,17 +633,17 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
return this;
},

// Alias for route('get', ...)
get: _routeWrapper('get'),
// Alias for route(GET, ...)
get: _routeWrapper(GET),

// Alias for route('post', ...)
post: _routeWrapper('post'),
post: _routeWrapper(POST),

// Alias for route('put', ...)
put: _routeWrapper('put'),
put: _routeWrapper(PUT),

// Alias for route('delete', ...)
del: _routeWrapper('delete'),
del: _routeWrapper(DELETE),

// Alias for route('any', ...)
any: _routeWrapper('any'),
Expand Down Expand Up @@ -685,7 +690,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
var app = this;
// build the callback
// if the arity is 2, callback is the second argument
if (typeof callback == 'undefined') { callback = data; }
if (typeof callback == UNDEFINED) { callback = data; }
var listener_callback = function() {
// pull off the context from the arguments to the callback
var e, context, data;
Expand Down Expand Up @@ -983,7 +988,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
this._running = true;
// set last location
this.last_location = null;
if (!(/\#(.+)/.test(this.getLocation())) && typeof start_url != 'undefined') {
if (!(/\#(.+)/.test(this.getLocation())) && typeof start_url != UNDEFINED) {
this.setLocation(start_url);
}
// check url
Expand Down Expand Up @@ -1067,7 +1072,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
// if a matching route can be found within the current defined set.
lookupRoute: function(verb, path) {
var app = this, routed = false, i = 0, l, route;
if (typeof this.routes[verb] != 'undefined') {
if (typeof this.routes[verb] != UNDEFINED) {
l = this.routes[verb].length;
for (; i < l; i++) {
route = this.routes[verb][i];
Expand Down Expand Up @@ -1117,7 +1122,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
}

this.trigger('run-route', {verb: verb, path: path, params: params});
if (typeof params == 'undefined') { params = {}; }
if (typeof params == UNDEFINED) { params = {}; }

$.extend(params, this._parseQueryString(path));

Expand Down Expand Up @@ -1251,7 +1256,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
if (typeof options === 'string' || _isRegExp(options)) {
options = {path: options};
}
if (typeof positive === 'undefined') {
if (typeof positive === UNDEFINED) {
positive = true;
}
// empty options always match
Expand Down Expand Up @@ -1342,7 +1347,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
// `Sammy.Cache` and `Sammy.Storage` so can easily be replaced with
// a persistent storage that lasts beyond the current request.
templateCache: function(key, value) {
if (typeof value != 'undefined') {
if (typeof value != UNDEFINED) {
return _template_cache[key] = value;
} else {
return _template_cache[key];
Expand All @@ -1359,7 +1364,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
// 404 behavior (i.e redirecting to / or showing a warning)
notFound: function(verb, path) {
var ret = this.error(['404 Not Found', verb, path].join(' '));
return (verb === 'get') ? ret : true;
return (verb === GET) ? ret : true;
},

// The base error handler takes a string `message` and an `Error`
Expand All @@ -1384,11 +1389,11 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
// get current location
location = this.getLocation();
// compare to see if hash has changed
if (!this.last_location || this.last_location[0] != 'get' || this.last_location[1] != location) {
if (!this.last_location || this.last_location[0] != GET || this.last_location[1] != location) {
// reset last location
this.last_location = ['get', location];
this.last_location = [GET, location];
// lookup route for current hash
returned = this.runRoute('get', location);
returned = this.runRoute(GET, location);
}
return returned;
},
Expand All @@ -1398,7 +1403,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
$_method = $form.find('input[name="_method"]');
if ($_method.length > 0) { verb = $_method.val(); }
if (!verb) { verb = $form[0].getAttribute('method'); }
if (!verb || verb === '') { verb = 'get'; }
if (!verb || verb === '') { verb = GET; }
return $.trim(verb.toString().toLowerCase());
},

Expand All @@ -1413,7 +1418,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
this.log('_checkFormSubmission', $form, path, verb);
}

if (verb === 'get') {
if (verb === GET) {
params = this._serializeFormParams($form);
if (params !== '') { path += '?' + params; }
this.setLocation(path);
Expand All @@ -1422,7 +1427,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
params = $.extend({}, this._parseFormParams($form));
returned = this.runRoute(verb, path, params, form.get(0));
}
return (typeof returned == 'undefined') ? false : returned;
return (typeof returned == UNDEFINED) ? false : returned;
},

_serializeFormParams: function($form) {
Expand Down Expand Up @@ -1467,7 +1472,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
},

_parseParamPair: function(params, key, value) {
if (typeof params[key] !== 'undefined') {
if (typeof params[key] !== UNDEFINED) {
if (_isArray(params[key])) {
params[key].push(value);
} else {
Expand Down Expand Up @@ -1604,7 +1609,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
// See `wait()` for an example.
next: function(content) {
this.waiting = false;
if (typeof content !== 'undefined') {
if (typeof content !== UNDEFINED) {
this.previous_content = this.content;
this.content = content;
}
Expand Down Expand Up @@ -1664,7 +1669,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
url: location,
data: {},
dataType: is_json ? 'json' : 'text',
type: 'get',
type: GET,
success: function(data) {
if (should_cache) {
context.event_context.app.templateCache(location, data);
Expand Down Expand Up @@ -1916,7 +1921,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
// is sent as `{content: content}`
trigger: function(name, data) {
return this.then(function(content) {
if (typeof data == 'undefined') { data = {content: content}; }
if (typeof data == UNDEFINED) { data = {content: content}; }
this.event_context.trigger(name, data);
return content;
});
Expand Down Expand Up @@ -2119,7 +2124,7 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {

// Triggers events on `app` within the current context.
trigger: function(name, data) {
if (typeof data == 'undefined') { data = {}; }
if (typeof data == UNDEFINED) { data = {}; }
if (!data.context) { data.context = this; }
return this.app.trigger(name, data);
},
Expand Down