Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

Converting JS Libraries to Clojure: Part 1



What are we doing right here?

This can be a sequence of articles with two functions: to enhance my Clojure and ClojureScript information and to assist JavaScript builders be taught Clojure.

The primary library we can be remodeling is titleize. We’re beginning with the best library I might suppose I ever used and possibly you used too.

Additionally as a result of sindresorhus has the biggest, high-quality, open-source codebase of reusable js/ts snippets and libraries, I’ve seen.

The advice right here will not be essentially to make use of it in a mission however to know the fundamentals of Clojure code that can be utilized on each Clojure and ClojureScript environments. Perhaps when you’ve got a Clojure(Script) mission you may set up this library, however when you simply need a titleize perform in your JavaScript code, simply npm set up the unique one.



The titleize perform

Let’s begin by digging into the unique code. That is the titleize perform. It has no dependencies. A quite simple
simple JavaScript regex shenanigans. Have a look:

export default perform titleize(string) {
    if (typeof string !== 'string') {
        throw new TypeError('Anticipated a string');
    }

    return string.toLowerCase().replaceAll(/(?:^|s|-)S/g, x => x.toUpperCase());
}
Enter fullscreen mode

Exit fullscreen mode

So the very first thing to do is to create this perform. I do not wish to spend an excessive amount of your time so let’s simply hearth a REPL
and begin doing stuff there. So run this:

$ clj

So now on the REPL, you may take a look at some code. Run some sum instance like (+ 1 2) and be sure you obtain 3 after which we
can begin.



String capabilities

So first let’s require Clojure’s string customary library with the next code.

(require [clojure.string :as string])
Enter fullscreen mode

Exit fullscreen mode

Within the unique code, we will see the creator utilizing the toLowerCase and toUpperCase strategies from JavaScript’s string sort. The equivalents in Clojure could be:

(string/lower-case "BEING DEV IS NICE") ; => being dev is good
(string/upper-case "being dev is cool") ; => BEING DEV IS COOL
Enter fullscreen mode

Exit fullscreen mode

Okay. We might additionally discuss with the capabilities instantly.

(require [clojure.string :refer [lower-case upper-case]])

(lower-case "BEING DEV IS NICE") ; => being dev is good
(upper-case "being dev is cool") ; => BEING DEV IS COOL
Enter fullscreen mode

Exit fullscreen mode

Then we will see on the unique implementation that we even have a utilization of replaceAll with some regex. The
equal will even be from clojure.string which is the change perform.



Regex

In Clojure, the regex syntax is a bit of bit totally different. In fact, it would depend upon the applying however for now, we’ll ignore the modifiers (we will see the /g on the titleize implementation) and simply rework that to our beloved clojure syntax:

Authentic:

/(?:^|s|-)S/g
Enter fullscreen mode

Exit fullscreen mode

Clojure:

#"(?:^|s|-)S"
Enter fullscreen mode

Exit fullscreen mode

Not trusting me? Okay. Be sure to know by your self then:

(sort #"") ; => java.util.regex.Sample
Enter fullscreen mode

Exit fullscreen mode



Glueing every thing collectively

Now let’s write our perform:

(require [clojure.string :refer [lower-case upper-case replace]])

(defn titleize [str]
    (change (lower-case str) #"(?:^|s|-)S" upper-case))
Enter fullscreen mode

Exit fullscreen mode

Executed! Run the above in your REPL after which you need to use it:

(titleize "the short brown fox jumps over the lazy canine") ; => The Fast Brown Fox Jumps Over The Lazy Canine
Enter fullscreen mode

Exit fullscreen mode



Let’s refactor it a bit of

Our code is ok now. It really works! However it nonetheless does not leverage top-of-the-line issues Clojure has: macros!

To make our code a bit of bit extra readable and, I’d say, higher to take care of we can be utilizing the thread first macro. Which is the ChatGPT phrases could be described as:

The thread-first macro -> in Clojure lets you write code in a extra sequential and readable method by threading the output of 1 perform name into the primary argument of the subsequent perform name. It simplifies nested perform calls by enhancing code readability and decreasing the necessity for intermediate variables. This macro assists in composing capabilities collectively in a pure left-to-right order, making the code simpler to know and preserve.

By making use of that, our code will now appear to be this:

(defn titleize [str]
  (-> str
    (lower-case)
    (change #"(?:^|s|-)S" upper-case)))
Enter fullscreen mode

Exit fullscreen mode

So fairly! Proper? Clojure has a bunch of helpful macros.



Checks

The unique library has a quite simple take a look at chain, evaluating a bunch of strings that now we have. The repo makes use of ava to run the checks. Have a look:

import take a look at from 'ava';
import titleize from './index.js';

take a look at('important', t => {
    t.is(titleize(''), '');
    t.is(titleize('unicorns and rainbows'), 'Unicorns And Rainbows');
    t.is(titleize('UNICORNS AND RAINBOWS'), 'Unicorns And Rainbows');
    t.is(titleize('unicorns-and-rainbows'), 'Unicorns-And-Rainbows');
    t.is(titleize('UNICORNS-AND-RAINBOWS'), 'Unicorns-And-Rainbows');
    t.is(titleize('unicorns   and rainbows'), 'Unicorns   And Rainbows');
});
Enter fullscreen mode

Exit fullscreen mode

For Clojure now we have the clojure.take a look at library so we simply want to repeat the take a look at and apply it on Clojure syntax:

(ns excelsia.titleize-test
  (:require [clojure.test :refer [deftest is testing]]
            [excelsia.titleize :as titleize]))

(def result-map {"" ""
                 "unicorns and rainbows" "Unicorns And Rainbows"
                 "UNICORNS AND RAINBOWS" "Unicorns And Rainbows"
                 "unicorns-and-rainbows" "Unicorns-And-Rainbows"
                 "UNICORNS-AND-RAINBOWS" "Unicorns-And-Rainbows"
                 "unicorns   and rainbows" "Unicorns   And Rainbows"})

(deftest titleize-test
  (testing "titleize"
    (doseq [[input expected] result-map]
      (is (= anticipated (titleize/titleize enter))))))
Enter fullscreen mode

Exit fullscreen mode

Now, I am not going too deep on that, however you may see on the repository that we’re operating checks each on the Clojure surroundings and on the Node.js surroundings to verify the code works correctly each on Clojure and ClojureScript.



Abstract

So we grabbed a really small JS library and ported it to Clojure library appropriate with ClojureScript. Inform me your
feedbacks and different small libraries chances are you’ll wish to see or use written in Clojure.



Code

https://github.com/excelsia-dev/titleize

Add a Comment

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?