Skip to content

Commit

Permalink
Merge pull request #4 from DanielJDufour/remove-comments
Browse files Browse the repository at this point in the history
remove comments
  • Loading branch information
DanielJDufour authored Sep 22, 2022
2 parents 2868153 + 491e299 commit 2a92f27
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ const tags = findTagByPath(xml, ["Metadata", "MDI"]);
// tags is an array of tags
```

## remove comments
```javascript
const removeComments = require("xml-utils/remove-comments");
const xml = `<list>
<!--<A/>-->
<B/>
</list>`;
removeComments(xml);
"<list>\n \n<B/><list>";
```


## setup
download test files with:
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ 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");

module.exports = {
getAttribute,
findTagByName,
findTagsByName,
findTagByPath,
findTagsByPath
findTagsByPath,
removeComments
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"find-tags-by-path.js",
"get-attribute.js",
"index-of-match.js",
"index-of-match-end.js"
"index-of-match-end.js",
"remove-comments.js"
],
"scripts": {
"f": "npm run format",
Expand Down
6 changes: 6 additions & 0 deletions remove-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function removeComments(xml) {
return xml.replace(/<!--[^]*-->/g, "");
}

module.exports = removeComments;
module.exports.default = removeComments;
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,26 @@ 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 getAttribute = require("../get-attribute.js");
const removeComments = require("../remove-comments.js");

const iso = readFileSync("test/data/iso.xml", "utf-8");
const mrf = readFileSync("test/data/m_3008501_ne_16_1_20171018.mrf", "utf-8");
const tiffAux = readFileSync("test/data/rgb_raster.tif.aux.xml", "utf-8");

const nested = "<Thing><Thing attr=1></Thing><Thing attr=2></Thing></Thing>";

const commented = `<Thing>
<!--
<Thing attr=1></Thing>
-->
<Thing attr=2></Thing>
</Thing>`;

test("removing comments", ({ eq }) => {
eq(removeComments(commented), "<Thing>\n\n<Thing attr=2></Thing>\n</Thing>");
eq(removeComments("<A><!--<B/>--><!--<C/>--></A>"), "<A></A>");
});

test("count substring", ({ eq }) => {
eq(countSubstring(nested, "<namespace:name"), 0);
eq(countSubstring(nested, "<Test"), 0);
Expand Down
4 changes: 1 addition & 3 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import test from "flug";
import { readFileSync } from "fs";
import { readFileSync } from "node:fs";
import { findTagByName, findTagsByName, findTagByPath, findTagsByPath, getAttribute } 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");
const tiffAux = readFileSync("test/data/rgb_raster.tif.aux.xml", "utf-8");

const nested = "<Thing><Thing attr=1></Thing><Thing attr=2></Thing></Thing>";

test("should find all the urls in iso.xml", ({ eq }) => {
const urls = findTagsByName(iso, "gmd:URL");
eq(urls[0].inner, "http://geomap.arpa.veneto.it/layers/geonode%3Aatlanteil");
Expand Down

0 comments on commit 2a92f27

Please sign in to comment.