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

translitro

v0.2.3

Published

Normalise and transform special characters and non-latin characters (including Chinese and Japanese) to basic latin characters

Downloads

22

Readme

Translitro

Normalise and transform special characters and non-latin characters (including Chinese and Japanese) to basic latin characters.

Perfect for when needing to transform data for searching, sorting and inserting into systems that support only the basic latin character set.

About

This package stands on the shoulders of these giants:

Note: this library is not intended for use within the browser, due to the size of the dependencies (Japanese data files are ~19mb). If you need to normalise/sanitise data, it is best to do it server-side anyway!

Installation

  npm i translitro
  yarn add translitro

Usage

Within your code, translitro returns a promise with the transliterated output:

const translitro = require("translitro").default;

translitro("åéîøü").then((o) => console.log(o));

// "aeiou"
import translitro from "translitro";

console.log(await translitro("åéîøü"));

// "aeiou"

It's also possible to transliterate values within arrays and objects with a single call:

const translitro = require("translitro").default;

// Array
translitro(["åéîøü", "مرحبا", "γεια σας", "여보세요"]).then((o) =>
  console.log(o)
);

/*
  [
    "aeiou",
    "mrHb",
    "geia sas",
    "yeoboseyo"
  ]
*/

// Object
translitro({
  specialCharacters: "åéîøü",
  arabic: "مرحبا",
  greek: "γεια σας",
  korean: "여보세요",
}).then((o) => console.log(o));

/*
  {
    specialCharacters: "aeiou",
    arabic: "mrHb",
    greek: "geia sas",
    korean: "yeoboseyo"
  }
*/

Post-processing

After transliterating your input, you can apply some post-processes:

// Object
translitro(
  {
    specialCharacters: "åéîøü",
    arabic: "مرحبا",
    greek: "γεια σας",
    korean: "여보세요",
  },
  {
    postProcess: ["upper"],
  }
).then((o) => console.log(o));

/*
  {
    specialCharacters: "AEIOU",
    arabic: "MRHB",
    greek: "GEIA SAS",
    korean: "YEOBOSEYO"
  }
*/

You can also specify multiple post-processes, including custom functions which take a single string input and return a string:

// Object
translitro(
  {
    specialCharacters: "åéîøü",
    arabic: "مرحبا",
    greek: "γεια σας",
    korean: "여보세요",
  },
  {
    postProcess: [
      "upper",
      (i) =>
        i
          .split(/\s+/g)
          .map((o) => o[0])
          .join(" "),
    ],
  }
).then((o) => console.log(o));

/*
  {
    specialCharacters: "A",
    arabic: "M",
    greek: "G S",
    korean: "Y"
  }
*/

Current supported post-processes are:

  • normal (aka normalize, normalise): converts any special characters to basic latin versions
  • upper (aka uppercase): does what it says
  • lower (aka lowercase): does what it says
  • title (aka titlecase, capital, capitalcase): does what it says

Note: post-processes are performed in the order that they are declared.

Transliterate Chinese and Japanese

When transliterating Chinese and/or Japanese you will have to do those values separately due to needing to specify the input from parameter:

Chinese

It's necessary to specify { from: "zh" } in options when transliterating Chinese characters:

translitro(["欢迎", "歡迎"], { from: "zh" }).then((o) => console.log(o));
// ["huan ying", "huan ying"]

You can also affect the output by specifying the to option (accepts "normal" | "tone2" | "to3ne" | "initials" | "firstLetter"):

translitro(["欢迎", "歡迎"], { from: "zh", to: "initials" }).then((o) =>
  console.log(o)
);
// ["h", "h"]

Simplified and Traditional forms are both handled easily for Chinese characters, thanks to the might of pinyin

Japanese

translitro(["良い一日", "こんにちは", "こうし", "フョ"], {
  from: "ja",
}).then((o) => console.log(o));
// ["yoi ichi nichi", "konnichiwa", "ko shi", "fuyo"]

Note: there are three romaji systems supported: passport, hepburn and nippon. passport is the default as hepburn and nippon can also include special characters outside the standard basic latin set, such as ô and ō.

It's also possible to transliterate Japanese to hiragana and katakana:

translitro(["良い一日", "こんにちは", "こうし" /*, "フョ" */], {
  from: "ja",
  to: "hiragana",
}).then((o) => console.log(o));
// ["よいいちにち", "こんにちは", "こうし" /*, "ふょ" */]

Note: there's an outstanding issue on kuroshiro where in some cases it can't convert katakana to hiragana.

translitro(["良い一日", "こんにちは", "こうし", "フョ"], {
  from: "ja",
  to: "katakana",
}).then((o) => console.log(o));
// ["ヨイイチニチ", "コンニチハ", "コウジ", "フョ"]

Japanese katakana, hiragana and kanji forms are handled thanks to the might of kuroshiro and kuroshiro-analyzer-kuromoji

Development

To download external dependencies:

  npm i

To run tests (using Jest):

  npm test
  npm run test:watch

Contribute

Got cool ideas? Have questions or feedback? Found a bug? Post an issue

Added a feature? Fixed a bug? Post a PR

License

Apache 2.0