Przeglądaj źródła

Removing warnings when running the test and fix a bug when warnings are disabled and non-objects are passed as arguments

Matt Coles 9 lat temu
rodzic
commit
dcfdc13300
4 zmienionych plików z 17 dodań i 17 usunięć
  1. 5 5
      lib/index.js
  2. 1 1
      lib/index.min.js
  3. 6 6
      spec/js-query-error-spec.js
  4. 5 5
      src/index.js

+ 5 - 5
lib/index.js

@@ -9,11 +9,11 @@ module.exports = {
9 9
   },
10 10
 
11 11
   convert: function convert(data, options) {
12
+    if (options === undefined) {
13
+      options = {};
14
+    }
15
+    options = this._merge_options(this._defaults, options);
12 16
     if ((typeof data === "undefined" ? "undefined" : _typeof(data)) === 'object') {
13
-      if (options === undefined) {
14
-        options = {};
15
-      }
16
-      options = this._merge_options(this._defaults, options);
17 17
       var result = "?";
18 18
       Object.keys(data).map(function (query_key) {
19 19
         var query_data = data[query_key];
@@ -50,8 +50,8 @@ module.exports = {
50 50
     } else {
51 51
       if (options.warn_on_invalid) {
52 52
         console.warn("Attempted to convert non-object to query string!");
53
-        return "";
54 53
       }
54
+      return "";
55 55
     }
56 56
   },
57 57
 

Plik diff jest za duży
+ 1 - 1
lib/index.min.js


+ 6 - 6
spec/js-query-error-spec.js

@@ -9,7 +9,7 @@ describe("Object -> Query String W/ Errors", function () {
9 9
       },
10 10
       s: Symbol("foo")
11 11
     };
12
-    expect(querify.convert(qs_object, { warn_on_invalid: true })).toBe("");
12
+    expect(querify.convert(qs_object)).toBe("");
13 13
   });
14 14
 
15 15
   it("Should fail for nulls", function () {
@@ -17,7 +17,7 @@ describe("Object -> Query String W/ Errors", function () {
17 17
       str: "mystr",
18 18
       second_str: null
19 19
     };
20
-    expect(querify.convert(qs_object, { warn_on_invalid: true })).toBe("?str=mystr");
20
+    expect(querify.convert(qs_object)).toBe("?str=mystr");
21 21
   });
22 22
 
23 23
   it("Should fail for undefined", function () {
@@ -25,16 +25,16 @@ describe("Object -> Query String W/ Errors", function () {
25 25
       str: "a string",
26 26
       second_str: undefined
27 27
     };
28
-    expect(querify.convert(qs_object, { warn_on_invalid: true })).toBe("?str=a%20string");
28
+    expect(querify.convert(qs_object)).toBe("?str=a%20string");
29 29
   });
30 30
 
31 31
   it("Should fail when trying to use non-objects", function () {
32 32
     var qs_object = "str";
33 33
     var qs_object2 = 2;
34 34
     var qs_object3 = function () { console.log("Hello World!"); };
35
-    expect(querify.convert(qs_object, { warn_on_invalid: true })).toBe("");
36
-    expect(querify.convert(qs_object2, { warn_on_invalid: true })).toBe("");
37
-    expect(querify.convert(qs_object3, { warn_on_invalid: true })).toBe("");
35
+    expect(querify.convert(qs_object)).toBe("");
36
+    expect(querify.convert(qs_object2)).toBe("");
37
+    expect(querify.convert(qs_object3)).toBe("");
38 38
   });
39 39
 
40 40
 });

+ 5 - 5
src/index.js

@@ -6,11 +6,11 @@ module.exports = {
6 6
   },
7 7
 
8 8
   convert: function (data, options) {
9
+    if (options === undefined) {
10
+      options = {};
11
+    }
12
+    options = this._merge_options(this._defaults, options);
9 13
     if (typeof data === 'object') {
10
-      if (options === undefined) {
11
-        options = {};
12
-      }
13
-      options = this._merge_options(this._defaults, options);
14 14
       let result = "?";
15 15
       Object.keys(data).map(function (query_key) {
16 16
         let query_data = data[query_key];
@@ -47,8 +47,8 @@ module.exports = {
47 47
     } else {
48 48
       if (options.warn_on_invalid) {
49 49
         console.warn("Attempted to convert non-object to query string!");
50
-        return "";
51 50
       }
51
+      return "";
52 52
     }
53 53
   },
54 54