]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/atom-space-pen-views/node_modules/fuzzaldrin/lib/matcher.js
57edff4c1539829ada26dee0caba313fcbedc553
[rbdr/dotfiles] / atom / packages / ex-mode / node_modules / atom-space-pen-views / node_modules / fuzzaldrin / lib / matcher.js
1 (function() {
2 var PathSeparator;
3
4 PathSeparator = require('path').sep;
5
6 exports.basenameMatch = function(string, query) {
7 var base, index, lastCharacter, slashCount;
8 index = string.length - 1;
9 while (string[index] === PathSeparator) {
10 index--;
11 }
12 slashCount = 0;
13 lastCharacter = index;
14 base = null;
15 while (index >= 0) {
16 if (string[index] === PathSeparator) {
17 slashCount++;
18 if (base == null) {
19 base = string.substring(index + 1, lastCharacter + 1);
20 }
21 } else if (index === 0) {
22 if (lastCharacter < string.length - 1) {
23 if (base == null) {
24 base = string.substring(0, lastCharacter + 1);
25 }
26 } else {
27 if (base == null) {
28 base = string;
29 }
30 }
31 }
32 index--;
33 }
34 return exports.match(base, query, string.length - base.length);
35 };
36
37 exports.match = function(string, query, stringOffset) {
38 var character, indexInQuery, indexInString, lowerCaseIndex, matches, minIndex, queryLength, stringLength, upperCaseIndex, _i, _ref, _results;
39 if (stringOffset == null) {
40 stringOffset = 0;
41 }
42 if (string === query) {
43 return (function() {
44 _results = [];
45 for (var _i = stringOffset, _ref = stringOffset + string.length; stringOffset <= _ref ? _i < _ref : _i > _ref; stringOffset <= _ref ? _i++ : _i--){ _results.push(_i); }
46 return _results;
47 }).apply(this);
48 }
49 queryLength = query.length;
50 stringLength = string.length;
51 indexInQuery = 0;
52 indexInString = 0;
53 matches = [];
54 while (indexInQuery < queryLength) {
55 character = query[indexInQuery++];
56 lowerCaseIndex = string.indexOf(character.toLowerCase());
57 upperCaseIndex = string.indexOf(character.toUpperCase());
58 minIndex = Math.min(lowerCaseIndex, upperCaseIndex);
59 if (minIndex === -1) {
60 minIndex = Math.max(lowerCaseIndex, upperCaseIndex);
61 }
62 indexInString = minIndex;
63 if (indexInString === -1) {
64 return [];
65 }
66 matches.push(stringOffset + indexInString);
67 stringOffset += indexInString + 1;
68 string = string.substring(indexInString + 1, stringLength);
69 }
70 return matches;
71 };
72
73 }).call(this);