• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hexojs / hexo-util / 16649341003

31 Jul 2025 12:44PM UTC coverage: 71.629% (-25.2%) from 96.875%
16649341003

Pull #426

github

web-flow
Merge ba0ee12e7 into d497bc760
Pull Request #426: build: support both ESM and CommonJS

730 of 1049 branches covered (69.59%)

1439 of 2000 new or added lines in 41 files covered. (71.95%)

6 existing lines in 1 file now uncovered.

5196 of 7254 relevant lines covered (71.63%)

68.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.06
/lib/pattern.ts
1
import escapeRegExp from './escape_regexp.js';
1✔
2

1✔
3
const rParam = /([:*])([\w?]*)?/g;
1✔
4

1✔
5
class Pattern {
1✔
6
  match: (str: string) => unknown;
9✔
7

9✔
8
  constructor(rule: Pattern | ((str: string) => unknown) | RegExp | string) {
9✔
9
    if (rule instanceof Pattern) {
9✔
10
      return rule;
1✔
11
    } else if (typeof rule === 'function') {
9✔
12
      this.match = rule;
1✔
13
    } else if (rule instanceof RegExp) {
8✔
14
      this.match = regexFilter(rule);
1✔
15
    } else if (typeof rule === 'string') {
7✔
16
      this.match = stringFilter(rule);
5✔
17
    } else {
6✔
18
      throw new TypeError('rule must be a function, a string or a regular expression.');
1✔
19
    }
1✔
20
  }
9✔
21

9✔
22
  test(str: string) {
9✔
23
    return Boolean(this.match(str));
2✔
24
  }
2✔
25
}
9✔
26

1✔
27
function regexFilter(rule: RegExp) {
1✔
28
  return (str: string) => str.match(rule);
1✔
29
}
1✔
30

1✔
31
function stringFilter(rule: string) {
5✔
32
  const params = [];
5✔
33

5✔
34
  const regex = escapeRegExp(rule)
5✔
35
    .replace(/\\([*?])/g, '$1')
5✔
36
    .replace(rParam, (match, operator, name) => {
5✔
37
      let str = '';
5✔
38

5✔
39
      if (operator === '*') {
5✔
40
        str = '(.*)?';
1✔
41
      } else {
5✔
42
        str = '([^\\/]+)';
4✔
43
      }
4✔
44

5✔
45
      if (name) {
5✔
46
        if (name[name.length - 1] === '?') {
5✔
47
          name = name.slice(0, name.length - 1);
1✔
48
          str += '?';
1✔
49
        }
1✔
50

5✔
51
        params.push(name);
5✔
52
      }
5✔
53

5✔
54
      return str;
5✔
55
    });
5✔
56

5✔
57
  return (str: string) => {
5✔
58
    const match = str.match(regex);
6✔
59
    if (!match) return;
6✔
60

5✔
61
    const result = {};
5✔
62

5✔
63
    for (let i = 0, len = match.length; i < len; i++) {
6✔
64
      const name = params[i - 1];
10✔
65
      result[i] = match[i];
10✔
66
      if (name) result[name] = match[i];
10✔
67
    }
10✔
68

5✔
69
    return result;
5✔
70
  };
5✔
71
}
5✔
72

1✔
73

1✔
74
// For ESM compatibility
1✔
75
export default Pattern;
1✔
76
// For CommonJS compatibility
1✔
77
if (typeof module !== 'undefined' && typeof module.exports === 'object' && module.exports !== null) {
1!
NEW
78
  module.exports = Pattern;
×
NEW
79
  // For ESM compatibility
×
NEW
80
  module.exports.default = Pattern;
×
NEW
81
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc