case-insensitive-object2
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Case Insensitive Object

By Tyson Jones

Enables the construction and manipulation of JavaScript objects whose keys are case insensitive. Built simply as a Proxy wrapper for regular case sensitive objects in which certain vital methods are intercepted and modified.

import CaseInsensitiveObject from "case-insensitive-object2"

const obj = new CaseInsensitiveObject();
obj.HELLO = 3;
console.log(obj.hello); // Prints 3

Supports all the usual Object functions.

const obj = new CaseInsensitiveObject();
const oldObj = { HELLO: 3 };
Object.assign(obj, oldObj);
console.log(obj.hello); // Prints 3
const obj = new CaseSensitiveObject();
Object.preventExtensions(obj);
obj.HELLO = 3; // Throws an exception

Retains the original keys.

const obj = new CaseSensitiveObject();
obj.ASDF = 1;
obj.qWeRt = 2;

console.log(Object.keys(obj)); // Prints ["ASDF", "qWeRt"]

Can be initialized with a previous object.

const obj = new CaseInsensitiveObject( { HELLO: 3 } );
console.log(obj.hello); // Prints 3

The prototype chain is preserved.

const obj = new CaseInsensitiveObject();
obj.prop = "Hello";
console.log(obj.propertyIsEnumerable("PROP")); // Prints true
console.log(obj.toString()); // Prints [object CaseInsensitiveObject]

Notes

  • A good alternative is this package when you're dealing with heavy TypeScript.
  • My implementation is very performant. Compared to a regular JavaScript object, my implementation only adds an overhead of about 3.5x (in terms of under 250 milliseconds for 100000 randomly generated keys).

Readme

Keywords

Package Sidebar

Install

npm i case-insensitive-object2

Weekly Downloads

3

Version

1.0.2

License

ISC

Unpacked Size

8.78 kB

Total Files

6

Last publish

Collaborators

  • chaptersevenseeds