Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code and tests for checking DOM Node types #4

Merged
merged 15 commits into from
Sep 15, 2023
26 changes: 26 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"env": {
"node": true,
"mocha": true,
"es6": false
},
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "deprecation"],
"parserOptions": {
"ecmaVersion": 6,
"project": "./tsconfig.eslint.json"
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"rules": {
"no-console": "error",
"deprecation/deprecation": "warn",
"@typescript-eslint/no-non-null-assertion": "error",
"@typescript-eslint/no-unused-vars": "error"
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
coverage/
.nyc_output/
dist/
10 changes: 10 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"diff": true,
"extension": "spec.ts",
"package": "./package.json",
"recursive": true,
"reporter": "spec",
"require": ["choma", "ts-node/register"],
"spec": "test/**/*.spec.ts",
"watch-files": "test/**/*.spec.ts"
}
7 changes: 7 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@istanbuljs/nyc-config-typescript",
"all": true,
"check-coverage": false,
"reporter": ["lcov", "text"],
"include": ["src"]
}
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"printWidth": 100
}
33 changes: 33 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Contributing to is-dom-node

First off, thank you for considering contributing to is-dom-node! It's people like you that make is-dom-node such a great tool.

## Getting Started

- Fork the repository on GitHub.
- Clone your fork to your local machine.
- Create a new branch from `main` for your work.

## Code Guidelines

- Ensure your code follows the ESLint and TypeScript configuration provided.
- Write tests for any new features or bug fixes.
- Style is enforced using ESLint and Prettier. Run `npm run lint` to check your code.

## Pull Requests

- Make sure all tests pass before submitting a pull request.
- Describe what your pull request does and what issue(s) it addresses.

## Issues

- Feel free to open issues for bugs, feature requests, or other topics.
- Please be as descriptive as possible and include any relevant code snippets or error messages.

## License

By contributing, you agree that your contributions will be licensed under the project's MIT License.

## Questions?

If you have any questions or need further clarification, feel free to reach out or open an issue. We appreciate your contribution and are happy to help.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2023 Chris Barth

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# is-dom-node

![Build Status](https://img.shields.io/travis/cjbarth/is-dom-node)
![Code Coverage](https://img.shields.io/codecov/c/github/cjbarth/is-dom-node)
cjbarth marked this conversation as resolved.
Show resolved Hide resolved
![NPM Version](https://img.shields.io/npm/v/is-dom-node)
cjbarth marked this conversation as resolved.
Show resolved Hide resolved
![License](https://img.shields.io/npm/l/is-dom-node)

## Description

`is-dom-node` is a versatile TypeScript library designed to provide robust utility functions for working with DOM nodes. Whether you're developing for the browser or dealing with XML in NodeJS, this library offers a comprehensive set of functions for checking and asserting various types of DOM nodes. It's particularly useful when working with NodeJS libraries like `xpath` and `xmldom`, as it can validate the objects returned by these libraries.
cjbarth marked this conversation as resolved.
Show resolved Hide resolved

## Installation

```shell
npm install is-dom-node
```

## Usage

Import the library and use it as follows:

```javascript
import * as isDomNode from 'is-dom-node';

const element = document.createElement('div');
const result = isDomNode.isElementNode(element); // Output: true
```

### TypeScript Example

```typescript
import * as isDomNode from 'is-dom-node';

function handleNode(node: Node) {
if (isDomNode.isElementNode(node)) {
// TypeScript now knows `node` is an Element
console.log(node.tagName);
} else if (isDomNode.isTextNode(node)) {
// TypeScript now knows `node` is a Text node
console.log(node.wholeText);
} else {
try {
// Assert that the node is a Comment node
isDomNode.assertIsCommentNode(node);
// TypeScript now knows `node` is a Comment node
console.log(node.nodeValue);
} catch (error) {
console.error("Node is not a Comment node:", error);
}
}
}
```

## API Overview

The API consists of two main types of functions:

### `is...` Functions

These functions return a boolean value indicating whether the given object meets certain criteria:

- `isNodeLike`: Checks if a given value resembles a DOM node.
- `isArrayOfNodes`: Checks if the given value is an array of DOM nodes.
- `isElementNode`, `isAttributeNode`, `isTextNode`, etc.: Check for specific types of DOM nodes.

### `assertIs...` Functions

These functions assert that a given object meets certain criteria and throw an error if it doesn't:

- `assertIsNodeLike`: Asserts that a given value is a DOM node.
- `assertIsArrayOfNodes`: Asserts that the given value is an array of DOM nodes.
- `assertIsElementNode`, `assertIsAttributeNode`, `assertIsTextNode`, etc.: Assert for specific types of DOM nodes.

### TypeScript Type Narrowing

Both the `is...` and `assertIs...` functions can be used for TypeScript type narrowing. After a successful check or assertion, TypeScript will recognize the specific type of the DOM node, allowing for more robust and error-free code.

## Features

- Lightweight
- Written in TypeScript
- Comprehensive tests
- Compatible with NodeJS XML libraries like `xpath` and `xmldom`

## Contributing

Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests.

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
Loading