-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathstring.test.ts
111 lines (95 loc) · 5.32 KB
/
string.test.ts
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
102
103
104
105
106
107
108
109
110
111
import { describe, expect, test } from 'vitest';
import { isMatchingPattern, stringMatchesSomePattern, truncate } from '../../src/utils-hoist/string';
describe('truncate()', () => {
test('it works as expected', () => {
expect(truncate('lolol', 3)).toEqual('lol...');
expect(truncate('lolol', 10)).toEqual('lolol');
expect(truncate('1'.repeat(1000), 300)).toHaveLength(303);
expect(truncate(new Array(1000).join('f'), 0)).toEqual(new Array(1000).join('f'));
expect(truncate(new Array(1000).join('f'), 0)).toEqual(new Array(1000).join('f'));
});
test('should bail out as an identity function when given non-string value', () => {
expect(truncate(null as any, 3)).toEqual(null);
expect(truncate(undefined as any, 3)).toEqual(undefined);
expect(truncate({} as any, 3)).toEqual({});
expect(truncate([] as any, 3)).toEqual([]);
});
});
describe('isMatchingPattern()', () => {
test('match using string substring if `requireExactStringMatch` not given', () => {
expect(isMatchingPattern('foobar', 'foobar')).toEqual(true);
expect(isMatchingPattern('foobar', 'foo')).toEqual(true);
expect(isMatchingPattern('foobar', 'bar')).toEqual(true);
expect(isMatchingPattern('foobar', 'nope')).toEqual(false);
});
test('match using string substring if `requireExactStringMatch` is `false`', () => {
expect(isMatchingPattern('foobar', 'foobar', false)).toEqual(true);
expect(isMatchingPattern('foobar', 'foo', false)).toEqual(true);
expect(isMatchingPattern('foobar', 'bar', false)).toEqual(true);
expect(isMatchingPattern('foobar', 'nope', false)).toEqual(false);
});
test('match using exact string match if `requireExactStringMatch` is `true`', () => {
expect(isMatchingPattern('foobar', 'foobar', true)).toEqual(true);
expect(isMatchingPattern('foobar', 'foo', true)).toEqual(false);
expect(isMatchingPattern('foobar', 'nope', true)).toEqual(false);
});
test('matches when `value` contains `pattern` but not vice-versa', () => {
expect(isMatchingPattern('foobar', 'foo')).toEqual(true);
expect(isMatchingPattern('foobar', 'foobarbaz')).toEqual(false);
});
test('match using regexp test', () => {
expect(isMatchingPattern('foobar', /^foo/)).toEqual(true);
expect(isMatchingPattern('foobar', /foo/)).toEqual(true);
expect(isMatchingPattern('foobar', /b.{1}r/)).toEqual(true);
expect(isMatchingPattern('foobar', /^foo$/)).toEqual(false);
});
test('should match empty pattern as true', () => {
expect(isMatchingPattern('foo', '')).toEqual(true);
expect(isMatchingPattern('bar', '')).toEqual(true);
expect(isMatchingPattern('', '')).toEqual(true);
});
test('should bail out with false when given non-string value', () => {
expect(isMatchingPattern(null as any, 'foo')).toEqual(false);
expect(isMatchingPattern(undefined as any, 'foo')).toEqual(false);
expect(isMatchingPattern({} as any, 'foo')).toEqual(false);
expect(isMatchingPattern([] as any, 'foo')).toEqual(false);
});
});
describe('stringMatchesSomePattern()', () => {
test('match using string substring if `requireExactStringMatch` not given', () => {
expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'])).toEqual(true);
expect(stringMatchesSomePattern('foobar', ['foo', 'nope'])).toEqual(true);
expect(stringMatchesSomePattern('foobar', ['baz', 'nope'])).toEqual(false);
});
test('match using string substring if `requireExactStringMatch` is `false`', () => {
expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'], false)).toEqual(true);
expect(stringMatchesSomePattern('foobar', ['foo', 'nope'], false)).toEqual(true);
expect(stringMatchesSomePattern('foobar', ['baz', 'nope'], false)).toEqual(false);
});
test('match using exact string match if `requireExactStringMatch` is `true`', () => {
expect(stringMatchesSomePattern('foobar', ['foobar', 'nope'], true)).toEqual(true);
expect(stringMatchesSomePattern('foobar', ['foo', 'nope'], true)).toEqual(false);
expect(stringMatchesSomePattern('foobar', ['baz', 'nope'], true)).toEqual(false);
});
test('matches when `testString` contains a pattern but not vice-versa', () => {
expect(stringMatchesSomePattern('foobar', ['foo', 'nope'])).toEqual(true);
expect(stringMatchesSomePattern('foobar', ['foobarbaz', 'nope'])).toEqual(false);
});
test('match using regexp test', () => {
expect(stringMatchesSomePattern('foobar', [/^foo/, 'nope'])).toEqual(true);
expect(stringMatchesSomePattern('foobar', [/foo/, 'nope'])).toEqual(true);
expect(stringMatchesSomePattern('foobar', [/b.{1}r/, 'nope'])).toEqual(true);
expect(stringMatchesSomePattern('foobar', [/^foo$/, 'nope'])).toEqual(false);
});
test('should match empty pattern as true', () => {
expect(stringMatchesSomePattern('foo', ['', 'nope'])).toEqual(true);
expect(stringMatchesSomePattern('bar', ['', 'nope'])).toEqual(true);
expect(stringMatchesSomePattern('', ['', 'nope'])).toEqual(true);
});
test('should bail out with false when given non-string value', () => {
expect(stringMatchesSomePattern(null as any, ['foo', 'nope'])).toEqual(false);
expect(stringMatchesSomePattern(undefined as any, ['foo', 'nope'])).toEqual(false);
expect(stringMatchesSomePattern({} as any, ['foo', 'nope'])).toEqual(false);
expect(stringMatchesSomePattern([] as any, ['foo', 'nope'])).toEqual(false);
});
});