Last active
August 29, 2015 14:07
-
-
Save danott/296f9fee2cb4ce07e3c4 to your computer and use it in GitHub Desktop.
Set an environment from html meta tags.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set an environment from html meta tags. | |
# | |
# Usage: | |
# | |
# <meta name="ENV.station" content="swan" /> | |
# <meta name="ENV.port" content="2342" /> | |
# | |
# The above meta tags would yield the following object: | |
# | |
# ENV = {'station': 'swan', 'port': 2342} | |
# | |
# Notice how the type is coerced where applicable. If you have a meta tag that | |
# you don't want to coerce, you can opt out with: | |
# | |
# <meta name="ENV.rawPort" content="2342" data-env-no-coerce /> | |
# | |
# This would result in: | |
# | |
# ENV = {'rawPort': '2342'} | |
# | |
# That's it! | |
ENV = {} | |
envRegex = /^ENV\./ | |
elements = [].slice.call(document.getElementsByTagName('meta')) | |
elements = elements.filter (e) -> e.name.match(envRegex) | |
for element in elements | |
propertyName = element.name.replace(envRegex, '') | |
propertyValue = element.content | |
try | |
unless element.dataset.hasOwnProperty('envNoCoerce') | |
eval("var __meta_env_eval__ = " + propertyValue) | |
propertyValue = __meta_env_eval__ | |
delete __meta_env_eval__ | |
catch | |
null | |
finally | |
ENV[propertyName] = propertyValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment