Skip to content

Commit

Permalink
Add tool to show positions of each character
Browse files Browse the repository at this point in the history
  • Loading branch information
kittsville committed Sep 9, 2024
1 parent af64fc1 commit 2940d68
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
48 changes: 48 additions & 0 deletions character-positions/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: Character Positions
tagline: Numbers the position of each character
layout: default
---

<button id="see-example" class="mdc-button mdc-button--raised" type="button">
<div class="mdc-button__ripple" aria-hidden="true"></div>
<i class="material-icons mdc-button__icon" aria-hidden="true">visibility</i>
<span class="mdc-button__label">Show Example</span>
</button>

<label
class="mdc-text-field mdc-text-field--outlined mdc-text-field--textarea mdc-text-field--no-label"
>
<span class="mdc-notched-outline">
<span class="mdc-notched-outline__leading"></span>
<span class="mdc-notched-outline__trailing"></span>
</span>
<span class="mdc-text-field__resizer">
<textarea
id="input-text"
class="mdc-text-field__input"
aria-label="Paste input"
></textarea>
</span>
</label>

<div id="output"></div>

<template id="table-template">
<table>
<thead>
<tr>
<th>#</th>
<th>Char</th>
</tr>
</thead>
<tbody></tbody>
</table>
</template>

<template id="row-template">
<tr>
<th></th>
<td></td>
</tr>
</template>
44 changes: 44 additions & 0 deletions character-positions/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const textarea = document.getElementById("input-text");

const snakeCaseText = (input) => {
const matches = input.match(matchBlocks);

if (matches) {
return matches.map((part) => part.toLowerCase()).join("_");
} else {
return "Failed to parse";
}
};

textarea.addEventListener("input", (e) => {
const outputEl = document.getElementById("output");
outputEl.textContent = "";
const template = document
.getElementById("table-template")
.content.cloneNode(true);
const tbody = template.querySelector("tbody");

e.target.value.split("").forEach((character, index) => {
const tr = document.getElementById("row-template").content.cloneNode(true)
.children[0];

tr.querySelector("th").textContent = index + 1;
tr.querySelector("td").textContent = character;

tbody.appendChild(tr);
});

outputEl.appendChild(template);
});

const exampleText = `FooBarBux`;

document.getElementById("see-example").addEventListener("click", () => {
textarea.value = exampleText;
textarea.dispatchEvent(
new Event("input", {
bubbles: true,
cancelable: true,
})
);
});
11 changes: 11 additions & 0 deletions character-positions/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
label.mdc-text-field--textarea,
pre {
margin: auto;
clear: both;
display: block;
}

button {
margin: 15px auto;
text-align: center;
}

0 comments on commit 2940d68

Please sign in to comment.