aboutsummaryrefslogtreecommitdiff
path: root/js/src/util.js
diff options
context:
space:
mode:
authorfat <[email protected]>2015-05-13 14:46:50 -0700
committerfat <[email protected]>2015-05-13 14:46:50 -0700
commiteaab1def7af7d7e1ab32ff69d043b46e2815ca22 (patch)
tree495965eef95b6c7e1ca485c04311e6e451695b29 /js/src/util.js
parentc2ced2292a6467b9c8a9fec3151982fd7ac8a239 (diff)
downloadbootstrap-eaab1def7af7d7e1ab32ff69d043b46e2815ca22.tar.xz
bootstrap-eaab1def7af7d7e1ab32ff69d043b46e2815ca22.zip
add simple type checker implementation
Diffstat (limited to 'js/src/util.js')
-rw-r--r--js/src/util.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/js/src/util.js b/js/src/util.js
index c9ffbe555..86bea6578 100644
--- a/js/src/util.js
+++ b/js/src/util.js
@@ -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}".`)
+ }
+ }
}
}