From 275feb204a01f2148e910a04e791082587c0571d Mon Sep 17 00:00:00 2001 From: Alexey Date: Sun, 1 Jul 2012 22:35:46 +0300 Subject: [PATCH] #51 improve index of for new browsers --- lib/routes.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/routes.js b/lib/routes.js index cb0a17e5..642ea994 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -96,6 +96,12 @@ return result; }, + smartIndexOf: function(array, item){ + if (Array.prototype.indexOf && array.indexOf === Array.prototype.indexOf) return array.indexOf(item); + for (var i = 0; i < array.length; i++) if (i in array && array[i] === item) return i; + return -1; + }, + build_path: function(required_parameters, optional_parts, route, args) { args = Array.prototype.slice.call(args); var opts = this.extract_options(required_parameters.length, args); @@ -104,12 +110,9 @@ } parameters = this.prepare_parameters(required_parameters, args, opts); - // Array#indexOf is not supported by IE <= 8 - for(var i = 0; i < optional_parts.length; i++) { - if (optional_parts[i] == "format") { - this.set_default_format(parameters); - break; - } + // Array#indexOf is not supported by IE <= 8, so we use custom method + if (Utils.smartIndexOf(optional_parts, 'format') !== -1) { + this.set_default_format(parameters); } var result = Utils.get_prefix(); var anchor = Utils.extract_anchor(parameters);