diff options
author | fat <fat@folders.local> | 2015-05-14 00:46:50 +0300 |
---|---|---|
committer | fat <fat@folders.local> | 2015-05-14 00:46:50 +0300 |
commit | eaab1def7af7d7e1ab32ff69d043b46e2815ca22 (patch) | |
tree | 495965eef95b6c7e1ca485c04311e6e451695b29 /js/dist/util.js | |
parent | c2ced2292a6467b9c8a9fec3151982fd7ac8a239 (diff) |
add simple type checker implementation
Diffstat (limited to 'js/dist/util.js')
-rw-r--r-- | js/dist/util.js | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/js/dist/util.js b/js/dist/util.js index f3654eacfa..a135c3f385 100644 --- a/js/dist/util.js +++ b/js/dist/util.js @@ -24,6 +24,15 @@ var Util = (function ($) { 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, @@ -116,6 +125,21 @@ var Util = (function ($) { supportsTransitionEnd: function supportsTransitionEnd() { return !!transition; + }, + + typeCheckConfig: function typeCheckConfig(componentName, config, configTypes) { + + for (var property in configTypes) { + var expectedTypes = configTypes[property]; + var value = config[property]; + var valueType = undefined; + + 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 + '".')); + } + } } }; |