From 6c1e82ebd2a434beefd68a089a362ebc040effff Mon Sep 17 00:00:00 2001 From: garrypas Date: Fri, 2 Jan 2015 16:45:40 +0000 Subject: [PATCH] Replaced some string literals with variables. I think this might shave a little off the minified file size --- lib/sammy.js | 63 ++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/lib/sammy.js b/lib/sammy.js index b0ff981c..9cd2e884 100644 --- a/lib/sammy.js +++ b/lib/sammy.js @@ -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 @@ -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); @@ -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); }); @@ -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); @@ -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]); @@ -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 @@ -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); @@ -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'), @@ -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; @@ -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 @@ -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]; @@ -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)); @@ -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 @@ -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]; @@ -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` @@ -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; }, @@ -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()); }, @@ -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); @@ -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) { @@ -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 { @@ -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; } @@ -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); @@ -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; }); @@ -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); },