Slugs, versions, entities and graphemes
Slugify a title, compare semantic versions, escape HTML entities, and count graphemes.
Apache-2.0Runs on this page
Package: @awacloud/fw
Slugify and truncate a title, compare a semantic version against a range, escape text for HTML and decode entities back, and count graphemes after Unicode normalisation.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
Any page that turns user text into URLs, previews or markup, or that reads version strings from a manifest and has to decide whether they fit a range.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link. - Keep the modules you use;
strbringsunicodealong throughdeps.
What it shows
str.slug(diacritics stripped, lower-case, hyphens),str.truncateon a word boundary with an ellipsis,str.escapeHTML.htmlEntities.decodeHtmlturning the escaped text back, including named entities.unicode.normalize(text, 'NFC')andunicode.graphemesso a combining accent or an emoji sequence counts as one character.semver.valid/parse/satisfies/comparewith pre-release handling and an explicit message for invalid input.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { str } from '@awacloud/fw/io/text/str.js';
import { semver } from '@awacloud/fw/io/text/semver.js';
import { htmlEntities } from '@awacloud/fw/io/text/html-entities.js';
import { unicode } from '@awacloud/fw/io/text/unicode.js';
fw.runtime.registerAllDeep([str, semver, htmlEntities, unicode]);
const STR = fw.runtime.resolve('str');
const SEMVER = fw.runtime.resolve('semver');
const ENTITIES = fw.runtime.resolve('htmlEntities');
const UNICODE = fw.runtime.resolve('unicode');
/**
* @param {string} title free text, possibly with markup characters
* @returns {{ slug: string, short: string, escaped: string, decoded: string, graphemes: number, units: number }}
*/
export function describeTitle(title) {
const nfc = UNICODE.normalize(title, 'NFC');
const escaped = STR.escapeHTML(nfc);
return {
slug: STR.slug(nfc),
short: STR.truncate(nfc, 24),
escaped,
decoded: ENTITIES.decodeHtml(escaped),
graphemes: [...UNICODE.graphemes(nfc)].length,
units: nfc.length,
};
}
/**
* @param {string} version
* @param {string} range
* @returns {string} a one-line verdict
*/
export function checkVersion(version, range) {
if (!SEMVER.valid(version)) return `"${version}" is not a valid semantic version`;
const parsed = SEMVER.parse(version);
const prerelease = parsed.prerelease.length ? ` (pre-release ${parsed.prerelease.join('.')})` : '';
let verdict;
try {
verdict = SEMVER.satisfies(version, range) ? `satisfies ${range}` : `does not satisfy ${range}`;
} catch {
return `"${range}" is not a valid range`;
}
const order = SEMVER.compare(version, '1.4.0');
let relation = 'equal';
if (order < 0) relation = 'lower';
if (order > 0) relation = 'higher';
return `${version}${prerelease} ${verdict}; compared with 1.4.0 it is ${relation}`;
}
/** @param {string} id */
const field = (id) => /** @type {HTMLInputElement} */ (document.getElementById(id));
/** @param {string} id @param {string} value */
const show = (id, value) => { /** @type {HTMLElement} */ (document.getElementById(id)).textContent = value; };
function updateTitle() {
const d = describeTitle(field('title').value);
show('slug', d.slug);
show('short', d.short);
show('escaped', d.escaped);
show('decoded', d.decoded);
show('graphemes', `${d.graphemes} / ${d.units}`);
}
function updateVersion() {
show('version', checkVersion(field('a').value.trim(), field('b').value.trim()));
}
field('title').addEventListener('input', updateTitle);
field('a').addEventListener('input', updateVersion);
field('b').addEventListener('input', updateVersion);
updateTitle();
updateVersion();index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Slugs, versions, entities and graphemes</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Slugs, versions, entities and graphemes</h1>
<label for="title">Title</label>
<input id="title" type="text" value="Crème brûlée & the 10 <best> desserts of 2026" autocomplete="off">
<dl>
<dt>Slug</dt><dd><output id="slug"></output></dd>
<dt>Truncated to 24</dt><dd><output id="short"></output></dd>
<dt>Escaped for HTML, then decoded back</dt>
<dd><output id="escaped"></output> → <output id="decoded"></output></dd>
<dt>Graphemes / UTF-16 units (after NFC)</dt><dd><output id="graphemes"></output></dd>
</dl>
<label for="a">Version</label>
<input id="a" type="text" value="1.4.0-beta.2" autocomplete="off">
<label for="b">Range</label>
<input id="b" type="text" value="^1.2.0" autocomplete="off">
<p><output id="version"></output></p>
</main>
</body>
</html>