turkish-text-utils
v1.0.0
Published
Utility functions for Turkish text normalization, slug generation, and insensitive text search.
Maintainers
Readme
turkish-text-utils
A lightweight, zero-dependency utility library for handling Turkish string operations. It provides reliable methods for Turkish text normalization, URL-friendly slug generation, and locale-aware case-insensitive substring searching.
Installation
Install using npm:
npm install turkish-text-utilsOr using yarn:
yarn add turkish-text-utilsOr using pnpm:
pnpm add turkish-text-utilsUsage
You can import the required utility functions directly from the package:
import { normalizeTr, toSlug, includesInsensitive } from 'turkish-text-utils';If you are using CommonJS:
const { normalizeTr, toSlug, includesInsensitive } = require('turkish-text-utils');API
normalizeTr(text: string): string
Converts all Turkish-specific characters in a given string to their English/ASCII lowercase equivalents.
// Example
const text = "Şeker Fıstıkçı Şahap ÖĞÜTÜCÜ";
console.log(normalizeTr(text));
// Output: "seker fistikci sahap ogutucu"toSlug(text: string): string
Generates a URL-friendly slug from a given Turkish string. It automatically normalizes Turkish characters, removes irregular symbols, and replaces spaces with hyphens -.
// Example
const title = "Türkiye'nin En Güzel Dağları ve Gölleri 2024!";
console.log(toSlug(title));
// Output: "turkiyenin-en-guzel-daglari-ve-golleri-2024"includesInsensitive(text: string, search: string): boolean
Performs a locale-aware, case-insensitive check to determine if the search string is found within text. This correctly handles the I/ı and İ/i problems inherent in the Turkish alphabet.
// Example
const content = "İstanbul BÜYÜKŞEHİR Belediyesi";
console.log(includesInsensitive(content, "istanbul")); // true
console.log(includesInsensitive(content, "BÜYÜK")); // true
console.log(includesInsensitive(content, "sehir")); // true
console.log(includesInsensitive(content, "Ankara")); // false