{"id":12277,"library":"underscore.string","title":"Underscore.string: String Utilities","description":"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.","status":"maintenance","version":"3.3.6","language":"javascript","source_language":"en","source_url":"https://github.com/epeli/underscore.string","tags":["javascript","underscore","string"],"install":[{"cmd":"npm install underscore.string","lang":"bash","label":"npm"},{"cmd":"yarn add underscore.string","lang":"bash","label":"yarn"},{"cmd":"pnpm add underscore.string","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primarily a CommonJS library. ESM imports are not directly supported without transpilation or bundler configuration.","wrong":"import s from 'underscore.string';","symbol":"s","correct":"var s = require('underscore.string');"},{"note":"Importing individual functions directly is recommended for bundlers like Browserify to minimize bundle size.","wrong":"var s = require('underscore.string'); s.slugify(...);","symbol":"slugify","correct":"var slugify = require('underscore.string/slugify');"},{"note":"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.","wrong":"var s = window.s;","symbol":"s (global)","correct":"s.capitalize('hello');"}],"quickstart":{"code":"var s = require('underscore.string');\nvar slugify = require('underscore.string/slugify');\n\nconsole.log(slugify('Hello world from individual import!'));\n// => \"hello-world-from-individual-import\"\n\nvar text = '   epeli  is a great   developer   ';\nvar processedText = s(text).trim().capitalize().replace('epeli', 'John').value();\nconsole.log(processedText);\n// => \"Epeli is a great John   developer\"\n\n// Example of number formatting\nconsole.log(s.numberFormat(1234567.89, 2, '.', ','));\n// => \"1,234,567.89\"","lang":"javascript","description":"Demonstrates requiring the full library for chaining and individual functions for specific utilities, alongside a number formatting example."},"warnings":[{"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.","message":"Upgrading 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.","severity":"breaking","affected_versions":">=3.0.0"},{"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(...)`).","message":"Mixing `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.","severity":"gotcha","affected_versions":">=2.0.0"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"The `s` global or `s` variable from `require('underscore.string')` was not properly loaded or imported before use.","error":"ReferenceError: s is not defined"},{"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.","cause":"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.","error":"TypeError: _.include is not a function"},{"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.","cause":"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.","error":"SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm"}