What is caseless?
The caseless npm package is a utility for managing HTTP headers in a case-insensitive manner. It allows you to get, set, and delete headers on an object without worrying about the case sensitivity of the header names.
What are caseless's main functionalities?
Setting headers
This feature allows you to set headers on an object. The headers are managed in a case-insensitive way, so setting 'A-Header' would overwrite 'a-header' if it already exists.
{"var caseless = require('caseless');\nvar headers = {};\nvar c = caseless(headers);\nc.set('a-Header', 'the value');\nconsole.log(headers); // Output will be { 'a-header': 'the value' }"}
Getting headers
This feature allows you to retrieve the value of headers regardless of the case of the header name you provide.
{"var caseless = require('caseless');\nvar headers = {'a-header': 'the value'};\nvar c = caseless(headers);\nconsole.log(c.get('A-Header')); // Output will be 'the value'"}
Checking if a header exists
This feature allows you to check if a header exists in the object, regardless of the case of the header name.
{"var caseless = require('caseless');\nvar headers = {'content-type': 'application/json'};\nvar c = caseless(headers);\nconsole.log(c.has('Content-Type')); // Output will be true"}
Deleting headers
This feature allows you to delete headers from the object. The deletion is case-insensitive.
{"var caseless = require('caseless');\nvar headers = {'content-type': 'application/json'};\nvar c = caseless(headers);\nc.delete('Content-Type');\nconsole.log(headers); // Output will be {}"}
Other packages similar to caseless
header-case-normalizer
This package provides functionality to normalize HTTP header names to a consistent case format. Unlike caseless, which allows for case-insensitive manipulation, header-case-normalizer focuses on converting header names to a specific case format.
http-headers-js
This package is similar to caseless in that it allows for case-insensitive handling of HTTP headers. It provides a more extensive API for parsing and formatting HTTP headers, which might be more suitable for complex header manipulation tasks.
Caseless -- wrap an object to set and get property with caseless semantics but also preserve caseing.
This library is incredibly useful when working with HTTP headers. It allows you to get/set/check for headers in a caseless manor while also preserving the caseing of headers the first time they are set.
Usage
var headers = {}
, c = caseless(headers)
;
c.set('a-Header', 'asdf')
c.get('a-header') === 'asdf'
has(key)
Has takes a name and if it finds a matching header will return that header name with the preserved caseing it was set with.
c.has('a-header') === 'a-Header'
set(key, value[, clobber=true])
Set is fairly straight forward except that if the header exists and clobber is disabled it will add ','+value
to the existing header.
c.set('a-Header', 'fdas')
c.set('a-HEADER', 'more', false)
c.get('a-header') === 'fdsa,more'
swap(key)
Swaps the casing of a header with the new one that is passed in.
var headers = {}
, c = caseless(headers)
;
c.set('a-Header', 'fdas')
c.swap('a-HEADER')
c.has('a-header') === 'a-HEADER'
headers === {'a-HEADER': 'fdas'}