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

feat: Added remove field to playground #444

Closed
88 changes: 51 additions & 37 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,46 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta charset="utf-8" />
<title>slug playground</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<form id="form" onSubmit="" style="max-width:60ch">
<fieldset>

<label>Input text</label>
<input type="text" id="input" placeholder="An Apple computer" autofocus>
</fieldset>



<fieldset>
<label for="replacement">Replacement character</label>
<input type="text" id="replacement" name="replacement" placeholder="-">
</fieldset>


<fieldset>
<label>
<input type="checkbox" name="lowercase" checked> Lowercase
</label>
</fieldset>

<fieldset>
<label>
<input type="checkbox" name="trim" checked> Trim leading and trailing replacement characters
</label>
</fieldset>

<fieldset>
<label>
<input type="checkbox" name="fallback" checked> Fallback
</label>
</fieldset>

<input type="submit" value="Slug it">

<form id="form" onSubmit="" style="max-width: 60ch">
<fieldset>
<label>Input text</label>
<input
type="text"
id="input"
placeholder="An Apple computer"
autofocus
/>
</fieldset>

<fieldset>
<label for="replacement">Replacement character</label>
<input
type="text"
id="replacement"
name="replacement"
placeholder="-"
/>
</fieldset>

<fieldset style="display: flex; justify-content: flex-start">
<label for="remove">Remove chars (regex)</label>
/<input type="text" id="remove" name="remove" />/<br />
<label> <input type="checkbox" name="regex_g" /> g </label>

<label> <input type="checkbox" name="regex_i" /> i </label>
</fieldset>

<fieldset>
<label>
<input type="checkbox" name="lowercase" checked /> Lowercase
</label>
</fieldset>

<fieldset>
<label>
<input type="checkbox" name="trim" checked /> Trim leading and
trailing replacement characters
</label>
</fieldset>

<fieldset>
<label>
<input type="checkbox" name="fallback" checked /> Fallback
</label>
</fieldset>

<input type="submit" value="Slug it" />
</form>
<p>Result: <span id="slugOutput"></span></p>
<script src="./slug.js"></script>
Expand Down
46 changes: 28 additions & 18 deletions playground.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
const form = document.getElementById('form')
const form = document.getElementById("form");

form.addEventListener('submit', function (e) {
e.preventDefault()
form.addEventListener("submit", function (e) {
e.preventDefault();
if (form === null) {
console.error('form element not found')
return
console.error("form element not found");
return;
}

const fd = new FormData(form)
const fd = new FormData(form);

const replacement = fd.get('replacement')
const lowercase = fd.get('lowercase')
const trim = fd.get('trim')
const fallback = fd.get('fallback')
const replacement = fd.get("replacement");
const lowercase = fd.get("lowercase");
const trim = fd.get("trim");
const fallback = fd.get("fallback");

const opts = {}
const remove = fd.get("remove");
const regex_i = fd.get("regex_i");
const regex_g = fd.get("regex_g");

const opts = {};

if (replacement.length > 0) {
opts.replacement = replacement
opts.replacement = replacement;
}

opts.lower = lowercase !== null
const regex = new RegExp(
remove,
`${regex_g !== null ? "g" : ""}${regex_i !== null ? "i" : ""}`
);
opts.remove = regex;

opts.lower = lowercase !== null;

opts.trim = trim !== null
opts.trim = trim !== null;

opts.fallback = fallback !== null
opts.fallback = fallback !== null;

const output = window.slug(document.getElementById('input').value, opts)
document.getElementById('slugOutput').innerText = output
})
const output = window.slug(document.getElementById("input").value, opts);
document.getElementById("slugOutput").innerText = output;
});
Loading