UNPKG

6.87 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.StringPredicate = void 0;
4const is_1 = require("@sindresorhus/is");
5const valiDate = require("vali-date");
6const predicate_1 = require("./predicate");
7class StringPredicate extends predicate_1.Predicate {
8 /**
9 @hidden
10 */
11 constructor(options) {
12 super('string', options);
13 }
14 /**
15 Test a string to have a specific length.
16
17 @param length - The length of the string.
18 */
19 length(length) {
20 return this.addValidator({
21 message: (value, label) => `Expected ${label} to have length \`${length}\`, got \`${value}\``,
22 validator: value => value.length === length
23 });
24 }
25 /**
26 Test a string to have a minimum length.
27
28 @param length - The minimum length of the string.
29 */
30 minLength(length) {
31 return this.addValidator({
32 message: (value, label) => `Expected ${label} to have a minimum length of \`${length}\`, got \`${value}\``,
33 validator: value => value.length >= length,
34 negatedMessage: (value, label) => `Expected ${label} to have a maximum length of \`${length - 1}\`, got \`${value}\``
35 });
36 }
37 /**
38 Test a string to have a maximum length.
39
40 @param length - The maximum length of the string.
41 */
42 maxLength(length) {
43 return this.addValidator({
44 message: (value, label) => `Expected ${label} to have a maximum length of \`${length}\`, got \`${value}\``,
45 validator: value => value.length <= length,
46 negatedMessage: (value, label) => `Expected ${label} to have a minimum length of \`${length + 1}\`, got \`${value}\``
47 });
48 }
49 /**
50 Test a string against a regular expression.
51
52 @param regex - The regular expression to match the value with.
53 */
54 matches(regex) {
55 return this.addValidator({
56 message: (value, label) => `Expected ${label} to match \`${regex}\`, got \`${value}\``,
57 validator: value => regex.test(value)
58 });
59 }
60 /**
61 Test a string to start with a specific value.
62
63 @param searchString - The value that should be the start of the string.
64 */
65 startsWith(searchString) {
66 return this.addValidator({
67 message: (value, label) => `Expected ${label} to start with \`${searchString}\`, got \`${value}\``,
68 validator: value => value.startsWith(searchString)
69 });
70 }
71 /**
72 Test a string to end with a specific value.
73
74 @param searchString - The value that should be the end of the string.
75 */
76 endsWith(searchString) {
77 return this.addValidator({
78 message: (value, label) => `Expected ${label} to end with \`${searchString}\`, got \`${value}\``,
79 validator: value => value.endsWith(searchString)
80 });
81 }
82 /**
83 Test a string to include a specific value.
84
85 @param searchString - The value that should be included in the string.
86 */
87 includes(searchString) {
88 return this.addValidator({
89 message: (value, label) => `Expected ${label} to include \`${searchString}\`, got \`${value}\``,
90 validator: value => value.includes(searchString)
91 });
92 }
93 /**
94 Test if the string is an element of the provided list.
95
96 @param list - List of possible values.
97 */
98 oneOf(list) {
99 return this.addValidator({
100 message: (value, label) => {
101 let printedList = JSON.stringify(list);
102 if (list.length > 10) {
103 const overflow = list.length - 10;
104 printedList = JSON.stringify(list.slice(0, 10)).replace(/]$/, `,…+${overflow} more]`);
105 }
106 return `Expected ${label} to be one of \`${printedList}\`, got \`${value}\``;
107 },
108 validator: value => list.includes(value)
109 });
110 }
111 /**
112 Test a string to be empty.
113 */
114 get empty() {
115 return this.addValidator({
116 message: (value, label) => `Expected ${label} to be empty, got \`${value}\``,
117 validator: value => value === ''
118 });
119 }
120 /**
121 Test a string to be not empty.
122 */
123 get nonEmpty() {
124 return this.addValidator({
125 message: (_, label) => `Expected ${label} to not be empty`,
126 validator: value => value !== ''
127 });
128 }
129 /**
130 Test a string to be equal to a specified string.
131
132 @param expected - Expected value to match.
133 */
134 equals(expected) {
135 return this.addValidator({
136 message: (value, label) => `Expected ${label} to be equal to \`${expected}\`, got \`${value}\``,
137 validator: value => value === expected
138 });
139 }
140 /**
141 Test a string to be alphanumeric.
142 */
143 get alphanumeric() {
144 return this.addValidator({
145 message: (value, label) => `Expected ${label} to be alphanumeric, got \`${value}\``,
146 validator: value => /^[a-z\d]+$/i.test(value)
147 });
148 }
149 /**
150 Test a string to be alphabetical.
151 */
152 get alphabetical() {
153 return this.addValidator({
154 message: (value, label) => `Expected ${label} to be alphabetical, got \`${value}\``,
155 validator: value => /^[a-z]+$/gi.test(value)
156 });
157 }
158 /**
159 Test a string to be numeric.
160 */
161 get numeric() {
162 return this.addValidator({
163 message: (value, label) => `Expected ${label} to be numeric, got \`${value}\``,
164 validator: value => /^[+-]?\d+$/i.test(value)
165 });
166 }
167 /**
168 Test a string to be a valid date.
169 */
170 get date() {
171 return this.addValidator({
172 message: (value, label) => `Expected ${label} to be a date, got \`${value}\``,
173 validator: valiDate
174 });
175 }
176 /**
177 Test a non-empty string to be lowercase. Matching both alphabetical & numbers.
178 */
179 get lowercase() {
180 return this.addValidator({
181 message: (value, label) => `Expected ${label} to be lowercase, got \`${value}\``,
182 validator: value => value.trim() !== '' && value === value.toLowerCase()
183 });
184 }
185 /**
186 Test a non-empty string to be uppercase. Matching both alphabetical & numbers.
187 */
188 get uppercase() {
189 return this.addValidator({
190 message: (value, label) => `Expected ${label} to be uppercase, got \`${value}\``,
191 validator: value => value.trim() !== '' && value === value.toUpperCase()
192 });
193 }
194 /**
195 Test a string to be a valid URL.
196 */
197 get url() {
198 return this.addValidator({
199 message: (value, label) => `Expected ${label} to be a URL, got \`${value}\``,
200 validator: is_1.default.urlString
201 });
202 }
203}
204exports.StringPredicate = StringPredicate;