Underscore.string: String Utilities

3.3.6 · maintenance · verified Sun Apr 19

Underscore.string is a comprehensive JavaScript library designed to augment native string capabilities, offering a wide array of utility functions for common string manipulations. Originally conceived as an extension for Underscore.js, it has evolved into a fully standalone utility, capable of independent use. The current stable version is 3.3.6. It provides functionalities such as `slugify` for URL-friendly strings, `numberFormat` for localized number formatting, `capitalize` for proper casing, and `levenshtein` for calculating string edit distances. While it can be integrated with Underscore.js or Lo-Dash via mixins, this approach is discouraged due to potential naming conflicts with core methods like `include` and `reverse`. The library is consumable in Node.js environments using CommonJS `require` statements and in browsers through a UMD build, which exposes a global `s` object. A key architectural advantage is the ability to import individual functions, which significantly helps in reducing bundle sizes when using module bundlers like Browserify or Webpack, as opposed to including the entire library. Its release cadence has transitioned from frequent updates to a more maintenance-focused schedule, with the last significant update being the 3.x series, followed by minor patches. It remains a robust option for projects requiring extensive string utility functions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates requiring the full library for chaining and individual functions for specific utilities, alongside a number formatting example.

var s = require('underscore.string');
var slugify = require('underscore.string/slugify');

console.log(slugify('Hello world from individual import!'));
// => "hello-world-from-individual-import"

var text = '   epeli  is a great   developer   ';
var processedText = s(text).trim().capitalize().replace('epeli', 'John').value();
console.log(processedText);
// => "Epeli is a great John   developer"

// Example of number formatting
console.log(s.numberFormat(1234567.89, 2, '.', ','));
// => "1,234,567.89"

view raw JSON →