forked from kpdecker/jsdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
101 lines (89 loc) · 4.36 KB
/
Copy pathstring.js
File metadata and controls
101 lines (89 loc) · 4.36 KB
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
91
92
93
94
95
96
97
98
99
100
101
import {longestCommonPrefix, longestCommonSuffix, replacePrefix, replaceSuffix, removePrefix, removeSuffix, maximumOverlap, leadingWs, trailingWs} from '../../libesm/util/string.js';
import {expect} from 'chai';
describe('#longestCommonPrefix', function() {
it('finds the longest common prefix', function() {
expect(longestCommonPrefix('food', 'foolish')).to.equal('foo');
expect(longestCommonPrefix('foolish', 'food')).to.equal('foo');
expect(longestCommonPrefix('foolish', 'foo')).to.equal('foo');
expect(longestCommonPrefix('foo', 'foolish')).to.equal('foo');
expect(longestCommonPrefix('foo', '')).to.equal('');
expect(longestCommonPrefix('', 'foo')).to.equal('');
expect(longestCommonPrefix('', '')).to.equal('');
expect(longestCommonPrefix('foo', 'bar')).to.equal('');
});
});
describe('#longestCommonSuffix', function() {
it('finds the longest common suffix', function() {
expect(longestCommonSuffix('bumpy', 'grumpy')).to.equal('umpy');
expect(longestCommonSuffix('grumpy', 'bumpy')).to.equal('umpy');
expect(longestCommonSuffix('grumpy', 'umpy')).to.equal('umpy');
expect(longestCommonSuffix('umpy', 'grumpy')).to.equal('umpy');
expect(longestCommonSuffix('foo', '')).to.equal('');
expect(longestCommonSuffix('', 'foo')).to.equal('');
expect(longestCommonSuffix('', '')).to.equal('');
expect(longestCommonSuffix('foo', 'bar')).to.equal('');
});
});
describe('#replacePrefix', function() {
it('replaces a prefix on a string with a different prefix', function() {
expect((replacePrefix('food', 'foo', 'gla'))).to.equal('glad');
expect((replacePrefix('food', '', 'good '))).to.equal('good food');
});
it("throws if the prefix to remove isn't present", function() {
// eslint-disable-next-line dot-notation
expect(() => replacePrefix('food', 'drin', 'goo')).to.throw();
});
});
describe('#replaceSuffix', function() {
it('replaces a suffix on a string with a different suffix', function() {
expect((replaceSuffix('bangle', 'gle', 'jo'))).to.equal('banjo');
expect((replaceSuffix('bun', '', 'gle'))).to.equal('bungle');
});
it("throws if the suffix to remove isn't present", function() {
// eslint-disable-next-line dot-notation
expect(() => replaceSuffix('food', 'ool', 'ondle')).to.throw();
});
});
describe('#removePrefix', function() {
it('removes a prefix', function() {
expect(removePrefix('inconceivable', 'in')).to.equal('conceivable');
expect(removePrefix('inconceivable', '')).to.equal('inconceivable');
expect(removePrefix('inconceivable', 'inconceivable')).to.equal('');
});
it("throws if the prefix to remove isn't present", function() {
// eslint-disable-next-line dot-notation
expect(() => removePrefix('food', 'dr')).to.throw();
});
});
describe('#removeSuffix', function() {
it('removes a suffix', function() {
expect(removeSuffix('counterfactual', 'factual')).to.equal('counter');
expect(removeSuffix('counterfactual', '')).to.equal('counterfactual');
expect(removeSuffix('counterfactual', 'counterfactual')).to.equal('');
});
it("throws if the suffix to remove isn't present", function() {
// eslint-disable-next-line dot-notation
expect(() => removeSuffix('food', 'dr')).to.throw();
});
});
describe('#maximumOverlap', function() {
it('finds the maximum overlap between the end of one string and the start of the other', function() {
expect(maximumOverlap('qwertyuiop', 'uiopasdfgh')).to.equal('uiop');
expect(maximumOverlap('qwertyuiop', 'qwertyuiop')).to.equal('qwertyuiop');
expect(maximumOverlap('qwertyuiop', 'asdfghjkl')).to.equal('');
expect(maximumOverlap('qwertyuiop', '')).to.equal('');
expect(maximumOverlap('uiopasdfgh', 'qwertyuiop')).to.equal('');
expect(maximumOverlap('x ', ' x')).to.equal(' ');
expect(maximumOverlap('', '')).to.equal('');
});
});
describe('leadingWs & trailingWs', function() {
it('returns leading/trailing whitespace (with diacritics on whitespace considered part of the whitespace iff we are in segmenter mode)', () => {
const segmenter = new Intl.Segmenter('en', { granularity: 'word' });
const text = '\t\u0300 foo bar\n baz qux\t \u0300 ';
expect(leadingWs(text)).to.equal('\t');
expect(leadingWs(text, segmenter)).to.equal('\t\u0300 ');
expect(trailingWs(text)).to.equal(' ');
expect(trailingWs(text, segmenter)).to.equal('\t \u0300 ');
});
});