npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

speaking-url-ts

v0.6.2

Published

Generate a slug – transliteration with a lot of options

Downloads

56

Readme

speaking-url-ts

Version License Build Status

speaking-url-ts is a flexible, language-agnostic library that generates readable, SEO-friendly slugs for any language. It supports custom transliterations and symbol replacements to create clean, URL-safe slugs.

Features

  • Supports multiple languages with custom language packs.
  • Handles transliterations from non-Latin scripts (e.g., Cyrillic, Arabic).
  • Applies custom replacements.
  • Flexible configuration with custom rules for slug formatting.

Installation

You can install the speaking-url-ts library using npm:

npm install speaking-url-ts

Usage

Basic Usage

To generate a slug, first import the library and then use the generateSlug function:

import { generateSlug, addLanguagePack } from 'speaking-url-ts';

// Use the default English language pack
const slug = generateSlug('Hello, World!', { locale: 'en' });
console.log(slug);  // Output: "hello-world"

Adding a Custom Language Pack

You can add a custom language pack to handle specific transliterations, symbol replacements and custom rules for your desired language.

Here’s how to create and add a custom language pack:

  1. Define Your Language Pack
const { LanguagePack } = require('speaking-url-ts');

const customLanguagePack = {
    transliterations: {
        '你好': 'nihao',    // Example: Chinese characters to Pinyin
        'мир': 'mir',       // Example: Russian to Latin
    },
    symbols: {
        '&': 'and',
        '@': 'at',
        '%': 'percent',
    },
    customRules: [
        (text) => text.replace(/м/g, 'm'), // Example: Replace all 'м' with 'm'
        (text) => text.trim(), // Trim spaces
    ]
};
  1. Add the Language Pack
addLanguagePack('custom', customLanguagePack);
  1. Generate a Slug Using the Custom Language Pack
const slug = generateSlug('Привет, мир!', { locale: 'custom' });
console.log(slug);  // Output: "privet-mir"

Complete Example

import { generateSlug, addLanguagePack } from 'speaking-url-ts';

// Define and add the custom language pack
const russianLanguagePack = {
    transliterations: {
        'Привет': 'Privet',
        'мир': 'mir',
        'й': 'i',
    },
    symbols: {
        '&': 'i',
        ',': '',
    },
    customRules: [
        text => text.replace(/м/g, 'm'), // Custom rule to replace 'м' with 'm'
    ]
};

addLanguagePack('ru', russianLanguagePack);

// Generate a slug
const slug = generateSlug('Привет, мир и Москва', { locale: 'ru' });
console.log(slug);  // Output: "privet-mir-i-moskva"

API Reference

generateSlug(input: string, options: { locale: string, separator?: string }): string

Generates a slug from the input string.

  • input: The text to be converted into a slug.
  • options:
    • locale: The language pack to use (e.g., 'en' for English).
    • separator: (Optional) The character used to separate words in the slug. Defaults to '-'.

addLanguagePack(locale: string, pack: LanguagePack): void

Adds a new language pack or updates an existing one.

  • locale: The locale identifier (e.g., 'ru' for Russian).
  • pack: The language pack object containing transliterations, replacements, stop words, and custom rules.

Language Pack Structure

A language pack is an object with the following optional fields:

  • transliterations: An object mapping characters or phrases to their transliterated equivalents.
  • symbols: An object mapping characters to replace.
  • customRules: An array of functions that apply custom transformations to the text.

Example Language Pack

interface LanguagePack {
    transliterations?: { [key: string]: string };
    symbols?: { [key: string]: string };
    customRules?: Array<(text: string) => string>;
}

Contributing

Contributions are welcome! Please fork the repository, create a new branch, and submit a pull request with your improvements or new language packs.

License

This project is licensed under the MIT License. See the LICENSE file for details.