Frage

I have function for validation for check below scenarios, but function used map and filter methods. so its not working in IE8.How to rewrite this function without using map and filter.

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 :

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

War es hilfreich?

Lösung

One possible approach is to call validateChunk function in a loop:

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

As a sidenote, there's exactly zero sense to use .match when .test clearly suffices (as you don't have to collect the results of match). So I'd rewrite the function like this:

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

Finally, can't help noticing that all this validation can be done with a single regex:

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

Actually, judging from your description, the pattern should be slightly different: first, _ symbol is valid one and should be included to character class; second, the same class should either include A-Z range as well, or instead a pattern should be given /i modififer. This demo includes all these changes.

Andere Tipps

You can use polyfills for these methods - these will make them work even in browsers that don't support them. I simply copied the polyfills from MDN.

Array.prototype.map polyfill:

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 polyfill:

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;
  };
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top