Simplify Current JS implementation

This commit is contained in:
Jose Farias
2024-10-12 23:06:34 -06:00
parent 2c6c821e20
commit bb5b0701ed
5 changed files with 17 additions and 28 deletions
-25
View File
@@ -1,25 +0,0 @@
// On-demand JavaScript objects from "current" HTML <meta> elements. Example:
//
// <meta name="current-identity-id" content="123">
// <meta name="current-identity-time-zone-name" content="Central Time (US & Canada)">
//
// >> current.identity
// => { id: "123", timeZoneName: "Central Time (US & Canada)" }
//
// >> current.foo
// => {}
export const current = new Proxy({}, {
get(_target, propertyName) {
const result = {}
const prefix = `current-${propertyName}-`
for (const { name, content } of document.head.querySelectorAll(`meta[name^=${prefix}]`)) {
const key = camelize(name.slice(prefix.length))
result[key] = content
}
return result
}
})
function camelize(string) {
return string.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase())
}
-1
View File
@@ -1,2 +1 @@
export * from "helpers/current_helpers"
export * from "helpers/timing_helpers"