Caseless Object Access

0.12.0 · maintenance · verified Tue Apr 21

Caseless is a JavaScript utility library designed to simplify interaction with objects, particularly HTTP headers, by providing caseless property access while preserving the original casing of the first-set key. This allows developers to retrieve or check for properties like 'Content-Type' using 'content-type' without explicit case manipulation. The current stable version is 0.12.0, indicating a mature, focused API that has likely been stable for some time, rather than undergoing rapid, frequent updates. Its primary differentiator is the unique combination of caseless lookup with the preservation of the original key casing, which is critical for scenarios where the exact string representation of a header might matter for display or specific protocol interpretations, even if its semantic meaning is case-insensitive. The library wraps a standard JavaScript object, adding a thin layer of caseless semantics without mutating the underlying data structure in unexpected ways.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize caseless with a plain object, set and get properties using caseless access, check for property existence while preserving original casing, and showcases the `clobber` option for appending values to existing properties.

import caseless from 'caseless';

const myHeaders = {};
const caselessProxy = caseless(myHeaders);

// Set a header with specific casing
caselessProxy.set('X-Request-ID', 'abc-123');
caselessProxy.set('Content-Type', 'application/json');

// Get a header using any casing
console.log(caselessProxy.get('x-request-id')); // Expected: 'abc-123'
console.log(caselessProxy.get('content-type')); // Expected: 'application/json'

// Check if a header exists and retrieve its preserved casing
console.log(caselessProxy.has('content-type')); // Expected: 'Content-Type'

// Demonstrate the clobber=false behavior for appending values
caselessProxy.set('Accept', 'application/xml');
caselessProxy.set('accept', 'application/json', false); // Append without clobbering
console.log(caselessProxy.get('accept')); // Expected: 'application/xml,application/json'

console.log('Original headers object:', myHeaders);

view raw JSON →