@pho9ubenaa/remark-mask-text-beta
Version:
A remark plugin to mask text content with block characters
81 lines • 2.97 kB
JavaScript
/**
* Centralized AST type definitions for remark-mask-text plugin
*
* This module provides unified type definitions for Abstract Syntax Tree nodes
* used throughout the plugin. These types ensure type safety and consistency
* across all modules while providing clear interfaces for AST manipulation.
*
* Why (Business Logic Background):
* - Type safety: To enable compile-time error checking during AST node manipulation
* - Development efficiency: To leverage IDE auto-completion and refactoring features
* - Maintainability: To clarify impact scope during AST structure changes and prevent breaking changes
* - Compatibility: To maintain consistency with remark ecosystem type definitions
* - Extensibility: To facilitate adding new node types
*
* These types bridge the gap between the minimal types needed for development
* and the full types from @types/mdast and @types/unist packages.
*/
import { NODE_PROPERTIES, NODE_TYPES } from "./constants.js";
/**
* Helper function to safely access object property
*/
const hasProperty = (obj, key) => {
return typeof obj === "object" && obj !== null && key in obj;
};
/**
* Type guard to check if a node is a parent node
*
* @param node - The node to check
* @returns true if the node has children property
*/
export const isParentNode = (node) => {
if (!hasProperty(node, NODE_PROPERTIES.TYPE))
return false;
if (!hasProperty(node, NODE_PROPERTIES.CHILDREN))
return false;
return typeof node.type === "string" && Array.isArray(node.children);
};
/**
* Type guard to check if a node is a text node
*
* @param node - The node to check
* @returns true if the node is a text node with string value
*/
export const isTextNode = (node) => {
if (!hasProperty(node, NODE_PROPERTIES.TYPE))
return false;
if (!hasProperty(node, NODE_PROPERTIES.VALUE))
return false;
return node.type === NODE_TYPES.TEXT && typeof node.value === "string";
};
/**
* Type guard to check if a node is an HTML node
*
* @param node - The node to check
* @returns true if the node is an HTML node with string value
*/
export const isHtmlNode = (node) => {
if (!hasProperty(node, NODE_PROPERTIES.TYPE))
return false;
if (!hasProperty(node, NODE_PROPERTIES.VALUE))
return false;
return node.type === NODE_TYPES.HTML && typeof node.value === "string";
};
/**
* Type guard to check if a node is a link node
*
* @param node - The node to check
* @returns true if the node is a link node with url property
*/
export const isLinkNode = (node) => {
if (!hasProperty(node, NODE_PROPERTIES.TYPE))
return false;
if (!hasProperty(node, NODE_PROPERTIES.URL))
return false;
if (!hasProperty(node, NODE_PROPERTIES.CHILDREN))
return false;
return (node.type === NODE_TYPES.LINK &&
typeof node.url === "string" &&
Array.isArray(node.children));
};
//# sourceMappingURL=ast-types.js.map