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.

Recent Entries

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Unfortunately, we often hear this message from clients who thought it would never happen to them... until it did. Some believed they could delay the expense of Implementing ColdFusion security best practices for one year, while others were tempted to put it off for just a few months. However, in today's rapidly evolving digital landscape, the security of web applications, including ColdFusio...

Cristobal Escobar
Cristobal Escobar
April 16, 2024
Ortus March Newsletter

Ortus March Newsletter

Welcome to Ortus Solutions’ monthly roundup, where we're thrilled to showcase cutting-edge advancements, product updates, and exciting events! Join us as we delve into the latest innovations shaping the future of technology.

Maria Jose Herrera
Maria Jose Herrera
April 01, 2024
Into the Box 2024 Last Early Bird Days!

Into the Box 2024 Last Early Bird Days!

Time is ticking, with less than 60 days remaining until the excitement of Into the Box 2024 unfolds! Don't let this golden opportunity slip away; our exclusive Early Bird Pricing is here for a limited time only, available until March 31st. Why wait? Secure your seat now and take advantage of this steal!

Maria Jose Herrera
Maria Jose Herrera
March 20, 2024