]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
fix: ensure `totype` always returns stringified null/undefined when null/undefined...
authorJohann-S <johann.servoire@gmail.com>
Wed, 18 Mar 2020 11:10:55 +0000 (12:10 +0100)
committerGitHub <noreply@github.com>
Wed, 18 Mar 2020 11:10:55 +0000 (13:10 +0200)
js/src/util/index.js
js/tests/unit/util/index.spec.js

index 8a5ae21566a432453b4af0da35a09ceb1cceb470..fca2a91977fb4909a75129d9a906a63079a9a50e 100644 (file)
@@ -10,7 +10,13 @@ const MILLISECONDS_MULTIPLIER = 1000
 const TRANSITION_END = 'transitionend'
 
 // Shoutout AngusCroll (https://goo.gl/pxwQGp)
-const toType = obj => ({}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase())
+const toType = obj => {
+  if (obj === null || obj === undefined) {
+    return `${obj}`
+  }
+
+  return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase()
+}
 
 /**
  * --------------------------------------------------------------------------
index 42c273f060bdbba9224476d86083a1d9b5ab271f..57ca1a9c73851252b0f84faf0ad1471beb0d97e5 100644 (file)
@@ -198,8 +198,9 @@ describe('Util', () => {
   })
 
   describe('typeCheckConfig', () => {
+    const namePlugin = 'collapse'
+
     it('should check type of the config object', () => {
-      const namePlugin = 'collapse'
       const defaultType = {
         toggle: 'boolean',
         parent: '(string|element)'
@@ -213,6 +214,34 @@ describe('Util', () => {
         Util.typeCheckConfig(namePlugin, config, defaultType)
       }).toThrow(new Error('COLLAPSE: Option "parent" provided type "number" but expected type "(string|element)".'))
     })
+
+    it('should return null stringified when null is passed', () => {
+      const defaultType = {
+        toggle: 'boolean',
+        parent: '(null|element)'
+      }
+      const config = {
+        toggle: true,
+        parent: null
+      }
+
+      Util.typeCheckConfig(namePlugin, config, defaultType)
+      expect().nothing()
+    })
+
+    it('should return undefined stringified when undefined is passed', () => {
+      const defaultType = {
+        toggle: 'boolean',
+        parent: '(undefined|element)'
+      }
+      const config = {
+        toggle: true,
+        parent: undefined
+      }
+
+      Util.typeCheckConfig(namePlugin, config, defaultType)
+      expect().nothing()
+    })
   })
 
   describe('makeArray', () => {