1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-09-27 13:59:06 +02:00

add simple type checker implementation

This commit is contained in:
fat
2015-05-13 14:46:50 -07:00
parent c2ced2292a
commit eaab1def7a
25 changed files with 258 additions and 28 deletions

View File

@@ -23,6 +23,15 @@ const Util = (($) => {
transition : 'transitionend'
}
// shoutout AngusCroll (https://goo.gl/pxwQGp)
function toType(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
function isElement(obj) {
return (obj[0] || obj).nodeType;
}
function getSpecialTransitionEndEvent() {
return {
bindType: transition.end,
@@ -115,6 +124,25 @@ const Util = (($) => {
supportsTransitionEnd() {
return !!transition
},
typeCheckConfig(componentName, config, configTypes) {
for (let property in configTypes) {
let expectedTypes = configTypes[property]
let value = config[property]
let valueType
if (value && isElement(value)) valueType = 'element'
else valueType = toType(value)
if (!new RegExp(expectedTypes).test(valueType)) {
throw new Error(
`${componentName.toUpperCase()}: ` +
`Option "${property}" provided type "${valueType}" ` +
`but expected type "${expectedTypes}".`)
}
}
}
}