Skip to content

Instantly share code, notes, and snippets.

@smithcommajoseph
smithcommajoseph / constantize.js
Last active March 12, 2021 02:43
A JS implementation of Ruby's constantize
// A basic example of how Ruby's constantize _could_ work in JS
// See https://apidock.com/rails/String/constantize
function constantize (str) {
if (typeof str !== 'string') {
throw new TypeError('must pass in type of string');
}
if (str.match(/\W|\d/)) {
throw new SyntaxError('must pass in a valid Javascript name');
}