Lowercase the keys of an object
npx @tessl/cli install tessl/npm-lowercase-keys@3.0.0Lowercase Keys is a utility function that creates a new object with all keys converted to lowercase while preserving the original values. It's designed as a lightweight, zero-dependency utility for data normalization and consistent key formatting.
npm install lowercase-keysimport lowercaseKeys from 'lowercase-keys';Note: This package is ES modules only and requires Node.js 12.20.0+.
import lowercaseKeys from 'lowercase-keys';
// Basic transformation
const result = lowercaseKeys({FOO: true, bAr: false});
console.log(result);
// => {foo: true, bar: false}
// With mixed case keys
const data = lowercaseKeys({
UserName: 'alice',
EMAIL_ADDRESS: 'alice@example.com',
isActive: true
});
console.log(data);
// => {username: 'alice', email_address: 'alice@example.com', isactive: true}Converts all keys of an object to lowercase while preserving the original values and their types.
/**
* Lowercase the keys of an object.
* @param {Record<string, T>} object - The input object whose keys should be lowercased
* @returns {Record<string, T>} A new object with the keys lowercased
*/
function lowercaseKeys<T>(object: Record<string, T>): Record<string, T>;Parameters:
object (Record<string, T>): Input object with string keys and values of any typeReturns:
Record<string, T>: New object with all keys converted to lowercase, values unchangedBehavior:
String.toLowerCase()Usage Examples:
// Simple object transformation
const config = lowercaseKeys({
API_KEY: 'abc123',
Database_URL: 'localhost:5432',
debug: true
});
// => {api_key: 'abc123', database_url: 'localhost:5432', debug: true}
// Works with any value types
const mixed = lowercaseKeys({
Count: 42,
Items: ['a', 'b', 'c'],
Settings: { enabled: true },
Handler: function() { return 'hello'; }
});
// => {count: 42, items: ['a', 'b', 'c'], settings: { enabled: true }, handler: function() { return 'hello'; }}
// Empty object
const empty = lowercaseKeys({});
// => {}TypeScript Support:
The function is fully typed with generic type preservation:
// Type is preserved through transformation
const typed: Record<string, number> = lowercaseKeys({FOO: 1, BAR: 2});
// typed has type Record<string, number>
// Works with interface types
interface User {
name: string;
age: number;
}
const users: Record<string, User> = lowercaseKeys({
'USER_1': { name: 'Alice', age: 25 },
'USER_2': { name: 'Bob', age: 30 }
});
// users has type Record<string, User>