Matt Coles před 9 roky
rodič
revize
3dda3d2e7b
8 změnil soubory, kde provedl 160 přidání a 1 odebrání
  1. 1 0
      .babelrc
  2. 10 0
      .jshintrc
  3. 63 0
      lib/index.js
  4. 1 0
      lib/index.min.js
  5. 9 1
      package.json
  6. 9 0
      spec/js-query-string.js
  7. 11 0
      spec/support/jasmine.json
  8. 56 0
      src/index.js

+ 1 - 0
.babelrc

@@ -0,0 +1 @@
1
+{ "presets": ["es2015"] }

+ 10 - 0
.jshintrc

@@ -0,0 +1,10 @@
1
+{
2
+    "esnext": true,
3
+    "browser": true,
4
+    "node": true,
5
+    "jasmine": true,
6
+    "curly": true,
7
+    "eqeqeq": true,
8
+    "undef": true,
9
+    "globals": {}
10
+}

+ 63 - 0
lib/index.js

@@ -0,0 +1,63 @@
1
+"use strict";
2
+
3
+var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
4
+
5
+module.exports = {
6
+
7
+  _defaults: {
8
+    "warn_on_invalid": true
9
+  },
10
+
11
+  convert: function convert(data, options) {
12
+    if ((typeof data === "undefined" ? "undefined" : _typeof(data)) === 'object') {
13
+      this._merge_options(this._defaults, options);
14
+      var result = "?";
15
+      Object.keys(data).map(function (query_key) {
16
+        var query_data = data[query_key];
17
+        var query_data_processed = void 0;
18
+        if (query_data === 'null') {
19
+          if (options.warn_on_invalid) {
20
+            console.warn("Attempted to convert null to query string!");
21
+          }
22
+        } else if (typeof query_data === 'number') {
23
+          query_data_processed = query_data.toString();
24
+        } else if (typeof query_data === 'string') {
25
+          query_data_processed = query_data;
26
+        } else if (typeof query_data === 'boolean') {
27
+          query_data_processed = query_data.toString();
28
+        } else if ((typeof query_data === "undefined" ? "undefined" : _typeof(query_data)) === 'object') {
29
+          query_data_processed = JSON.stringify(query_data);
30
+        } else if (typeof query_data === 'undefined') {
31
+          if (options.warn_on_invalid) {
32
+            console.warn("Attempted to convert undefined to query string!");
33
+          }
34
+        } else {
35
+          if (options.warn_on_invalid) {
36
+            console.warn("Attempted to convert function or symbol to query string!");
37
+          }
38
+        }
39
+        if (query_data_processed !== 'undefined') {
40
+          var append = query_key + "=" + encodeURIComponent(query_data_processed) + "&";
41
+          result += append;
42
+        }
43
+      });
44
+      return result.substring(0, result.length - 1);
45
+    } else {
46
+      if (options.warn_on_invalid) {
47
+        console.warn("Attempted to convert non-object to query string!");
48
+        return "";
49
+      }
50
+    }
51
+  },
52
+
53
+  _merge_options: function _merge_options(obj1, obj2) {
54
+    var obj3 = {};
55
+    Object.keys(obj1).map(function (attrname) {
56
+      obj3[attrname] = obj1[attrname];
57
+    });
58
+    Object.keys(obj2).map(function (attrname) {
59
+      obj3[attrname] = obj2[attrname];
60
+    });
61
+    return obj3;
62
+  }
63
+};

Diff nebyl zobrazen, protože je příliš veliký
+ 1 - 0
lib/index.min.js


+ 9 - 1
package.json

@@ -4,6 +4,7 @@
4 4
   "description": "Prepare native JS objects to be used in URL queries",
5 5
   "main": "lib/index.js",
6 6
   "scripts": {
7
+    "build": "babel src -d lib && jshint lib/index.js && uglifyjs lib/index.js > lib/index.min.js",
7 8
     "test": "echo \"Error: no test specified\" && exit 1"
8 9
   },
9 10
   "repository": {
@@ -15,5 +16,12 @@
15 16
   "bugs": {
16 17
     "url": "https://github.com/Alpha-Atom/js-query-string/issues"
17 18
   },
18
-  "homepage": "https://github.com/Alpha-Atom/js-query-string#readme"
19
+  "homepage": "https://github.com/Alpha-Atom/js-query-string#readme",
20
+  "devDependencies": {
21
+    "babel-cli": "^6.7.5",
22
+    "babel-preset-es2015": "^6.6.0",
23
+    "jasmine": "^2.4.1",
24
+    "jshint": "^2.9.1",
25
+    "uglify-js": "^2.6.2"
26
+  }
19 27
 }

+ 9 - 0
spec/js-query-string.js

@@ -0,0 +1,9 @@
1
+var querify = require("./lib/index.js");
2
+
3
+describe("Object -> Query String", function () {
4
+
5
+  it("Should work for objects with strings", function () {
6
+    var querystring = 
7
+  });
8
+
9
+});

+ 11 - 0
spec/support/jasmine.json

@@ -0,0 +1,11 @@
1
+{
2
+  "spec_dir": "spec",
3
+  "spec_files": [
4
+    "**/*[sS]pec.js"
5
+  ],
6
+  "helpers": [
7
+    "helpers/**/*.js"
8
+  ],
9
+  "stopSpecOnExpectationFailure": false,
10
+  "random": false
11
+}

+ 56 - 0
src/index.js

@@ -0,0 +1,56 @@
1
+"use strict";
2
+module.exports = {
3
+
4
+  _defaults: {
5
+    "warn_on_invalid": true
6
+  },
7
+
8
+  convert: function (data, options) {
9
+    if (typeof data === 'object') {
10
+      this._merge_options(this._defaults, options);
11
+      let result = "?";
12
+      Object.keys(data).map(function (query_key) {
13
+        let query_data = data[query_key];
14
+        let query_data_processed;
15
+        if (query_data === 'null') {
16
+          if (options.warn_on_invalid) {
17
+            console.warn("Attempted to convert null to query string!");
18
+          }
19
+        } else if (typeof query_data === 'number') {
20
+          query_data_processed = query_data.toString();
21
+        } else if (typeof query_data === 'string') {
22
+          query_data_processed = query_data;
23
+        } else if (typeof query_data === 'boolean') {
24
+          query_data_processed = query_data.toString();
25
+        } else if (typeof query_data === 'object') {
26
+          query_data_processed = JSON.stringify(query_data);
27
+        } else if (typeof query_data === 'undefined') {
28
+          if (options.warn_on_invalid) {
29
+            console.warn("Attempted to convert undefined to query string!");
30
+          }
31
+        } else {
32
+          if (options.warn_on_invalid) {
33
+            console.warn("Attempted to convert function or symbol to query string!");
34
+          }
35
+        }
36
+        if (query_data_processed !== 'undefined') {
37
+          let append = query_key + "=" + encodeURIComponent(query_data_processed) + "&";
38
+          result += append;
39
+        }
40
+      });
41
+      return result.substring(0, result.length-1);
42
+    } else {
43
+      if (options.warn_on_invalid) {
44
+        console.warn("Attempted to convert non-object to query string!");
45
+        return "";
46
+      }
47
+    }
48
+  },
49
+
50
+  _merge_options: function (obj1,obj2) {
51
+    var obj3 = {};
52
+    Object.keys(obj1).map(function (attrname) { obj3[attrname] = obj1[attrname]; });
53
+    Object.keys(obj2).map(function (attrname) { obj3[attrname] = obj2[attrname]; });
54
+    return obj3;
55
+  }
56
+}