Skip to content

Commit

Permalink
added: [N4S] startsWith rule (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
syncush committed Sep 26, 2020
1 parent b20aa66 commit 17f74e1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/n4s/src/rules/endsWith/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Tests isArray rule', () => {
});

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

it('Should expose negativeForm property', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/n4s/src/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import matches from './matches';
import numberEquals from './numberEquals';
import shorterThan from './shorterThan';
import shorterThanOrEquals from './shorterThanOrEquals';
import startsWith from './startsWith';

const rules = {
endsWith,
Expand All @@ -50,7 +51,8 @@ const rules = {
matches,
numberEquals,
shorterThan,
shorterThanOrEquals
shorterThanOrEquals,
startsWith
};

export default extendRules(rules);
4 changes: 3 additions & 1 deletion packages/n4s/src/rules/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const negativeRules = [
'numberNotEquals',
'isNotNull',
'doesNotEndWith',
'doesNotStartWith'
];

const positiveRules = [
Expand Down Expand Up @@ -59,7 +60,8 @@ const positiveRules = [
'shorterThan',
'shorterThanOrEquals',
'isNull',
'endsWith'
'endsWith',
'startsWith'
];

const allRules = [...new Set([...positiveRules, ...negativeRules])];
7 changes: 7 additions & 0 deletions packages/n4s/src/rules/startsWith/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function startsWith(value, arg1) {
return typeof value === 'string' && typeof arg1 === 'string' && value.startsWith(arg1);
}

startsWith.negativeForm = 'doesNotStartWith';

export default startsWith;
33 changes: 33 additions & 0 deletions packages/n4s/src/rules/startsWith/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import startsWith from '.';

describe('Tests isArray rule', () => {
const word = 'meow';
const totallyDifferentWord = 'lorem';
it('Should return true for the same word', () => {
expect(startsWith(word, word)).toBe(true);
});

it('Should return true for a prefix', () => {
expect(startsWith(word, word.substring(0, word.length / 2))).toBe(true);
});

it('Should return true for empty prefix', () => {
expect(startsWith(word, '')).toBe(true);
});

it('Should return false for a wrong prefix', () => {
expect(startsWith(word, word.substring(1, word.length - 1))).toBe(false);
});

it('Should return false for a prefix which is a totally different word', () => {
expect(startsWith(word, totallyDifferentWord)).toBe(false);
});

it('Should return false for a prefix longer than the word', () => {
expect(startsWith(word, word.repeat(2))).toBe(false);
});

it('Should expose negativeForm property', () => {
expect(startsWith.negativeForm).toBe('doesNotStartWith');
});
});
35 changes: 35 additions & 0 deletions packages/vest/docs/enforce.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Enforce rules are functions that allow you to test your data against different c
- [isEven](#iseven)
- [endsWith](#endswith)
- [doesNotEndWith](#doesnotendwith)
- [startsWith](#startsWith)
- [doesNotStartWith](#doesnotstartwith)

### equals

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

## startsWith

### Description

Determines whether a string starts with the characters of a specified string.

### Usage examples:

```js
enforce('aba').startsWith('ab');
enforce('some_string').startsWith('some_');
enforce('string with spaces').startsWith('string with s');
enforce('aaaa ').startsWith('aaaa ');
// passes
```

```js
enforce('for').startsWith('tor');
enforce('aaaab').startsWith('aab');
enforce('aa').startsWith('aaa');
enforce(42).startsWith('b');
enforce(42).startsWith(50);
enforce(true).startsWith(100);
// throws
```

## doesNotStartWith

### Description

Determines whether a string does not start with the characters of a specified string.
Reverse implementation of `startsWith`.

# Custom enforce rules

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".
Expand Down
2 changes: 2 additions & 0 deletions packages/vest/src/typings/vest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ export interface IEnforceRules<T = {}> {
lt: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
lte: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
endsWith: (suffix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
startsWith: (prefix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
doesNotEndWith: (suffix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
doesNotStartWith: (prefix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
numberNotEquals: (
expected: number | string
) => IEnforceRules<T> & EnforceExtendMap<T>;
Expand Down

0 comments on commit 17f74e1

Please sign in to comment.