Skip to content

Instantly share code, notes, and snippets.

@dpeek
Created April 1, 2025 07:48
Show Gist options
  • Save dpeek/ea07fc08130fbe1915fa6ef9879d56fb to your computer and use it in GitHub Desktop.
Save dpeek/ea07fc08130fbe1915fa6ef9879d56fb to your computer and use it in GitHub Desktop.
(function(casing = "snake") {
function convert(key, casing) {
switch (casing) {
case "snake":
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
case "kebab":
return key.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
case "pascal":
return key.charAt(0).toUpperCase() + key.slice(1);
case "screaming":
if (key === key.toUpperCase()) return key;
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toUpperCase();
default:
return key;
}
}
function patchCamelCase(proto, casing) {
Object.getOwnPropertyNames(proto).forEach(function(key) {
if (!/[A-Z]/.test(key)) return;
var newKey = convert(key, casing);
if (newKey in proto) return;
var originalDesc = Object.getOwnPropertyDescriptor(proto, key);
if (!originalDesc) return;
var aliasDescriptor;
if ("value" in originalDesc) {
aliasDescriptor = {
get: function() { return originalDesc.value; },
set: function(val) { originalDesc.value = val; },
configurable: true,
enumerable: true
};
} else {
aliasDescriptor = {
get: originalDesc.get ? function() { return originalDesc.get.call(this); } : undefined,
set: originalDesc.set ? function(val) { return originalDesc.set.call(this, val); } : undefined,
configurable: true,
enumerable: true
};
}
try {
Object.defineProperty(proto, newKey, aliasDescriptor);
} catch (e) {}
if (originalDesc.configurable) {
try {
Object.defineProperty(proto, key, Object.assign({}, originalDesc, { enumerable: false }));
} catch (e) {}
}
});
}
var prototypes = [
typeof window !== "undefined" ? window.__proto__ : null,
typeof Document !== "undefined" ? Document.prototype : null,
typeof DocumentFragment !== "undefined" ? DocumentFragment.prototype : null,
typeof Element !== "undefined" ? Element.prototype : null,
typeof Node !== "undefined" ? Node.prototype : null,
typeof Event !== "undefined" ? Event.prototype : null,
Array.prototype,
Set.prototype,
Map.prototype,
Function.prototype,
String.prototype,
Number.prototype,
Boolean.prototype,
RegExp.prototype
].filter(function(p) { return p; });
prototypes.forEach(function(proto) {
patchCamelCase(proto, casing);
});
})("snake");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment