From aa9bea2e767cb2c6d11c1a81fccadfc2b853812c Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 19 Mar 2015 11:16:56 -0400 Subject: [PATCH] code review --- src/js/element-picker.js | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/js/element-picker.js b/src/js/element-picker.js index 4be6fbfff..601245d9f 100644 --- a/src/js/element-picker.js +++ b/src/js/element-picker.js @@ -237,19 +237,9 @@ var removeElements = function(elems) { var incrementalNetFilter = (function() { var lastHostname = ''; var lastNetFilter = ''; - var reTokenizer = /([^0-9a-z%*]+)([0-9a-z%]+|\*)/gi; + var reTokenizer = /[^0-9a-z%*]+|[0-9a-z%]+|\*/gi; var a = document.createElement('a'); - var tokenize = function(s) { - var out = []; - var match; - reTokenizer.lastIndex = 0; - while ( match = reTokenizer.exec(s) ) { - out.push(match[1], match[2]); - } - return out; - }; - var compute = function(to, out) { a.href= to; to = a.pathname + a.search; @@ -270,24 +260,30 @@ var incrementalNetFilter = (function() { // Related URLs lastHostname = a.host; - var fromTokens = tokenize(from); - var toTokens = tokenize(to); - var toIndex = 0, i; + var fromTokens = from.match(reTokenizer); + var toTokens = to.match(reTokenizer); + var toCount = toTokens.length, toIndex = 0; + var fromToken, pos; for ( var fromIndex = 0; fromIndex < fromTokens.length; fromIndex += 1 ) { - if ( fromTokens[fromIndex] === '*' ) { + fromToken = fromTokens[fromIndex]; + if ( fromToken === '*' ) { continue; } - i = toTokens.indexOf(fromTokens[fromIndex], toIndex); - if ( i === -1 ) { + pos = toTokens.indexOf(fromToken, toIndex); + if ( pos === -1 ) { fromTokens[fromIndex] = '*'; continue; } - if ( i !== toIndex ) { + if ( pos !== toIndex ) { fromTokens.splice(fromIndex, 0, '*'); fromIndex += 1; } - toIndex = i + 1; + toIndex = pos + 1; + if ( toIndex === toCount ) { + fromTokens = fromTokens.slice(0, fromIndex + 1); + break; + } } from = fromTokens.join('').replace(/\*\*+/g, '*'); if ( from !== '/*' ) {