Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(n4s): added endsWith and doesNotEndWith rule #409

Merged
merged 8 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/n4s/src/rules/endsWith/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function endsWith(value, arg1) {
return typeof value === 'string' && typeof arg1 === 'string' && value.endsWith(arg1);
}

endsWith.negativeForm = 'doesNotEndWith';

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

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

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

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

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

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

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

it('Should expose negativeForm property', () => {
expect(endsWith.negativeForm).toBe('doesNotEndWith');
});
});
4 changes: 3 additions & 1 deletion packages/n4s/src/rules/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { extendRules } from '../lib';
import endsWith from './endsWith';
import equals from './equals';
import greaterThan from './greaterThan';
import greaterThanOrEquals from './greaterThanOrEquals';
Expand All @@ -25,6 +26,7 @@ import shorterThan from './shorterThan';
import shorterThanOrEquals from './shorterThanOrEquals';

const rules = {
endsWith,
equals,
greaterThan,
greaterThanOrEquals,
Expand All @@ -48,7 +50,7 @@ const rules = {
matches,
numberEquals,
shorterThan,
shorterThanOrEquals,
shorterThanOrEquals
};

export default extendRules(rules);
2 changes: 2 additions & 0 deletions packages/n4s/src/rules/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const negativeRules = [
'notMatches',
'numberNotEquals',
'isNotNull',
'doesNotEndWith',
];

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

const allRules = [...new Set([...positiveRules, ...negativeRules])];
34 changes: 34 additions & 0 deletions packages/vest/docs/enforce.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Enforce rules are functions that allow you to test your data against different c
- [isNotString](#isnotstring)
- [isOdd](#isodd)
- [isEven](#iseven)
- [endsWith](#endswith)
- [doesNotEndWith](#doesnotendwith)

### equals

Expand Down Expand Up @@ -995,6 +997,38 @@ enforce([0]).isEven();
// throws
```

## endsWith

### Description

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

### Usage examples:

```js
enforce('aba').endsWith('ba');
enforce('some_string').endsWith('_string');
enforce('string with spaces').endsWith('ng with spaces');
enforce('aaaa ').endsWith(' ');
// passes
```

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

## doesNotEndWith

### Description

Determines whether a string does not end with the characters of a specified string.
Reverse implementation of `endsWith`.

# Custom enforce rules

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 @@ -135,6 +135,8 @@ export interface IEnforceRules<T = {}> {
gte: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
lt: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
lte: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
endsWith: (suffix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
doesNotEndWith: (suffix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
numberNotEquals: (
expected: number | string
) => IEnforceRules<T> & EnforceExtendMap<T>;
Expand Down