DEV Community

Cover image for Building a Nim library: Monika
AranaDeDoros
AranaDeDoros

Posted on

Building a Nim library: Monika

Recently I went through a phase of learning Nim. It was a good time, but I ultimately decided not to dwell too deep into it. This journey however spawned a microlibrary: Monika.


I started Nim on a whim because I wanted to have a system's programming language under my belt or at least native compilation, better speed and for the sake of applying what I was learning. I did C a lot of years ago, but I didn't want to deal with pointers directly and Rust syntax was a bit too much for me (even though I'm used to seeing some unsightly Scala's method signatures).

This library is actually a port from my own library JpnUtils written in Scala. It has a few extra functions that I didn't include in JpnUtils.

Monika is a Japanese strings microlibrary for Nim. It provides a clean, "implicit-style" API for handling characters, strings, and conversions.

Features

  • Implicit Conversions: Treat standard strings as Japanese objects via Nim converters.
  • Script Detection: Easily check for Hiragana, Katakana, and Kanji.
  • Diacritics: Detect Dakuten (voiced) and Handakuten (semi-voiced) marks.
  • Half-Width Conversion: Efficiently convert between full-width and half-width forms.
  • Punctuation Wrappers: Scala-style string wrapping for Japanese quotes.

Usage

Monika uses a converter to extend standard strings. Simply import Monika and start using the utility methods.

import monika/japaneseutils
import monika/punctuation
import monika/halfwidthconverter

if "こんにちは".hasHiragana:
  echo "Contains Hiragana!"

if "モニカ".hasKatakana:
  echo "Contains Katakana!"

if "学校".hasKanji:
  echo "Contains Kanji!"

# full-width to half-width
echo "ハロー、ワールド!".toHalfWidth
# Output: ハロー、ワールド!

# Check for voiced marks
if "が".hasDakuten:
  echo "This character is voiced."

let msg = "Hello"
echo msg.wrapInSingleQuotes # Output: 「Hello」
echo msg.wrapInDoubleQuotes # Output: 『Hello』

let s = "ガキ"
let h = "が".asRune()
let k = "エ".asRune()

if h.isSome:
  echo h.get.hiraToKata() # Output: カ
else:
  echo "empty string"

if k.isSome:
  echo k.get.kataToHira() # Output: え
else:
  echo "empty string"

let str = "日本語abcカナ"
echo str.containsOnly({Kanji, Katakana})  # false
let summary = str.scriptSummary()

echo summary.hiragana  # 0
echo summary.katakana  # 2
echo summary.kanji     # 3
echo summary.other     # 3
Enter fullscreen mode Exit fullscreen mode

It was a good experience, but If I were to be honest; I don't see myself using Nim that much outside of really niche things and even so, I might reconsider it. Maybe I'll make a post in the future about what I don't like about the actual Nim ecosystem.

If you wanna check it out, the repo's here

Top comments (0)