Blog

Eric Peterson

December 16, 2017

Spread the word


Share your thoughts

Modules don't have to be big. In fact, the [Unix philosophy](https://en.wikipedia.org/wiki/Unix_philosophy) says to do one thing and do it well. While modules on ForgeBox run the gamut of [doing one thing](https://www.forgebox.io/view/orm-reload-interceptor) and [full blown frameworks](https://www.forgebox.io/view/coldbox), today we will look at one of the utility libraries on ForgeBox — a string manipulation library called [`str`](https://www.forgebox.io/view/str).

str

Rather than walk you through the entire API, let me share with you some of the methods from this library that I think will be interesting to you.

capitalize & capitalizeWords

str.capitalize( "my post title" );
// "My post title"

Super simple function but annoying to write over and over again. Glad I don't have to write myself it again anymore.

str.capitalizeWords( "my post title" );
// "My Post Title"

I've always been jealous of the built in ucwords in PHP. Well, this method makes that jealousy obsolete.

startsWith

str.startsWith( "getDynamicMethod", "get" );
// true

str.startsWith( "setDynamicMethod", "get" );
// false

Even more simple than the last one, it's implemented in the source code as follows: return left( word, len( substring ) ) == substring;. But this is such an improvement in readability!

slug, snake, and kebab

str.slug( "My Post Title", "-" );
// "my-post-title"

slug is actually the method that powers a whole slew of case transformations in str such as snake and kebab.

plural

str.plural( "entity" );
// "entities"

str.plural( "tree", 1 );
// "tree"

I saved the best for last here. This will handle the edge cases around pluralizing nouns for you. You're welcome.

Benefits of a shared utility library

Before we end, let's address a common concern I've heard:

I could write all those utilities myself! Why rely on a third party library for it?

Let me give you three reasons:

  1. Well tested

    A shared utility library is likely to have a good test suite. This isn't to say that your utilities aren't tested. But by having the library used by more people in more situations, it is more battle-tested and ready for your next project.

  2. One command installs and updates

    When you need your own hand-written utility, you go find the repo or find the code in another project and copy and paste it over. Conversely, you could spend a few seconds typing an install slug in to CommandBox and be done. Additionally, when there are updates to the library, whether new features or bug fixes, all of the places that use the shared library can be updated via CommandBox. The alternative? A frantic find and replace.

  3. Familiarity between developers

    This point will vary from project to project, but it's more likely that another developer is familiar with a shared utility library than your own hand written library. If you need proof of this, think of Underscore or Lodash in JavaScript. This familiarity can range from being familiar with the code to being familar with the concepts and names. Both are valuable.

Wrap Up

str is one of many utility libraries on ForgeBox. Next time you need a utility, first check on ForgeBox. You might find you don't need to write it yourself. You may find a library that could use a pull request from you. Or you may find an opportunity to share your own utlities with the rest of us.

Add Your Comment

Recent Entries

ColdBox 7.2.0 Released

ColdBox 7.2.0 Released

ColdBox, a widely used development platform for ColdFusion (CFML), has unveiled version 7.2. Packed with compelling new features, bug fixes, and enhancements, this release is designed to empower developers by boosting productivity, refining scheduled task capabilities, and enhancing the overall reliability and efficiency of application development. This article will delve into the key highlights of ColdBox 7.2 and elucidate how these advancements can positively impact developers in their daily coding endeavors.

Luis Majano
Luis Majano
November 20, 2023
Into the Box 2023 Series on CFCast

Into the Box 2023 Series on CFCast

Excitement is in the air as we unleash the highly anticipated ITB 2023 series exclusively for our valued CFCast subscribers – and the best part? It's FREE for CFCast members! Now is the perfect time if you haven't joined the CFCast community yet. Plus, we've got an incredible End-of-Year deal that's too good to miss

Maria Jose Herrera
Maria Jose Herrera
November 20, 2023
Ortus Deals are Finally Here!

Ortus Deals are Finally Here!

The much-anticipated Ortus End-of-the-Year Sale has arrived, and it's time to elevate your development experience! Whether you're a seasoned developer, a tech enthusiast, or someone on the lookout for top-notch projects, Ortus has something special in store for you. Brace yourself for incredible discounts across a wide array of products and services, including Ortus annual events, books, cutting-edge services, and more.

Maria Jose Herrera
Maria Jose Herrera
November 15, 2023