-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
strings.js
130 lines (120 loc) · 3.28 KB
/
strings.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* @alias toBeString
* @summary <code>expect(string).toBeString();</code>
*/
matchers.toBeString = function() {
return priv.is(this.actual, 'String');
};
/**
* @alias toBeEmptyString
* @summary <code>expect(string).toBeEmptyString();</code>
*/
matchers.toBeEmptyString = function() {
return this.actual === '';
};
/**
* @alias toBeNonEmptyString
* @summary <code>expect(string).toBeNonEmptyString();</code>
*/
matchers.toBeNonEmptyString = function() {
return matchers.toBeString.call(this) &&
this.actual.length > 0;
};
/**
* @alias toBeHtmlString
* @summary <code>expect(string).toBeHtmlString();</code>
*/
matchers.toBeHtmlString = function() {
// < start with opening tag "<"
// ( start group 1
// "[^"]*" allow string in "double quotes"
// | OR
// '[^']*' allow string in "single quotes"
// | OR
// [^'">] cant contains one single quotes, double quotes and ">"
// ) end group 1
// * 0 or more
// > end with closing tag ">"
return matchers.toBeString.call(this) &&
this.actual.search(/<("[^"]*"|'[^']*'|[^'">])*>/) !== -1;
};
/**
* @alias toBeJsonString
* @summary <code>expect(string).toBeJsonString();</code>
*/
matchers.toBeJsonString = function() {
var isParseable;
var json;
try {
json = JSON.parse(this.actual);
} catch (e) {
isParseable = false;
}
return isParseable !== false &&
json !== null;
};
/**
* @alias toBeWhitespace
* @summary <code>expect(string).toBeWhitespace();</code>
*/
matchers.toBeWhitespace = function() {
return matchers.toBeString.call(this) &&
this.actual.search(/\S/) === -1;
};
/**
* @alias toStartWith
* @summary <code>expect(string).toStartWith(expected:String);</code>
*/
matchers.toStartWith = function(expected) {
if (!matchers.toBeNonEmptyString.call(this) || !matchers.toBeNonEmptyString.call({
actual: expected
})) {
return false;
}
return this.actual.slice(0, expected.length) === expected;
};
/**
* @alias toEndWith
* @summary <code>expect(string).toEndWith(expected:String);</code>
*/
matchers.toEndWith = function(expected) {
if (!matchers.toBeNonEmptyString.call(this) || !matchers.toBeNonEmptyString.call({
actual: expected
})) {
return false;
}
return this.actual.slice(this.actual.length - expected.length, this.actual.length) === expected;
};
/**
* @alias toBeLongerThan
* @summary <code>expect(string).toBeLongerThan(other:String);</code>
*/
matchers.toBeLongerThan = function(other) {
return matchers.toBeString.call(this) &&
matchers.toBeString.call({
actual: other
}) &&
this.actual.length > other.length;
};
/**
* @alias toBeShorterThan
* @summary <code>expect(string).toBeShorterThan(other:String);</code>
*/
matchers.toBeShorterThan = function(other) {
return matchers.toBeString.call(this) &&
matchers.toBeString.call({
actual: other
}) &&
this.actual.length < other.length;
};
/**
* @alias toBeSameLengthAs
* @summary <code>expect(string).toBeSameLengthAs(other:String);</code>
*/
matchers.toBeSameLengthAs = function(other) {
return matchers.toBeString.call(this) &&
matchers.toBeString.call({
actual: other
}) &&
this.actual.length === other.length;
};