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 'isKeyOf' rule #771

Merged
merged 6 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/n4s/docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Enforce rules are functions that allow you to test your data against different c
- [doesNotStartWith](#doesnotstartwith)
- [isNegative](#isnegative)
- [isPositive](#ispositive)
- [isKeyOf](#iskeyof)
- [isNotKeyOf](#isnotkeyof)

## equals

Expand Down Expand Up @@ -1280,3 +1282,52 @@ enforce('10.12').isPositive(); //passes
enforce(-10).isPositive(); // throws
enforce('-10.12').isPositive(); // throws
```

## isKeyOf
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but just a request - I am in the process of replacing the docs folder and moving it someplace else. Can you please move this change to here: https://github.com/ealush/vest/blob/latest/website/docs/enforce/enforce_rules.md instead?


### Description

Determines whether a value is a key of an object

### Usage examples:

```js
enforce('banans').isKeyOf({ banans: 5 });
enforce(1976).isKeyOf({ 1976: 'Rocky' });

// passes
```

```js
enforce('avocados').isKeyOf({ cantelopes: 5 });
enforce(1967).isKeyOf({ 1988: 'Rain Man' });
enforce('key').isKeyOf(undefined);
enforce(15).isKeyOf(null);
enforce('star').isKeyOf(false);
enforce('triangle').isKeyOf(true);
// throws
```

## isNotKeyOf

### Description

Determines whether a value **is not** a key of an object

### Usage examples:

```js
enforce('avocados').isNotKeyOf({ cantelopes: 5 });
enforce(1967).isNotKeyOf({ 1988: 'Rain Man' });
enforce('key').isNotKeyOf(undefined);
enforce(15).isNotKeyOf(null);
enforce('star').isNotKeyOf(false);
enforce('triangle').isNotKeyOf(true);
// passes
```

```js
enforce('banans').isNotKeyOf({ banans: 5 });
enforce(1976).isNotKeyOf({ 1976: 'Rocky' });
// throws
```
36 changes: 36 additions & 0 deletions packages/n4s/src/rules/__tests__/isKeyOf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { isKeyOf } from 'isKeyOf';
galezra marked this conversation as resolved.
Show resolved Hide resolved

const FRUITES = {
apples: 5,
bananas: 2,
cantelopes: 0,
};

const TOP_GROSSING_MOVIES = {
1976: 'Rocky',
1988: 'Rain Man',
2008: 'The Dark Knight',
};

const DUMMY_KEY = 'key';

describe('Tests isKeyOf rule', () => {
it('When the key exists in the object', () => {
expect(isKeyOf('bananas', FRUITES)).toBe(true);
});

it('When the key not exists in the object', () => {
expect(isKeyOf('avocados', FRUITES)).toBe(false);
});

it('When the key exists in the object (numeric)', () => {
expect(isKeyOf(1976, TOP_GROSSING_MOVIES)).toBe(true);
});

it.each([undefined, null, false, true, Object, [], '', Function.prototype])(
'Should return false for %s object',
v => {
expect(isKeyOf(DUMMY_KEY, v)).toBe(false);
}
);
galezra marked this conversation as resolved.
Show resolved Hide resolved
});
13 changes: 13 additions & 0 deletions packages/n4s/src/rules/isKeyOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import bindNot from 'bindNot';
import { isBoolean } from 'isBooleanValue';
import { isNullish } from 'isNullish';
import { isStringValue } from 'isStringValue';

export function isKeyOf(key: string | symbol | number, obj: any): boolean {
const shouldTerminate =
isNullish(obj) || isBoolean(obj) || isStringValue(obj);

return shouldTerminate ? false : key in obj;
galezra marked this conversation as resolved.
Show resolved Hide resolved
}

export const isNotKeyOf = bindNot(isKeyOf);
3 changes: 3 additions & 0 deletions packages/n4s/src/runtime/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isBlank, isNotBlank } from 'isBlank';
import { isBoolean, isNotBoolean } from 'isBoolean';
import { isEmpty, isNotEmpty } from 'isEmpty';
import { isEven } from 'isEven';
import { isKeyOf, isNotKeyOf } from 'isKeyOf';
import { isNaN, isNotNaN } from 'isNaN';
import { isPositive, isNegative } from 'isNegative';
import { isNull, isNotNull } from 'isNull';
Expand Down Expand Up @@ -52,13 +53,15 @@ export default function rules() {
isEmpty,
isEven,
isFalsy,
isKeyOf,
isNaN,
isNegative,
isNotArray,
isNotBetween,
isNotBlank,
isNotBoolean,
isNotEmpty,
isNotKeyOf,
isNotNaN,
isNotNull,
isNotNullish,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.