File size: 2,962 Bytes
b498cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"use strict";

describe("matchAt", function () {

  var matchAt;

  beforeEach(function () {
    matchAt = require("../matchAt.js");
  });

  it("matches a simple regex", function () {
    expect(matchAt(/l/, "hello", 0)).toBe(null);
    expect(matchAt(/l/, "hello", 1)).toBe(null);
    expect(matchAt(/l/, "hello", 4)).toBe(null);
    expect(matchAt(/l/, "hello", 5)).toBe(null);

    var match = matchAt(/l/, "hello", 2);
    expect(Array.isArray(match)).toBe(true);
    expect(match.index).toBe(2);
    expect(match.input).toBe("hello");
    expect(match[0]).toBe("l");
    expect(match[1]).toBe(undefined);
    expect(match.length).toBe(1);

    var match = matchAt(/l/, "hello", 3);
    expect(Array.isArray(match)).toBe(true);
    expect(match.index).toBe(3);
    expect(match.input).toBe("hello");
    expect(match[0]).toBe("l");
    expect(match[1]).toBe(undefined);
    expect(match.length).toBe(1);
  });

  it("matches a zero-length regex", function () {
    expect(matchAt(/(?=l)/, "hello", 0)).toBe(null);
    expect(matchAt(/(?=l)/, "hello", 1)).toBe(null);
    expect(matchAt(/(?=l)/, "hello", 4)).toBe(null);
    expect(matchAt(/(?=l)/, "hello", 5)).toBe(null);

    var match = matchAt(/(?=l)/, "hello", 2);
    expect(Array.isArray(match)).toBe(true);
    expect(match.index).toBe(2);
    expect(match.input).toBe("hello");
    expect(match[0]).toBe("");
    expect(match[1]).toBe(undefined);
    expect(match.length).toBe(1);

    var match = matchAt(/(?=l)/, "hello", 3);
    expect(Array.isArray(match)).toBe(true);
    expect(match.index).toBe(3);
    expect(match.input).toBe("hello");
    expect(match[0]).toBe("");
    expect(match[1]).toBe(undefined);
    expect(match.length).toBe(1);
  });

  it("matches a regex with capturing groups", function () {
    expect(matchAt(/(l)(l)?/, "hello", 0)).toBe(null);
    expect(matchAt(/(l)(l)?/, "hello", 1)).toBe(null);
    expect(matchAt(/(l)(l)?/, "hello", 4)).toBe(null);
    expect(matchAt(/(l)(l)?/, "hello", 5)).toBe(null);

    var match = matchAt(/(l)(l)?/, "hello", 2);
    expect(Array.isArray(match)).toBe(true);
    expect(match.index).toBe(2);
    expect(match.input).toBe("hello");
    expect(match[0]).toBe("ll");
    expect(match[1]).toBe("l");
    expect(match[2]).toBe("l");
    expect(match.length).toBe(3);

    var match = matchAt(/(l)(l)?/, "hello", 3);
    expect(Array.isArray(match)).toBe(true);
    expect(match.index).toBe(3);
    expect(match.input).toBe("hello");
    expect(match[0]).toBe("l");
    expect(match[1]).toBe("l");
    expect(match[2]).toBe(undefined);
    expect(match.length).toBe(3);
  });

  it("copies flags over", function () {
    expect(matchAt(/L/i, "hello", 0)).toBe(null);
    expect(matchAt(/L/i, "hello", 1)).toBe(null);
    expect(matchAt(/L/i, "hello", 2)).not.toBe(null);
    expect(matchAt(/L/i, "hello", 3)).not.toBe(null);
    expect(matchAt(/L/i, "hello", 4)).toBe(null);
    expect(matchAt(/L/i, "hello", 5)).toBe(null);
  });
});