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

Is Cloud the Answer for Your ColdFusion Dilemma?

Is Cloud the Answer for Your ColdFusion Dilemma?

Feeling the limits of an on-premise ColdFusion setup?

Many businesses face high costs, limited scalability, and performance bottlenecks, leaving them wondering if the cloud could be the answer.

In our FREE whitepaper, "Is Cloud the Answer for Your ColdFusion Dilemma?", we explore:

  • Benefits of Migrating to the Cloud: From cost savings to increased flexibility, find out what a cloud-based ColdFusion setup can do.
Cristobal Escobar
Cristobal Escobar
November 13, 2024
Mastering Events and Listeners in CBWIRE

Mastering Events and Listeners in CBWIRE

In CBWIRE, events and listeners are the backbone of building responsive, modular applications without relying heavily on JavaScript. This guide walks you through setting up and using CBWIRE events to create seamless interactions between components, from dispatching events in CFML and frontend templates to listening with Alpine.js and JavaScript. Learn how to make your applications feel dynamic and engaging by effortlessly connecting components. Whether you’re triggering events to update a dashboard or targeting specific parts of your app with dispatchTo, these techniques will empower you to create a modern, interactive CFML experience with ease.

Grant Copley
Grant Copley
November 11, 2024
10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

10 Key Benefits of Hiring a Specialized ColdFusion Consulting Team

ColdFusion remains a powerful and versatile platform for building dynamic web applications. However, keeping your ColdFusion environment optimized, secure, and scalable requires specialized expertise. Whether managing a long-standing ColdFusion application or planning new development projects, hiring a dedicated ColdFusion consulting and support team can be a game-changer for CTOs, CIOs, and developers. Here's why:

1. Expert Guidance on ColdFusion Web Development

...

Cristobal Escobar
Cristobal Escobar
November 08, 2024