-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(n4s): isValueOf and isNotValueOf (#760)
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35723af
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: