forked from ealush/vest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(n4s): added 'isKeyOf' rule (ealush#771)
- Loading branch information
Showing
6 changed files
with
153 additions
and
29 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,62 @@ | ||
import { enforce } from 'enforce'; | ||
import { isKeyOf, isNotKeyOf } from 'isKeyOf'; | ||
|
||
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', () => { | ||
describe('When the key exists in the object', () => { | ||
it('Should return true', () => { | ||
expect(isKeyOf('bananas', FRUITES)).toBe(true); | ||
expect(isKeyOf(1976, TOP_GROSSING_MOVIES)).toBe(true); | ||
}); | ||
|
||
it('Should return true using enforce', () => { | ||
enforce('bananas').isKeyOf(FRUITES); | ||
enforce(1976).isKeyOf(TOP_GROSSING_MOVIES); | ||
}); | ||
}); | ||
|
||
describe('When the key does not exists in the object', () => { | ||
it('Should return false', () => { | ||
expect(isKeyOf('avocado', FRUITES)).toBe(false); | ||
expect(isKeyOf(1999, TOP_GROSSING_MOVIES)).toBe(false); | ||
}); | ||
|
||
it.each([undefined, null, false, true, Object, [], '', Function.prototype])( | ||
'Should throw when %s is an object', | ||
v => { | ||
expect(() => enforce(DUMMY_KEY).isKeyOf({ v })).toThrow(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe('Tests isNotKeyOf rule', () => { | ||
describe('When the key does not exists in the object', () => { | ||
it('Should return true', () => { | ||
expect(isNotKeyOf('avocado', FRUITES)).toBe(true); | ||
}); | ||
|
||
it('Should return true using enforce', () => { | ||
enforce(1999).isNotKeyOf(TOP_GROSSING_MOVIES); | ||
}); | ||
}); | ||
|
||
describe('When the key exists in the object', () => { | ||
it('Should return false', () => { | ||
expect(isNotKeyOf(1976, TOP_GROSSING_MOVIES)).toBe(false); | ||
}); | ||
}); | ||
}); |
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,7 @@ | ||
import bindNot from 'bindNot'; | ||
|
||
export function isKeyOf(key: string | symbol | number, obj: any): boolean { | ||
return key in obj; | ||
} | ||
|
||
export const isNotKeyOf = bindNot(isKeyOf); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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