Projekt

Obecné

Profil

Stáhnout (2.18 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
'use strict';
2
3
var micromatch = require('micromatch');
4
var normalize = require('normalize-path');
5
var path = require('path'); // required for tests.
6
var arrify = function(a) { return a == null ? [] : (Array.isArray(a) ? a : [a]); };
7
8
var anymatch = function(criteria, value, returnIndex, startIndex, endIndex) {
9
  criteria = arrify(criteria);
10
  value = arrify(value);
11
  if (arguments.length === 1) {
12
    return anymatch.bind(null, criteria.map(function(criterion) {
13
      return typeof criterion === 'string' && criterion[0] !== '!' ?
14
        micromatch.matcher(criterion) : criterion;
15
    }));
16
  }
17
  startIndex = startIndex || 0;
18
  var string = value[0];
19
  var altString, altValue;
20
  var matched = false;
21
  var matchIndex = -1;
22
  function testCriteria(criterion, index) {
23
    var result;
24
    switch (Object.prototype.toString.call(criterion)) {
25
    case '[object String]':
26
      result = string === criterion || altString && altString === criterion;
27
      result = result || micromatch.isMatch(string, criterion);
28
      break;
29
    case '[object RegExp]':
30
      result = criterion.test(string) || altString && criterion.test(altString);
31
      break;
32
    case '[object Function]':
33
      result = criterion.apply(null, value);
34
      result = result || altValue && criterion.apply(null, altValue);
35
      break;
36
    default:
37
      result = false;
38
    }
39
    if (result) {
40
      matchIndex = index + startIndex;
41
    }
42
    return result;
43
  }
44
  var crit = criteria;
45
  var negGlobs = crit.reduce(function(arr, criterion, index) {
46
    if (typeof criterion === 'string' && criterion[0] === '!') {
47
      if (crit === criteria) {
48
        // make a copy before modifying
49
        crit = crit.slice();
50
      }
51
      crit[index] = null;
52
      arr.push(criterion.substr(1));
53
    }
54
    return arr;
55
  }, []);
56
  if (!negGlobs.length || !micromatch.any(string, negGlobs)) {
57
    if (path.sep === '\\' && typeof string === 'string') {
58
      altString = normalize(string);
59
      altString = altString === string ? null : altString;
60
      if (altString) altValue = [altString].concat(value.slice(1));
61
    }
62
    matched = crit.slice(startIndex, endIndex).some(testCriteria);
63
  }
64
  return returnIndex === true ? matchIndex : matched;
65
};
66
67
module.exports = anymatch;