import { accentMap, emptyMap, compareMatch, cleanPath, cleanPattern, random, basename, dirname, extname, telExpand, telColapse, strip_tags, shortString } from '@pim.sk/utils/strings.mjs'
Removes diacritics and accent marks — replaces characters like á, č, ž, ü with their base ASCII equivalents. Uses Unicode NFD normalisation. Optional addPatterns allows extra replacements (default handles ß→S).
accentMap( "Ján Novák" ) // → "Jan Novak"
accentMap( "München" ) // → "Munchen" (ü, ü → u)
accentMap( "Ján Müller" ) // → "Jan Muller"
accentMap( "straße", [["ß","ss"]] ) // → "strasse" (custom pattern)
accentMap( "Ján", [] ) // → "Jan" (empty patterns — skip ß default)
accentMap( "Ján Novák" ) // → "Jan Novak"
accentMap( "München" ) // → "Munchen"
// custom extra replacement:
accentMap( "straße", [["ß","ss"]] ) // → "strasse"
Replaces spaces and disallowed characters (punctuation, brackets, quotes…) with a joiner string (default "-"). Trims leading/trailing joiners and collapses consecutive ones. Typically combined with accentMap first to create URL slugs.
emptyMap( "Hello World" ) // → "Hello-World"
emptyMap( "Hello World", "_" ) // → "Hello_World"
emptyMap( " a b c " ) // → "a-b-c"
emptyMap( "foo (bar), baz!" ) // → "foo-bar-baz"
// URL slug — combine both:
emptyMap( accentMap( "Ján Novák & spol." ) )
// accentMap → "Jan Novak & spol."
// emptyMap → "Jan-Novak-spol"
// basic:
emptyMap( "Hello World" ) // → "Hello-World"
emptyMap( "Hello World", "_" ) // → "Hello_World"
// URL slug:
const slug = emptyMap( accentMap( str ) )
Path utilities — extract parts of a file path string. Works with both forward slashes and backslashes.
const path = "/var/www/images/photo.jpg"
basename( path ) // → "photo.jpg"
dirname( path ) // → "/var/www/images"
extname( path ) // → "jpg"
basename( "/var/www/images/photo.jpg" ) // → "photo.jpg"
dirname( "/var/www/images/photo.jpg" ) // → "/var/www/images"
extname( "/var/www/images/photo.jpg" ) // → "jpg"
Removes leading and trailing slashes. The optional stripRelative parameter (default true) controls whether relative prefixes (../, ./) are also stripped — set to false to preserve them.
// default: stripRelative = true
cleanPath( "../uploads/file.pdf" ) // → "uploads/file.pdf"
cleanPath( "./assets/img/" ) // → "assets/img"
cleanPath( "/folder/sub/" ) // → "folder/sub"
cleanPath( "//cdn.example.com/js" ) // → "cdn.example.com/js"
// stripRelative = false — zachova ./ a ../
cleanPath( "../uploads/file.pdf", false ) // → "../uploads/file.pdf"
cleanPath( "./assets/img/", false ) // → "./assets/img"
cleanPath( "/folder/sub/", false ) // → "folder/sub"
// stripRelative = true (default) — odstrani ../ ./
cleanPath( "../uploads/file.pdf" ) // → "uploads/file.pdf"
cleanPath( "./assets/img/" ) // → "assets/img"
cleanPath( "/folder/sub/" ) // → "folder/sub"
// stripRelative = false — zachova ../ ./
cleanPath( "../uploads/file.pdf", false ) // → "../uploads/file.pdf"
cleanPath( "./assets/img/", false ) // → "./assets/img"
cleanPath( "/folder/sub/", false ) // → "folder/sub"
Phone number normalisation (Slovak conventions). telColapse strips spaces/dashes, converts 00421 and leading 0 prefixes to +421 international format. telExpand formats back with spaces in groups of 3.
// telColapse — normalise to +421… format:
telColapse( "0905 123 456" ) // → "+421905123456"
telColapse( "00421905123456" ) // → "+421905123456"
telColapse( "+421 905 123 456" ) // → "+421905123456"
telColapse( "0905-123-456" ) // → "+421905123456"
// telExpand — add spaces (groups of 3):
telExpand( "+421905123456" ) // → "+421 905 123 456"
telExpand( "0905 123 456" ) // → "+421 905 123 456" (collapses first)
telColapse( "0905 123 456" ) // → "+421905123456"
telExpand( "0905 123 456" ) // → "+421 905 123 456"
// telExpand calls telColapse internally:
telExpand( "+421-905-123-456" ) // → "+421 905 123 456"
Miscellaneous string helpers: truncate with ellipsis, strip HTML tags, generate random ID codes.
// shortString( str, n = 10, dots = "..." )
shortString( "Hello World", 5 ) // → "Hello..."
shortString( "Hello World", 5, " →" ) // → "Hello →"
shortString( "Hi", 10 ) // → "Hi" (shorter than n — no dots)
// strip_tags( str )
strip_tags( "<p>Hello <strong>World</strong></p>" ) // → "Hello World"
strip_tags( "<a href=\"#\">link</a> text" ) // → "link text"
// random( n?, prefix?, suffix? )
random() // → "3847" (4 digits, default)
random( 6 ) // → "481920" (6 digits)
random( 4, "ID-" ) // → "ID-2391"
random( 4, "REF-", "-SK" ) // → "REF-7823-SK"
shortString( "Hello World", 5 ) // → "Hello..."
shortString( "Hello World", 5, " →" ) // → "Hello →"
strip_tags( "<p>Hello <strong>World</strong></p>" )
// → "Hello World"
random() // → "3847"
random( 6, "ID-", "-SK" ) // → "ID-481920-SK"
Calculates how many words from s1 appear in s2, returns a percentage. Separator defaults to space. Useful for fuzzy search or similarity ranking.
compareMatch( "John Doe", "John Doe" ) // → 100 (all words match)
compareMatch( "John Doe", "John Smith" ) // → 50 (1 of 2 words)
compareMatch( "foo bar baz", "foo baz" ) // → 66.67 (2 of 3 words)
compareMatch( "foo bar baz", "nothing" ) // → 0
// custom separator:
compareMatch( "a,b,c", "a,c", "," ) // → 66.67 (2 of 3 segments)
// custom decimal precision:
compareMatch( "foo bar baz", "foo baz", " ", 0 ) // → 67 (0 decimal places)
compareMatch( "John Doe", "John Doe" ) // → 100
compareMatch( "John Doe", "John Smith" ) // → 50
compareMatch( "foo bar baz", "foo baz" ) // → 66.67
// custom separator "," and 0 decimal places:
compareMatch( "a,b,c", "a,c", ",", 0 ) // → 67