2013-12-18 19 views
2

我有驗證下面的場景檢測功能,但功能使用的地圖和過濾方法。所以its not working in IE8。如何重寫這個功能,而無需使用地圖和過濾器。定製JavaScript驗證不使用地圖,過濾

var case1 = "stack(2),flow(2),over(4),temp(7)"; - true 
var case2 = "stack(2),flow(3),over(4),temp(k)"; - false 
var case3 = "stack(2),flow(2),over(4),temp(0"; - false 
var case4 = "stack(2),flow(2),over(,temp)"; - false 
var case5 = "stack(2),flow(2),over(4)temp(8)"; - false 
var case6 = "stack(1),flow(7),over,temp"; - true 
var case7 = "stack(1),flow(7),OVER,Temp"; - true 
var case8 = "stack(1),flow(7),over_r,temp_t"; - true 

JavaScript函數:

function validateChunk(s) 
{ 
    return !!s.match(/^[a-z]+(?:\(\d+\))?$/); 
} 

function filterValid(v) 
{ 
    return !v; 
} 

function testCases(str) 
{ 
    var chunks = str.split(","); 
    var validated = chunks.map(validateChunk); 
    return (0 === validated.filter(filterValid).length); 

} 

jsfiddle

回答

3

一種可能的方法是調用validateChunk功能在一個循環:

for (var i = 0, l = chunks.length; i < l; i++) { 
    if (! validateChunk(chunks[i])) { 
    return false; 
    } 
} 
return true; 

一點題外話,有完全的零感使用.match.test CLEA可以滿足要求(因爲你不必收集匹配結果)。所以我重寫這樣的功能:

function validateChunk(str) { 
    return /^[a-z]+(?:\(\d+\))?$/.test(str); 
} 

最後,不能不注意到,這一切的驗證可以用一個正則表達式來完成:

function testCases(str) 
{ 
    var pattern = /^[a-z]+(?:\(\d+\))?(?:,[a-z]+(?:\(\d+\))?)*$/ 
    return pattern.test(str); 
} 

其實,從你的描述來看,圖案應該是稍有不同:第一,_符號是有效的一個,並應被包括以字符類;第二,相同的類要麼包括A-Z範圍爲好,或代替圖案應給予/i modififer。 This demo包含所有這些更改。

+0

+1其實你的循環似乎比原來更好的解決方案。 – kapa

+0

很好,它的作品很好。同樣感謝簡化了這個方法。 – user2848031

3

可以使用polyfills這些方法 - 這些都會使他們即使在不支持它們的瀏覽器。我只是複製了來自MDN的polyfills。

Array.prototype.map填充工具:

if (!Array.prototype.map) 
{ 
    Array.prototype.map = function(fun /*, thisArg */) 
    { 
    "use strict"; 

    if (this === void 0 || this === null) 
     throw new TypeError(); 

    var t = Object(this); 
    var len = t.length >>> 0; 
    if (typeof fun !== "function") 
     throw new TypeError(); 

    var res = new Array(len); 
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0; 
    for (var i = 0; i < len; i++) 
    { 
     // NOTE: Absolute correctness would demand Object.defineProperty 
     //  be used. But this method is fairly new, and failure is 
     //  possible only if Object.prototype or Array.prototype 
     //  has a property |i| (very unlikely), so use a less-correct 
     //  but more portable alternative. 
     if (i in t) 
     res[i] = fun.call(thisArg, t[i], i, t); 
    } 

    return res; 
    }; 
} 

Array.prototype.filter填充工具:

if (!Array.prototype.filter) 
{ 
    Array.prototype.filter = function(fun /*, thisArg */) 
    { 
    "use strict"; 

    if (this === void 0 || this === null) 
     throw new TypeError(); 

    var t = Object(this); 
    var len = t.length >>> 0; 
    if (typeof fun != "function") 
     throw new TypeError(); 

    var res = []; 
    var thisArg = arguments.length >= 2 ? arguments[1] : void 0; 
    for (var i = 0; i < len; i++) 
    { 
     if (i in t) 
     { 
     var val = t[i]; 

     // NOTE: Technically this should Object.defineProperty at 
     //  the next index, as push can be affected by 
     //  properties on Object.prototype and Array.prototype. 
     //  But that method's new, and collisions should be 
     //  rare, so use the more-compatible alternative. 
     if (fun.call(thisp, val, i, t)) 
      res.push(val); 
     } 
    } 

    return res; 
    }; 
}