Skip to content

Commit

Permalink
Rename is.domElement() to is.htmlElement() (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar authored Aug 10, 2023
1 parent ee79af3 commit bcec30d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ Check if `value` (number) is in the range of `0` to `upperBound`.
is.inRange(3, 10);
```

##### .domElement(value)
##### .htmlElement(value)

Returns `true` if `value` is an [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement).

Expand Down
27 changes: 17 additions & 10 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export type AssertionTypeDescription = typeof assertionTypeDescriptions[number];
const getObjectType = (value: unknown): ObjectTypeName | undefined => {
const objectTypeName = Object.prototype.toString.call(value).slice(8, -1);

if (/HTML\w+Element/.test(objectTypeName) && isDomElement(value)) {
if (/HTML\w+Element/.test(objectTypeName) && isHtmlElement(value)) {
return 'HTMLElement';
}

Expand Down Expand Up @@ -240,7 +240,8 @@ const is = Object.assign(
date: isDate,
detect,
directInstanceOf: isDirectInstanceOf,
domElement: isDomElement,
/** @deprecated Renamed to `htmlElement` */
domElement: isHtmlElement,
emptyArray: isEmptyArray,
emptyMap: isEmptyMap,
emptyObject: isEmptyObject,
Expand All @@ -259,6 +260,7 @@ const is = Object.assign(
function_: isFunction,
generator: isGenerator,
generatorFunction: isGeneratorFunction,
htmlElement: isHtmlElement,
infinite: isInfinite,
inRange: isInRange,
int16Array: isInt16Array,
Expand Down Expand Up @@ -493,7 +495,7 @@ const DOM_PROPERTIES_TO_CHECK: Array<(keyof HTMLElement)> = [
'nodeValue',
];

export function isDomElement(value: unknown): value is HTMLElement {
export function isHtmlElement(value: unknown): value is HTMLElement {
return isObject(value)
&& (value as HTMLElement).nodeType === NODE_TYPE_ELEMENT
&& isString((value as HTMLElement).nodeName)
Expand Down Expand Up @@ -889,7 +891,9 @@ type Assert = {
typedArray: (value: unknown) => asserts value is TypedArray;
arrayLike: <T = unknown>(value: unknown) => asserts value is ArrayLike<T>;
tupleLike: <T extends Array<TypeGuard<unknown>>>(value: unknown, guards: [...T]) => asserts value is ResolveTypesOfTypeGuardsTuple<T>;
/** @deprecated Renamed to `htmlElement` */
domElement: (value: unknown) => asserts value is HTMLElement;
htmlElement: (value: unknown) => asserts value is HTMLElement;
observable: (value: unknown) => asserts value is ObservableLike;
nodeStream: (value: unknown) => asserts value is NodeStream;
infinite: (value: unknown) => asserts value is number;
Expand Down Expand Up @@ -946,7 +950,7 @@ export const assert: Assert = {
dataView: assertDataView,
date: assertDate,
directInstanceOf: assertDirectInstanceOf,
domElement: assertDomElement,
domElement: assertHtmlElement,
emptyArray: assertEmptyArray,
emptyMap: assertEmptyMap,
emptyObject: assertEmptyObject,
Expand All @@ -964,6 +968,7 @@ export const assert: Assert = {
function_: assertFunction,
generator: assertGenerator,
generatorFunction: assertGeneratorFunction,
htmlElement: assertHtmlElement,
infinite: assertInfinite,
inRange: assertInRange,
int16Array: assertInt16Array,
Expand Down Expand Up @@ -1038,6 +1043,7 @@ const methodTypeMap = {
isDataView: 'DataView',
isDate: 'Date',
isDirectInstanceOf: 'T',
/** @deprecated */
isDomElement: 'HTMLElement',
isEmptyArray: 'empty array',
isEmptyMap: 'empty map',
Expand All @@ -1055,6 +1061,7 @@ const methodTypeMap = {
isFunction: 'Function',
isGenerator: 'Generator',
isGeneratorFunction: 'GeneratorFunction',
isHtmlElement: 'HTMLElement',
isInfinite: 'infinite number',
isInRange: 'in range',
isInt16Array: 'Int16Array',
Expand Down Expand Up @@ -1250,12 +1257,6 @@ export function assertDirectInstanceOf<T>(instance: unknown, class_: Class<T>):
}
}

export function assertDomElement(value: unknown): asserts value is HTMLElement {
if (!isDomElement(value)) {
throw new TypeError(typeErrorMessage('HTMLElement', value));
}
}

export function assertEmptyArray(value: unknown): asserts value is never[] {
if (!isEmptyArray(value)) {
throw new TypeError(typeErrorMessage('empty array', value));
Expand Down Expand Up @@ -1353,6 +1354,12 @@ export function assertGeneratorFunction(value: unknown): asserts value is Genera
}
}

export function assertHtmlElement(value: unknown): asserts value is HTMLElement {
if (!isHtmlElement(value)) {
throw new TypeError(typeErrorMessage('HTMLElement', value));
}
}

export function assertInfinite(value: unknown): asserts value is number {
if (!isInfinite(value)) {
throw new TypeError(typeErrorMessage('infinite number', value));
Expand Down
29 changes: 14 additions & 15 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class ErrorSubclassFixture extends Error {}

const {window} = new JSDOM();
const {document} = window;
const createDomElement = (element: string) => document.createElement(element);

const structuredClone = globalThis.structuredClone ?? (x => x);

Expand Down Expand Up @@ -516,9 +515,9 @@ const types = new Map<string, Test>([
typename: 'number',
typeDescription: 'integer',
}],
['domElement', {
is: is.domElement,
assert: assert.domElement,
['htmlElement', {
is: is.htmlElement,
assert: assert.htmlElement,
fixtures: [
'div',
'input',
Expand All @@ -527,14 +526,14 @@ const types = new Map<string, Test>([
'canvas',
'script',
]
.map(fixture => createDomElement(fixture)),
.map(fixture => document.createElement(fixture)),
typeDescription: 'HTMLElement',
}],
['non-domElements', {
is: value => !is.domElement(value),
['non-htmlElement', {
is: value => !is.htmlElement(value),
assert(value: unknown) {
invertAssertThrow('HTMLElement', () => {
assert.domElement(value);
assert.htmlElement(value);
}, value);
},
fixtures: [
Expand Down Expand Up @@ -1622,11 +1621,11 @@ test('is.inRange', t => {
});
});

test('is.domElement', t => {
testType(t, 'domElement');
t.false(is.domElement({nodeType: 1, nodeName: 'div'}));
test('is.htmlElement', t => {
testType(t, 'htmlElement');
t.false(is.htmlElement({nodeType: 1, nodeName: 'div'}));
t.throws(() => {
assert.domElement({nodeType: 1, nodeName: 'div'});
assert.htmlElement({nodeType: 1, nodeName: 'div'});
});

const tagNames = [
Expand All @@ -1636,11 +1635,11 @@ test('is.domElement', t => {
'img',
'canvas',
'script',
];
] as const;

for (const tagName of tagNames) {
const domElement = createDomElement(tagName);
t.is(is(domElement), 'HTMLElement');
const element = document.createElement(tagName);
t.is(is(element), 'HTMLElement');
}
});

Expand Down

0 comments on commit bcec30d

Please sign in to comment.