Add is.directInstanceOf() (#38)
This commit is contained in:
parent
4ce2ee9d39
commit
70b08940be
3 changed files with 29 additions and 0 deletions
14
readme.md
14
readme.md
|
|
@ -136,6 +136,20 @@ is.boundFunction(function () {});
|
|||
|
||||
#### Miscellaneous
|
||||
|
||||
##### .directInstanceOf(value, class)
|
||||
|
||||
Returns `true` if `value` is a direct instance of `class`.
|
||||
|
||||
```js
|
||||
is.directInstanceOf(new Error(), Error);
|
||||
//=> true
|
||||
|
||||
class UnicornError extends Error {};
|
||||
|
||||
is.directInstanceOf(new UnicornError(), Error);
|
||||
//=> false
|
||||
```
|
||||
|
||||
##### .truthy(value)
|
||||
|
||||
Returns `true` for all values that evaluate to true in a boolean context:
|
||||
|
|
|
|||
|
|
@ -162,6 +162,10 @@ namespace is { // tslint:disable-line:no-namespace
|
|||
export const arrayBuffer = isObjectOfType(TypeName.ArrayBuffer);
|
||||
export const sharedArrayBuffer = isObjectOfType(TypeName.SharedArrayBuffer);
|
||||
|
||||
// TODO: Remove `object` checks when targeting ES2015 or higher
|
||||
// See `Notes`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
|
||||
export const directInstanceOf = (instance: any, klass: any) => object(instance) && object(klass) && Object.getPrototypeOf(instance) === klass.prototype;
|
||||
|
||||
export const truthy = (value: any) => Boolean(value);
|
||||
export const falsy = (value: any) => !value;
|
||||
|
||||
|
|
|
|||
|
|
@ -507,6 +507,17 @@ test('is.dataView', t => {
|
|||
testType(t, 'arrayBuffer');
|
||||
});
|
||||
|
||||
test('is.directInstanceOf', t => {
|
||||
const error = new Error();
|
||||
const errorSubclass = new ErrorSubclassFixture();
|
||||
|
||||
t.true(m.directInstanceOf(error, Error));
|
||||
t.true(m.directInstanceOf(errorSubclass, ErrorSubclassFixture));
|
||||
|
||||
t.false(m.directInstanceOf(error, ErrorSubclassFixture));
|
||||
t.false(m.directInstanceOf(errorSubclass, Error));
|
||||
});
|
||||
|
||||
test('is.truthy', t => {
|
||||
t.true(m.truthy('unicorn'));
|
||||
t.true(m.truthy('🦄'));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue