Skip to content

Commit 17f74e1

Browse files
authored
added: [N4S] startsWith rule (#414)
1 parent b20aa66 commit 17f74e1

File tree

7 files changed

+84
-3
lines changed

7 files changed

+84
-3
lines changed

packages/n4s/src/rules/endsWith/spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('Tests isArray rule', () => {
2424
});
2525

2626
it('Should return false for a suffix longer than the word', () => {
27-
expect(endsWith(word, word + 'aaa')).toBe(false);
27+
expect(endsWith(word, word.repeat(2))).toBe(false);
2828
});
2929

3030
it('Should expose negativeForm property', () => {

packages/n4s/src/rules/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import matches from './matches';
2424
import numberEquals from './numberEquals';
2525
import shorterThan from './shorterThan';
2626
import shorterThanOrEquals from './shorterThanOrEquals';
27+
import startsWith from './startsWith';
2728

2829
const rules = {
2930
endsWith,
@@ -50,7 +51,8 @@ const rules = {
5051
matches,
5152
numberEquals,
5253
shorterThan,
53-
shorterThanOrEquals
54+
shorterThanOrEquals,
55+
startsWith
5456
};
5557

5658
export default extendRules(rules);

packages/n4s/src/rules/spec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const negativeRules = [
2727
'numberNotEquals',
2828
'isNotNull',
2929
'doesNotEndWith',
30+
'doesNotStartWith'
3031
];
3132

3233
const positiveRules = [
@@ -59,7 +60,8 @@ const positiveRules = [
5960
'shorterThan',
6061
'shorterThanOrEquals',
6162
'isNull',
62-
'endsWith'
63+
'endsWith',
64+
'startsWith'
6365
];
6466

6567
const allRules = [...new Set([...positiveRules, ...negativeRules])];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function startsWith(value, arg1) {
2+
return typeof value === 'string' && typeof arg1 === 'string' && value.startsWith(arg1);
3+
}
4+
5+
startsWith.negativeForm = 'doesNotStartWith';
6+
7+
export default startsWith;
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import startsWith from '.';
2+
3+
describe('Tests isArray rule', () => {
4+
const word = 'meow';
5+
const totallyDifferentWord = 'lorem';
6+
it('Should return true for the same word', () => {
7+
expect(startsWith(word, word)).toBe(true);
8+
});
9+
10+
it('Should return true for a prefix', () => {
11+
expect(startsWith(word, word.substring(0, word.length / 2))).toBe(true);
12+
});
13+
14+
it('Should return true for empty prefix', () => {
15+
expect(startsWith(word, '')).toBe(true);
16+
});
17+
18+
it('Should return false for a wrong prefix', () => {
19+
expect(startsWith(word, word.substring(1, word.length - 1))).toBe(false);
20+
});
21+
22+
it('Should return false for a prefix which is a totally different word', () => {
23+
expect(startsWith(word, totallyDifferentWord)).toBe(false);
24+
});
25+
26+
it('Should return false for a prefix longer than the word', () => {
27+
expect(startsWith(word, word.repeat(2))).toBe(false);
28+
});
29+
30+
it('Should expose negativeForm property', () => {
31+
expect(startsWith.negativeForm).toBe('doesNotStartWith');
32+
});
33+
});

packages/vest/docs/enforce.md

+35
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Enforce rules are functions that allow you to test your data against different c
6666
- [isEven](#iseven)
6767
- [endsWith](#endswith)
6868
- [doesNotEndWith](#doesnotendwith)
69+
- [startsWith](#startsWith)
70+
- [doesNotStartWith](#doesnotstartwith)
6971

7072
### equals
7173

@@ -1030,6 +1032,39 @@ enforce(true).endsWith(100);
10301032
Determines whether a string does not end with the characters of a specified string.
10311033
Reverse implementation of `endsWith`.
10321034

1035+
## startsWith
1036+
1037+
### Description
1038+
1039+
Determines whether a string starts with the characters of a specified string.
1040+
1041+
### Usage examples:
1042+
1043+
```js
1044+
enforce('aba').startsWith('ab');
1045+
enforce('some_string').startsWith('some_');
1046+
enforce('string with spaces').startsWith('string with s');
1047+
enforce('aaaa ').startsWith('aaaa ');
1048+
// passes
1049+
```
1050+
1051+
```js
1052+
enforce('for').startsWith('tor');
1053+
enforce('aaaab').startsWith('aab');
1054+
enforce('aa').startsWith('aaa');
1055+
enforce(42).startsWith('b');
1056+
enforce(42).startsWith(50);
1057+
enforce(true).startsWith(100);
1058+
// throws
1059+
```
1060+
1061+
## doesNotStartWith
1062+
1063+
### Description
1064+
1065+
Determines whether a string does not start with the characters of a specified string.
1066+
Reverse implementation of `startsWith`.
1067+
10331068
# Custom enforce rules
10341069

10351070
To make it easier to reuse logic across your application, sometimes you would want to encapsulate bits of logic in rules that you can use later on, for example, "what's considered a valid email".

packages/vest/src/typings/vest.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ export interface IEnforceRules<T = {}> {
136136
lt: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
137137
lte: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
138138
endsWith: (suffix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
139+
startsWith: (prefix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
139140
doesNotEndWith: (suffix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
141+
doesNotStartWith: (prefix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
140142
numberNotEquals: (
141143
expected: number | string
142144
) => IEnforceRules<T> & EnforceExtendMap<T>;

0 commit comments

Comments
 (0)