diff options
author | Minteck <contact@minteck.org> | 2022-10-18 08:59:09 +0200 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-10-18 08:59:09 +0200 |
commit | 2c4ae43e688a9873e86211ea0e7aeb9ba770dd77 (patch) | |
tree | 17848d95522dab25d3cdeb9c4a6450e2a234861f /alarm/node_modules/string-strip-html/examples | |
parent | 108525534c28013cfe1897c30e4565f9893f3766 (diff) | |
download | pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.gz pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.tar.bz2 pluralconnect-2c4ae43e688a9873e86211ea0e7aeb9ba770dd77.zip |
Update
Diffstat (limited to 'alarm/node_modules/string-strip-html/examples')
13 files changed, 417 insertions, 0 deletions
diff --git a/alarm/node_modules/string-strip-html/examples/_quickTake.js b/alarm/node_modules/string-strip-html/examples/_quickTake.js new file mode 100644 index 0000000..741f1ea --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/_quickTake.js @@ -0,0 +1,28 @@ +// Quick Take + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +assert.equal( + stripHtml(`Some text <b>and</b> text.`).result, + `Some text and text.` +); + +// prevents accidental string concatenation +assert.equal(stripHtml(`aaa<div>bbb</div>ccc`).result, `aaa bbb ccc`); + +// tag pairs with content, upon request +assert.equal( + stripHtml(`a <pre><code>void a;</code></pre> b`, { + stripTogetherWithTheirContents: [ + "script", // default + "style", // default + "xml", // default + "pre", // <-- custom-added + ], + }).result, + `a b` +); + +// detects raw, legit brackets: +assert.equal(stripHtml(`a < b and c > d`).result, `a < b and c > d`); diff --git a/alarm/node_modules/string-strip-html/examples/api.json b/alarm/node_modules/string-strip-html/examples/api.json new file mode 100644 index 0000000..28adb26 --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/api.json @@ -0,0 +1 @@ +{"_quickTake.js":{"title":"Quick Take","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nassert.equal(\n stripHtml(`Some text <b>and</b> text.`).result,\n `Some text and text.`\n);\n\n// prevents accidental string concatenation\nassert.equal(stripHtml(`aaa<div>bbb</div>ccc`).result, `aaa bbb ccc`);\n\n// tag pairs with content, upon request\nassert.equal(\n stripHtml(`a <pre><code>void a;</code></pre> b`, {\n stripTogetherWithTheirContents: [\n \"script\", // default\n \"style\", // default\n \"xml\", // default\n \"pre\", // <-- custom-added\n ],\n }).result,\n `a b`\n);\n\n// detects raw, legit brackets:\nassert.equal(stripHtml(`a < b and c > d`).result, `a < b and c > d`);"},"cb-which-does-nothing.js":{"title":"A Bypass Callback and a Do-Nothing Callback","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\n// this callback just pushes proposed result to \"rangesArr\",\n// that's what gets used in the result calculation:\nconst cb1 = ({\n tag,\n deleteFrom,\n deleteTo,\n insert,\n rangesArr,\n proposedReturn,\n}) => {\n rangesArr.push(deleteFrom, deleteTo, insert);\n};\nconst result1 = stripHtml(\"abc<hr>def\", { cb: cb1 }).result;\nassert.equal(result1, `abc def`);\n\n// to prove it works, don't do anything:\nconst cb2 = ({\n tag,\n deleteFrom,\n deleteTo,\n insert,\n rangesArr,\n proposedReturn,\n}) => {\n // nothing here π\n};\nconst result2 = stripHtml(\"abc<hr>def\", { cb: cb2 }).result;\nassert.equal(result2, \"abc<hr>def\");"},"extract-html-head-contents.js":{"title":"Extract HTML `<head>` contents","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `<!DOCTYPE html>\n<html lang=\"en\" dir=\"ltr\">\n <head>\n <meta charset=\"utf-8\">\n <title>the title</title>\n </head>\n <body>\n the content\n </body>\n</html>`;\n\n// The task asks not to include <head...> and </head>.\n// First, extract head tag-to-head tag, including contents\nconst headWithHeadTags = stripHtml(someHtml, {\n onlyStripTags: [\"head\"],\n stripTogetherWithTheirContents: [\"head\"],\n})\n .filteredTagLocations.reduce(\n (acc, [from, to]) => `${acc}${someHtml.slice(from, to)}`,\n \"\"\n )\n .trim();\n\nassert.equal(\n headWithHeadTags,\n `<head>\n <meta charset=\"utf-8\">\n <title>the title</title>\n </head>`\n);\n\nconst headContents = headWithHeadTags.replace(/<\\/?head>/g, \"\").trim();\nassert.equal(\n headContents,\n `<meta charset=\"utf-8\">\n <title>the title</title>`\n);"},"inline-tags.js":{"title":"Just deletes inline tags","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `This has an <b>un</b>bold word.`;\n\n// default behaviour:\nassert.equal(stripHtml(someHtml).result, `This has an un bold word.`);\n\n// let's tackle inline tags:\nassert.equal(\n stripHtml(someHtml, {\n cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr }) => {\n if ([\"b\", \"strong\"].includes(tag.name)) {\n rangesArr.push(tag.lastOpeningBracketAt, tag.lastClosingBracketAt + 1);\n } else {\n rangesArr.push(deleteFrom, deleteTo, insert);\n }\n },\n }).result,\n `This has an unbold word.`\n);"},"leave-only-html.js":{"title":"Leave only HTML","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `<!DOCTYPE html>\n<html lang=\"en\" dir=\"ltr\">\n <head>\n <meta charset=\"utf-8\">\n <title></title>\n </head>\n <body>\n <h1>Title</h1>\n Some text.\n </body>\n</html>`;\n\nassert.equal(\n stripHtml(someHtml).allTagLocations.reduce(\n (acc, [from, to]) => `${acc}${someHtml.slice(from, to)}`,\n \"\"\n ),\n `<!DOCTYPE html><html lang=\"en\" dir=\"ltr\"><head><meta charset=\"utf-8\"><title></title></head><body><h1></h1></body></html>`\n);"},"leave-only-opening-td.js":{"title":"Leave only opening `td` tags","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `<table width=\"100\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n <tr>\n <td class=\"col1\">\n cell1\n </td>\n <td class=\"col2\">\n cell2\n </td>\n </tr>\n <tr>\n <td class=\"col3\">\n cell3\n </td>\n <td class=\"col4\">\n cell4\n </td>\n </tr>\n</table>`;\n\n// the first way\n// -----------------------------------------------------------------------------\n\nassert.equal(\n stripHtml(someHtml, {\n // notice there's no: onlyStripTags: [\"td\"]\n // we operate purely via callback\n cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr, proposedReturn }) => {\n if (tag.name === \"td\" && !tag.slashPresent) {\n rangesArr.push(proposedReturn);\n }\n },\n }).ranges.reduce(\n (acc, [from, to]) => `${acc}${someHtml.slice(from, to).trim()}`,\n \"\"\n ),\n `<td class=\"col1\"><td class=\"col2\"><td class=\"col3\"><td class=\"col4\">`\n);\n\n// the second way:\n// -----------------------------------------------------------------------------\n\nlet resultStr = \"\";\n// notice we don't even assign stripHtml() output to anything - we rely only\n// on the callback, it mutates the \"resultStr\" in the upper scope\nstripHtml(someHtml, {\n // notice there's no: onlyStripTags: [\"td\"]\n // we operate purely via callback\n cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr, proposedReturn }) => {\n if (tag.name === \"td\" && !tag.slashPresent) {\n resultStr += someHtml.slice(deleteFrom, deleteTo).trim();\n }\n },\n});\nassert.equal(\n resultStr,\n `<td class=\"col1\"><td class=\"col2\"><td class=\"col3\"><td class=\"col4\">`\n);"},"leave-only-td.js":{"title":"Leave only `td` tags","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `<table width=\"100\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n <tr>\n <td class=\"col1\">\n cell1\n </td>\n <td class=\"col2\">\n cell2\n </td>\n </tr>\n <tr>\n <td class=\"col3\">\n cell3\n </td>\n <td class=\"col4\">\n cell4\n </td>\n </tr>\n</table>`;\n\nassert.equal(\n stripHtml(someHtml, {\n onlyStripTags: [\"td\"],\n }).filteredTagLocations.reduce(\n (acc, [from, to]) => `${acc}${someHtml.slice(from, to)}`,\n \"\"\n ),\n `<td class=\"col1\"></td><td class=\"col2\"></td><td class=\"col3\"></td><td class=\"col4\"></td>`\n);"},"minimal-ranges.js":{"title":"Minimal example using Ranges","content":"// We strip tags and fix apostrophes\n// that's part of what https://codsen.com/os/detergent/ does\n\nimport { strict as assert } from \"assert\";\nimport { rApply } from \"ranges-apply\";\nimport { stripHtml } from \"string-strip-html\";\nimport { convertAll } from \"string-apostrophes\";\n\nfunction stripAndFixApos(str) {\n if (!str || typeof str !== \"string\") {\n return \"\";\n }\n // Keep in mind, Ranges are array of 2-3 element arrays.\n // But absent Ranges are marked as null, not empty array.\n // It's so that we could test in \"if-else\" easily - null\n // is falsy but empty array is truthy.\n // That's why below we take precautions with \"|| []\".\n return rApply(\n str,\n (stripHtml(str).ranges || []).concat(convertAll(str).ranges || [])\n );\n}\n\n// strips tags and fixes apostrophes:\nassert.equal(\n stripAndFixApos(`Let's Go <strong>Larval</strong>`),\n `Letβs Go Larval`\n);\n\n// no tags, no apostrophes:\nassert.equal(stripAndFixApos(`zzz`), `zzz`);"},"remove-html.js":{"title":"Remove all HTML from a string","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\n\nconst someHtml = `<!DOCTYPE html>\n<html lang=\"en\" dir=\"ltr\">\n <head>\n <meta charset=\"utf-8\">\n <title></title>\n </head>\n <body>\n <h1>Title</h1>\n Some text.\n </body>\n</html>`;\n\nassert.equal(stripHtml(someHtml).result, `Title\\nSome text.`);"},"strip-from-json.js":{"title":"Strip HTML from a raw JSON string","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\nimport { traverse } from \"ast-monkey-traverse\";\n\nconst stripFromJsonStr = (str) => {\n return traverse(JSON.parse(str), (key, val) => {\n // if currently an object is traversed, you get both \"key\" and \"val\"\n // if it's array, only \"key\" is present, \"val\" is undefined\n const current = val !== undefined ? val : key;\n if (\n // ensure it's a plain object, not array (monkey will report only \"key\" in\n // arrays and \"val\" will be undefined)\n // also ensure object's value a string, not boolean or number, because we\n // don't strip HTML from booleans or numbers or anything else than strings\n typeof val === \"string\"\n ) {\n // monkey's callback is like Array.map - whatever you return gets written:\n return stripHtml(val).result;\n }\n // default return, do nothing:\n return current;\n });\n};\n\n// nothing to strip, \"<\" is false alarm:\nassert.equal(\n JSON.stringify(stripFromJsonStr(`{\"Operator\":\"<\",\"IsValid\":true}`), null, 0),\n `{\"Operator\":\"<\",\"IsValid\":true}`\n);\n\n// some HTML within one of key values, monkey will skip the boolean:\nassert.equal(\n JSON.stringify(\n stripFromJsonStr(`{\"Operator\":\"a <div>b</div> c\",\"IsValid\":true}`),\n null,\n 0\n ),\n `{\"Operator\":\"a b c\",\"IsValid\":true}`\n);"},"title-case-with-tag-skipping.js":{"title":"Set the title case using `title` package","content":"// This program will not touch any single tags (<br class=\"z\"/> for example)\n// or in case of paired tags, paired tags and content between\n\nimport { strict as assert } from \"assert\";\nimport title from \"title\";\nimport { rInvert } from \"ranges-invert\";\nimport { rApply } from \"ranges-apply\";\nimport { rRegex } from \"ranges-regex\";\nimport { stripHtml } from \"string-strip-html\";\n\nfunction tagAwareTitle(str) {\n const whitelist = [\"eslint\", \"readme\", \"npm\"];\n const { filteredTagLocations } = stripHtml(str, {\n stripTogetherWithTheirContents: [\"*\"],\n });\n const inverted = rInvert(\n filteredTagLocations.concat(\n whitelist.reduce((acc, curr) => {\n const rangesFindings = rRegex(new RegExp(curr, \"gi\"), str);\n if (rangesFindings) {\n return acc.concat(rangesFindings);\n }\n return acc;\n }, [])\n ),\n str.length\n );\n\n if (Array.isArray(inverted) && inverted.length) {\n // take inverted ranges, for example, [[3, 4], [10, 15]]\n // and add third element, replacement, which is same character\n // indexes only processed through \"title\":\n return rApply(\n str,\n inverted.map(([from, to]) => [from, to, title(str.slice(from, to))])\n );\n }\n // otherwise, just apply title() on the whole string:\n return title(str);\n}\n\n// middle:\nassert.equal(\n tagAwareTitle(`This is a title with some <code>code</code> in it`),\n `This Is a Title with Some <code>code</code> In It`\n);\n\n// leading:\nassert.equal(\n tagAwareTitle(`<span class=\"xyz\">abc<span> defgh ESLint`),\n `<span class=\"xyz\">abc<span> Defgh ESLint`\n);"},"widow-word-removal-from-html.js":{"title":"Widow word removal from text within HTML","content":"import { strict as assert } from \"assert\";\nimport { stripHtml } from \"string-strip-html\";\nimport { removeWidows } from \"string-remove-widows\";\n\nconst someHtml = `The quick brown fox jumps of the lazy dog.<div class=\"a\">`;\n\n// default widow word removal libs are not aware of HTML:\n// -----------------------------------------------------------------------------\n\nassert.equal(\n removeWidows(someHtml).res,\n `The quick brown fox jumps of the lazy dog.<div class=\"a\">` // π±\n);\n\n// luckily, removeWidows() consumes optional HTML tag locations\nassert.equal(\n removeWidows(someHtml, {\n tagRanges: stripHtml(someHtml)\n // remove the third argument, what to insert (\" \" string in these cases)\n .ranges.map(([from, to]) => [from, to]),\n }).res,\n `The quick brown fox jumps of the lazy dog.<div class=\"a\">` // β
\n);"}}
\ No newline at end of file diff --git a/alarm/node_modules/string-strip-html/examples/cb-which-does-nothing.js b/alarm/node_modules/string-strip-html/examples/cb-which-does-nothing.js new file mode 100644 index 0000000..2145c46 --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/cb-which-does-nothing.js @@ -0,0 +1,33 @@ +// A Bypass Callback and a Do-Nothing Callback + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +// this callback just pushes proposed result to "rangesArr", +// that's what gets used in the result calculation: +const cb1 = ({ + tag, + deleteFrom, + deleteTo, + insert, + rangesArr, + proposedReturn, +}) => { + rangesArr.push(deleteFrom, deleteTo, insert); +}; +const result1 = stripHtml("abc<hr>def", { cb: cb1 }).result; +assert.equal(result1, `abc def`); + +// to prove it works, don't do anything: +const cb2 = ({ + tag, + deleteFrom, + deleteTo, + insert, + rangesArr, + proposedReturn, +}) => { + // nothing here π +}; +const result2 = stripHtml("abc<hr>def", { cb: cb2 }).result; +assert.equal(result2, "abc<hr>def"); diff --git a/alarm/node_modules/string-strip-html/examples/extract-html-head-contents.js b/alarm/node_modules/string-strip-html/examples/extract-html-head-contents.js new file mode 100644 index 0000000..5c09d52 --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/extract-html-head-contents.js @@ -0,0 +1,42 @@ +// Extract HTML `<head>` contents + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +const someHtml = `<!DOCTYPE html> +<html lang="en" dir="ltr"> + <head> + <meta charset="utf-8"> + <title>the title</title> + </head> + <body> + the content + </body> +</html>`; + +// The task asks not to include <head...> and </head>. +// First, extract head tag-to-head tag, including contents +const headWithHeadTags = stripHtml(someHtml, { + onlyStripTags: ["head"], + stripTogetherWithTheirContents: ["head"], +}) + .filteredTagLocations.reduce( + (acc, [from, to]) => `${acc}${someHtml.slice(from, to)}`, + "" + ) + .trim(); + +assert.equal( + headWithHeadTags, + `<head> + <meta charset="utf-8"> + <title>the title</title> + </head>` +); + +const headContents = headWithHeadTags.replace(/<\/?head>/g, "").trim(); +assert.equal( + headContents, + `<meta charset="utf-8"> + <title>the title</title>` +); diff --git a/alarm/node_modules/string-strip-html/examples/inline-tags.js b/alarm/node_modules/string-strip-html/examples/inline-tags.js new file mode 100644 index 0000000..622999a --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/inline-tags.js @@ -0,0 +1,23 @@ +// Just deletes inline tags + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +const someHtml = `This has an <b>un</b>bold word.`; + +// default behaviour: +assert.equal(stripHtml(someHtml).result, `This has an un bold word.`); + +// let's tackle inline tags: +assert.equal( + stripHtml(someHtml, { + cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr }) => { + if (["b", "strong"].includes(tag.name)) { + rangesArr.push(tag.lastOpeningBracketAt, tag.lastClosingBracketAt + 1); + } else { + rangesArr.push(deleteFrom, deleteTo, insert); + } + }, + }).result, + `This has an unbold word.` +); diff --git a/alarm/node_modules/string-strip-html/examples/leave-only-html.js b/alarm/node_modules/string-strip-html/examples/leave-only-html.js new file mode 100644 index 0000000..93e21a8 --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/leave-only-html.js @@ -0,0 +1,24 @@ +// Leave only HTML + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +const someHtml = `<!DOCTYPE html> +<html lang="en" dir="ltr"> + <head> + <meta charset="utf-8"> + <title></title> + </head> + <body> + <h1>Title</h1> + Some text. + </body> +</html>`; + +assert.equal( + stripHtml(someHtml).allTagLocations.reduce( + (acc, [from, to]) => `${acc}${someHtml.slice(from, to)}`, + "" + ), + `<!DOCTYPE html><html lang="en" dir="ltr"><head><meta charset="utf-8"><title></title></head><body><h1></h1></body></html>` +); diff --git a/alarm/node_modules/string-strip-html/examples/leave-only-opening-td.js b/alarm/node_modules/string-strip-html/examples/leave-only-opening-td.js new file mode 100644 index 0000000..e8ba37f --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/leave-only-opening-td.js @@ -0,0 +1,62 @@ +// Leave only opening `td` tags + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +const someHtml = `<table width="100" border="0" cellpadding="0" cellspacing="0"> + <tr> + <td class="col1"> + cell1 + </td> + <td class="col2"> + cell2 + </td> + </tr> + <tr> + <td class="col3"> + cell3 + </td> + <td class="col4"> + cell4 + </td> + </tr> +</table>`; + +// the first way +// ----------------------------------------------------------------------------- + +assert.equal( + stripHtml(someHtml, { + // notice there's no: onlyStripTags: ["td"] + // we operate purely via callback + cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr, proposedReturn }) => { + if (tag.name === "td" && !tag.slashPresent) { + rangesArr.push(proposedReturn); + } + }, + }).ranges.reduce( + (acc, [from, to]) => `${acc}${someHtml.slice(from, to).trim()}`, + "" + ), + `<td class="col1"><td class="col2"><td class="col3"><td class="col4">` +); + +// the second way: +// ----------------------------------------------------------------------------- + +let resultStr = ""; +// notice we don't even assign stripHtml() output to anything - we rely only +// on the callback, it mutates the "resultStr" in the upper scope +stripHtml(someHtml, { + // notice there's no: onlyStripTags: ["td"] + // we operate purely via callback + cb: ({ tag, deleteFrom, deleteTo, insert, rangesArr, proposedReturn }) => { + if (tag.name === "td" && !tag.slashPresent) { + resultStr += someHtml.slice(deleteFrom, deleteTo).trim(); + } + }, +}); +assert.equal( + resultStr, + `<td class="col1"><td class="col2"><td class="col3"><td class="col4">` +); diff --git a/alarm/node_modules/string-strip-html/examples/leave-only-td.js b/alarm/node_modules/string-strip-html/examples/leave-only-td.js new file mode 100644 index 0000000..af0b2cd --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/leave-only-td.js @@ -0,0 +1,33 @@ +// Leave only `td` tags + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +const someHtml = `<table width="100" border="0" cellpadding="0" cellspacing="0"> + <tr> + <td class="col1"> + cell1 + </td> + <td class="col2"> + cell2 + </td> + </tr> + <tr> + <td class="col3"> + cell3 + </td> + <td class="col4"> + cell4 + </td> + </tr> +</table>`; + +assert.equal( + stripHtml(someHtml, { + onlyStripTags: ["td"], + }).filteredTagLocations.reduce( + (acc, [from, to]) => `${acc}${someHtml.slice(from, to)}`, + "" + ), + `<td class="col1"></td><td class="col2"></td><td class="col3"></td><td class="col4"></td>` +); diff --git a/alarm/node_modules/string-strip-html/examples/minimal-ranges.js b/alarm/node_modules/string-strip-html/examples/minimal-ranges.js new file mode 100644 index 0000000..91dcbf7 --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/minimal-ranges.js @@ -0,0 +1,33 @@ +// Minimal example using Ranges + +// We strip tags and fix apostrophes +// that's part of what https://codsen.com/os/detergent/ does + +import { strict as assert } from "assert"; +import { rApply } from "../../ranges-apply"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; +import { convertAll } from "../../string-apostrophes"; + +function stripAndFixApos(str) { + if (!str || typeof str !== "string") { + return ""; + } + // Keep in mind, Ranges are array of 2-3 element arrays. + // But absent Ranges are marked as null, not empty array. + // It's so that we could test in "if-else" easily - null + // is falsy but empty array is truthy. + // That's why below we take precautions with "|| []". + return rApply( + str, + (stripHtml(str).ranges || []).concat(convertAll(str).ranges || []) + ); +} + +// strips tags and fixes apostrophes: +assert.equal( + stripAndFixApos(`Let's Go <strong>Larval</strong>`), + `Letβs Go Larval` +); + +// no tags, no apostrophes: +assert.equal(stripAndFixApos(`zzz`), `zzz`); diff --git a/alarm/node_modules/string-strip-html/examples/remove-html.js b/alarm/node_modules/string-strip-html/examples/remove-html.js new file mode 100644 index 0000000..b45894e --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/remove-html.js @@ -0,0 +1,18 @@ +// Remove all HTML from a string + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +const someHtml = `<!DOCTYPE html> +<html lang="en" dir="ltr"> + <head> + <meta charset="utf-8"> + <title></title> + </head> + <body> + <h1>Title</h1> + Some text. + </body> +</html>`; + +assert.equal(stripHtml(someHtml).result, `Title\nSome text.`); diff --git a/alarm/node_modules/string-strip-html/examples/strip-from-json.js b/alarm/node_modules/string-strip-html/examples/strip-from-json.js new file mode 100644 index 0000000..95d8bd0 --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/strip-from-json.js @@ -0,0 +1,41 @@ +// Strip HTML from a raw JSON string + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; +import { traverse } from "../../ast-monkey-traverse"; + +const stripFromJsonStr = (str) => { + return traverse(JSON.parse(str), (key, val) => { + // if currently an object is traversed, you get both "key" and "val" + // if it's array, only "key" is present, "val" is undefined + const current = val !== undefined ? val : key; + if ( + // ensure it's a plain object, not array (monkey will report only "key" in + // arrays and "val" will be undefined) + // also ensure object's value a string, not boolean or number, because we + // don't strip HTML from booleans or numbers or anything else than strings + typeof val === "string" + ) { + // monkey's callback is like Array.map - whatever you return gets written: + return stripHtml(val).result; + } + // default return, do nothing: + return current; + }); +}; + +// nothing to strip, "<" is false alarm: +assert.equal( + JSON.stringify(stripFromJsonStr(`{"Operator":"<","IsValid":true}`), null, 0), + `{"Operator":"<","IsValid":true}` +); + +// some HTML within one of key values, monkey will skip the boolean: +assert.equal( + JSON.stringify( + stripFromJsonStr(`{"Operator":"a <div>b</div> c","IsValid":true}`), + null, + 0 + ), + `{"Operator":"a b c","IsValid":true}` +); diff --git a/alarm/node_modules/string-strip-html/examples/title-case-with-tag-skipping.js b/alarm/node_modules/string-strip-html/examples/title-case-with-tag-skipping.js new file mode 100644 index 0000000..c248346 --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/title-case-with-tag-skipping.js @@ -0,0 +1,54 @@ +// Set the title case using `title` package + +// This program will not touch any single tags (<br class="z"/> for example) +// or in case of paired tags, paired tags and content between + +import { strict as assert } from "assert"; +import title from "title"; +import { rInvert } from "../../ranges-invert"; +import { rApply } from "../../ranges-apply"; +import { rRegex } from "../../ranges-regex"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; + +function tagAwareTitle(str) { + const whitelist = ["eslint", "readme", "npm"]; + const { filteredTagLocations } = stripHtml(str, { + stripTogetherWithTheirContents: ["*"], + }); + const inverted = rInvert( + filteredTagLocations.concat( + whitelist.reduce((acc, curr) => { + const rangesFindings = rRegex(new RegExp(curr, "gi"), str); + if (rangesFindings) { + return acc.concat(rangesFindings); + } + return acc; + }, []) + ), + str.length + ); + + if (Array.isArray(inverted) && inverted.length) { + // take inverted ranges, for example, [[3, 4], [10, 15]] + // and add third element, replacement, which is same character + // indexes only processed through "title": + return rApply( + str, + inverted.map(([from, to]) => [from, to, title(str.slice(from, to))]) + ); + } + // otherwise, just apply title() on the whole string: + return title(str); +} + +// middle: +assert.equal( + tagAwareTitle(`This is a title with some <code>code</code> in it`), + `This Is a Title with Some <code>code</code> In It` +); + +// leading: +assert.equal( + tagAwareTitle(`<span class="xyz">abc<span> defgh ESLint`), + `<span class="xyz">abc<span> Defgh ESLint` +); diff --git a/alarm/node_modules/string-strip-html/examples/widow-word-removal-from-html.js b/alarm/node_modules/string-strip-html/examples/widow-word-removal-from-html.js new file mode 100644 index 0000000..bcc07b1 --- /dev/null +++ b/alarm/node_modules/string-strip-html/examples/widow-word-removal-from-html.js @@ -0,0 +1,25 @@ +// Widow word removal from text within HTML + +import { strict as assert } from "assert"; +import { stripHtml } from "../dist/string-strip-html.esm.js"; +import { removeWidows } from "../../string-remove-widows"; + +const someHtml = `The quick brown fox jumps of the lazy dog.<div class="a">`; + +// default widow word removal libs are not aware of HTML: +// ----------------------------------------------------------------------------- + +assert.equal( + removeWidows(someHtml).res, + `The quick brown fox jumps of the lazy dog.<div class="a">` // π± +); + +// luckily, removeWidows() consumes optional HTML tag locations +assert.equal( + removeWidows(someHtml, { + tagRanges: stripHtml(someHtml) + // remove the third argument, what to insert (" " string in these cases) + .ranges.map(([from, to]) => [from, to]), + }).res, + `The quick brown fox jumps of the lazy dog.<div class="a">` // β
+); |