Skip to content

Commit

Permalink
feat: new isDocumentFragment & isShadowRoot functions
Browse files Browse the repository at this point in the history
  • Loading branch information
VicGUTT committed Apr 24, 2022
1 parent ee2a383 commit bb8eaaf
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import isClass from './isClass';
import isDate from './isDate';
import isDefined from './isDefined';
import isDerivative from './isDerivative';
import isDocumentFragment from './isDocumentFragment';
import isElement from './isElement';
import isEvent from './isEvent';
import isFalsy from './isFalsy';
Expand All @@ -36,6 +37,7 @@ import isObject from './isObject';
import isPrimitive from './isPrimitive';
import isPromise from './isPromise';
import isSet from './isSet';
import isShadowRoot from './isShadowRoot';
import isSizey from './isSizey';
import isString from './isString';
import isSymbol from './isSymbol';
Expand Down Expand Up @@ -82,6 +84,7 @@ export default {
date: isDate,
defined: isDefined,
derivative: isDerivative,
documentFragment: isDocumentFragment,
element: isElement,
empty: isEmpty,
event: isEvent,
Expand Down Expand Up @@ -110,6 +113,7 @@ export default {
primitive: isPrimitive,
promise: isPromise,
set: isSet,
shadowRoot: isShadowRoot,
sizey: isSizey,
string: isString,
symbol: isSymbol,
Expand Down
21 changes: 21 additions & 0 deletions src/isDocumentFragment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import instanceOf from './utils/instanceOf';

/**
* Determines whether the given value is a DocumentFragment.
*
* @example
* ```js
* isDocumentFragment({}); // false
* isDocumentFragment(Document); // false
* isDocumentFragment(Window); // false
* isDocumentFragment(document.getRootNode()); // false
* isDocumentFragment(document.body); // false
* isDocumentFragment(document.querySelector('html')); // false
* isDocumentFragment(document.createElement('img')); // false
* isDocumentFragment(new DocumentFragment()); // true
* isDocumentFragment(document.createDocumentFragment()); // true
* ```
*/
export default function isDocumentFragment(value: unknown): value is ShadowRoot {
return instanceOf(value, DocumentFragment);
}
20 changes: 20 additions & 0 deletions src/isShadowRoot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import instanceOf from './utils/instanceOf';

/**
* Determines whether the given value is a ShadowRoot.
*
* @example
* ```js
* isShadowRoot({}); // false
* isShadowRoot(Document); // false
* isShadowRoot(Window); // false
* isShadowRoot(document.getRootNode()); // false
* isShadowRoot(document.body); // false
* isShadowRoot(document.querySelector('html')); // false
* isShadowRoot(document.createElement('img')); // false
* isShadowRoot(document.createElement('span').attachShadow({ mode: 'open' })); // true
* ```
*/
export default function isShadowRoot(value: unknown): value is ShadowRoot {
return instanceOf(value, ShadowRoot);
}
7 changes: 7 additions & 0 deletions tests/__Fixtures/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export const nodeLists = [
document.querySelectorAll('div'), // length = 3
];

export const alternativeDOMs = [
document.createDocumentFragment(),
document.createElement('span').attachShadow({ mode: 'open' }),
];

export const functions = [
(): void => {}, // eslint-disable-line @typescript-eslint/no-empty-function
function(): void {}, // eslint-disable-line @typescript-eslint/no-empty-function
Expand Down Expand Up @@ -240,6 +245,7 @@ export const all = [
...arrays,
...elements,
...nodeLists,
...alternativeDOMs,
...functions,
...classes,
...constructors,
Expand All @@ -260,6 +266,7 @@ export default {
arrays,
elements,
nodeLists,
alternativeDOMs,
functions,
classes,
constructors,
Expand Down
120 changes: 120 additions & 0 deletions tests/is/isDocumentFragment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* ❌: The expectation should be equal to false
* ✅: The expectation should be equal to true
* ➖: The expectation can be equal to true or false
*/

import values from '../__Fixtures/values';
import isDocumentFragment from '../../src/isDocumentFragment';

const KEY = 'documentFragments';

describe('is:isDocumentFragment', () => {
it(`❌ nil !== ${KEY}`, () => {
values.nil.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ booleans !== ${KEY}`, () => {
values.booleans.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ integers !== ${KEY}`, () => {
values.integers.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ floats !== ${KEY}`, () => {
values.floats.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ numbers !== ${KEY}`, () => {
values.numbers.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ bigInts !== ${KEY}`, () => {
values.bigInts.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ strings !== ${KEY}`, () => {
values.strings.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ symbols !== ${KEY}`, () => {
values.symbols.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ arrays !== ${KEY}`, () => {
values.arrays.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ DOM elements !== ${KEY}`, () => {
values.elements.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ DOM nodeLists !== ${KEY}`, () => {
values.nodeLists.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`➖ DOM alternative contexts !== || === ${KEY}`, () => {
values.alternativeDOMs.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(value instanceof DocumentFragment);
});
});

it(`❌ functions !== ${KEY}`, () => {
values.functions.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ classes !== ${KEY}`, () => {
values.classes.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ object constructors !== ${KEY}`, () => {
values.constructors.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ invoked object constructors !== ${KEY}`, () => {
values.invokedConstructors.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ object instances !== ${KEY}`, () => {
values.instances.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});

it(`❌ objects !== ${KEY}`, () => {
values.objects.forEach((value) => {
expect(isDocumentFragment(value)).toEqual(false);
});
});
});
120 changes: 120 additions & 0 deletions tests/is/isShadowRoot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* ❌: The expectation should be equal to false
* ✅: The expectation should be equal to true
* ➖: The expectation can be equal to true or false
*/

import values from '../__Fixtures/values';
import isShadowRoot from '../../src/isShadowRoot';

const KEY = 'shadowRoots';

describe('is:isShadowRoot', () => {
it(`❌ nil !== ${KEY}`, () => {
values.nil.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ booleans !== ${KEY}`, () => {
values.booleans.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ integers !== ${KEY}`, () => {
values.integers.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ floats !== ${KEY}`, () => {
values.floats.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ numbers !== ${KEY}`, () => {
values.numbers.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ bigInts !== ${KEY}`, () => {
values.bigInts.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ strings !== ${KEY}`, () => {
values.strings.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ symbols !== ${KEY}`, () => {
values.symbols.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ arrays !== ${KEY}`, () => {
values.arrays.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ DOM elements !== ${KEY}`, () => {
values.elements.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ DOM nodeLists !== ${KEY}`, () => {
values.nodeLists.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`➖ DOM alternative contexts !== || === ${KEY}`, () => {
values.alternativeDOMs.forEach((value) => {
expect(isShadowRoot(value)).toEqual(value instanceof ShadowRoot);
});
});

it(`❌ functions !== ${KEY}`, () => {
values.functions.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ classes !== ${KEY}`, () => {
values.classes.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ object constructors !== ${KEY}`, () => {
values.constructors.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ invoked object constructors !== ${KEY}`, () => {
values.invokedConstructors.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ object instances !== ${KEY}`, () => {
values.instances.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});

it(`❌ objects !== ${KEY}`, () => {
values.objects.forEach((value) => {
expect(isShadowRoot(value)).toEqual(false);
});
});
});

0 comments on commit bb8eaaf

Please sign in to comment.