Registry / serialization / underscore.string

underscore.string

JSON →
library3.3.6jsnpmunverified

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.

npm install underscore.string
INSTALL
IMPORT
SIG · UNDERSCORE.STRING
U
underscore.string
serializationjavascriptv3.3.6
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

s
✓ var s = require('underscore.string');
✗ import s from 'underscore.string';
Primarily a CommonJS library. ESM imports are not directly supported without transpilation or bundler configuration.
slugify
✓ var slugify = require('underscore.string/slugify');
✗ var s = require('underscore.string'); s.slugify(...);
Importing individual functions directly is recommended for bundlers like Browserify to minimize bundle size.
s (global)
✓ s.capitalize('hello');
✗ var s = window.s;
When used in a browser environment via the UMD build (e.g., from `dist/underscore.string.js`), the library exposes itself as a global `s` object.

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"
Debug
Known issues
breakingUpgrading from 2.x to 3.x introduced several breaking changes. Functions like `s.include`, `s.contains`, `s.reverse`, and `s.join` were removed to prevent collisions with Underscore.js methods. `s.chars` behavior changed, argument orders for `s.count` were altered, and `s.chop` was removed.
fix
Consult the 3.0.0 changelog (https://github.com/epeli/underscore.string/blob/master/CHANGELOG.markdown#300) for a comprehensive list of changes and adapt your code accordingly.
affects: >=3.0.0
gotchaMixing `underscore.string` into Underscore.js or Lo-Dash using `_.mixin(s.exports())` can lead to collisions. Specifically, `include`, `contains`, `reverse`, and `join` from `underscore.string` are dropped during mixin because Underscore.js already defines them, potentially leading to unexpected behavior if your code relies on `underscore.string`'s versions.
fix
It is generally not recommended to use the `mixin` approach. Prefer importing `underscore.string` as a standalone object (`var s = require('underscore.string')`) and calling its methods directly (e.g., `s.slugify(...)`).
affects: >=2.0.0
gotchaWhen using bundlers like Browserify or Webpack for client-side applications, `require('underscore.string')` (the full library) will include all functions, even if you only use a few. This can significantly increase your bundle size.
fix
To optimize bundle size, import individual functions directly, e.g., `var slugify = require('underscore.string/slugify');`. This allows tree-shaking (if configured) to eliminate unused code.
affects: >=2.0.0
Errors
Common errors & fixes
ReferenceError: s is not defined
The `s` global or `s` variable from `require('underscore.string')` was not properly loaded or imported before use.
fix
For Node.js, ensure you have `var s = require('underscore.string');` at the top of your file. For browsers, ensure `underscore.string.js` is loaded via a `<script>` tag before your code executes, or via an AMD loader.
TypeError: _.include is not a function
You are attempting to use `_.include` (or `_.contains`, `_.reverse`, `_.join`) from `underscore.string` after mixing it into Underscore.js/Lo-Dash. These functions are dropped by `underscore.string` during the mixin process because of name collisions with the host library.
fix
Avoid using `_.mixin(s.exports())`. Instead, use `underscore.string` as a standalone module (e.g., `s.include(...)`) or use the equivalent functions provided by Underscore.js/Lo-Dash directly.
SyntaxError: Cannot use import statement outside a module
You are attempting to use ES Module `import` syntax (e.g., `import s from 'underscore.string'`) in a CommonJS environment (e.g., a `.js` file not treated as a module, or older Node.js versions) without proper transpilation.
fix
For CommonJS environments, use `var s = require('underscore.string');`. If you need ESM syntax, ensure your project is configured to treat files as ES Modules (e.g., `"type": "module"` in `package.json`) and/or use a bundler/transpiler like Babel or Webpack.
Upgrade
Version history
3.3.6latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
underscore.string — npm install underscore.string · libregistry