• 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

94.37
/lib/permalink.ts
1
import escapeRegExp from './escape_regexp.js';
1✔
2

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

1✔
5
interface Options {
1✔
6
  segments?: {
1✔
7
    [key: string]: RegExp | string;
1✔
8
  }
1✔
9
}
1✔
10

1✔
11
class Permalink {
1✔
12
  rule: string;
7✔
13
  regex: RegExp;
7✔
14
  params: string[];
7✔
15

7✔
16
  constructor(rule: string, options: Options = {}) {
7✔
17
    if (!rule) { throw new TypeError('rule is required!'); }
7✔
18
    const segments = options.segments || {};
7✔
19
    const params = [];
7✔
20
    const regex = escapeRegExp(rule)
7✔
21
      .replace(rParam, (match, name) => {
7✔
22
        params.push(name);
18✔
23
        if (Object.prototype.hasOwnProperty.call(segments, name)) {
18✔
24
          const segment = segments[name];
6✔
25
          if (segment instanceof RegExp) {
6✔
26
            return segment.source;
3✔
27
          }
3✔
28
          return segment;
3✔
29
        }
3✔
30
        return '(.+?)';
12✔
31
      });
7✔
32
    this.rule = rule;
7✔
33
    this.regex = new RegExp(`^${regex}$`);
7✔
34
    this.params = params;
7✔
35
  }
7✔
36

7✔
37
  test(str: string) {
7✔
38
    return this.regex.test(str);
2✔
39
  }
2✔
40

7✔
41
  parse(str: string) {
7✔
42
    const match = str.match(this.regex);
2✔
43
    const { params } = this;
2✔
44
    const result = {};
2✔
45
    if (!match) { return; }
2✔
46
    for (let i = 1, len = match.length; i < len; i++) {
2✔
47
      result[params[i - 1]] = match[i];
4✔
48
    }
4✔
49
    return result;
1✔
50
  }
1✔
51

7✔
52
  stringify(data) {
7✔
53
    return this.rule.replace(rParam, (match, name) => {
3✔
54
      const descriptor = Object.getOwnPropertyDescriptor(data, name);
6✔
55
      if (descriptor && typeof descriptor.get === 'function') {
6✔
56
        throw new Error('Invalid permalink setting!');
2✔
57
      }
2✔
58
      return data[name];
4✔
59
    });
3✔
60
  }
3✔
61
}
7✔
62

1✔
63

1✔
64
// For ESM compatibility
1✔
65
export default Permalink;
1✔
66
// For CommonJS compatibility
1✔
67
if (typeof module !== 'undefined' && typeof module.exports === 'object' && module.exports !== null) {
1!
NEW
68
  module.exports = Permalink;
×
NEW
69
  // For ESM compatibility
×
NEW
70
  module.exports.default = Permalink;
×
NEW
71
}
×
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