Add shared hashtag normalization helper

This commit is contained in:
thePR0M3TH3AN committed 2026-01-18 13:35:34 -05:00
1 parent cedc2a91d0
commit 0ac64867da
10 files changed
+145 -77

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
// js/utils/hashtagNormalization.js
const HASH_PREFIX_PATTERN = /^#+/;
/**
* Normalize hashtags consistently across storage, matching, and UI validation.
*
* Normalization rules:
* 1) Trim surrounding whitespace.
* 2) Strip leading "#" characters.
* 3) Lowercase the remaining value.
*
* Note: We do not strip punctuation today. Only add punctuation stripping if
* product explicitly decides to change the rules.
*
* @param {string} value
* @returns {string} normalized hashtag or empty string when invalid.
*/
export function normalizeHashtag(value) {
if (typeof value !== "string") {
return "";
}
const trimmed = value.trim();
if (!trimmed) {
return "";
}
const withoutHashes = trimmed.replace(HASH_PREFIX_PATTERN, "").trim();
if (!withoutHashes) {
return "";
}
return withoutHashes.toLowerCase();
}
/**
* Format a hashtag for display, ensuring a single leading hash.
*
* @param {string} value
* @returns {string}
*/
export function formatHashtag(value) {
const normalized = normalizeHashtag(value);
if (!normalized) {
return "";
}
return `#${normalized}`;
}