Skip to content

Commit

Permalink
add(n4s): isValueOf and isNotValueOf (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
saard authored Jan 10, 2022
1 parent 3041ce6 commit 35723af
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
38 changes: 38 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)
- [isValueOf](#isvalueof)
- [isNotValueOf](#isnotvalueof)

## equals

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

## isValueOf

### Description

Determines whether a string value exists as one of the values for a key in an object.

### Usage examples:

```js
enforce('Bravo').isValueOf({ a: 'Alpha', b: 'Bravo', c: 'Charlie' });
// passes
```

```js
enforce('Delta').isValueOf({ a: 'Alpha', b: 'Bravo', c: 'Charlie' });
// throws
```

## isNotValueOf

### Description

Determines whether a string is not a value of any key in an object.

### Usage examples:

```js
enforce('Delta').isNotValueOf({ a: 'Alpha', b: 'Bravo', c: 'Charlie' });
// passes
```

```js
enforce('Bravo').isValueOf({ a: 'Alpha', b: 'Bravo', c: 'Charlie' });
// throws
```
72 changes: 72 additions & 0 deletions packages/n4s/src/rules/__tests__/isValueOf.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { enforce } from 'enforce';
import { isValueOf, isNotValueOf } from 'isValueOf';

const testObject = {
a: 'Bravo',
b: false,
c: 42,
};

const testObject2 = {
d: {
greet: 'hello',
},
e: null,
f: undefined,
};

describe('isValueOf tests', () => {
describe('When the value exists in the object', () => {
it('Should return true using enforce', () => {
enforce('Bravo').isValueOf(testObject);
enforce(42).isValueOf(testObject);
enforce(false).isValueOf(testObject);
enforce(null).isValueOf(testObject2);
enforce(undefined).isValueOf(testObject2);
});

it('Should return true', () => {
expect(isValueOf('Bravo', testObject)).toBe(true);
expect(isValueOf(42, testObject)).toBe(true);
expect(isValueOf(false, testObject)).toBe(true);
expect(isValueOf(null, testObject2)).toBe(true);
expect(isValueOf(undefined, testObject2)).toBe(true);
});
});
describe('When the value does not exist in the object', () => {
it('Should return false', () => {
expect(isValueOf('Alpha', testObject)).toBe(false);
expect(isValueOf(1, testObject)).toBe(false);
expect(isValueOf(true, testObject)).toBe(false);
expect(isValueOf(null, testObject)).toBe(false);
expect(isValueOf({ greet: 'hello' }, testObject2)).toBe(false);
});

it('Should throw using enforce', () => {
expect(() => enforce('Alpha').isValueOf(testObject)).toThrow();
expect(() => enforce(null).isValueOf(testObject)).toThrow();
});
});
});

describe('isNotValueOf tests', () => {
describe('When the value does not exist in the object', () => {
it('Should return true using enforce', () => {
enforce('Delta').isNotValueOf(testObject);
});
it('Should return true', () => {
expect(isNotValueOf('Alpha', testObject)).toBe(true);
expect(isNotValueOf(1, testObject)).toBe(true);
expect(isNotValueOf(true, testObject)).toBe(true);
expect(isNotValueOf(null, testObject)).toBe(true);
});
});
describe('When the value exists in the object', () => {
it('Should return false', () => {
expect(isNotValueOf('Bravo', testObject)).toBe(false);
});
it('Should throw using enforce', () => {
expect(() => enforce(42).isNotValueOf(testObject)).toThrow();
});
});
});
17 changes: 17 additions & 0 deletions packages/n4s/src/rules/isValueOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import bindNot from 'bindNot';
import { isNullish } from 'isNullish';

export function isValueOf(value: any, objectToCheck: any): boolean {
if (isNullish(objectToCheck)) {
return false;
}

for (const key in objectToCheck) {
if (objectToCheck[key] === value) {
return true;
}
}

return false;
}
export const isNotValueOf = bindNot(isValueOf);
3 changes: 3 additions & 0 deletions packages/n4s/src/runtime/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { isOdd } from 'isOdd';
import { isString, isNotString } from 'isString';
import { isTruthy, isFalsy } from 'isTruthy';
import { isUndefined, isNotUndefined } from 'isUndefined';
import { isValueOf, isNotValueOf } from 'isValueOf';
import { lengthEquals, lengthNotEquals } from 'lengthEquals';
import { lessThan } from 'lessThan';
import { lessThanOrEquals } from 'lessThanOrEquals';
Expand Down Expand Up @@ -66,6 +67,7 @@ export default function rules() {
isNotNumeric,
isNotString,
isNotUndefined,
isNotValueOf,
isNull,
isNullish,
isNumber,
Expand All @@ -75,6 +77,7 @@ export default function rules() {
isString,
isTruthy,
isUndefined,
isValueOf,
lengthEquals,
lengthNotEquals,
lessThan,
Expand Down

1 comment on commit 35723af

@vercel
Copy link

@vercel vercel bot commented on 35723af Jan 10, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.