Skip to content

Commit

Permalink
chore(typings): switch custom typings to parse5 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
aimee-gm authored May 14, 2020
1 parent fbc67e7 commit 5a28516
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/helpers/attributes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ParentNode, Attribute } from "../types";
import { Attribute } from "parse5";

import { ParentNode } from "../types";

export const getAttribute = (
node: ParentNode,
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/nodeMatchers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DefaultTreeTextNode } from "parse5";

import { MixedNode, ParentNode } from "../types";
import {
getAttribute,
Expand All @@ -19,6 +21,9 @@ const propClassRegex = classRegex("(p|e|u|dt)");
export const isParentNode = (node: MixedNode): node is ParentNode =>
Boolean(node.hasOwnProperty("tagName") && node.hasOwnProperty("childNodes"));

export const isTextNode = (node: MixedNode): node is DefaultTreeTextNode =>
Boolean(node.hasOwnProperty("value"));

export const isMicroformatV2Root = (node: ParentNode): boolean =>
getClassNames(node).some((cl) => cl.match(rootClassRegex));

Expand Down
16 changes: 11 additions & 5 deletions src/helpers/textContent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { MixedNode, ParentNode } from "../types";
import { getAttributeValue } from "./attributes";

const isParentNode = (node: MixedNode): node is ParentNode =>
Boolean(node.hasOwnProperty("tagName") && node.hasOwnProperty("childNodes"));
import { isParentNode, isTextNode } from "./nodeMatchers";

const walk = (current: string, node: MixedNode): string => {
/* istanbul ignore else */
if (isParentNode(node)) {
if (["style", "script"].includes(node.tagName)) {
return current;
Expand All @@ -20,12 +19,16 @@ const walk = (current: string, node: MixedNode): string => {
}

return node.childNodes.reduce<string>(walk, current);
} else {
} else if (isTextNode(node)) {
return `${current}${node.value}`;
}

/* istanbul ignore next */
return current;
};

const impliedWalk = (current: string, node: MixedNode): string => {
/* istanbul ignore else */
if (isParentNode(node)) {
if (["style", "script"].includes(node.tagName)) {
return current;
Expand All @@ -37,9 +40,12 @@ const impliedWalk = (current: string, node: MixedNode): string => {
}

return node.childNodes.reduce<string>(impliedWalk, current);
} else {
} else if (isTextNode(node)) {
return `${current}${node.value}`;
}

/* istanbul ignore next */
return current;
};

export const textContent = (node: ParentNode): string =>
Expand Down
24 changes: 8 additions & 16 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
DefaultTreeElement,
DefaultTreeNode,
DefaultTreeTextNode,
} from "parse5";

import { BackcompatRoot } from "./backcompat";

export interface ParserOptions {
Expand All @@ -16,22 +22,8 @@ export interface ParsedDocument {
items: MicroformatRoot[];
}

export interface Attribute {
name: string;
value: string;
}

export interface ParentNode {
attrs: Attribute[];
childNodes: (ParentNode | TextNode)[];
tagName: string;
}

export interface TextNode {
value: string;
}

export type MixedNode = ParentNode | TextNode;
export type ParentNode = DefaultTreeElement;
export type MixedNode = ParentNode | DefaultTreeTextNode | DefaultTreeNode;

export type MicroformatProperties = Record<string, MicroformatProperty[]>;

Expand Down

0 comments on commit 5a28516

Please sign in to comment.