-
-
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(guardObjectKeysIn()): add function to find the keys in object pr…
…ototype chain.
- Loading branch information
1 parent
b13c6ef
commit 21e523b
Showing
3 changed files
with
230 additions
and
2 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,27 @@ | ||
// Function. | ||
import { isObjectKeysIn } from '../../is/lib/is-object-keys-in.func'; | ||
// Type. | ||
import { CallbackPayload } from '../../type/callback-payload.type'; | ||
import { ResultCallback } from '../../type/result-callback.type'; | ||
/** | ||
* Guards the value to be an `object` of a generic type variable `Obj` with specified keys in it(or its prototype chain). | ||
* @param value An object of a generic type variable `Obj`, by default of the type captured from the `value` that contains | ||
* (or its prototype chain) the given `keys` to guard. | ||
* @param keys An `Array` of property keys to check that the `value` contains(or its prototype chain). | ||
* @param callback An optional `ResultCallback` function to handle the result before returns. | ||
* @param payload An optional `object` of `CallbackPayload<Payload>` that is assigned to the `payload` of the provided `callback` function. | ||
* @returns The `value` is a `boolean` indicating whether the `value` is an `object` with specified `keys` in it(or its prototype chain). | ||
*/ | ||
export const guardObjectKeysIn = < | ||
Obj extends object, | ||
Key extends keyof Obj, | ||
Payload extends object = object | ||
>( | ||
value: Obj, | ||
keys: Key[], | ||
callback?: ResultCallback< | ||
Obj, | ||
CallbackPayload<{ keys: typeof keys } & Payload> | ||
>, | ||
payload?: CallbackPayload<Payload> | ||
): value is Obj => isObjectKeysIn(value, keys, callback, payload as any); |
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,108 @@ | ||
// Function. | ||
import { guardObjectKeysIn } from '../lib/guard-object-keys-in.func'; | ||
// Testing. | ||
import { | ||
// Main. | ||
Testing, | ||
|
||
// Constant. | ||
TESTING_CLASS, | ||
TESTING_NUMBER, | ||
TESTING_OBJECT, | ||
TESTING_SYMBOL_NUMBER, | ||
TESTING_SYMBOL_STRING, | ||
} from '@angular-package/testing'; | ||
// Execute tests. | ||
import { tests } from '../../execute-tests'; | ||
/** | ||
* Initialize testing. | ||
*/ | ||
const testing = new Testing( | ||
tests.guard.objectKeys.describe, | ||
tests.guard.objectKeys.it | ||
); | ||
/** | ||
* Tests. | ||
*/ | ||
testing.describe(guardObjectKeysIn.name, () => { | ||
testing | ||
// Defined. | ||
.it('is DEFINED', () => expect(guardObjectKeysIn).toBeDefined()) | ||
|
||
// Checks ... | ||
.describe(`guards`, () => { | ||
testing | ||
// ... instance. | ||
.describe(`instance`, () => { | ||
testing.describe(`TESTING_CLASS`, () => { | ||
testing | ||
// number. | ||
.it('has number key', () => { | ||
expect(guardObjectKeysIn(TESTING_CLASS, [1030405027])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_CLASS, [5])).toBeTrue(); | ||
}) | ||
|
||
.it('finds getter number', () => expect(guardObjectKeysIn(TESTING_CLASS, [TESTING_NUMBER])).toBeTrue()) | ||
|
||
// string. | ||
.it('has string key', () => expect(guardObjectKeysIn(TESTING_CLASS, ['surname'])).toBeTrue()) | ||
|
||
// symbol. | ||
.it('finds getter symbol key', () => { | ||
expect(guardObjectKeysIn(TESTING_CLASS, [TESTING_SYMBOL_NUMBER])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_CLASS, [TESTING_SYMBOL_STRING])).toBeTrue(); | ||
}); | ||
}); | ||
}) | ||
|
||
// ... objects. | ||
.describe('object', () => { | ||
testing | ||
.describe(`TESTING_OBJECT`, () => { | ||
testing | ||
// number. | ||
.it('has number key', () => { | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [1030405027])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [5])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_NUMBER])).toBeTrue(); | ||
}) | ||
|
||
// string. | ||
.it('has string key', () => { | ||
expect(guardObjectKeysIn(TESTING_OBJECT, ['key as string'])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_OBJECT, ['x'])).toBeTrue(); | ||
}) | ||
|
||
// symbol. | ||
.it('has symbol key', () => { | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_SYMBOL_NUMBER, TESTING_SYMBOL_STRING])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_SYMBOL_STRING])).toBeTrue(); | ||
}); | ||
}); | ||
}) | ||
|
||
.describe('object with some and every key', () => { | ||
testing.describe(`TESTING_OBJECT`, () => { | ||
testing | ||
// number. | ||
.it('has number key or any key', () => { | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [1030405027, 'key as string'])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [5])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_NUMBER])).toBeTrue(); | ||
}) | ||
|
||
// string. | ||
.it('has string key', () => { | ||
expect(guardObjectKeysIn(TESTING_OBJECT, ['key as string'])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_OBJECT, ['x'])).toBeTrue(); | ||
}) | ||
|
||
// symbol. | ||
.it('has symbol key', () => { | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_SYMBOL_NUMBER])).toBeTrue(); | ||
expect(guardObjectKeysIn(TESTING_OBJECT, [TESTING_SYMBOL_STRING])).toBeTrue(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |