dom/manipulator.js: minor simplification. (#28358)

Combine two checks since we return the same value for both.
This commit is contained in:
XhmikosR 2019-02-26 13:13:01 +02:00 committed by GitHub
parent 7933ee3282
commit f7b55da450
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 15 deletions

View File

@ -5,18 +5,20 @@
* --------------------------------------------------------------------------
*/
const regexDataKey = /[A-Z]/g
function normalizeData(val) {
if (val === 'true') {
return true
} else if (val === 'false') {
}
if (val === 'false') {
return false
} else if (val === 'null') {
return null
} else if (val === Number(val).toString()) {
}
if (val === Number(val).toString()) {
return Number(val)
} else if (val === '') {
}
if (val === '' || val === 'null') {
return null
}
@ -24,7 +26,7 @@ function normalizeData(val) {
}
function normalizeDataKey(key) {
return key.replace(regexDataKey, (chr) => chr.toLowerCase())
return key.replace(/[A-Z]/g, (chr) => chr.toLowerCase())
}
const Manipulator = {
@ -45,18 +47,15 @@ const Manipulator = {
...element.dataset
}
Object.keys(attributes)
.forEach((key) => {
attributes[key] = normalizeData(attributes[key])
})
Object.keys(attributes).forEach((key) => {
attributes[key] = normalizeData(attributes[key])
})
return attributes
},
getDataAttribute(element, key) {
return normalizeData(element
.getAttribute(`data-${normalizeDataKey(key)}`)
)
return normalizeData(element.getAttribute(`data-${normalizeDataKey(key)}`))
},
offset(element) {