CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-humps

Underscore-to-camelCase converter (and vice versa) for strings and object keys in JavaScript.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

humps

humps is a lightweight JavaScript library that provides comprehensive case conversion utilities for strings and object keys. It enables seamless transformation between camelCase, PascalCase, and underscore-separated naming conventions, with deep object processing and customizable conversion behavior.

Package Information

  • Package Name: humps
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install humps or bower install humps

Core Imports

const humps = require("humps");

For ES modules:

import humps from "humps";

Individual imports:

const { camelize, decamelize, camelizeKeys, decamelizeKeys } = require("humps");

Browser (global):

<script src="humps.js"></script>
<script>
  // Access via global humps object
  humps.camelize("hello_world");
</script>

Basic Usage

const humps = require("humps");

// String conversions
const camelCased = humps.camelize("hello_world"); // "helloWorld"
const underscored = humps.decamelize("helloWorld"); // "hello_world"
const pascalCased = humps.pascalize("hello_world"); // "HelloWorld"

// Object key conversions
const obj = { first_name: "John", last_name: "Doe" };
const camelObj = humps.camelizeKeys(obj); // { firstName: "John", lastName: "Doe" }
const snakeObj = humps.decamelizeKeys(camelObj); // { first_name: "John", last_name: "Doe" }

// Deep object processing
const nested = {
  user_info: {
    first_name: "Jane",
    contact_details: [
      { phone_number: "123-456-7890" },
      { email_address: "jane@example.com" }
    ]
  }
};
const camelNested = humps.camelizeKeys(nested);
// Result: { userInfo: { firstName: "Jane", contactDetails: [{ phoneNumber: "123-456-7890" }, { emailAddress: "jane@example.com" }] } }

Capabilities

String Conversion

Camelize

Converts underscore/hyphen/space-separated strings to camelCase.

/**
 * Converts underscore/hyphen/space-separated strings to camelCase
 * @param {string} string - The string to convert
 * @returns {string} The camelCased string
 */
function camelize(string);

Usage Examples:

humps.camelize("hello_world"); // "helloWorld"
humps.camelize("hello-world"); // "helloWorld"
humps.camelize("hello world"); // "helloWorld"
humps.camelize("HelloWorld"); // "helloWorld" (first char lowercased)
humps.camelize("123"); // "123" (numbers unchanged)

Decamelize

Converts camelCase strings to underscore-separated (or custom separator).

/**
 * Converts camelCase strings to underscore-separated strings
 * @param {string} string - The camelCase string to convert
 * @param {Object} [options] - Configuration options
 * @param {string} [options.separator="_"] - Custom separator character
 * @param {RegExp} [options.split=/(?=[A-Z])/] - Custom word splitting pattern
 * @returns {string} The converted string
 */
function decamelize(string, options);

Usage Examples:

humps.decamelize("helloWorld"); // "hello_world"
humps.decamelize("helloWorld", { separator: "-" }); // "hello-world"
humps.decamelize("helloWorld1"); // "hello_world1" (numbers not split by default)
humps.decamelize("helloWorld1", { split: /(?=[A-Z0-9])/ }); // "hello_world_1"

Pascalize

Converts underscore/hyphen/space-separated strings to PascalCase.

/**
 * Converts underscore/hyphen/space-separated strings to PascalCase
 * @param {string} string - The string to convert
 * @returns {string} The PascalCased string
 */
function pascalize(string);

Usage Examples:

humps.pascalize("hello_world"); // "HelloWorld"
humps.pascalize("hello-world"); // "HelloWorld"
humps.pascalize("hello world"); // "HelloWorld"

Depascalize

Alias for decamelize function - converts PascalCase/camelCase strings to underscore-separated.

/**
 * Alias for decamelize - converts PascalCase/camelCase strings to underscore-separated
 * @param {string} string - The PascalCase/camelCase string to convert
 * @param {Object} [options] - Configuration options (same as decamelize)
 * @returns {string} The converted string
 */
function depascalize(string, options);

Object Key Conversion

Camelize Keys

Recursively converts object keys to camelCase.

/**
 * Recursively converts object keys to camelCase
 * @param {Object|Array} object - Object or array to process
 * @param {Function|Object} [options] - Processing callback or options object
 * @returns {Object|Array} Object with camelCased keys
 */
function camelizeKeys(object, options);

Callback Options:

/**
 * Custom processing callback for camelizeKeys
 * @param {string} key - The current key being processed
 * @param {Function} convert - The default conversion function
 * @returns {string} The processed key
 */
type ProcessorCallback = (key, convert) => string;

Usage Examples:

// Basic usage
const obj = { first_name: "John", last_name: "Doe" };
humps.camelizeKeys(obj); // { firstName: "John", lastName: "Doe" }

// Array processing
const users = [{ first_name: "John" }, { first_name: "Jane" }];
humps.camelizeKeys(users); // [{ firstName: "John" }, { firstName: "Jane" }]

// Custom processing callback
humps.camelizeKeys(obj, function(key, convert) {
  return key === "first_name" ? key : convert(key);
}); // { first_name: "John", lastName: "Doe" }

// Nested objects
const nested = {
  user_info: {
    contact_details: { phone_number: "123-456-7890" }
  }
};
humps.camelizeKeys(nested);
// { userInfo: { contactDetails: { phoneNumber: "123-456-7890" } } }

Decamelize Keys

Recursively converts camelCase object keys to underscore-separated.

/**
 * Recursively converts camelCase object keys to underscore-separated
 * @param {Object|Array} object - Object or array to process
 * @param {Function|Object} [options] - Processing callback or options object
 * @returns {Object|Array} Object with underscore-separated keys
 */
function decamelizeKeys(object, options);

Options Object:

/**
 * Options for decamelizeKeys function
 * @typedef {Object} DecamelizeOptions
 * @property {string} [separator="_"] - Custom separator character
 * @property {RegExp} [split=/(?=[A-Z])/] - Custom word splitting pattern
 * @property {Function} [process] - Custom processing callback
 */
type DecamelizeOptions = {
  separator?: string;
  split?: RegExp;
  process?: (key: string, convert: Function, options: DecamelizeOptions) => string;
};

Usage Examples:

// Basic usage
const obj = { firstName: "John", lastName: "Doe" };
humps.decamelizeKeys(obj); // { first_name: "John", last_name: "Doe" }

// Custom separator
humps.decamelizeKeys(obj, { separator: "-" }); // { "first-name": "John", "last-name": "Doe" }

// Custom processing with options object
humps.decamelizeKeys(obj, {
  separator: "-",
  process: function(key, convert, options) {
    return key === "firstName" ? key : convert(key, options);
  }
}); // { firstName: "John", "last-name": "Doe" }

// Direct callback (legacy format)
humps.decamelizeKeys(obj, function(key, convert, options) {
  return key === "firstName" ? key : convert(key, options);
}); // { firstName: "John", last_name: "Doe" }

Pascalize Keys

Recursively converts object keys to PascalCase.

/**
 * Recursively converts object keys to PascalCase
 * @param {Object|Array} object - Object or array to process
 * @param {Function} [options] - Processing callback
 * @returns {Object|Array} Object with PascalCased keys
 */
function pascalizeKeys(object, options);

Usage Examples:

const obj = { first_name: "John", last_name: "Doe" };
humps.pascalizeKeys(obj); // { FirstName: "John", LastName: "Doe" }

// With callback
humps.pascalizeKeys(obj, function(key, convert) {
  return key === "first_name" ? key : convert(key);
}); // { first_name: "John", LastName: "Doe" }

Depascalize Keys

Alias for decamelizeKeys function - recursively converts PascalCase/camelCase object keys to underscore-separated.

/**
 * Alias for decamelizeKeys - recursively converts PascalCase/camelCase object keys to underscore-separated
 * @param {Object|Array} object - Object or array to process
 * @param {Function|Object} [options] - Processing callback or options object (same as decamelizeKeys)
 * @returns {Object|Array} Object with underscore-separated keys
 */
function depascalizeKeys(object, options);

Type Handling

The library handles various JavaScript types during object key processing:

  • Objects: Keys are converted, values are recursively processed
  • Arrays: Each element is recursively processed
  • Dates: Preserved as-is (not converted)
  • RegExp: Preserved as-is (not converted)
  • Functions: Preserved as-is (not converted)
  • Booleans: Preserved as-is (not converted)
  • Primitives: Strings, numbers, null, undefined are preserved as-is

Advanced Usage

Custom Processing Callbacks

All object key conversion functions support custom processing callbacks for fine-grained control:

// Skip conversion for keys matching certain patterns
const skipUppercase = (key, convert) => {
  return /^[A-Z0-9_]+$/.test(key) ? key : convert(key);
};

const obj = { API_KEY: "secret", user_name: "john" };
humps.camelizeKeys(obj, skipUppercase); // { API_KEY: "secret", userName: "john" }

Integration with APIs

Common pattern for converting between JavaScript and Ruby/Rails API formats:

// Converting Ruby/Rails API response to JavaScript format
const apiResponse = {
  user_data: {
    first_name: "John",
    last_name: "Doe",
    created_at: new Date(),
    contact_info: [
      { phone_number: "123-456-7890" },
      { email_address: "john@example.com" }
    ]
  }
};

const jsFormat = humps.camelizeKeys(apiResponse);
// Process in JavaScript...

// Converting back for API request
const railsFormat = humps.decamelizeKeys(jsFormat);

Error Handling and Edge Cases

The humps library handles edge cases gracefully:

  • Numerical strings: Numeric strings are preserved unchanged during string conversions
  • Empty objects/arrays: Empty containers are processed without errors
  • Null/undefined values: Primitive values are preserved as-is
  • Circular references: While not explicitly handled, the library processes objects recursively
  • Special objects: Dates, RegExp, Functions, and Booleans are preserved without key conversion

All functions are designed to be safe with various input types and will not throw errors on unexpected input.

docs

index.md

tile.json