Skip to content

Commit

Permalink
added remove-tags-by-name.js
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Apr 9, 2023
1 parent 5cb8039 commit a45b9a8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 15 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ Here's an example of a tag:

## get attribute
```javascript
const getAttribute = require("xml-utils/get-attribute");
const getAttribute = require("xml-utils/get-attribute.js");
const xml = `<MDI key="INTERLEAVE">PIXEL</MDI>`;
const key = getAttribute(xml, "key");
// key is "INTERLEAVE"
```

## find one tag by name
```javascript
const findTagByName = require("xml-utils/find-tag-by-name");
const findTagByName = require("xml-utils/find-tag-by-name.js");

const xml = `<Metadata domain="IMAGE_STRUCTURE"><MDI key="INTERLEAVE">PIXEL</MDI></Metadata>`
const tag = findTagByName(xml, "MDI");
Expand All @@ -50,7 +50,7 @@ tag is
```
## find multiple tags with the same name
```javascript
const findTagsByName = require("xml-utils/find-tags-by-name");
const findTagsByName = require("xml-utils/find-tags-by-name.js");
const xml = `
<Metadata>
<MDI key="SourceBandIndex">1</MDI>
Expand All @@ -65,7 +65,7 @@ const tags = findTagsByName(xml, "MDI");
```
## find one tag by path
```javascript
const findTagByPath = require("xml-utils/find-tag-by-path");
const findTagByPath = require("xml-utils/find-tag-by-path.js");
const xml = `
<gmd:referenceSystemIdentifier>
<gmd:RS_Identifier>
Expand All @@ -87,14 +87,14 @@ const tag = findTagByPath(xml, ["gmd:RS_Identifier", "gmd:code", "gco:CharacterS
## find multiple tags by path
To get an array of tags that follow a path:
```javascript
const findTagsByPath = require("xml-utils/find-tags-by-path");
const findTagsByPath = require("xml-utils/find-tags-by-path.js");
const tags = findTagByPath(xml, ["Metadata", "MDI"]);
// tags is an array of tags
```

## remove comments
```javascript
const removeComments = require("xml-utils/remove-comments");
const removeComments = require("xml-utils/remove-comments.js");
const xml = `<list>
<!--<A/>-->
<B/>
Expand All @@ -103,6 +103,15 @@ removeComments(xml);
"<list>\n \n<B/><list>";
```

## remove tags by name
```js
const removeTagsByName = require("xml-utils/remove-tags-by-name.js");

const xml = "<ul><li>A</li><li>B</li></ul>";
removeTagsByName(xml, "li")
"<ul></ul>"
```


## setup
download test files with:
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ export function findTagByPath(
): Tag | undefined;

export function getAttribute(tag: string | Tag, attributeName: string, options?: { debug?: boolean } ): string;

export function removeTagsByName(xml: string, tagName: string, options?: { debug?: boolean }): string;
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const getAttribute = require("./get-attribute");
const findTagByName = require("./find-tag-by-name");
const findTagsByName = require("./find-tags-by-name");
const findTagByPath = require("./find-tag-by-path");
const findTagsByPath = require("./find-tags-by-path");
const removeComments = require("./remove-comments");
const getAttribute = require("./get-attribute.js");
const findTagByName = require("./find-tag-by-name.js");
const findTagsByName = require("./find-tags-by-name.js");
const findTagByPath = require("./find-tag-by-path.js");
const findTagsByPath = require("./find-tags-by-path.js");
const removeComments = require("./remove-comments.js");
const removeTagsByName = require("./remove-tags-by-name.js");

module.exports = {
getAttribute,
findTagByName,
findTagsByName,
findTagByPath,
findTagsByPath,
removeComments
removeComments,
removeTagsByName
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"./get-attribute": "./get-attribute.js",
"./get-attribute.js": "./get-attribute.js",
"./remove-comments": "./remove-comments.js",
"./remove-comments.js": "./remove-comments.js"
"./remove-comments.js": "./remove-comments.js",
"./remove-tags-by-name": "./remove-tags-by-name",
"./remove-tags-by-name.js": "./remove-tags-by-name.js"
},
"files": [
"index.d.ts",
Expand Down
14 changes: 14 additions & 0 deletions remove-tags-by-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const findTagByName = require("./find-tag-by-name.js");

function removeTagsByName(xml, tagName, options) {
const debug = (options && options.debug) || false;
let tag;
while ((tag = findTagByName(xml, tagName, { debug }))) {
xml = xml.substring(0, tag.start) + xml.substring(tag.end);
if (debug) console.log("[xml-utils] removed:", tag);
}
return xml;
}

module.exports = removeTagsByName;
module.exports.default = removeTagsByName;
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const findTagByPath = require("../find-tag-by-path.js");
const findTagsByPath = require("../find-tags-by-path.js");
const getAttribute = require("../get-attribute.js");
const removeComments = require("../remove-comments.js");
const removeTagsByName = require("../remove-tags-by-name.js");

const iso = readFileSync("test/data/iso.xml", "utf-8");
const mrf = readFileSync("test/data/m_3008501_ne_16_1_20171018.mrf", "utf-8");
Expand Down Expand Up @@ -199,3 +200,13 @@ test("should handle nested tags", ({ eq }) => {
test("getAttribute with single quotes", ({ eq }) => {
eq(getAttribute("<link href='https://example.org'/>", "href"), "https://example.org");
});

test("removeTagsByName", ({ eq }) => {
const xml = `
<ul>
<li>A</li>
<li>B</li>
</ul>
`;
eq(removeTagsByName(xml, "li").trim(), "<ul>\n \n \n </ul>");
});
6 changes: 5 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from "flug";
// @ts-ignore
import { readFileSync } from "node:fs";
import { findTagByName, findTagsByName, findTagByPath, findTagsByPath, getAttribute } from "../index";
import { findTagByName, findTagsByName, findTagByPath, findTagsByPath, getAttribute, removeTagsByName } from "../index";

const iso = readFileSync("test/data/iso.xml", "utf-8");
const mrf = readFileSync("test/data/m_3008501_ne_16_1_20171018.mrf", "utf-8");
Expand Down Expand Up @@ -103,3 +103,7 @@ test("should handle nested tags", ({ eq }) => {
]);
eq(findTagByPath(xml, ["Thing"])!.outer, xml);
});

test("removeTagsByName", ({ eq }) => {
eq(removeTagsByName("<ul><li>A</li><li>B</li></ul>", "li"), "<ul></ul>");
});

0 comments on commit a45b9a8

Please sign in to comment.